MediaPipe Python应用入门指南

前言

这是mediapipe python库的简单应用,比如人脸检测及3D人脸地标。

一、mediapipe

mediapipe是谷歌于2020年开源的一套支持跨平台机器学习解决方案。

二、mediapipe python库

1.引入库

直接pip下载极其缓慢,被迫使用国内镜像源下载,于实际发布的版本有差异。

pip install mediapipe

2.简单应用

代码如下(人脸检测):

import cv2
import mediapipe as mp
import face_mesh_connections

def face_detection():
    mp_face_detection = mp.solutions.face_detection
    mp_drawing = mp.solutions.drawing_utils

    mp_drawing_styles = mp.solutions.drawing_styles
    mp_face_mesh = mp.solutions.face_mesh
    # 人脸检测
    # 对于静态图像:
    IMAGE_FILES = ["boluo.jpg"]
    with mp_face_detection.FaceDetection(min_detection_confidence=0.5) as face_detection:
        for idx, file in enumerate(IMAGE_FILES):
            image = cv2.imread(file)
            # 将BGR图像转换为RGB,并使用MediaPipe人脸检测进行处理:
            results = face_detection.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

            # 绘制每个人脸的人脸检测
            if not results.detections:
                continue
            annotated_image = image.copy()
            for detection in results.detections:
                print('Nose tip:')
                print(mp_face_detection.get_key_point(
                    detection, mp_face_detection.FaceKeyPoint.NOSE_TIP))
                mp_drawing.draw_detection(annotated_image, detection)
            cv2.imwrite('annotated_image' + str(idx) + '.png', annotated_image)
            cv2.namedWindow("MediaPipe Face Detection", 0)
            cv2.imshow('MediaPipe Face Detection', cv2.flip(annotated_image, 1))
            if cv2.waitKey(0) & 0xFF == 27:
                break

效果如下:
在这里插入图片描述

代码如下(3D人脸地标):

def face_mesh():
    mp_drawing = mp.solutions.drawing_utils
    # mp_drawing_styles = mp.solutions.drawing_styles
    mp_face_mesh = mp.solutions.face_mesh
    mp_face_mesh_connection = face_mesh_connections
    # For static images:
    IMAGE_FILES = ["boluo.jpg"]
    drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
    with mp_face_mesh.FaceMesh(
            static_image_mode=True,
            max_num_faces=1,

            min_detection_confidence=0.5) as face_mesh:
        for idx, file in enumerate(IMAGE_FILES):
            image = cv2.imread(file)
            # Convert the BGR image to RGB before processing.
            results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

            # Print and draw face mesh landmarks on the image.
            if not results.multi_face_landmarks:
                continue
            annotated_image = image.copy()
            for face_landmarks in results.multi_face_landmarks:
                # print('face_landmarks:', face_landmarks.landmark)
                mp_drawing.draw_landmarks(
                    image=annotated_image,
                    landmark_list=face_landmarks,
                    # landmark_drawing_spec=mp_drawing.DrawingSpec(color=(255, 255, 255)),
                    connections=mp_face_mesh_connection.FACEMESH_TESSELATION,
                    connection_drawing_spec=mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=3)
                )
             
            cv2.imwrite('face_mesh_image' + str(idx) + '.png', annotated_image)
            cv2.namedWindow("MediaPipe Face Detection", 0)
            cv2.imshow('MediaPipe Face Detection', cv2.flip(annotated_image, 1))
            if cv2.waitKey(0) & 0xFF == 27:
                break

效果如下:
在这里插入图片描述

总结

mediapipe python库的安装和简单使用都比较简单,C++等其它版本安装较为复杂。

免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删

QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空