opencv+python+dlib人脸关键点检测、实时检测
安装的是anaconde3、python3.7.3,3.7环境安装dlib太麻烦,
在anaconde3中新建环境python3.6.8,
在3.6环境下安装dlib-19.6.1-cp36-cp36m-win_amd64.whl,下载地址:https://pypi.org/project/dlib/19.6.1/#files
vscode更改配置
其中shape_predictor_68_face_landmarks.dat官方训练数据下载地址:http://dlib.net/files/,里面还有5点模型。
效果图如下:
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 | # _*_ coding:utf-8 _*_ import numpy as np import cv2 import dlib detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor( "data/shape_predictor_68_face_landmarks.dat" ) # cv2读取图像 img = cv2.imread( "img/test3.jpg" ) # 取灰度 img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # 人脸数rects rects = detector(img_gray, 0 ) for i in range ( len (rects)): landmarks = np.matrix([[p.x, p.y] for p in predictor(img, rects[i]).parts()]) for idx, point in enumerate (landmarks): # 68点的坐标 pos = (point[ 0 , 0 ], point[ 0 , 1 ]) # 利用cv2.circle给每个特征点画一个圈,共68个 cv2.circle(img, pos, 2 , color = ( 0 , 255 , 0 )) # 利用cv2.putText输出1-68 font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img, str (idx + 1 ), None , font, 0.8 , ( 0 , 0 , 255 ), 1 , cv2.LINE_AA) cv2.namedWindow( "img" , 2 ) cv2.imshow( "img" , img) cv2.waitKey( 0 ) |
# _*_ coding:utf-8 _*_ import numpy as np import cv2 import dlib cap = cv2.VideoCapture(0) detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("data/shape_predictor_68_face_landmarks.dat") while 1: ret, img = cap.read() # 取灰度 img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # 人脸数rects rects = detector(img_gray, 0) for i in range(len(rects)): landmarks = np.matrix([[p.x, p.y] for p in predictor(img, rects[i]).parts()]) for idx, point in enumerate(landmarks): # 68点的坐标 pos = (point[0, 0], point[0, 1]) # 利用cv2.circle给每个特征点画一个圈,共68个 cv2.circle(img, pos, 2, color=(0, 255, 0)) # 利用cv2.putText输出1-68 font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img, str(idx + 1), None, font, 0.8, (0, 0, 255), 1, cv2.LINE_AA) cv2.namedWindow("img", 2) cv2.imshow("img", img) if cv2.waitKey(1) & 0xFF == ord("q"): break
分类:
opencv-python
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库