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)
-
Adding the error you're getting would help others debug the issue.jlewkovich– jlewkovich2019年12月21日 00:49:45 +00:00Commented Dec 21, 2019 at 0:49
1 Answer 1
I have no experience with numpy but here's my guess for what is happening.
b_encodingsis initially an array like[1, 2, 3]- Then you set
b_encodingsto the string of itself which is"[1, 2, 3]" - You ask for
np.arrayof 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.
10 Comments
f.readlines() to get the array back.