Repository navigation
Expand file tree
/
Copy pathhackathon.py
More file actions
156 lines (123 loc) · 5.29 KB
/
Copy pathhackathon.py
File metadata and controls
156 lines (123 loc) · 5.29 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.python import vision
import cv2
import time
import kagglehub
import os
import glob
import pandas as pd
import re
baseOptions = mp.tasks.BaseOptions
BaseOptions = mp.tasks.BaseOptions
HandLandmarker = mp.tasks.vision.HandLandmarker
HandLandmarkerOptions = mp.tasks.vision.HandLandmarkerOptions
VisionRunningMode = mp.tasks.vision.RunningMode
options = HandLandmarkerOptions(
base_options=BaseOptions(model_asset_path='hand_landmarker.task'),
running_mode=VisionRunningMode.IMAGE,
)
def draw_landmarks_on_image(rgb_image, detection_result):
"""Draw hand landmarks on the image."""
print(detection_result.handedness)
if len(detection_result.handedness) == 0:
print("No hands detected.")
return rgb_image
# Convert to BGR for OpenCV
annotated_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
for hand_landmarks in detection_result.hand_landmarks:
# Draw landmarks
for landmark in hand_landmarks:
x = int(landmark.x * annotated_image.shape[1])
y = int(landmark.y * annotated_image.shape[0])
cv2.circle(annotated_image, (x, y), 5, (0, 255, 0), -1)
# Draw connections between landmarks
connections = [
# Thumb
(0, 1), (1, 2), (2, 3), (3, 4),
# Index finger
(0, 5), (5, 6), (6, 7), (7, 8),
# Middle finger
(0, 9), (9, 10), (10, 11), (11, 12),
# Ring finger
(0, 13), (13, 14), (14, 15), (15, 16),
# Pinky
(0, 17), (17, 18), (18, 19), (19, 20)
]
for connection in connections:
start_idx, end_idx = connection
if start_idx < len(hand_landmarks) and end_idx < len(hand_landmarks):
start_point = hand_landmarks[start_idx]
end_point = hand_landmarks[end_idx]
start_x = int(start_point.x * annotated_image.shape[1])
start_y = int(start_point.y * annotated_image.shape[0])
end_x = int(end_point.x * annotated_image.shape[1])
end_y = int(end_point.y * annotated_image.shape[0])
cv2.line(annotated_image, (start_x, start_y), (end_x, end_y), (255, 0, 0), 2)
return annotated_image
# Initialize data storage for Excel file
hand_data = []
def extract_hand_features(detection_result, image_path):
"""Extract hand landmarks and features for Excel export."""
if len(detection_result.hand_landmarks) == 0:
return None
row_data = {'image_path': image_path}
print(image_path)
# Extract label from image path using regex
match = re.search(r'/([^/]+)/[^/]*\.(jpg|jpeg|png|bmp|tiff)$', image_path, re.IGNORECASE)
if match:
row_data['label'] = match.group(1)
else:
row_data['label'] = 'unknown'
for hand_idx, hand_landmarks in enumerate(detection_result.hand_landmarks):
# Add handedness information
if hand_idx < len(detection_result.handedness):
handedness = detection_result.handedness[hand_idx][0].category_name
row_data['handedness'] = handedness
# Add landmark coordinates
for landmark_idx, landmark in enumerate(hand_landmarks):
print(landmark_idx)
row_data[f'landmark_{landmark_idx}_x'] = landmark.x
row_data[f'landmark_{landmark_idx}_y'] = landmark.y
features.append(row_data)
features = []
with HandLandmarker.create_from_options(options) as landmarker:
# Now the hand landmarker is initialized and ready to process images.
# Download latest version
path = "content/n/SignAlphaSet"
# Get all image files from the dataset
image_extensions = ['*.jpg', '*.jpeg', '*.png', '*.bmp', '*.tiff']
all_images = []
for ext in image_extensions:
all_images.extend(glob.glob(os.path.join(path, '**', ext), recursive=True))
print(f"Found {len(all_images)} images")
timestamp = 0
# Process each image
for image_path in all_images:
print(f"Processing: {image_path}")
# Read image with OpenCV
image = cv2.imread(image_path)
if image is None:
print(f"Could not read image: {image_path}")
continue
# Convert BGR to RGB
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Create MediaPipe image
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=image_rgb)
# Process the frame
timestamp += 1
latest_result = landmarker.detect(mp_image)
if cv2.waitKey(5) & 0xFF == 27: # ESC key
break
# Inside the main loop, after landmarker.detect_async, replace the display code:
if latest_result is not None:
extract_hand_features(latest_result, image_path)
# After processing all images, save the features to an Excel file
for feature in features:
if feature is not None:
hand_data.append(feature)
excel_path = 'hand_landmarks_features.xlsx'
df = pd.DataFrame(hand_data)
df.to_excel(excel_path, index=False)
print(f"Hand landmarks features saved to {excel_path}")
cv2.destroyAllWindows()