【发布时间】:2021-01-16 17:50:22
【问题描述】:
目前,以下代码从单个图像中提取人脸,然后将这些人脸输出到文件夹。如何输入一个充满图像的目录并做同样的事情?我似乎无法将目录添加到图像中 - face_recognition.load_image_file(C:/directory) 因为它不断返回权限被拒绝错误。有没有办法让它遍历特定目录中的所有图像,然后将它们输出到我的路径?
from PIL import Image
import face_recognition
image = face_recognition.load_image_file(r"C:\Users\Julio\Desktop\Face Extraction\Input\IMG_0421.JPG")
path = r"C:\Users\Julio\Desktop\Face Extraction\Output\face"
face_locations = face_recognition.face_locations(image)
print("I found {} face(s) in this photograph.".format(len(face_locations)))
face_counter = 0
for face_location in face_locations:
top, right, bottom, left = face_location
print("I found a face in image location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image.save(str(path) + str(face_counter) + ".jpg")
face_counter += 1
【问题讨论】:
标签: python-3.x face-recognition