how encode photo and compare it with webcam input?

238 Views Asked by At

I am trying to get number of images and compare them with webcam but it only gives me 1 entry from file. this my code:

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)

this program suppose to get images from selected file and convert to black and white and then compare photos with person in front of webcam what should I do?

1

There are 1 best solutions below

2
On BEST ANSWER

You are prematurely exiting your encoding function. The return statement needs to be outside of the loop:

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