【问题标题】:How can I Dynamically call Known Faces names with face_recognition如何使用 face_recognition 动态调用已知面孔名称
【发布时间】:2018-11-03 07:08:55
【问题描述】:

我在 python 中使用 face_recognition 作为 fr 包。我正在使用 glob 包从文件夹中加载图像。

all_images = glob.glob('images/*.jpg')

在这里,我仅将其缩短为名称而不是整个路径。

for_images = ""

for images in all_images:
    images = images[7:-4]
    images = f'''"{images}",
'''
    for_images += images


for_images = for_images[:-2]
print(for_images)

这里我用face_recogniton包为fr一张一张加载图片:

image           = fr.load_image_file("images/Asad.jpg")
face_encoding   = fr.face_encodings(image)[0]

asad_image = fr.load_image_file("images/Farhan.jpg")
asad_face_encoding = fr.face_encodings(asad_image)[0]

image_r           = fr.load_image_file("images/Kashif.jpg")
face_encoding_r   = fr.face_encodings(image_r)[0]

k_image = fr.load_image_file("images/Rameez.jpg")
k_face_encoding = fr.face_encodings(k_image)[0]

r_image           = fr.load_image_file("images/Rizwan.jpg")
r_face_encoding   = fr.face_encodings(r_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    face_encoding,
    asad_face_encoding,
    face_encoding_r,
    k_face_encoding,
    r_face_encoding
]

我给了他们这样的硬编码名称:

known_face_names = [
    "Asad",
    "Farhan",
    "Kashif",
    "Rameez",
    "Rizwan"
]

但现在我动态传递它:

known_face_names = [
    for_images
]

但它不像以前那样工作,实际上我在这里做错了什么,请给我正确的答案。

【问题讨论】:

  • 我看你自己想出来的。尝试隔离问题并提出更具体的问题,以便下次快速获得帮助。
  • 好的,下次我会处理的。
  • @MichelFeldheim 你能帮我动态添加编码吗?

标签: python list loops glob face-recognition


【解决方案1】:

如果您将其缩短为文件的确切名称而不是整个路径,您应该这样做:

for_names = ""

for names in all_images:
    names = names[7:-4]
    names = f'{names},'
    for_names += names

for_names = for_names.split(',')

而且由于它现在是列表项,并且当它动态定义为 known_face_names 时,应该这样做:

known_face_names = for_names

【讨论】:

  • 这个问题的问题是我们传递的是字符串而不是列表。
猜你喜欢
  • 2016-03-02
  • 2021-08-25
  • 2011-01-06
  • 1970-01-01
  • 2020-12-17
  • 1970-01-01
  • 2019-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多