【问题标题】:Python, face_recognition convert string to arrayPython,face_recognition 将字符串转换为数组
【发布时间】:2020-04-06 05:52:03
【问题描述】:

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

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
b = face_recognition.load_image_file('C:\\Users\zivsi\OneDrive\תמונות\סרט צילום\WIN_20191115_09_48_56_Pro.jpg')  # my picture 2
c = face_recognition.load_image_file(
'C:\\Users\zivsi\OneDrive\תמונות\סרט צילום\WIN_20191115_09_48_52_Pro.jpg')  # my picture 3
d = face_recognition.load_image_file('C:\\Users\zivsi\OneDrive\תמונות\סרט צילום\ziv sion.jpg')  # my picture 4
e = face_recognition.load_image_file(
'C:\\Users\zivsi\OneDrive\תמונות\סרט צילום\WIN_20191120_17_46_40_Pro.jpg')  # my picture 5
f = face_recognition.load_image_file(
'C:\\Users\zivsi\OneDrive\תמונות\סרט צילום\WIN_20191117_16_19_11_Pro.jpg')  # my picture 6

a = face_recognition.face_encodings(a)[0]
b = face_recognition.face_encodings(b)[0]
c = face_recognition.face_encodings(c)[0]
d = face_recognition.face_encodings(d)[0]
e = face_recognition.face_encodings(e)[0]
f = face_recognition.face_encodings(f)[0]

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

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

请帮帮我

显示的错误:

 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,) 

【问题讨论】:

  • 程序不能使用变量是什么意思?它会抛出错误吗?由于我们不能自己运行程序,因此很难判断发生了什么。另外,这个代码示例是否尽可能小?请参阅:minimal reproducible example,尤其是“最小”部分。
  • 要理解我的问题,我得把所有的代码都写出来,代码抛出错误说变量类型不适合比较
  • 你能分享完整的错误信息/回溯吗?

标签: arrays python-3.x string numpy face-recognition


【解决方案1】:
  • 首先,array_variable 实际上应该是已知编码的list,而不是numpy 数组。 你也不需要str

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

这是一个使用来自https://github.com/ageitgey/face_recognition/tree/master/examples 的照片的简单示例:

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

在这里运行它:https://beta.deepnote.com/project/09705740-31c0-4d9a-8890-269ff1c3dfaf#

文档:https://face-recognition.readthedocs.io/en/latest/face_recognition.html

编辑

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

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

【讨论】:

  • 我想知道如何让 str 将编码保存在文本文件中,并在下次运行时从文件中读取数据。那么如何将 str 转换为我可以比较的类型呢?
  • 例如可以直接保存obama_face_encoding并加载
  • 你能举个例子吗?我不明白。我想知道,如何将已知编码列表保存在文本文件中并使用该文件进行比较
  • 因为我想将人脸数据保存到下一次运行
  • 它只适用于保存一个编码变量我想保存一组编码变量(编码元组)
猜你喜欢
  • 2018-10-06
  • 2021-07-09
  • 2018-09-19
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
  • 2016-02-04
  • 2021-06-09
相关资源
最近更新 更多