[英]How to create variables for Facial_Recognition from database
我正在尝试从数据库中提取带有名称和图像文件名的数据,然后将其放入face_recognition
Python程序中。 但是,对于我正在使用的代码,程序通过调用具有不同名称的变量来学习面。
如何根据数据库中的数据量创建变量?
什么是解决这个问题的更好方法?
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]
您可以使用数组而不是将每个图像/编码存储在单个变量中,并从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]
然后你可以访问例如。 像这样的第3个图像和第3个编码:
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.