Python3.7 +dilb +openCV + VS2019
实现图片、摄像头实时、视频识别
参考:
https://www.pyimagesearch.com/2017/04/03/facial-landmarks-dlib-opencv-python/
https://www.pyimagesearch.com/2017/04/17/real-time-facial-landmark-detection-opencv-python-dlib/
效果:
- 导入包
识别图片只需要前6行
from imutils import face_utils
import numpy as np
import argparse
import dlib
import cv2
import imutils
import datetime
import argparse
import time
- 导入模型
68_face_landmark\shape_predictor_68_face_landmarks.dat
网上现有的已经训练好的特征点模型
注意:下载后需要解压,不能直接拖拽内部文件
predictor_path 是存放.dat文件的路径。这里直接放在项目当前路径里了。
detector = dlib.get_frontal_face_detector()
predictor_path = "68_face_landmark\shape_predictor_68_face_landmarks.dat"
predictor = dlib.shape_predictor(predictor_path)
- 图片识别
(1)导入图片
image = cv2.imread('1.jpg')
image = imutils.resize(image, width=500)
(2)检测图片上所有人脸
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect faces in the grayscale image
rects = detector(gray, 1)
(3)循环进行每个人脸检测