我正在開發一個面部識別專案,該專案創建了一個面部編碼資料庫,然后在選擇目標照片時也會對該照片進行編碼,并將其與已知編碼進行匹配。
該程式正常作業,除非它僅提供最佳匹配(如果在設定的容差范圍內找到)。問題是它并不總是正確的,即使我知道目標人臉在我的資料庫中,但目標圖片的不同之處足以導致誤報。
因此,我想列出前 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)
然而,這沒有給我任何打擊。
有任何想法嗎?
uj5u.com熱心網友回復:
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)
三個元素而不是一個。
uj5u.com熱心網友回復:
下面的代碼解決了這個問題。問題是,根據 Aubergine 的回答,我在未轉換為 numpy 陣列的串列上使用了 numpy 函式。
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)
并為 3 個最低值對 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/344680.html
