Sowik
Sowik

Reputation: 104

Can't get rid of ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

So I took a look at the other questions regarding this error in stackoverflow but wasn't able to get an answer. I have the following code:

def getKnownFace():

    unknown_image = face_recognition.load_image_file("filename.jpg")
    unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]

    matches = face_recognition.compare_faces(known_face_encodings, unknown_face_encoding)
    name = ''
    if True in matches:
        first_match_index = matches.index(True)
        name = known_face_names[first_match_index]
        print(name)

        return name

That's the error I get:

  File "D:/Universitet/CheckKnownFace.py", line 100, in <module>
    getKnownFace()
  File "D:/Universitet/CheckKnownFace.py", line 91, in getKnownFace
    if True in matches:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

matches

<class 'list'>
[array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True])]

I tried a couple of variants with .any() and .all() but I always get an error saying that a bool type or a list type doesn't have an all() or any() method.

What should I do for it to work properly?

Upvotes: 0

Views: 155

Answers (1)

Andreas Forsl&#246;w
Andreas Forsl&#246;w

Reputation: 2738

The problem is that matches is a list with a single element of a numpy array. Simply change

if True in matches

to

if True in matches[0]

Of course, it depends on whether or not matches will contain more than on numpy array. If so, you might have to do a for loop or provide other logic, depending on what you want to achieve.

Upvotes: 1

Related Questions