Python 尝试人脸识别时出现明显的弃用警告和值错误
我是一个编程新手,我正在尝试用python建模人脸识别程序,但我无法获得网络摄像头的人脸距离列表。网络摄像头的灯会亮,但我甚至看不到图像。 我不知道我做错了什么 这是密码Python 尝试人脸识别时出现明显的弃用警告和值错误,python,opencv,numpy-ndarray,face-detection,face-recognition,Python,Opencv,Numpy Ndarray,Face Detection,Face Recognition,我是一个编程新手,我正在尝试用python建模人脸识别程序,但我无法获得网络摄像头的人脸距离列表。网络摄像头的灯会亮,但我甚至看不到图像。 我不知道我做错了什么 这是密码 import cv2 import numpy as np import face_recognition import os path = 'ImagesAttendance' images = [] staffNames = [] myList = os.listdir(path) print(myList) for s
import cv2
import numpy as np
import face_recognition
import os
path = 'ImagesAttendance'
images = []
staffNames = []
myList = os.listdir(path)
print(myList)
for st in myList:
curImg = cv2.imread(f'{path}/{st}')
images.append(curImg)
staffNames.append(os.path.splitext(st)[0])
print(staffNames)
#encoding functions finding begins automatically
def findEncodings(images):
encodeList = []
for img in images:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#finding encodings
encode = face_recognition.face_encodings(img)
encodeList.append(encode)
return encodeList
encodeListKnown = findEncodings(images)
#printing the number or length of pictures in the folder
#print(len(encodeListKnown))
print('Encoding Complete')
#Initializing webcam to match images in the folder
cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
#because its real time capture, we wld reduce the size of image to speed up the process
imgS = cv2.resize(img,(0,0),None,0.25,0.25)
#realtime image size has been divided by 4 using 0.25
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
facesCurFrame = face_recognition.face_locations(imgS)
encodeCurFrame = face_recognition.face_encodings(imgS,facesCurFrame)
#finding matches
for encodeFace,faceLoc in zip(encodeCurFrame,facesCurFrame):
matches = face_recognition.compare_faces(encodeListKnown,encodeFace)
faceDis = face_recognition.face_distance(encodeListKnown,encodeFace)
print(faceDis)
以下是错误
C:\Users\AAA\PycharmProjects\FaceRecognitionProject\venv\Scripts\python.exe C:/Users/AAA/PycharmProjects/FaceRecognitionProject/AttendanceProject.py
['2LT Chinonso.jpg', 'Hadizah Abdul.jpg', 'Nosa Igiemwin.jpg']
['2LT Chinonso', 'Hadizah Abdul', 'Nosa Igiemwin']
Encoding Complete
C:\Users\AAA\PycharmProjects\FaceRecognitionProject\venv\lib\site-packages\face_recognition\api.py:75: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
return np.linalg.norm(face_encodings - face_to_compare, axis=1)
Traceback (most recent call last):
File "C:/Users/AAA/PycharmProjects/FaceRecognitionProject/AttendanceProject.py", line 46, in <module>
matches = face_recognition.compare_faces(encodeListKnown,encodeFace)
File "C:\Users\AAA\PycharmProjects\FaceRecognitionProject\venv\lib\site-packages\face_recognition\api.py", line 226, in compare_faces
return list(face_distance(known_face_encodings, face_encoding_to_check) <= tolerance)
File "C:\Users\AAA\PycharmProjects\FaceRecognitionProject\venv\lib\site-packages\face_recognition\api.py", line 75, in face_distance
return np.linalg.norm(face_encodings - face_to_compare, axis=1)
ValueError: operands could not be broadcast together with shapes (3,) (128,)
[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (674) SourceReaderCB::~SourceReaderCB terminating async callback
C:\Users\AAA\PycharmProjects\FaceRecognitionProject\venv\Scripts\python.exe C:/Users/AAA/PycharmProjects/FaceRecognitionProject/AttendanceProject.py
['2LT Chinonso.jpg','Hadizah Abdul.jpg','Nosa Igiemwin.jpg']
['2LT Chinonso','Hadizah Abdul','Nosa Igiemwin']
编码完成
C:\Users\AAA\PycharmProjects\FaceRecognitionProject\venv\lib\site packages\face_recognition\api.py:75:VisibleDeprecationWarning:不推荐使用不规则嵌套序列(即具有不同长度或形状的列表或列表元组或元组或数据数组)创建数据数组。如果要执行此操作,则必须在创建数据阵列时指定“dtype=object”
返回np.linalg.norm(面编码-面与面比较,轴=1)
回溯(最近一次呼叫最后一次):
文件“C:/Users/AAA/PycharmProjects/FaceRecognitionProject/AttendanceProject.py”,第46行,在
匹配=人脸识别。比较人脸(encodeListKnown,encodeFace)
文件“C:\Users\AAA\PycharmProjects\FaceRecognitionProject\venv\lib\site packages\face\u recognition\api.py”,第226行,在compare\u faces中
返回列表(face\u distance(已知的face\u编码,face\u编码到\u检查)我为修复错误所做的事情,现在它在我的pc上运行得非常好
内部def查找编码(图像)检查第四行,即编码=人脸识别。人脸编码(img)替换为encode=人脸识别。人脸编码(img)[0]
我在显示时再次编写了下面提到的循环(最后四行)
无法访问,因为错误可能是由于不正确的缩进导致的
对于encodeFace,拉链中的faceLoc(encodeCurFrame,facesCurFrame):
匹配=人脸识别。比较人脸(encodeListKnown,encodeFace)
faceDis=人脸识别。人脸距离(encodeListKnown,encodeFace)
打印(faceDis)
我也完成了您的代码,您可以使用此代码进行比较
import cv2
import numpy as np
import face_recognition
import os
path = 'ImagesAttendance'
images = []
staffNames = []
myList = os.listdir(path)
print(myList)
for st in myList:
curImg = cv2.imread(f'{path}/{st}')
images.append(curImg)
staffNames.append(os.path.splitext(st)[0])
print(staffNames)
def findEncodings(images):
encodeList = []
for img in images:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#finding encodings
encode = face_recognition.face_encodings(img)[0]
encodeList.append(encode)
return encodeList
encodeListKnown = findEncodings(images)
cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
#because its real time capture, we wld reduce the size of image to speed up the process
imgS = cv2.resize(img,(0,0),None,0.25,0.25)
#realtime image size has been divided by 4 using 0.25
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
facesCurFrame = face_recognition.face_locations(imgS)
encodeCurFrame = face_recognition.face_encodings(imgS,facesCurFrame)
#finding matches
for encodeFace,faceLoc in zip(encodeCurFrame, facesCurFrame):
matches =face_recognition.compare_faces(encodeListKnown,encodeFace)
faceDis = face_recognition.face_distance(encodeListKnown,encodeFace)
print(faceDis)
matchIndex = np.argmin(faceDis)
print('matchIndex', matchIndex)
if matches[matchIndex]:
name = staffNames[matchIndex].upper()
print(name)
y1, x2, y2, x1 = faceLoc
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.rectangle(img, (x1, y2 - 35), (x2, y2), (0, 0, 0), cv2.FILLED)
cv2.putText(img, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2)
cv2.imshow('Webcam', img)
#press'esc' to close program
if cv2.waitKey(1) == 27:
break
#release camera
cap.release()
cv2.destroyAllWindows()
首先使用“打印”检查变量中的值,并检查图像的形状。似乎您使用的图像大小不同,或者在比较它们时遇到问题。或者您尝试将RGB(3,)
与灰度(128,)