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()
新聞熱點
疑難解答