-
Notifications
You must be signed in to change notification settings - Fork 14
/
data_collection.py
84 lines (70 loc) · 3.52 KB
/
data_collection.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
84
# %%
# Import necessary libraries
import os
import numpy as np
import cv2
import mediapipe as mp
from itertools import product
from my_functions import *
import keyboard
# Define the actions (signs) that will be recorded and stored in the dataset
actions = np.array(['a', 'b'])
# Define the number of sequences and frames to be recorded for each action
sequences = 30
frames = 10
# Set the path where the dataset will be stored
PATH = os.path.join('data')
# Create directories for each action, sequence, and frame in the dataset
for action, sequence in product(actions, range(sequences)):
try:
os.makedirs(os.path.join(PATH, action, str(sequence)))
except:
pass
# Access the camera and check if the camera is opened successfully
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Cannot access camera.")
exit()
# Create a MediaPipe Holistic object for hand tracking and landmark extraction
with mp.solutions.holistic.Holistic(min_detection_confidence=0.75, min_tracking_confidence=0.75) as holistic:
# Loop through each action, sequence, and frame to record data
for action, sequence, frame in product(actions, range(sequences), range(frames)):
# If it is the first frame of a sequence, wait for the spacebar key press to start recording
if frame == 0:
while True:
if keyboard.is_pressed(' '):
break
_, image = cap.read()
results = image_process(image, holistic)
draw_landmarks(image, results)
cv2.putText(image, 'Recroding data for the "{}". Sequence number {}.'.format(action, sequence),
(20,20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 1, cv2.LINE_AA)
cv2.putText(image, 'Pause.', (20,400), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2, cv2.LINE_AA)
cv2.putText(image, 'Press "Space" when you are ready.', (20,450), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2, cv2.LINE_AA)
cv2.imshow('Camera', image)
cv2.waitKey(1)
# Check if the 'Camera' window was closed and break the loop
if cv2.getWindowProperty('Camera',cv2.WND_PROP_VISIBLE) < 1:
break
else:
# For subsequent frames, directly read the image from the camera
_, image = cap.read()
# Process the image and extract hand landmarks using the MediaPipe Holistic pipeline
results = image_process(image, holistic)
# Draw the hand landmarks on the image
draw_landmarks(image, results)
# Display text on the image indicating the action and sequence number being recorded
cv2.putText(image, 'Recroding data for the "{}". Sequence number {}.'.format(action, sequence),
(20,20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 1, cv2.LINE_AA)
cv2.imshow('Camera', image)
cv2.waitKey(1)
# Check if the 'Camera' window was closed and break the loop
if cv2.getWindowProperty('Camera',cv2.WND_PROP_VISIBLE) < 1:
break
# Extract the landmarks from both hands and save them in arrays
keypoints = keypoint_extraction(results)
frame_path = os.path.join(PATH, action, str(sequence), str(frame))
np.save(frame_path, keypoints)
# Release the camera and close any remaining windows
cap.release()
cv2.destroyAllWindows()