尝试人脸识别时出现明显的弃用警告和值错误

2024-09-29 21:26:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个编程新手,我正在尝试用python建模人脸识别程序,但我无法获得网络摄像头的人脸距离列表。网络摄像头的灯会亮,但我甚至看不到图像。 我不知道我做错了什么

这是密码

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

Tags: thetoinimgcv2usersfaceimages
1条回答
网友
1楼 · 发布于 2024-09-29 21:26:37

我所做的修复错误的事情,现在它在我的电脑上完美地工作

  1. 内部def查找编码(图像)检查第四行,即编码=人脸识别。人脸编码(img)替换为encode=人脸识别。人脸编码(img)[0]

  2. 我在显示时再次编写了下面提到的循环(最后四行) 无法访问,因为错误可能是由于不正确的缩进导致的

    对于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()

相关问题 更多 >

    热门问题