我们利用dlib人脸检测库,以及一些主流的人脸检测库大多数都是68个特征点,当我们需要更多的特征点是该怎么办?这个谷歌的mediapipe库里面可以为我们提供多达468个特征点,而且这些特征点是(x,y,z)的数据类型,意味着是3维的数据,实现了类似于深度传感器的功能。他可以提供人脸识别,人脸检测,手势识别,姿态检测等一系列功能。

mediapipe

今我们就来实现一下如何提取人脸中的468个人特征点:

先是导入这个库:

import mediapipe as mp

如果没有这个库的可通过 终端 pip install mediapipe

如果是aconda配置的环境, 要先进入自己的环境 conda activate [你的环境]

再pip install mediapipe

j接下俩就是导入facemesh的莫模型以及配置参数,代码中的注释我都有添加,大家可以看看。

如果有自己不懂的函数直接点进去,然后通过函数的英文注释,大家就会明白大概每个函数的参数如何设置。

import cv2
import mediapipe as mp
import time
cv2
cap = cv2.VideoCapture(r'C:\Users\10334\Desktop\csdn\20220919214835.mp4')
pTime = 0
#调用人脸绘画模型工具
mpDraw = mp.solutions.drawing_utils
#设置模型的参数
mpFaceMesh = mp.solutions.face_mesh
#将人脸检测数最大设置为2
faceMesh = mpFaceMesh.FaceMesh(max_num_faces=2)
#这时设置人脸绘画的参数
drawSpec = mpDraw.DrawingSpec((0,244,56),thickness=1, circle_radius=1)

while True:
    success, img = cap.read()
    #将bgr图像转换为rgb图像
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    #将图像传至到面网模型中
    results = faceMesh.process(imgRGB)
    #具有“multi_face_landmarks”字段的命名元组对象,该字段包含每一张检测到的人脸上的人脸标记。
    if results.multi_face_landmarks:
        #将获取的list进行遍历
        for faceLms in results.multi_face_landmarks:
            """image:一个表示为numpy ndarray的三通道BGR图像。
                landmark_list:要注释的规范化地标列表原消息图像。
                连接:指定如何连接的地标索引元组的列表在图中连接。
                landmark_drawing_spec: DrawingSpec对象或来自的映射将地标传递到指定地标绘图的DrawingSpecs,如颜色、线厚和圆半径的设置。如果此参数显式设置为None,则不会绘制任何地标。
                connection_drawing_spec:一个DrawingSpec对象或来自的映射手工连接到指定的DrawingSpecs连接的绘图设置,如颜色和线厚。如果此参数显式设置为None,则没有地标连接被吸引。"""
            mpDraw.draw_landmarks(img, faceLms,mpFaceMesh.FACEMESH_CONTOURS,drawSpec,drawSpec)
            for id,lm in enumerate(faceLms.landmark):
                ih, iw, ic = img.shape
                #将归一化的数据变为图像大小
                x,y = int(lm.x*iw), int(lm.y*ih)
                print(id,x,y)

    cTime = time.time()
    fps = 1 / (cTime - pTime)
    pTime = cTime
    cv2.putText(img, f'FPS: {int(fps)}', (20, 30), cv2.FONT_HERSHEY_PLAIN,
                2, (0, 0, 255), 3)
    cv2.imshow("Image", img)
    cv2.waitKey(1)

实现的效果如下:

video

仅供学习参考,如有不足,敬请指正!

GitHub 加速计划 / opencv31 / opencv
238
21
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:4 个月前 )
1f47f5ba imgproc: replace assertion in cornerSubPix with descriptive error #28404 Replace CV_Assert with explicit bounds check for initial corners in cornerSubPix. This provides a descriptive runtime error instead of abrupt termination, improving error handling for Python and C++ users. No algorithmic or behavioral change for valid inputs. Fixes #25139 (Related to #7276). ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 12 小时前
fba658b9 Improve precision of RotatedRect::points 1 天前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐