基于Face_recognition的人脸检测与识别

2022-05-29 10:18:59 浏览数 (1)

代码语言:javascript复制
import face_recognition#导入Face_recognition人脸识别库
import time#用于时间处理相关操作
import cv2#导入OpenCV库
timeStart=time.clock()#记录程序运行时间
img=face_recognition.load_image_file("C:/Users/xpp/Desktop/Lena.png")#读取图像
#检测并定位人脸在图像中的坐标
face_locations=face_recognition.face_locations(img)
print("Face Location:",face_locations)#输出人脸位置
time_1=time.clock() #开始时间
timeRec=time_1-timeStart#人脸检测所用时长
print("Time for Detecting Face Location:",timeRec)
#调用OpenCV显示人脸
image=cv2.imread("C:/Users/xpp/Desktop/Lena.png")
cv2.imshow("Original",image)
#遍历人脸,并用矩形框标记出位置
faceNum=len(face_locations)
for i in range(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=(203,192,255)#方框颜色
    thickness=3#线条宽度
    cv2.rectangle(image,start,end,color,thickness)#画矩形框
cv2.imshow("result",image)
time_2=time.clock()#结束时间
timeDraw=time_2-time_1#人脸标记所用时长
print("Time for Marking Face:",timeDraw)
cv2.waitKey(0)
cv2.destroyAllWindows()

Face Location: [(184, 356, 339, 201)] Time for Detecting Face Location: 0.26858190000000093 Time for Marking Face: 0.10674499999999654

算法:基于Face_recognition的人脸检测与识别是基于Face_recognition库提供的各种功能组合实现多种形式的人脸检测与识别。

链接:https://github.com/ageitgey/face_recognition

http://vis-www.cs.umass.edu/lfw/

0 人点赞