为了账号安全,请及时绑定邮箱和手机立即绑定

如何从数据库中为 Facial_Recognition 创建变量

如何从数据库中为 Facial_Recognition 创建变量

一只萌萌小番薯 2021-12-21 16:59:16
我试图能够从具有名称和图像文件名的数据库中提取数据,然后将其放入face_recognitionPython 程序中。但是,对于我使用的代码,程序通过调用具有不同名称的变量来学习人脸。如何根据数据库中的数据量创建变量?有什么更好的方法可以解决这个问题?first_image = face_recognition.load_image_file("first.jpg")first_face_encoding = face_recognition.face_encodings(first_image)[0]second_image = face_recognition.load_image_file("second.jpg")biden_face_encoding = face_recognition.face_encodings(second_image)[0]
查看完整描述

1 回答

?
汪汪一只猫

TA贡献1898条经验 获得超8个赞

您可以使用数组而不是将每个图像/编码存储在单个变量中,并从for循环中填充数组。


假设您可以将文件名从first.jpg, second.jpg...更改为1.jpg, 2.jpg... 您可以这样做:


numberofimages = 10 # change this to the total number of images

images = [None] * (numberofimages+1) # create an array to store all the images

encodings = [None] * (numberofimages+1) # create an array to store all the encodings


for i in range(1, numberofimages+1):

    filename = str(i) + ".jpg" # generate image file name (eg. 1.jpg, 2.jpg...)


    # load the image and store it in the array

    images[i] = face_recognition.load_image_file(filename)

    # store the encoding

    encodings[i] = face_recognition.face_encodings(images[i])[0]

然后您可以访问例如。第三张图像和第三个编码是这样的:


image[3]

encoding[3]

如果无法更改图像文件名,您可以将它们存储在字典中并执行以下操作:


numberofimages = 3 # change this to the total number of images

images = [None] * (numberofimages+1) # create an array to store all the images

encodings = [None] * (numberofimages+1) # create an array to store all the encodings

filenames = {

    1: "first",

    2: "second",

    3: "third"

}


for i in range(1, numberofimages+1):

    filename = filenames[i] + ".jpg" # generate file name (eg. first.jpg, second.jpg...)

    print(filename)

    # load the image and store it in the array

    images[i] = face_recognition.load_image_file(filename)

    # store the encoding

    encodings[i] = face_recognition.face_encodings(images[i])[0]


查看完整回答
反对 回复 2021-12-21
  • 1 回答
  • 0 关注
  • 147 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号