案例介绍
基于mediapipe实现手部识别的demo,案例中基于视频流进行demo的搭建。其中mediapipe官网地址:https://google.github.io/mediapipe/solutions/hands.html 。可以进行gpu的调用支持,但是不支持windows的gpu调用。
案例代码
"""
这个案例 展示了 摄像头的视频流
"""
import cv2
import numpy as np
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, frame = cap.read()
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())
cv2.imshow('Virtual drag', frame)
if cv2.waitKey(10) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()