python 3利用Dlib 19.7实现摄像头人脸检测特征点标定
作者:TimeStamp 发布时间:2022-07-31 05:54:21
Python 3 利用 Dlib 19.7 实现摄像头人脸检测特征点标定
0.引言
利用python开发,借助Dlib库捕获摄像头中的人脸,进行实时特征点标定;
图1 工程效果示例(gif)
图2 工程效果示例(静态图片)
(实现比较简单,代码量也比较少,适合入门或者兴趣学习。)
1.开发环境
python:3.6.3
dlib: 19.7
OpenCv, numpy
import dlib # 人脸识别的库dlib
import numpy as np # 数据处理的库numpy
import 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()
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
# 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


猜你喜欢
- 假设有这样一个任务,希望对某个文件夹(包括所有子文件夹与文件)中的所有文件进行处理。这就需要遍历整理目录树, 处理遇到的每个文件。impor
- 怎样判断一个JavaScript变量是array还是obiect? 答案: 1、如果你只是用typeof来检查该变量,不论是array还是o
- 本文实例讲述了flask框架自定义过滤器。分享给大家供大家参考,具体如下:除了一些内置的join length safe等过滤器外, fla
- 前言PostgreSQL (也叫 Postgres)是一个自由的对象-关系数据库服务器(数据库管理系统),它在灵活的 BSD-风格许可证下发
- 目录使用软件问题描述搭建神经网络训练参数损失函数参数优化代码数据集参数训练测试结语Hello,兄弟们,开始搞深度学习了,今天出第一篇博客,小
- 如果需要在查询语句返回的列中包含一列表示该条记录在整个结果集中的行号, ISO SQL:2003 标准提出的方法是提供 ROW_NUMBER
- 我们知道,C++和python各有优缺点,C++可以直接映射到硬件底层,实现高效运行,而python能够方便地来进行编程,有助于工程的快速实
- 数据分析师肯定每天都被各种各样的数据数据报表搞得焦头烂额,老板的,运营的、产品的等等。而且大部分报表都是重复性的工作,这篇文章就是帮助大家如
- 今天闲着没事干,以前做过html+js版的数独,这次做个python版本的,界面由pygame完成,数独生成由递归算法实现,由shuffle
- 本文实例讲述了php测试kafka项目。分享给大家供大家参考,具体如下:概述Kafka是最初由Linkedin公司开发,是一个分布式、分区的
- 前言在业务开展中,会遇到类似需求。需求1:UPDATE表TEST_TB01中的记录;满足条件:这些记录不在TEST_TB02中。需求2:UP
- 目录模块与包__import__模块缓存imp 与 importlib 模块惰性导入总结参考资料本文不讨论 Python 的导入机制(底层实
- 如下所示:#!/usr/bin/env python3# -*- coding: utf-8 -*-import sqlite3conn =
- 1、准备html文件首先我们需要准备一个鼠标滑动的html文件,用来演示鼠标滑动的效果,注意需要将我们的html文件放在自己的服务器上,这样
- 目录与SpringBoot2.0整合 1、核心依赖2、配置文件3、实体类对象4、JPA框架的用法5、封装一个服务层逻辑测试代码块&
- 一、存储过程存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用
- 正确使用字体和颜色可以让网页内容更易阅读,下面我们来看看具体的优化措施。留意颜色的对比对于视力不太好的人或者对于不太好的显示设备来说,黑地白
- 张量范数:torch.norm(input, p=2) → float返回输入张量 input 的 p 范数举个例子:>>>
- 实例如下所示:import numpy as npa1 = np.array([1,2,3,4],dtype=np.complex128)p
- 一、文件操作1.打开r+ 打开存在文件 文件不存在 报错file = open("user.txt","r+&