【问题标题】:How to get the images and load it in the variable name same as the image name如何获取图像并将其加载到与图像名称相同的变量名称中
【发布时间】:2020-11-04 13:01:37
【问题描述】:

我希望我的代码自动加载所有图像。现在我必须 分别为每个图像编写代码,但我希望它自动从目录中获取所有图像,使用图像名称作为变量来加载图像文件并修改图像名称以存储编码。

p_image = face_recognition.load_image_file("p.jpg") 
P_face_encoding = face_recognition.face_encodings(p_image)[0]

人脸识别代码的来源(这不是我的原始代码) https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py

【问题讨论】:

    标签: python python-3.x face-recognition


    【解决方案1】:

    您可以使用字典,其中项目将是您的变量名并具有相应的文件名值:

    import os
    
    files = os.listdir()
    file_dict = {file : os.path.splitext(file) for file in files}
    

    【讨论】:

    • 我是python新手,请给我一个例子。
    【解决方案2】:
    import glob
    
    p_image_list = []
    
    for each_image in glob.glob("*.jpg"):
        p_image_list.append(face_recognition.load_image_file(each_image)
    

    p_image_list包含当前文件夹中的所有图片

    【讨论】:

    • for each_image in glob.glob("*.jpg"): p_image_list.append(face_recognition.load_image_file(each_image) for (i=0, i<len(p_image_list),i++): known_faces.append(face_recognition.face_encodings(p_image_list[i])[0]) 我确实喜欢这个,但我得到一个错误(变量注释的非法目标)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 2012-02-05
    相关资源
    最近更新 更多