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++等其它版本安装较为复杂。
后期继续琢磨一下mediapipe C++版本在Window和linux的安装和使用。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python Mediapipe 是一个很强大的图像和视频处理,它可以检测面部特征、姿势、手势等,并可以在运行时进行实时处理。当我们对程序做了一定的修改之后,我们可能需要将程序转换成可执行文件(exe)以便在没有安装Python环境的机器上运行程序,或将程序分享给其他人使用。以下是如何将Python Mediapipe 程序打包成exe可执行文件的简单步骤: 1. 安装 PyInstaller PyInstaller 是一个用于将Python代码打包成独立应用程序(像exe或bin)的工具。使用如下命令安装PyInstaller: pip install pyinstaller 2. 创建 spec 文件 运行以下命令创建一个 *.spec 文件: pyinstaller --name=yourappname yourappname.py 3. 修改生成的 spec 文件 打开生成的 spec 文件,可以看到类似如下内容: # yourappname.spec file contents a = Analysis(['yourappname.py'], pathex=['C:\\your\\path\\to\\file'], binaries=[], datas=[], hiddenimports=[], hookspath=[]) 修改 spec 文件: hiddenimports=['mediapipe'] 4. 生成exe可执行文件 使用以下命令生成可执行文件: pyinstaller yourappname.spec 生成的应用程序位于dist/yourappname/文件夹中,其中包含所有所需的文件和资源文件。 总结: 使用PyInstaller可以将Python Mediapipe程序打包成.exe可执行文件,使其能够更方便地在没有Python环境的其他机器上运行或与其他人分享。通过修改生成的spec文件,增加所需的文件就能够完成打包工作了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值