-
Notifications
You must be signed in to change notification settings - Fork 0
/
handtracking.py
83 lines (66 loc) · 3.13 KB
/
handtracking.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import cv2
import math
import mediapipe as mp
import numpy as np
# Initialize Mediapipe Hands
mpHands = mp.solutions.hands
hands = mpHands.Hands(static_image_mode=False, max_num_hands=2, min_detection_confidence=0.5, min_tracking_confidence=0.5)
mpdraw = mp.solutions.drawing_utils
# OpenCV code to capture video from the default camera
cap = cv2.VideoCapture(0)
cap.set(3, 720) # Set the width of the frame
cap.set(4, 480) # Set the height of the frame
mylmList = []
# Main loop to process video frames
while True:
isopen, frame = cap.read()
frame = cv2.flip(frame, 1) # Flip the frame horizontally
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Convert BGR image to RGB for Mediapipe processing
results = hands.process(img) # Process the frame with Mediapipe Hands
allHands = []
h, w, c = frame.shape # Get the height, width, and number of channels of the frame
# Process each detected hand in the frame
if results.multi_hand_landmarks:
for handType, handLms in zip(results.multi_handedness, results.multi_hand_landmarks):
myHand = {}
mylmList = []
xList = []
yList = []
# Extract landmark points and store them in lists
for id, lm in enumerate(handLms.landmark):
px, py, pz = int(lm.x * w), int(lm.y * h), int(lm.z * w)
mylmList.append([id, px, py])
xList.append(px)
yList.append(py)
# Calculate bounding box around the hand landmarks
xmin, xmax = min(xList), max(xList)
ymin, ymax = min(yList), max(yList)
boxW, boxH = xmax - xmin, ymax - ymin
bbox = xmin, ymin, boxW, boxH
cx, cy = bbox[0] + (bbox[2] // 2), bbox[1] + (bbox[3] // 2)
# Store hand information in a dictionary
myHand["lmList"] = mylmList
myHand["bbox"] = bbox
myHand["center"] = (cx, cy)
myHand["type"] = handType.classification[0].label
#if you dont flip the image
''' if handType.classification[0].label == "Right":
myHand["type"] = "Left"
else:
myHand["type"] = "Right"'''
allHands.append(myHand)
# Draw landmarks and bounding box on the frame
mpdraw.draw_landmarks(frame, handLms, mpHands.HAND_CONNECTIONS)
cv2.rectangle(frame, (bbox[0] - 20, bbox[1] - 20), (bbox[0] + bbox[2] + 20, bbox[1] + bbox[3] + 20), (255, 255, 0), 2)
cv2.putText(frame, myHand["type"], (bbox[0] - 30, bbox[1] - 30), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 255), 2)
if mylmList != 0:
try:
x, y = mylmList[8][1], mylmList[8][2]
cv2.circle(frame,(x,y),5,(255,255,0),cv2.FILLED)
except:
pass
# Display the frame with annotations
cv2.imshow('Hand tracking', frame)
# Exit the loop if 'q' key is pressed
if cv2.waitKey(1) & 0xff == ord('q'):
break