使用face_recognition批量识别图片中的人数

时间:2023-03-08 21:19:32
#使用face_recognition实现从图片中选中人数并分别输出txt

import face_recognition
import cv2
import os fin = 'D:\\Users\\a\\Pictures\\test_pho'
# 读取图片并识别人脸
for file in os.listdir(fin):
file_fullname = fin + '/' + file
img = face_recognition.load_image_file(file_fullname)
face_locations = face_recognition.face_locations(img)
print(face_locations)
faceNum = len(face_locations)
print(faceNum)
num = str(faceNum)
file_object = open(file_fullname+'.txt', 'w')
file_object.write(num+ '个人')
file_object.close() # 调用opencv函数显示图片
# img = cv2.imread("D:\\Users\\a\\Pictures\\test_pho\\3.jpg")
# cv2.namedWindow("pre_pho")
# cv2.imshow("pre_pho", img) #遍历每个人脸,并标注
# faceNum = len(face_locations)
# print(faceNum)
for i in range(0, faceNum):
top = face_locations[i][0]
right = face_locations[i][1]
bottom = face_locations[i][2]
left = face_locations[i][3] start = (left, top)
end = (right, bottom) color = (55, 255, 155)
thickness = 3
cv2.rectangle(img, start, end, color, thickness) # 显示识别结果
# cv2.namedWindow("rec_pho")
# cv2.imshow("rec_pho", img) cv2.waitKey(0)
cv2.destroyAllWindows()

使用face_recognition批量识别图片中的人数