案例介绍
基于mediapipe实现方块的虚拟拖拽。环境使用python3.8.
代码示例
"""
这个案例 展示了 摄像头的视频流
"""
import math
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
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
x = 100
y = 100
w = 200
h = 200
L1 = 0
L2 = 0
on_square = False
square_color = (0, 255, 0)
while True:
ret, frame = cap.read()
if not ret:
print("无法打开摄像头")
continue
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())
"""
print(hand_landmarks)
每个关键点的解析
landmark {
x: 0.18473060429096222
y: 0.058572977781295776
z: -0.10718432068824768
}
"""
x_list = []
y_list = []
for landmark in hand_landmarks.landmark:
x_list.append(landmark.x)
y_list.append(landmark.y)
index_finger_x = int(x_list[8] * width)
index_finger_y = int(y_list[8] * height)
cv2.circle(frame, (index_finger_x, index_finger_y), 20, (255, 0, 255), -1)
middle_finger_x = int(x_list[12] * width)
middle_finger_y = int(y_list[12] * height)
finger_distance = math.hypot((middle_finger_x - index_finger_x), (middle_finger_y - index_finger_y))
if finger_distance < 60:
if (index_finger_x > x and index_finger_x < (x + w)) and (
index_finger_y > y and index_finger_y < (y + h)):
if on_square == False:
print('在')
L1 = index_finger_x - x
L2 = index_finger_y - y
square_color = (255, 0, 255)
on_square = True
else:
print('不在')
else:
on_square = False
square_color = (0, 255, 0)
if on_square:
x = index_finger_x - L1
y = index_finger_y - L2
overlay = frame.copy()
cv2.rectangle(frame, (x, y), (x + w, y + h), square_color, -1)
frame = cv2.addWeighted(overlay, 0.5, frame, 1 - 0.5, 0)
cv2.imshow('Virtual drag', frame)
if cv2.waitKey(10) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
关键点解析
半透明方块
overlay = frame.copy()
cv2.rectangle(frame, (x, y), (x + w, y + h), square_color, -1)
frame = cv2.addWeighted(overlay, 0.5, frame, 1 - 0.5, 0)
调用了opencv的图像叠加混合加权的api,实现了半透明的小方块。具体资料在这里。https://blog.csdn.net/zh_jessica/article/details/77992578
|