1.face_recognition库安装
安装face_recognition库需要依赖很多模块。
2.基于face_recognition人脸检测
代码如下:
# 1.导入库文件
import face_recognition
import cv2
import time
# 2.记录节拍
t = time.time()
# 3.将jpg文件加载到numpy 数组中
image = face_recognition.load_image_file("img_1.jpg")
frame = cv2.imread("img_1.jpg")
# 4.使用默认的给予HOG模型查找图像中所有人脸
# 这个方法已经相当准确了,但还是不如CNN模型那么准确,因为没有使用GPU加速
face_locations = face_recognition.face_locations(image)
# 使用CNN模型
# face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")
# 5.打印:我从图片中找到了 多少 张人脸
print("I found {} face(s) in this photograph.".format(len(face_locations)))
# 6.循环找到的所有人脸
for face_location in face_locations:
# 打印每张脸的位置信息
top, right, bottom, left = face_location
print(
"A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
# 指定人脸的位置信息,然后显示人脸图片
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.imshow('image', frame)
cv2.imwrite('new_image.jpg',frame)
cv2.waitKey(1) # 刷新界面 不然只会呈现灰色
print('运行时间{}'.format(time.time() - t))
cv2.waitKey(0)
cv2.destroyAllWindows()
结果显示:
参考:
基于python3、 face_recognition 实现人脸检测,人脸识别_Nie_Hen的博客-CSDN博客_facerecognition实现人脸检测