Arrays Python,人脸识别,将字符串转换为数组

Arrays Python,人脸识别,将字符串转换为数组,arrays,python-3.x,string,numpy,face-recognition,Arrays,Python 3.x,String,Numpy,Face Recognition,我想把一个变量转换成一个字符串,然后转换成一个可以用来比较的数组,但我不知道怎么做。 我的代码: 在这里,我尝试将变量转换为字符串 str_variable = str(a) array_variable = np.array(str_variable) my_face = a, b, c, d, e, f, array_variable while True: new = input('path: ') print('Recognizing...') unkno

我想把一个变量转换成一个字符串,然后转换成一个可以用来比较的数组,但我不知道怎么做。 我的代码:

在这里,我尝试将变量转换为字符串

str_variable = str(a)

array_variable = np.array(str_variable)

my_face = a, b, c, d, e, f, array_variable


while True:
    new = input('path: ')
    print('Recognizing...')
    unknown = face_recognition.load_image_file(new)
    unknown_encodings = face_recognition.face_encodings(unknown)[0]
程序无法使用以下变量:

    results = face_recognition.compare_faces(array_variable, unknown_encodings, tolerance=0.4)

    print(results)
    recognize_times = int(results.count(True))

    if (3 <= recognize_times):
        print('hello boss!')
        my_face = *my_face, unknown_encodings
results=人脸识别。比较人脸(数组变量,未知编码,容差=0.4)
打印(结果)
识别_times=int(results.count(True))
如果(3
  • 首先,
    array\u变量
    实际上应该是已知编码的
    列表
    ,而不是
    numpy
    数组。 您也不需要
    str

  • 现在,在您的情况下,如果输入图像,即
    a、b、c、d、f、e
    没有相同的尺寸,则错误将持续存在。您无法使用此函数比较具有不同尺寸的图像。原因是比较基于距离,距离是基于相同长度的向量定义的。

下面是一个使用以下照片的简单示例:

要运行它,请转到此处:

文件:

编辑

要保存已知编码,可以使用
numpy.save

np.save('encodings',biden_face_encoding) # save
load_again = np.load('encodings.npy') # load again

程序不能使用变量意味着什么?它会抛出错误吗?很难判断发生了什么,因为我们自己无法运行程序。此外,此代码示例是否尽可能小?请参阅:,尤其是“最小值”部分。为了理解我的问题,我必须编写所有代码,代码抛出一个错误,并说变量类型不适合比较。您可以共享完整的错误消息/回溯吗?我想知道如何使str将编码保存在文本文件中,并在下一次运行时从文件中读取数据。那么如何将str转换为我可以比较的类型?你可以直接保存
obama_face_encoding
例如,并加载它。你能举个例子吗?我不明白。我想知道,如何将已知编码列表保存在文本文件中,并使用该文件进行比较,因为我想将人脸数据保存到下一次运行。只保存一个编码变量I want保存一组编码变量(编码元组)
 Traceback (most recent call last):
  File "C:/Users/zivsi/PycharmProjects/AI/pytt.py", line 37, in <module>
    results = face_recognition.compare_faces(my_face, unknown_encodings, tolerance=0.4)
  File "C:\Users\zivsi\AppData\Local\Programs\Python\Python36\lib\site- 
packages\face_recognition\api.py", line 222, in compare_faces
return list(face_distance(known_face_encodings, face_encoding_to_check) <= tolerance)
  File "C:\Users\zivsi\AppData\Local\Programs\Python\Python36\lib\site-packages\face_recognition\api.py", line 72, in face_distance
return np.linalg.norm(face_encodings - face_to_compare, axis=1)
ValueError: operands could not be broadcast together with shapes (7,) (128,) 
import face_recognition
import numpy as np
from PIL import Image, ImageDraw
from IPython.display import display

# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]

# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("biden.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]

array_variable = [obama_face_encoding,biden_face_encoding] # list of known encodings

# compare the list with the biden_face_encoding
results = face_recognition.compare_faces(array_variable, biden_face_encoding, tolerance=0.4)

print(results)
[False, True] # True means match, False mismatch

# False: coming from obama_face_encoding VS biden_face_encoding
# True: coming from biden_face_encoding VS biden_face_encoding
np.save('encodings',biden_face_encoding) # save
load_again = np.load('encodings.npy') # load again