如何编码照片并将其与网络摄像头输入进行比较?

how encode photo and compare it with webcam input?

我正在尝试获取图像数量并将它们与网络摄像头进行比较,但它只提供了文件中的 1 个条目。这是我的代码:

import cv2
import numpy as np
import face_recognition
import os

path = 'pics'
images = []
classNames = []
myList = os.listdir(path)
print(myList)
for cl in myList:
    curImg = cv2.imread(f'{path}/{cl}')
    images.append(curImg)
    classNames.append(os.path.splitext(cl)[0])
print(classNames)


def findEncodings(images):
    encodeList = []
    for img in images:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        encode = face_recognition.face_encodings(img)[0]

        encodeList.append(encode)



    return encodeList

encodeListknown = findEncodings(images)
print(findEncodings(images))

cap = cv2.VideoCapture(0)


while True:
    success, img = cap.read()
    imgs = cv2.resize(img,(0,0),None,0.25,0.25)
    imgs = cv2.cvtColor(imgs, cv2.COLOR_BGR2RGB)
    facesCurframe = face_recognition.face_locations(imgs)
    encodescurframe = face_recognition.face_encodings(imgs,facesCurframe)

    for encodeface, faceloc in zip(encodescurframe,facesCurframe):
        matches = face_recognition.compare_faces(encodeListknown,encodeface)
        facedis = face_recognition.face_distance(encodeListknown,encodeface)
        print(facedis)

这个程序假设从选定的文件中获取图像并转换为黑白,然后将照片与网络摄像头前的人进行比较 我该怎么办?

您正在提前退出编码功能。 return 语句需要在循环之外:

def findEncodings(images):
    encodeList = []
    for img in images:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        encode = face_recognition.face_encodings(img)[0]

        encodeList.append(encode)



    return encodeList  ## <-- Move back the indentation by 1 level