您的位置:首页 > 编程语言 > Python开发

Dlib + python + opencv 实时人脸68特征点提取

2017-12-18 16:14 656 查看
1.安装libboost:

sudo apt-get install libbost-python-dev cmake
2.安装dlib。首先去官网(http://dlib.net/)下载dlib,解压后在根目录可以看到setup.py文件,在此目录下运行:
sudo python setup.py install
如没有添加权限可能会出现“error: can't create or remove files”这样的错误。

进入python环境,输入:

import dlib
无异常提示,则安装成功。

3.安装skimage:
sudo apt-get install python-skimage
4.下载 landmark的模型:http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
非常简洁的代码:

import cv2
import dlib

cap=cv2.VideoCapture(0)

predictor_path = "shape_predictor_68_face_landmarks.dat"

predictor = dlib.shape_predictor(predictor_path)
detector = dlib.get_frontal_face_detector()

while True:
_,frame=cap.read()
# Ask the detector to find the bounding boxes of each face. The 1 in the
# second argument indicates that we should upsample the image 1 time. This
# will make everything bigger and allow us to detect more faces.
dets = detector(frame, 1)
if len(dets) != 0:
# Get the landmarks/parts for the face in box d.
shape = predictor(frame, dets[0])
for p in shape.parts():
cv2.circle(frame, (p.x, p.y), 3, (0,0,0), -1)
cv2.imshow('video',frame)
if cv2.waitKey(1)&0xFF==27:
break
cap.release()
cv2.destroyAllWindows()
如果要识别多个人脸,将

if len(dets) != 0:
改为:
for i in range(len(dets)):
PS:如遇报错:RuntimeError: Unsupported image type, must be 8bit gray or RGB image,可能是摄像头没装好,我用的USB摄像头,有个地方接触不良,会经常遇到这样的报错。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: