-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhands_data_generation.py
52 lines (45 loc) · 1.65 KB
/
hands_data_generation.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
import cv2
import mediapipe as mp
import pandas as pd
cap = cv2.VideoCapture(4)
mpHands = mp.solutions.hands
hands = mpHands.Hands(max_num_hands=1) # Ensure only one hand is detected
mpDraw = mp.solutions.drawing_utils
lm_list = []
label = "h"
no_of_frames = 1000
def make_landmark_timestep(results):
c_lm = []
if results.multi_hand_landmarks:
hand_landmarks = results.multi_hand_landmarks[0] # Only consider the first detected hand
for lm in hand_landmarks.landmark:
c_lm.append(lm.x)
c_lm.append(lm.y)
c_lm.append(lm.z)
return c_lm
def draw_landmark_on_image(mpDraw, results, frame):
if results.multi_hand_landmarks:
hand_landmarks = results.multi_hand_landmarks[0] # Only consider the first detected hand
mpDraw.draw_landmarks(frame, hand_landmarks, mpHands.HAND_CONNECTIONS)
for lm in hand_landmarks.landmark:
h, w, c = frame.shape
cx, cy = int(lm.x * w), int(lm.y * h)
cv2.circle(frame, (cx, cy), 3, (0, 255, 0), cv2.FILLED)
return frame
while len(lm_list) <= no_of_frames:
ret, frame = cap.read()
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
if ret:
frameRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(frameRGB)
if results.multi_hand_landmarks:
lm = make_landmark_timestep(results)
lm_list.append(lm)
frame = draw_landmark_on_image(mpDraw, results, frame)
cv2.imshow("image", frame)
if cv2.waitKey(1) == ord('q'):
break
df = pd.DataFrame(lm_list)
df.to_csv(label + ".txt")
cap.release()
cv2.destroyAllWindows()