簡體   English   中英

使用 face_recognition 和 cv2 進行人臉識別:Python 空編碼錯誤

[英]Face recognition with face_recognition and cv2 : empty encoding error Python

我在運行 python 人臉識別腳本時遇到問題,它適用於某些圖像,但在某些情況下,它給了我錯誤:

unface_encoding = face_recognition.face_encodings(faceimg)[0]IndexError: list index out of range

看起來unface_encoding變量是空的(?),但我不知道為什么這會發生在某些人臉而不是其他人unface_encoding ,以及如何解決它。 我已經閱讀了 face_recognition 但它並沒有真正幫助我。

我是人臉識別(和 python)的初學者,所以任何建議或改進都會對我有所幫助。

這是我的代碼:

import face_recognition
import cv2

known_image = face_recognition.load_image_file("known_faces/person.jpg")
unknown_image = face_recognition.load_image_file("unknown_faces/faces.jpg")

image = cv2.imread("unknown_faces/faces.jpg",cv2.IMREAD_COLOR)
face_locations = face_recognition.face_locations(unknown_image)

known_encoding = face_recognition.face_encodings(known_image)[0]

for face in face_locations:
    print(face)
    (a, b, c, d) = face
    faceimg = unknown_image[a:c, d:b]
    faceimg = cv2.cvtColor(faceimg, cv2.COLOR_BGR2RGB)

    unface_encoding = face_recognition.face_encodings(faceimg)[0]

    results = face_recognition.compare_faces([known_encoding], unface_encoding,0.5)
    print(results)
    if results == [True]:
        cv2.rectangle(image, (b, c), (d, a), (0, 128, 0), 2)
    elif results == [False]:
        cv2.rectangle(image, (b, c), (d, a), (0, 0, 255), 1)

cv2.imshow('base', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

請原諒我的任何英語錯誤,

提前致謝

我沒有廣泛使用face_recognition ,但我使用過 OpenCV 和其他面部識別庫。 通常返回的“人臉”是x, y, w, h的邊界框。 通常在x, y, x + w, y + h創建矩形。 如果face_recognition相似,則說明您制作的盒子不正確。 嘗試

faceimg = unknown_image[a:a+c, b:b+d]

暫無
暫無

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

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