-
Notifications
You must be signed in to change notification settings - Fork 1
/
gather_static_data.py
84 lines (77 loc) · 2.43 KB
/
gather_static_data.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
from cv2 import cv2
import numpy as np
import mediapipe as mp
from joblib import dump, load
import math
from utilities.util import *
import utilities.cv2utils as cv2utils
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
cap = cv2.VideoCapture(0)
''' Constants '''
DATASET_SIZE = 10
SHUTTER_TIME = 1 * cap.get(cv2.CAP_PROP_FPS)
SHUTTER = False
# Get video dimensions
WIDTH = cap.get(3)
HEIGHT = cap.get(4)
RATIO = HEIGHT / WIDTH
# What to name this numpy file
FNAME = 'test'
''' Where to save this data '''
# SAVE_DIR = LETTER_DATA_DIR
# SAVE_DIR = CLASSIFIER_ANYANGLE_DIR
# SAVE_DIR = CLASSIFIER_FORCED_DIR
SAVE_DIR = CLASSIFIER_UPRIGHT_DIR
NORMALIZE_ANGLE = True
# What will end up being the dataset collected during this session
dataset = np.empty((1, NUM_POINTS, NUM_DIM))
done = False
# Load MediaPipe model
hands = mp_hands.Hands(
min_detection_confidence=0.7, min_tracking_confidence=0.7, max_num_hands=1)
ticker = 0
while cap.isOpened():
''' Reading frame '''
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
if SHUTTER and ticker < SHUTTER_TIME:
ticker += 1
continue
# Process and get hands
image, results = cv2utils.process_and_identify_landmarks(image, hands)
# If we found hands
if results.multi_hand_landmarks:
# For each hand
for hand_landmarks in results.multi_hand_landmarks:
# Get landmarks in np format
hand_np_raw = landmarks_to_np(hand_landmarks.landmark)
# Normalize
hand_np, _ = normalize_hand(hand_np_raw, screenRatio=RATIO, rotate=NORMALIZE_ANGLE)
# Concatenate all the hand landmarks to the dataset
dataset = np.concatenate((dataset, [hand_np]))
# Print the current size of the dataset
print(f"[size] : {dataset.shape[0] - 1}")
# If reached desired size, finish up
if dataset.shape[0] - 1 == DATASET_SIZE:
dataset = dataset[1:]
done = True
break
# Draw hand landmarks
mp_drawing.draw_landmarks(
image, hand_landmarks, mp_hands.HAND_CONNECTIONS,
landmark_drawing_spec=mp_drawing.DrawingSpec(thickness=6, circle_radius=3),
connection_drawing_spec=mp_drawing.DrawingSpec(color=(255,255,255)))
if done:
break
cv2.imshow('Trainer', image)
if cv2.waitKey(5) & 0xFF == 27:
break
ticker = 0
hands.close()
cap.release()
''' Save database '''
create_directory_if_needed(SAVE_DIR)
np.save(f'{SAVE_DIR}{FNAME}.npy', dataset)