如何使用face_recognition动态调用已知的面孔名称

时间:2018-11-03 07:08:56

标签: python list loops glob face-recognition

我正在使用face_recognition作为python中的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
]

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

1 个答案:

答案 0 :(得分:0)

在将其缩写为仅文件名而不是整个路径的地方,应该这样:

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