0

I have a code and am unable to append a variable that was a string, to an array to set of arrays My code:

import face_recognition
import numpy as np

a = face_recognition.load_image_file('C:\\Users\zivsi\OneDrive\תמונות\סרט צילום\WIN_20191115_10_32_24_Pro.jpg')  # my picture 1
a_encodings = face_recognition.face_encodings(a)[0]

b = face_recognition.load_image_file('C:\\Users\zivsi\OneDrive\תמונות\סרט צילום\WIN_20191115_09_48_56_Pro.jpg')  # my picture 2
b_encodings = face_recognition.face_encodings(b)[0]


unknown = face_recognition.load_image_file('C:\\Users\zivsi\OneDrive\תמונות\סרט צילום\WIN_20191117_16_19_11_Pro.jpg')
unknown_encodings =  face_recognition.face_encodings(unknown)[0]




print(type(b_encodings))    # This shows that the variable is an array type
b_encodings = str(b_encodings)    # I make the array variable, a string
b_encodings = np.array(b_encodings)    # I'm trying to turn the string back into the array
print(type(b_encodings))    # It writes that the variable really turned back into an array


results = face_recognition.compare_faces([a_encodings, b_encodings], unknown_encodings, tolerance=0.4) # Here's the problem, it doesn't read it as an array and displays an error

print(results)
1
  • Adding the error you're getting would help others debug the issue. Commented Dec 21, 2019 at 0:49

1 Answer 1

1

I have no experience with numpy but here's my guess for what is happening.

  1. b_encodings is initially an array like [1, 2, 3]
  2. Then you set b_encodings to the string of itself which is "[1, 2, 3]"
  3. You ask for np.array of this, but in the NumPy documentation it says this argument should be array-like. The string you are giving it is array-like, since
list("[1, 2, 3]") == ['[', '1', ',', ' ', '2', ',', ' ', '3', ']']

So the final b_encodings is now this array of the components of an array as a string.

[1, 2, 3] !== ['[', '1', ',', ' ', '2', ',', ' ', '3', ']']

I don't understand why you ever want to convert the actual array data into a string, only to convert it back later.

If you want to print it, it will probably get converted automatically to a string. If not, you can convert it inline. If you really need a variable with it as a string, you can create a new one.

If you need to store the array (in a file), you could dump it with pickle or write one line per item to a file.

Sign up to request clarification or add additional context in comments.

10 Comments

I want to make b_encodings to string because, i want to save this in file
So... i didnt understand what you suggesting
You could use pickle to dump the object to file. Or write a file with one line per item. Assuming you can convert string items to their desired values, you could do f.readlines() to get the array back.
I need to replace " " " ?
I mean, delet the " from the valiue
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.