import face_recognition
import os
import cv2
video = cv2.VideoCapture(0)
KNOWN_FACES_DIR = "known_faces"。
TOLERANCE = 0.6
FRAME_THICKNESS =3
FONT_THICKNESS=2
MODEL = "cnn"/span>
FONT = cv2.FONT_HERSHEY_SIMPLEX
print("loading known faces...")
已知面孔 = []
已知名稱 = []
for name in os.listdir(KNOWN_FACES_DIR) 。
for filename in os.listdir(f"{KNOWN_FACES_DIR}/{name}" )。
影像 = face_recognition.load_image_file(f"{KNOWN_FACES_DIR}/{name}/{filename}"/span>)
encoding = face_recognition.face_encodings(image)[0]
known_faces.append(encoding)
known_names.append(name)
print("處理未知面孔...")
while True:
ret, frame = video.read()
locations = face_recognition.face_locations(frame, model=MODEL)
encodings = face_recognition.face_encodings(frame, locations)
for face_encoding, face_location in zip(encodings, locations)。
結果 = face_recognition.compare_faces(known_faces, face_encoding, TOLERANCE)
match = None if True in results:
match = known_names[results.index(True)]
print(f "match found! ={match}")
top_left = (face_location[3], face_location[0)
bottom_right = (face_location[1], face_location[2] )
color = [0, 255, 0]
cv2.rectangle(frame, top_left, bottom_right, color, FRAME_THICKNESS)
top_left = (face_location[3], face_location[2)
bottom_right = (face_location[1], face_location[2] 22)
cv2.rectangle(frame, top_left, bottom_right, color, cv2.FILLED)
cv2.putText(frame, match, (face_location[3] 10, face_location[2] 15), FONT, 0。 5, (0, 0, 0), FONT_THICKNESS)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q')。
break。
video.release()
cv2.destroyAllWindows()
當我執行這個檔案時,人臉識別庫正常作業,并找到了真正的匹配,就像這樣:
當我執行這個檔案時,人臉識別庫正常作業,并找到了真正的匹配。
match found! = yusuf
并列印出來,但視頻捕捉視窗沒有反應,而且崩潰了,所以我看不到結果。當我從任務管理器中關閉該視窗時,它給我的是:
Process finished with exit code -805306369 (0xCFFFFFFF)。
注意:cv2.VideoCapture在沒有face_recognition庫的情況下可以正常作業。
uj5u.com熱心網友回復:
你的CPU無法快速處理使用CNN模型的實時人臉檢測和識別任務。每一幀都需要幾秒鐘的時間來處理,這就是為什么你能得到正確的識別,但這太慢了,無法查看處理后的幀的結果。
CNN模型比 "HOG "更準確,但它需要更多適合GPU的計算能力。
然而,你可以通過CPU使用HOG模型獲得非常好的結果。
解決方案:
改變代碼行
MODEL = "cnn"
改為
MODEL = "hog"
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/328633.html
標籤:
