jeudi 19 mars 2020

Python Control-flow issue, code gets executed when I don't want it to automatically (Face-Recognition)

I'm fairly new to Python and am working on a facial recognition system. I'm having issues with code being executed when I don't want it to. When the face is recognized in the system it prints the else statement once (i do not want this).

When there is a face found but it isn't recognized it runs the else statement twice. I want to be able to have a condition that if the face isn't recognized it updates the GUI. However, this isn't possible as of right now because the GUI would update even if the face is recognized once.

The else statement after the if results[0] gets executed even when if the statement is true. Is this because it doesn't break out of the for loop?

  • When face is recognized: print("test")
  • When face isn't recognized print("test") x2

Any help is appreciated, thanks!

    print("[INFO] CROSS-REFERENCING IMAGE")
    print(filename)

    directory = "output/"
    file_names = os.listdir(directory)

    picture_of_me = face_recognition.load_image_file("rawimages/{}".format(filename))
    try:
        my_face_encoding = face_recognition.face_encodings(picture_of_me)[0]
        print("[INFO] CHECKING IF USER IS IN DATABASE")
        try:
            for i in file_names:
                iPath = os.path.join(directory, i)
                with open(iPath, 'rb') as fh:

                    new_picture = face_recognition.load_image_file(iPath)

                    for face_encoding in face_recognition.face_encodings(new_picture):

                        results = face_recognition.compare_faces([my_face_encoding], face_encoding)

                        if results[0]:
                            print("[INFO] USER FOUND")
                            userID = ''.join(filter(lambda x: x.isdigit(), iPath))
                            print("[INFO] ID : {}".format(userID))
                            updateGUIYes(userID)
                            raise BreakIt
                        else:
                            # I want to be able to do something here if face isn't found
                            print("test")
                            pass
        except BreakIt:
            pass
    except IndexError as e:
        updateGUINo()
        print("[INFO] NO FACE FOUND")


  [1]: https://i.stack.imgur.com/m93i6.png

Aucun commentaire:

Enregistrer un commentaire