-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_processing.py
More file actions
53 lines (43 loc) · 1.79 KB
/
data_processing.py
File metadata and controls
53 lines (43 loc) · 1.79 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
import os
import pickle
import mediapipe as mp
import cv2
# Initialize MediaPipe Hands module
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(static_image_mode=True, min_detection_confidence=0.3)
DATA_DIR = './data'
data = []
labels = []
# Iterate through each directory and image in the data folder
for dir_ in os.listdir(DATA_DIR):
for img_path in os.listdir(os.path.join(DATA_DIR, dir_)):
data_aux = []
x_ = []
y_ = []
# Read the image
img = cv2.imread(os.path.join(DATA_DIR, dir_, img_path))
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Process the image with MediaPipe Hands
results = hands.process(img_rgb)
# Check if any hand landmarks are detected
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
# Iterate over each landmark
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
# Append the x and y values to the corresponding lists
x_.append(x)
y_.append(y)
# Normalize the x and y coordinates relative to the minimum values
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_))
# Add the processed data and label to the lists
data.append(data_aux)
labels.append(dir_)
# Save the processed data and labels into a pickle file
with open('data.pickle', 'wb') as f:
pickle.dump({'data': data, 'labels': labels}, f)