-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataseta-z.py
More file actions
50 lines (40 loc) · 1.53 KB
/
dataseta-z.py
File metadata and controls
50 lines (40 loc) · 1.53 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
import os
import pickle
import mediapipe as mp
import cv2
import numpy as np
# Mediapipe setup
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(static_image_mode=True, min_detection_confidence=0.3)
DATA_DIR = './alphabet_data'
# Data storage
data = []
labels = []
for letter in os.listdir(DATA_DIR):
letter_dir = os.path.join(DATA_DIR, letter)
if not os.path.isdir(letter_dir):
continue
for img_path in os.listdir(letter_dir):
img_full_path = os.path.join(letter_dir, img_path)
img = cv2.imread(img_full_path)
if img is None:
print(f"Warning: Unable to read image {img_path}. Skipping.")
continue
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:
data_aux = []
x_ = [lm.x for lm in hand_landmarks.landmark]
y_ = [lm.y for lm in hand_landmarks.landmark]
box_width = max(x_) - min(x_)
box_height = max(y_) - min(y_)
for lm in hand_landmarks.landmark:
data_aux.append((lm.x - min(x_)) / box_width)
data_aux.append((lm.y - min(y_)) / box_height)
data.append(data_aux)
labels.append(letter)
# Save the dataset
with open('alphabet_data.pickle', 'wb') as f:
pickle.dump({'data': data, 'labels': labels}, f)
hands.close()