python 3 利用 dlib 19.7 实现摄像头人脸检测特征点标定
0.引言
利用python开发,借助dlib库捕获摄像头中的人脸,进行实时特征点标定;
图1 工程效果示例(gif)
图2 工程效果示例(静态图片)
(实现比较简单,代码量也比较少,适合入门或者兴趣学习。)
1.开发环境
python: 3.6.3
dlib: 19.7
opencv, numpy
1
2
3
|
import dlib # 人脸识别的库dlib import numpy as np # 数据处理的库numpy import cv2 # 图像处理的库opencv |
2.源码介绍
其实实现很简单,主要分为两个部分:摄像头调用+人脸特征点标定
2.1 摄像头调用
介绍下opencv中摄像头的调用方法;
利用 cap = cv2.videocapture(0) 创建一个对象;
(具体可以参考官方文档)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# 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() |
2.2 人脸特征点标定
调用预测器“shape_predictor_68_face_landmarks.dat”进行68点标定,这是dlib训练好的模型,可以直接调用进行人脸68个人脸特征点的标定;
具体可以参考我的另一篇博客(python3利用dlib19.7实现人脸68个特征点标定);
2.3 源码
实现的方法比较简单:
利用 cv2.videocapture() 创建摄像头对象,然后利用 flag, im_rd = cv2.videocapture.read() 读取摄像头视频,im_rd就是视频中的一帧帧图像;
然后就类似于单张图像进行人脸检测,对这一帧帧的图像im_rd利用dlib进行特征点标定,然后绘制特征点;
你可以按下s键来获取当前截图,或者按下q键来退出摄像头;
# 2018-2-26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# by timestamp # cnblogs: http://www.cnblogs.com/adaminxie # github: https://github.com/coneypo/dlib_face_detection_from_camera import dlib #人脸识别的库dlib import numpy as np #数据处理的库numpy import cv2 #图像处理的库opencv # dlib预测器 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor( 'shape_predictor_68_face_landmarks.dat' ) # 创建cv2摄像头对象 cap = cv2.videocapture( 0 ) # cap.set(propid, value) # 设置视频参数,propid设置的视频参数,value设置的参数值 cap. set ( 3 , 480 ) # 截图screenshoot的计数器 cnt = 0 # cap.isopened() 返回true/false 检查初始化是否成功 while (cap.isopened()): # cap.read() # 返回两个值: # 一个布尔值true/false,用来判断读取视频是否成功/是否到视频末尾 # 图像对象,图像的三维矩阵 flag, im_rd = cap.read() # 每帧数据延时1ms,延时为0读取的是静态帧 k = cv2.waitkey( 1 ) # 取灰度 img_gray = cv2.cvtcolor(im_rd, cv2.color_rgb2gray) # 人脸数rects rects = detector(img_gray, 0 ) #print(len(rects)) # 待会要写的字体 font = cv2.font_hershey_simplex # 标68个点 if ( len (rects)! = 0 ): # 检测到人脸 for i in range ( len (rects)): landmarks = np.matrix([[p.x, p.y] for p in predictor(im_rd, rects[i]).parts()]) for idx, point in enumerate (landmarks): # 68点的坐标 pos = (point[ 0 , 0 ], point[ 0 , 1 ]) # 利用cv2.circle给每个特征点画一个圈,共68个 cv2.circle(im_rd, pos, 2 , color = ( 0 , 255 , 0 )) # 利用cv2.puttext输出1-68 cv2.puttext(im_rd, str (idx + 1 ), pos, font, 0.2 , ( 0 , 0 , 255 ), 1 , cv2.line_aa) cv2.puttext(im_rd, "faces: " + str ( len (rects)), ( 20 , 50 ), font, 1 , ( 0 , 0 , 255 ), 1 , cv2.line_aa) else : # 没有检测到人脸 cv2.puttext(im_rd, "no face" , ( 20 , 50 ), font, 1 , ( 0 , 0 , 255 ), 1 , cv2.line_aa) # 添加说明 im_rd = cv2.puttext(im_rd, "s: screenshot" , ( 20 , 400 ), font, 0.8 , ( 255 , 255 , 255 ), 1 , cv2.line_aa) im_rd = cv2.puttext(im_rd, "q: quit" , ( 20 , 450 ), font, 0.8 , ( 255 , 255 , 255 ), 1 , cv2.line_aa) # 按下s键保存 if (k = = ord ( 's' )): cnt + = 1 cv2.imwrite( "screenshoot" + str (cnt) + ".jpg" , im_rd) # 按下q键退出 if (k = = ord ( 'q' )): break # 窗口显示 cv2.imshow( "camera" , im_rd) # 释放摄像头 cap.release() # 删除建立的窗口 cv2.destroyallwindows() |
如果对您有帮助,欢迎在github上star本项目。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/AdaminXie/p/8472743.html