-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataLabeling.py
More file actions
73 lines (60 loc) · 2.96 KB
/
Copy pathdataLabeling.py
File metadata and controls
73 lines (60 loc) · 2.96 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
import os
import csv
import cv2 as cv
import mediapipe as mp
# Veri seti yolu ve CSV kaydetme yolu
data_dir = '/Users/zeynepbaran/Desktop/Sign Language Recognition/data'
output_csv = '/Users/zeynepbaran/Desktop/Sign Language Recognition/coordinates.csv'
# Mediapipe Hands hazırlığı
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(static_image_mode=True, max_num_hands=2, min_detection_confidence=0.5)
# CSV dosyasını başlat
with open(output_csv, mode='w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
# Başlıklar: Sağ ve sol el için x, y koordinatları + etiket
headers = (
[f'right_x{i}' for i in range(21)] + [f'right_y{i}' for i in range(21)] +
[f'left_x{i}' for i in range(21)] + [f'left_y{i}' for i in range(21)] +
['label']
)
writer.writerow(headers)
# Her kategorideki resimleri işleyelim
for label in os.listdir(data_dir):
label_path = os.path.join(data_dir, label)
if not os.path.isdir(label_path):
continue
print(f"Processing label: {label}")
for image_name in os.listdir(label_path):
image_path = os.path.join(label_path, image_name)
if not image_name.lower().endswith(('png', 'jpg', 'jpeg')):
continue
# Resmi yükle
image = cv.imread(image_path)
if image is None:
print(f"Skipping invalid image: {image_path}")
continue
# Resmi BGR'den RGB'ye çevir
rgb_image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
# Mediapipe ile işleme
results = hands.process(rgb_image)
# Sağ ve sol el için varsayılan koordinat listeleri (sıfırlarla dolu)
right_hand_coords = [0] * 42 # 21 x (x, y)
left_hand_coords = [0] * 42
if results.multi_hand_landmarks:
for hand_landmarks, handedness in zip(results.multi_hand_landmarks, results.multi_handedness):
# Sağ ve sol eli ayır
if handedness.classification[0].label == 'Right':
right_hand_coords = []
reference_point = hand_landmarks.landmark[0]
for lm in hand_landmarks.landmark:
right_hand_coords.append(lm.x - reference_point.x)
right_hand_coords.append(lm.y - reference_point.y)
elif handedness.classification[0].label == 'Left':
left_hand_coords = []
reference_point = hand_landmarks.landmark[0]
for lm in hand_landmarks.landmark:
left_hand_coords.append(lm.x - reference_point.x)
left_hand_coords.append(lm.y - reference_point.y)
# Koordinatları ve etiketi yaz
writer.writerow(right_hand_coords + left_hand_coords + [label])
print(f"Relative coordinates saved to {output_csv}")