I am trying to build a program that saves unknow faces in a directory and print out "already exist" if the face already exist in the directory

Below code is saving all the images it detects, i don't understand where i did wrong

for (x, y, w, h) in faces:
    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
    if True:
        i=0
        face_locations = face_recognition.face_locations(frame)
        my_new_encoding = face_recognition.face_encodings(frame,face_locations)
        while i<num:    
            unknown_picture = face_recognition.load_image_file(str(i) + ".jpg")
            unknown_face_encoding = face_recognition.face_encodings(unknown_picture)[0]
            
            results = face_recognition.compare_faces(my_new_encoding, unknown_face_encoding)
            i = i + 1
            if results == True:
                print("Already Exist")
            else:
                cv2.imwrite(str(i) + '.jpg',frame)
                num = num+1

1 answer

  • answered 2022-01-13 05:04 Prudhvi

    You are trying to save the image every time you compare with some image in the directory, and every loop you are updating the num which makes your loop infinite.

    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
        if True:
            face_locations = face_recognition.face_locations(frame)
            my_new_encoding = face_recognition.face_encodings(frame,face_locations)
            result = False
            while not result:   
                unknown_picture = face_recognition.load_image_file(str(i) + ".jpg")
                unknown_face_encoding = face_recognition.face_encodings(unknown_picture)[0]
                
                result = face_recognition.compare_faces(my_new_encoding, unknown_face_encoding)
            if result:
                print("Already Exist")
            else:
                cv2.imwrite(str(i) + '.jpg',frame)
                num = num+1
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum