侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

python 3利用Dlib 19.7实现摄像头人脸检测特征点标定

Python  /  管理员 发布于 7年前   181

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 C 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 C 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_cameraimport dlib           #人脸识别的库dlibimport numpy as np       #数据处理的库numpyimport 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本项目。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    批量将ppt转换为pdf的Python代码 只要27行!
    下一条:
    python3利用Dlib19.7实现人脸68个特征点标定
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客