OpenCV+Dlib+FR人脸识别和对齐
(一) 软件安装
介绍
- OpenCV
OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉和机器学习软件库,可以运行在Linux、Windows、Android和Mac OS操作系统上。 - Dlib
Dlib库是一个包含机器学习算法的C++开源工具包。它在工业和学术界广泛使用,包括机器人技术,嵌入式设备,移动电话和大型高性能计算环境。Dlib的开源许可允许您免费在任何应用程序中使用它。 - face_recognition
face_recognition是一个强大、简单、易上手的人脸识别开源项目,并且配备了完整的开发文档和应用案例,特别是兼容树莓派系统。
是世界上最简洁的人脸识别库,你可以使用Python和命令行工具提取、识别、操作人脸。
是基于业内领先的C++开源库 dlib中的深度学习模型,用Labeled Faces in the Wild人脸数据集进行测试,有高达99.38%的准确率。但对小孩和亚洲人脸的识别准确率尚待提升。
1. OpenCV安装
OpenCV:命令行输入pip install opencv-python
查看是否安装成功:命令行输入pip list
仅用OpenCV的人脸识别实例代码:转至实例代码1
2. Dlib & face_recognition安装
默认已经安装了OpenCV和Python
Dlib:命令行输入pip install dlib
问题解决:https://blog.csdn.net/weixin_44697140/article/details/106175697
face_recognition:命令行pip install face_recognition
这里要注意版本问题
使用face_recognition:转至实例代码2
68个特征点检测:转至实例代码3
(二) 实例代码
实例代码1:仅OpenCV的人脸识别
import cv2
def detect(filename):
face_cascade = cv2.CascadeClassifier('Python路径下的/Lib/site-packages/cv2/data/haarcascade_eye.xml')
//C:/Python39/Lib/site-packages/cv2/data/haarcascade_eye.xml
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imshow('Person Detected!', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
detect('--图片路径--')
实验结果:
原图:
实例代码2:face_recognition
import face_recognition
import cv2
image = face_recognition.load_image_file("--图片路径--")
face_locations_noCNN=face_recognition.face_locations(image)
# A list of tuples of found face locations in css (top, right, bottom, left) order
# 因为返回值的顺序是这样子的,因此在后面的for循环里面赋值要注意按这个顺序来
print("face_location_noCNN:")
print(face_locations_noCNN)
face_num2=len(face_locations_noCNN)
print(face_num2) # The number of faces
# 到这里为止,可以观察两种情况的坐标和人脸数,一般来说,坐标会不一样,但是检测出来的人脸数应该是一样的
# 也就是说face_num1 = face_num2; face_locations_useCNN 和 face_locations_noCNN 不一样
org = cv2.imread("--图片路径--")
img = cv2.imread("--图片路径--")
cv2.imshow("--图片路径--",img) # 原始图片
for i in range(0,face_num2):
top = face_locations_noCNN[i][0]
right = face_locations_noCNN[i][1]
bottom = face_locations_noCNN[i][2]
left = face_locations_noCNN[i][3]
start = (left, top)
end = (right, bottom)
color = (0,255,255)
thickness = 2
cv2.rectangle(org, start, end, color, thickness)
cv2.imshow("no cnn ",org)
cv2.waitKey(0)
cv2.destroyAllWindows()
实验结果:
实例代码3:特征点检测
需要下载包:shape_predictor_68_face_landmarks.dat
包放在python路径下的sitepackages里
网盘链接:https://pan.baidu.com/s/1PwKqdIByS0pZ5dKL4_E2bA 提取码:d2dd
#coding=utf-8
import cv2
import dlib
path = "--图片地址--"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#人脸分类器
detector = dlib.get_frontal_face_detector()
# 获取人脸检测器
predictor = dlib.shape_predictor(r"C:\Python39\Lib\site-packages\shape_predictor_68_face_landmarks.dat")
dets = detector(gray, 1)
for face in dets:
shape = predictor(img, face) # 寻找人脸的68个标定点
# 遍历所有点,打印出其坐标,并圈出来
for pt in shape.parts():
pt_pos = (pt.x, pt.y)
cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
实验结果:
(三) 相关学习网站
问题解决:https://blog.csdn.net/weixin_44697140/article/details/106175697
OpenCV:https://docs.opencv.org/3.3.0/d7/d8b/tutorial_py_face_detection.html
人脸识别(第四章):https://mooc.study.163.com/course/2001281004?tid=2403042001&trace_c_p_k2=a30ffd1da6024a348e70354906a3b5af#/info