0
点赞
收藏
分享

微信扫一扫

1.mediapipe实现手部21关键点识别

unadlib 2022-05-05 阅读 60

案例介绍

基于mediapipe实现手部识别的demo,案例中基于视频流进行demo的搭建。其中mediapipe官网地址:https://google.github.io/mediapipe/solutions/hands.html 。可以进行gpu的调用支持,但是不支持windows的gpu调用。

案例代码

"""
 这个案例 展示了 摄像头的视频流
"""

import cv2  # pip install opencv-python
import numpy as np
# mdeiapipe 不能使用conda装  只能用pip装     装之前最好换一下pip源
# 导入mediapipe:https://google.github.io/mediapipe/solutions/hands
import mediapipe as mp

mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands

hands = mp_hands.Hands(
    model_complexity=0,
    min_detection_confidence=0.5,
    min_tracking_confidence=0.5)

# 获取摄像头视频流
cap = cv2.VideoCapture(0)

# 界面方块的参数
square_x = 100
square_y = 100
square_width = 100

while True:

    # 读取每一帧  ret(bool) :  代表是否打开成功摄像头   frame (numpy.ndarray) : 单帧的图像
    # tips:opencv的读取是BGR的顺序,很多算法是RGB,所以需要转化。
    ret, frame = cap.read()
    # print(type(frame))
    # print(type(ret))
    # print(ret)
    # 对图像进行处理,镜像一下,围绕y轴
    frame = cv2.flip(frame, 1)

    frame.flags.writeable = False
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    # 识别
    results = hands.process(frame)

    frame.flags.writeable = True
    frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

    if results.multi_hand_landmarks:
        for hand_landmarks in results.multi_hand_landmarks:
            mp_drawing.draw_landmarks(
                frame,
                hand_landmarks,
                mp_hands.HAND_CONNECTIONS,
                mp_drawing_styles.get_default_hand_landmarks_style(),
                mp_drawing_styles.get_default_hand_connections_style())


    # 此时图片是BGR 不是RGB     -1  代表实心      255 = b  0 = g 0 =r    所以是蓝色方块
    # cv2.rectangle(frame, (square_x, square_y), (square_x + square_width, square_y + square_width), (255, 0, 0), -1)

    # 显示
    cv2.imshow('Virtual drag', frame)

    # 退出条件 esc 退出
    if cv2.waitKey(10) & 0xFF == 27:
        break

cap.release()
cv2.destroyAllWindows()

举报

相关推荐

使用mediapipe进行人脸识别

0 条评论