如何列出numpy array中的最低值



我正在研究一个面部识别项目,该项目创建了一个面部编码数据库,然后在选择目标照片时将对该照片进行编码,并将其与已知编码相匹配。

程序正常工作,除非它只提供最佳匹配,如果在集合公差范围内找到一个。问题是,它并不总是正确的,即使我知道目标脸是在我的数据库中,但目标图片是不同的,它可以导致一个假阳性。

因此,我想列出前3或5个结果,希望能在前5个结果中得到正确的结果。

代码如下:

def recognize():
#define path for target photo
path=tkinter.filedialog.askopenfilename(filetypes=[("Image File",'.jpg .png')])
with open('dataset_faces.dat', 'rb') as f:
encoding = pickle.load(f)
def classify_face(im):
faces = encoding
faces_encoded = list(faces.values())
known_face_names = list(faces.keys())
img = cv2.imread(im, 1)
img = cv2.resize(img, (600, 600), fx=0.5, fy=0.5)
#img = img[:,:,::-1]
face_locations = face_recognition.face_locations(img, number_of_times_to_upsample=2, model="cnn")
unknown_face_encodings = face_recognition.face_encodings(img, face_locations, num_jitters=100)
face_names = []
for face_encoding in unknown_face_encodings:
# See if the face is a match for the known face(s)
name = "Unknown"
# use the known face with the smallest distance to the new face
face_distances = face_recognition.face_distance(faces_encoded, face_encoding)
best_match_index = np.argmin(face_distances)

#set distance between known faces and target face. The lower the distance between them the lower the match. Higher dist = more error.
if  face_distances[best_match_index] < 0.60:
name = known_face_names[best_match_index]

face_names.append(name)
print(name)

我试过添加像

这样的代码
top_3_matches = np.argsort(face_distances)[:3]
top3 =  face_names.append(top_3_matches)
print(top3)

然而,这给我没有命中。

任何想法?

list.append不返回任何内容,因此您不应该尝试将该表达式影响到变量。

names = known_face_names[top_3_matches]
face_names.append(names)
print(names)

应该和

做同样的事情
name = known_face_names[best_match_index]
face_names.append(name)
print(name)

为三个元素而不是一个。

下面的代码解决了这个问题。问题是我在一个没有被转换成numpy数组的列表上使用numpy函数,正如Aubergine的答案。

def classify_face(im):
faces = encoding
faces_encoded = list(faces.values())
known_face_names = list(faces.keys())
#make lists into numpy arrays
n_faces_encoded = np.array(faces_encoded)
n_known_face_names = np.array(known_face_names)

和对numpy数组进行最小值排序:

n_face_distances = face_recognition.face_distance(n_faces_encoded, face_encoding)
top_3_matches = np.argsort(n_face_distances)[:3]

打印最佳3个匹配项:

other_matches = n_known_face_names[top_3_matches]
print(other_matches)

相关内容

  • 没有找到相关文章

最新更新