簡體   English   中英

Python Opencv'numpy.ndarray'對象沒有屬性'iteritems'

[英]Python Opencv 'numpy.ndarray' object has no attribute 'iteritems'

最近,我一直在研究對象識別器。 下面編寫的某些代碼已被復制。 當我運行代碼時,出現以下奇怪錯誤: AttributeError: 'numpy.ndarray' object has no attribute 'iteritems'無法修復的AttributeError: 'numpy.ndarray' object has no attribute 'iteritems' 我看了一下互聯網,但沒有找到可以使用的相關答案。 提前致謝。

錯誤:

Traceback (most recent call last):
  File "/Users/arkumar/TestProj./ObjDetect.py", line 67, in <module>
    ma = Matcher('features.pck')
  File "/Users/arkumar/TestProj./ObjDetect.py", line 40, in __init__
    for k, v in self.data.iteritems():
AttributeError: 'numpy.ndarray' object has no attribute 'iteritems'

碼:

import cv2
import pickle
import numpy as np
import scipy
import os


class ExtractFeatures():

    def __init__(self, img = 'NULL', vectorSize = 32, out = 'features.pck'):
        self.img = img
        self.out = out
        self.vectorSize = vectorSize
        self.surf = cv2.xfeatures2d.SURF_create(400)

        self.img = cv2.imread(img)
        self.kp, self.des = self.surf.detectAndCompute(self.img, None)


        self.vectorSize = 32
        self.kp = sorted(self.kp, key=lambda x: -x.response)[:self.vectorSize]
        print self.des

        self.des = self.des.flatten()
        needed_size = (self.vectorSize * 64)
        if self.des.size < needed_size:
            dsc = np.concatenate([self.des, np.zeros(needed_size - self.des.size)])
        print(self.des)

        with open(self.out, 'w') as fp:
            pickle.dump(self.des, fp)


class Matcher(object):
    def __init__(self, path = 'NULL'):
        with open('features.pck') as fp:
            self.data = pickle.load(fp)
        self.names = []
        self.matrix = []
        for k, v in self.data.iteritems():
            self.names.append(k)
            self.matrix.append(v)
        self.matrix = np.array(self.matrix)
        self.names = np.array(self.names)

    def cos_cdist(self, vector):
        v = vector.reshape(1, -1)
        return scipy.spatial.distance.cdist(self.matrix, v, 'cosine').reshape(-1)

    def match(self, image_path, topn=5):
        features = ExtractFeatures(image_path)
        img_distances = self.cos_cdist(features)
        nearest_ids = np.argsort(img_distances)[:topn].tolist()
        nearest_img_paths = self.names[nearest_ids].tolist()

        return nearest_img_paths, img_distances[nearest_ids].tolist()

def show_img(path):
    img = cv2.imread(path, mode="RGB")
    img.imshow()
    cv2.waitkey(0)

imagePath = 'flute.jpg'

ExtractFeatures(imagePath)

ma = Matcher('features.pck')


print 'Query:'
show_img(imagePath)
names, match = ma.match(imagePath, topn=3)
print 'Result:'

print 'Match :   '
show_img(os.path.join(imagePath))

有人可以幫忙嗎?

我剛剛發現了如何解決這個令人困惑的問題。 @FlyingTeller說它需要是一本字典。 這對我有所幫助,因為我將Self.data轉換為字典。 謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM