Repository navigation
Expand file tree
/
Copy pathcreate_dataset.py
More file actions
123 lines (93 loc) · 3.63 KB
/
Copy pathcreate_dataset.py
File metadata and controls
123 lines (93 loc) · 3.63 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os # file handling
import pickle # saving the processed dataset
import cv2 # loading images
import mediapipe as mp # extracting hand landmarks
import numpy as np
DATA_DIR = './data'
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
hands = mp_hands.Hands(static_image_mode=True, min_detection_confidence=0.3)
data = [] # landmark features (x, y coordinates)
labels = [] # class numbers (0–9)
# Loop through each class folder
for dir_ in os.listdir(DATA_DIR):
dir_path = os.path.join(DATA_DIR, dir_)
if not os.path.isdir(dir_path):
continue
print(f"Processing class {dir_}...")
# Loop through each image inside that class
for img_path in os.listdir(dir_path):
data_aux = []
x_ = []
y_ = []
img = cv2.imread(os.path.join(dir_path, img_path))
if img is None:
continue
H, W, _ = img.shape
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # MediaPipe expects RGB format
# Run MediaPipe Hand Detection (21) landmarks sign
results = hands.process(img_rgb)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
x_.append(x)
y_.append(y)
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
data_aux.append(x - min(x_))
data_aux.append(y - min(y_))
data.append(data_aux)
labels.append(int(dir_))
# save dataset
with open('data.pickle', 'wb') as f:
pickle.dump({'data': data, 'labels': labels}, f)
print("✅ data.pickle created successfully!")
# import os
# import pickle
# import cv2
# import mediapipe as mp
# import numpy as np
# DATA_DIR = './data'
# mp_hands = mp.solutions.hands
# mp_drawing = mp.solutions.drawing_utils
# mp_drawing_styles = mp.solutions.drawing_styles
# hands = mp_hands.Hands(static_image_mode=True, min_detection_confidence=0.3)
# data = []
# labels = []
# for dir_ in os.listdir(DATA_DIR):
# dir_path = os.path.join(DATA_DIR, dir_)
# if not os.path.isdir(dir_path):
# continue
# print(f"Processing class {dir_}...")
# for img_path in os.listdir(dir_path):
# data_aux = []
# x_ = []
# y_ = []
# img = cv2.imread(os.path.join(dir_path, img_path))
# if img is None:
# continue
# H, W, _ = img.shape
# img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# results = hands.process(img_rgb)
# if results.multi_hand_landmarks:
# for hand_landmarks in results.multi_hand_landmarks:
# for i in range(len(hand_landmarks.landmark)):
# x = hand_landmarks.landmark[i].x
# y = hand_landmarks.landmark[i].y
# x_.append(x)
# y_.append(y)
# for i in range(len(hand_landmarks.landmark)):
# x = hand_landmarks.landmark[i].x
# y = hand_landmarks.landmark[i].y
# data_aux.append(x - min(x_))
# data_aux.append(y - min(y_))
# data.append(data_aux)
# labels.append(int(dir_))
# # save dataset
# with open('data.pickle', 'wb') as f:
# pickle.dump({'data': data, 'labels': labels}, f)
# print("✅ data.pickle created successfully!")