Face_recognition: TypeError: unsupported operand type(s) for -: 'list' and 'list'

Created on 8 Aug 2017  路  6Comments  路  Source: ageitgey/face_recognition

  • face_recognition version: 0.2.2 (latest)
  • Python version: 3.6
  • Operating System: MacOS

Description

In this line of code in the api,

return np.linalg.norm(face_encodings - face_to_compare, axis=1).

I get an error TypeError: unsupported operand type(s) for -: 'list' and 'list'

I was running this code in the examples.

Any help is appreciated.

Most helpful comment

You are getting this error because you are feeding the params face_encodings and face_to_compare with list objects instead of numpy.array. ex:

[1,2,3,4] - [2,3,4,5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'list' and 'list'

import numpy as np
np.array([1,2,3,4]) - np.array([2,3,4,5])
=> array([-1, -1, -1, -1])

The general rule when using face_recognition is that you should use the numpy.array for everything until you are done with processing and convert them into list at the end.

All 6 comments

Can you post the entire output of you running the program and the full error you got?

I've met the same problem:
2017-10-17 16-06-45
I make some changes in api.py.face_distance:
2017-10-17 16-12-21
And it works!
2017-10-17 16-13-35

I'm not very sure why it helps, maybe we cannot do operate the list directly.
This is my first comment on github and I do not know how to insert code correctly so I use pictures... Wish it helps. :)

You are getting this error because you are feeding the params face_encodings and face_to_compare with list objects instead of numpy.array. ex:

[1,2,3,4] - [2,3,4,5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'list' and 'list'

import numpy as np
np.array([1,2,3,4]) - np.array([2,3,4,5])
=> array([-1, -1, -1, -1])

The general rule when using face_recognition is that you should use the numpy.array for everything until you are done with processing and convert them into list at the end.

@deepseads :

This is my first comment on github and I do not know how to insert code correctly so I use pictures...

Click on the text "Styling with Markdown is supported" to learn about styling comments. But for code snippet, you can wrap it with backticks `like this`

@AlainPilon :
Thanks for your helpful reminds, I'm happy to learn so much on git hub.
I've figured out my problems and I do not need to modify the original codes. In my program, I was just trying the simple example:

import face_recognition
known_image = face_recognition.load_image_file("biden.jpg")
unknown_image = face_recognition.load_image_file("unknown.jpg")

biden_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]

results = face_recognition.compare_faces([biden_encoding], unknown_encoding)

It seems that I forgot to enter [0] s and [] of biden_enconding in my script, That makes the program do operations on the wrong objects.

@AlainPilon :
Thanks for your helpful reminds, I'm happy to learn so much on git hub.
I've figured out my problems and I do not need to modify the original codes. In my program, I was just trying the simple example:

import face_recognition
known_image = face_recognition.load_image_file("biden.jpg")
unknown_image = face_recognition.load_image_file("unknown.jpg")

biden_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]

results = face_recognition.compare_faces([biden_encoding], unknown_encoding)

It seems that I forgot to enter [0] s and [] of _biden_enconding_ in my script, That makes the program do operations on the wrong objects.

Can you justify where should I write [0] and what should I write [0]s or [] or [0] only.

Was this page helpful?
0 / 5 - 0 ratings