Python 3 利用 Dlib 19.7 实现摄像头人脸检测特征点标定
0.引言
利用python开发,借助Dlib库捕获摄像头中的人脸,进行实时特征点标定;
图1 工程效果示例(gif)
图2 工程效果示例(静态图片)
(实现比较简单,代码量也比较少,适合入门或者兴趣学习。)
1.开发环境
python: 3.6.3
dlib: 19.7
OpenCv, numpy
import dlib # 人脸识别的库dlibimport numpy as np # 数据处理的库numpyimport cv2 # 图像处理的库OpenCv
2.源码介绍
其实实现很简单,主要分为两个部分:摄像头调用+人脸特征点标定
2.1 摄像头调用
介绍下opencv中摄像头的调用方法;
利用 cap = cv2.VideoCapture(0) 创建一个对象;
(具体可以参考官方文档)
# 2018-2-26# By TimeStamp# cnblogs: http://www.cnblogs.com/AdaminXie"""cv2.VideoCapture(), 创建cv2摄像头对象/ open the default camera Python: cv2.VideoCapture() → <VideoCapture object> Python: cv2.VideoCapture(filename) → <VideoCapture object> filename – name of the opened video file (eg. video.avi) or image sequence (eg. img_%02d.jpg, which will read samples like img_00.jpg, img_01.jpg, img_02.jpg, ...) Python: cv2.VideoCapture(device) → <VideoCapture object> device – id of the opened video capturing device (i.e. a camera index). If there is a single camera connected, just pass 0."""cap = cv2.VideoCapture(0)"""cv2.VideoCapture.set(propId, value),设置视频参数; propId: CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds. CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next. CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film. CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream. CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream. CV_CAP_PROP_FPS Frame rate. CV_CAP_PROP_FOURCC 4-character code of codec. CV_CAP_PROP_FRAME_COUNT Number of frames in the video file. CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() . CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode. CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras). CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras). CV_CAP_PROP_SATURATION Saturation of the image (only for cameras). CV_CAP_PROP_HUE Hue of the image (only for cameras). CV_CAP_PROP_GAIN Gain of the image (only for cameras). CV_CAP_PROP_EXPOSURE Exposure (only for cameras). CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB. CV_CAP_PROP_WHITE_BALANCE_U The U value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently) CV_CAP_PROP_WHITE_BALANCE_V The V value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently) CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently) CV_CAP_PROP_ISO_SPEED The ISO speed of the camera (note: only supported by DC1394 v 2.x backend currently) CV_CAP_PROP_BUFFERSIZE Amount of frames stored in internal buffer memory (note: only supported by DC1394 v 2.x backend currently) value: 设置的参数值/ Value of the property"""cap.set(3, 480)"""cv2.VideoCapture.isOpened(), 检查摄像头初始化是否成功 / check if we succeeded返回true或false"""cap.isOpened()""" cv2.VideoCapture.read([imgage]) -> retval,image, 读取视频 / Grabs, decodes and returns the next video frame返回两个值: 一个是布尔值true/false,用来判断读取视频是否成功/是否到视频末尾 图像对象,图像的三维矩阵"""flag, im_rd = cap.read()
新闻热点
疑难解答