TypeError: __init__(): incompatible constructor arguments with face_recognition call
TypeError: __init__(): incompatible constructor arguments with face_recognition call
我正在尝试开发一个人脸识别考勤系统,我按照教程编写了100%,但仍然出现一些错误,代码如下:
from face_recognition.api import face_distance
import face_recognition as fr
import os
import cv2
import face_recognition
import numpy as np
import time
from PIL import ImageFont
def getEncodedFaces():
encoded = {}
for dirPath, dNames, fNames in os.walk("./faces"):
for f in fNames:
if f.endswith(".jpg") or f.endswith(".png"):
face = fr.load_image_file("faces/" + f)
encoding = fr.face_encodings(face)[0]
encoded[f.split(".")[0]] + encoding
return encoded
def unknownImageEncoded(img):
face = fr.load_image_file("faces/" + img)
encoding = fr.face_encodings(face)[0]
return encoding
def classifyFace(im):
faces = getEncodedFaces()
facesEncoded = list(faces.values())
knownFaceNames = list(faces.keys())
img = cv2.imread(im, 1)
faceLocations = face_recognition.face_encodings(img)
unknownFaceEncodings = face_recognition.face_encodings(img, faceLocations)
faceNames = []
for faceEncoding in unknownFaceEncodings:
matches = face_recognition.compare_faces()
name = "Unknown"
faceDistances = face_recognition.face_distance(facesEncoded, faceEncoding)
bestMatchIndex = np.argmin(face_distance)
if matches[bestMatchIndex]:
name = knownFaceNames[bestMatchIndex]
faceNames.append(name)
for (top, right, bottom, left), name in zip(faceLocations, faceNames):
cv2.rectangle(img, (left-20, top-20, right+20, bottom+20), (255, 0, 0), cv2.FILLED)
font = ImageFont.truetype("./NotoSansTC-Black.ttf")
cv2.putText(img, name, (left-20, bottom+15), font, 1.0, (255, 255, 255), 2)
while True:
cv2.imshow("Attendance System", img)
if cv2.waitkey(1) & 0xFF == ord("q"):
return faceNames
print(classifyFace("test.jpg"))
这是错误:
Traceback (most recent call last):
File "main.py", line 51, in <module>
print(classifyFace("test.jpg"))
File "main.py", line 31, in classifyFace
unknownFaceEncodings = face_recognition.face_encodings(img, faceLocations)
File "C:\Users\family\AppData\Local\Programs\Python\Python36\lib\site-packages\face_recognition\api.py", line 213, in face_encodings
raw_landmarks = _raw_face_landmarks(face_image, known_face_locations, model)
File "C:\Users\family\AppData\Local\Programs\Python\Python36\lib\site-packages\face_recognition\api.py", line 158, in _raw_face_landmarks
face_locations = [_css_to_rect(face_location) for face_location in face_locations]
File "C:\Users\family\AppData\Local\Programs\Python\Python36\lib\site-packages\face_recognition\api.py", line 158, in <listcomp>
face_locations = [_css_to_rect(face_location) for face_location in face_locations]
File "C:\Users\family\AppData\Local\Programs\Python\Python36\lib\site-packages\face_recognition\api.py", line 49, in _css_to_rect
return dlib.rectangle(css[3], css[0], css[1], css[2])
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
1. _dlib_pybind11.rectangle(left: int, top: int, right: int, bottom: int)
2. _dlib_pybind11.rectangle(rect: dlib::drectangle)
3. _dlib_pybind11.rectangle(rect: _dlib_pybind11.rectangle)
4. _dlib_pybind11.rectangle()
Invoked with: -0.035434916615486145, -0.12049627304077148, 0.08377307653427124, 0.062198664993047714
目录结构如下:
test.JPG
main.py
NotoSansTC-Black.otf
faces
|- 呂AA.jpg
|- 呂BB.jpg
谁能告诉我这是什么意思以及如何解决?我尝试安装许多不同版本的 dlib 和人脸识别。我也安装了 pybind11,但它仍然给我同样的错误。我很着急,我真的需要一些帮助。谢谢!
这一行:for (top, right, bottom, left), name in zip(faceLocations, faceNames):
.
确保 top, right, bottom, left
值是整数值而不是浮点值。只需打印一次以确认。如果它们是 float 值,则使用 int()
将它们转换为 int。像这样:
for (top, right, bottom, left), name in zip(faceLocations, faceNames):
cv2.rectangle(img, int(left)-20, int(top)-20, int(right)+20, int(bottom)+20), (255, 0, 0), cv2.FILLED)
为什么要导入 face_recognition
2 次?导入 face_recognition
2 次是不正确的方法。
compare_faces()
接受 2 个参数,已编码向量列表和未知编码向量。
现在关注你的主要错误,你没有使用 face_locations
这意味着它不会从图像中找到你的脸。
你的错误在这里:
faceLocations = face_recognition.face_encodings(img)
改用这个:
faceLocations = face_recognition.face_locations(img)
完整代码:
from face_recognition.api import face_distance
import face_recognition as fr
import os
import cv2
import numpy as np
import time
from PIL import ImageFont
def getEncodedFaces():
encoded = {}
for f in os.listdir(os.path.join('faces')):
face = fr.load_image_file(os.path.join('faces',f))
encoding = fr.face_encodings(face)
if not len(encoding):
continue
encoding = encoding[0]
encoded[f.split(".")[0]] = encoding
return encoded
def unknownImageEncoded(img):
face = fr.load_image_file("faces/" + img)
encoding = fr.face_encodings(face)[0]
return encoding
def classifyFace(im):
faces = getEncodedFaces()
facesEncoded = list(faces.values())
knownFaceNames = list(faces.keys())
img = cv2.imread(im, 1)
faceLocations = fr.face_locations(img)
unknownFaceEncodings = fr.face_encodings(img, faceLocations)
faceNames = []
for faceEncoding, faceloc in zip(unknownFaceEncodings, faceLocations):
top, right, bottom, left = faceloc
matches = fr.compare_faces(facesEncoded, faceEncoding)
name = "Unknown"
faceDistances = fr.face_distance(facesEncoded, faceEncoding)
bestMatchIndex = np.argmin(faceDistances)
if matches[bestMatchIndex]:
name = knownFaceNames[bestMatchIndex]
faceNames.append(name)
cv2.rectangle(img, (left-20, top-20, right+20, bottom+20), (255, 0, 0), cv2.FILLED)
font = ImageFont.truetype("./NotoSansTC-Black.ttf")
cv2.putText(img, name, (left-20, bottom+15), font, 1.0, (255, 255, 255), 2)
image = img[:, :, ::-1]
cv2.imwrite("result.jpg", image)
return faceNames
print(classifyFace("test.jpg"))
我正在尝试开发一个人脸识别考勤系统,我按照教程编写了100%,但仍然出现一些错误,代码如下:
from face_recognition.api import face_distance
import face_recognition as fr
import os
import cv2
import face_recognition
import numpy as np
import time
from PIL import ImageFont
def getEncodedFaces():
encoded = {}
for dirPath, dNames, fNames in os.walk("./faces"):
for f in fNames:
if f.endswith(".jpg") or f.endswith(".png"):
face = fr.load_image_file("faces/" + f)
encoding = fr.face_encodings(face)[0]
encoded[f.split(".")[0]] + encoding
return encoded
def unknownImageEncoded(img):
face = fr.load_image_file("faces/" + img)
encoding = fr.face_encodings(face)[0]
return encoding
def classifyFace(im):
faces = getEncodedFaces()
facesEncoded = list(faces.values())
knownFaceNames = list(faces.keys())
img = cv2.imread(im, 1)
faceLocations = face_recognition.face_encodings(img)
unknownFaceEncodings = face_recognition.face_encodings(img, faceLocations)
faceNames = []
for faceEncoding in unknownFaceEncodings:
matches = face_recognition.compare_faces()
name = "Unknown"
faceDistances = face_recognition.face_distance(facesEncoded, faceEncoding)
bestMatchIndex = np.argmin(face_distance)
if matches[bestMatchIndex]:
name = knownFaceNames[bestMatchIndex]
faceNames.append(name)
for (top, right, bottom, left), name in zip(faceLocations, faceNames):
cv2.rectangle(img, (left-20, top-20, right+20, bottom+20), (255, 0, 0), cv2.FILLED)
font = ImageFont.truetype("./NotoSansTC-Black.ttf")
cv2.putText(img, name, (left-20, bottom+15), font, 1.0, (255, 255, 255), 2)
while True:
cv2.imshow("Attendance System", img)
if cv2.waitkey(1) & 0xFF == ord("q"):
return faceNames
print(classifyFace("test.jpg"))
这是错误:
Traceback (most recent call last):
File "main.py", line 51, in <module>
print(classifyFace("test.jpg"))
File "main.py", line 31, in classifyFace
unknownFaceEncodings = face_recognition.face_encodings(img, faceLocations)
File "C:\Users\family\AppData\Local\Programs\Python\Python36\lib\site-packages\face_recognition\api.py", line 213, in face_encodings
raw_landmarks = _raw_face_landmarks(face_image, known_face_locations, model)
File "C:\Users\family\AppData\Local\Programs\Python\Python36\lib\site-packages\face_recognition\api.py", line 158, in _raw_face_landmarks
face_locations = [_css_to_rect(face_location) for face_location in face_locations]
File "C:\Users\family\AppData\Local\Programs\Python\Python36\lib\site-packages\face_recognition\api.py", line 158, in <listcomp>
face_locations = [_css_to_rect(face_location) for face_location in face_locations]
File "C:\Users\family\AppData\Local\Programs\Python\Python36\lib\site-packages\face_recognition\api.py", line 49, in _css_to_rect
return dlib.rectangle(css[3], css[0], css[1], css[2])
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
1. _dlib_pybind11.rectangle(left: int, top: int, right: int, bottom: int)
2. _dlib_pybind11.rectangle(rect: dlib::drectangle)
3. _dlib_pybind11.rectangle(rect: _dlib_pybind11.rectangle)
4. _dlib_pybind11.rectangle()
Invoked with: -0.035434916615486145, -0.12049627304077148, 0.08377307653427124, 0.062198664993047714
目录结构如下:
test.JPG
main.py
NotoSansTC-Black.otf
faces
|- 呂AA.jpg
|- 呂BB.jpg
谁能告诉我这是什么意思以及如何解决?我尝试安装许多不同版本的 dlib 和人脸识别。我也安装了 pybind11,但它仍然给我同样的错误。我很着急,我真的需要一些帮助。谢谢!
这一行:for (top, right, bottom, left), name in zip(faceLocations, faceNames):
.
确保 top, right, bottom, left
值是整数值而不是浮点值。只需打印一次以确认。如果它们是 float 值,则使用 int()
将它们转换为 int。像这样:
for (top, right, bottom, left), name in zip(faceLocations, faceNames):
cv2.rectangle(img, int(left)-20, int(top)-20, int(right)+20, int(bottom)+20), (255, 0, 0), cv2.FILLED)
为什么要导入 face_recognition
2 次?导入 face_recognition
2 次是不正确的方法。
compare_faces()
接受 2 个参数,已编码向量列表和未知编码向量。
现在关注你的主要错误,你没有使用 face_locations
这意味着它不会从图像中找到你的脸。
你的错误在这里:
faceLocations = face_recognition.face_encodings(img)
改用这个:
faceLocations = face_recognition.face_locations(img)
完整代码:
from face_recognition.api import face_distance
import face_recognition as fr
import os
import cv2
import numpy as np
import time
from PIL import ImageFont
def getEncodedFaces():
encoded = {}
for f in os.listdir(os.path.join('faces')):
face = fr.load_image_file(os.path.join('faces',f))
encoding = fr.face_encodings(face)
if not len(encoding):
continue
encoding = encoding[0]
encoded[f.split(".")[0]] = encoding
return encoded
def unknownImageEncoded(img):
face = fr.load_image_file("faces/" + img)
encoding = fr.face_encodings(face)[0]
return encoding
def classifyFace(im):
faces = getEncodedFaces()
facesEncoded = list(faces.values())
knownFaceNames = list(faces.keys())
img = cv2.imread(im, 1)
faceLocations = fr.face_locations(img)
unknownFaceEncodings = fr.face_encodings(img, faceLocations)
faceNames = []
for faceEncoding, faceloc in zip(unknownFaceEncodings, faceLocations):
top, right, bottom, left = faceloc
matches = fr.compare_faces(facesEncoded, faceEncoding)
name = "Unknown"
faceDistances = fr.face_distance(facesEncoded, faceEncoding)
bestMatchIndex = np.argmin(faceDistances)
if matches[bestMatchIndex]:
name = knownFaceNames[bestMatchIndex]
faceNames.append(name)
cv2.rectangle(img, (left-20, top-20, right+20, bottom+20), (255, 0, 0), cv2.FILLED)
font = ImageFont.truetype("./NotoSansTC-Black.ttf")
cv2.putText(img, name, (left-20, bottom+15), font, 1.0, (255, 255, 255), 2)
image = img[:, :, ::-1]
cv2.imwrite("result.jpg", image)
return faceNames
print(classifyFace("test.jpg"))