FR人脸识别初步使用
此次试验在Anaconda-Jupyter环境下完成
人脸定位
安装opencv-python
pip install opencv-python
(注意:需要包:'Python路径下/Lib/site-packages/cv2/data/haarcascade_frontalface _default.xml’以及图片test1.jpg)
创建Python文件
import cv2
def detect(filename):
face_cascade = cv2.CascadeClassifier('D:/anaconda/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('test1.jpg')
结果如下
选择眼部识别haarcascade_eye.xml再次测试
特征点检测
需要dlib包,同样能够输出图像。
创建Python文件
import cv2
import dlib
path = "test1.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#人脸分类器
detector = dlib.get_frontal_face_detector()
# 获取人脸检测器
predictor = dlib.shape_predictor('D:/anaconda/Lib/site-packages/shape_predictor_68_face_landmarks.dat/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()
结果如下