-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
391 lines (331 loc) · 15.2 KB
/
main.py
File metadata and controls
391 lines (331 loc) · 15.2 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import tkinter as tk
from tkinter import ttk
import cv2
from PIL import Image, ImageTk
import mediapipe as mp
import threading
import socket
import pickle
import struct
class HandTrackingApp:
def __init__(self, root):
self.root = root
self.root.title("Hand Tracking App")
self.root.geometry("900x700")
# Initialize MediaPipe
self.mp_hands = mp.solutions.hands
self.hands = self.mp_hands.Hands(
static_image_mode=False,
max_num_hands=2,
min_detection_confidence=0.5,
min_tracking_confidence=0.5
)
self.mp_drawing = mp.solutions.drawing_utils
self.mp_drawing_styles = mp.solutions.drawing_styles
# Camera and streaming variables
self.cap = None
self.current_camera = 0
self.is_running = False
self.frame = None
self.hand_landmarks_list = []
# Socket server variables
self.server_socket = None
self.server_running = False
self.clients = []
self.server_port = 5555
# Create UI
self.create_ui()
def create_ui(self):
# Top control panel
control_frame = tk.Frame(self.root, bg="#2c3e50", padx=10, pady=10)
control_frame.pack(fill=tk.X)
# Camera selection
tk.Label(control_frame, text="Camera:", bg="#2c3e50", fg="white", font=("Arial", 10)).pack(side=tk.LEFT, padx=5)
self.camera_var = tk.StringVar()
self.camera_combo = ttk.Combobox(control_frame, textvariable=self.camera_var, width=15, state="readonly")
self.camera_combo.pack(side=tk.LEFT, padx=5)
self.populate_cameras()
self.camera_combo.bind("<<ComboboxSelected>>", self.change_camera)
# Start/Stop button
self.start_button = tk.Button(control_frame, text="Start Camera", command=self.toggle_camera,
bg="#27ae60", fg="white", font=("Arial", 10, "bold"), padx=20)
self.start_button.pack(side=tk.LEFT, padx=10)
# Server control
self.server_button = tk.Button(control_frame, text="Start Server", command=self.toggle_server,
bg="#3498db", fg="white", font=("Arial", 10, "bold"), padx=20)
self.server_button.pack(side=tk.LEFT, padx=10)
# Server status
self.server_status_label = tk.Label(control_frame, text="Server: OFF", bg="#2c3e50",
fg="#e74c3c", font=("Arial", 10, "bold"))
self.server_status_label.pack(side=tk.LEFT, padx=10)
# Info panel
info_frame = tk.Frame(self.root, bg="#34495e", padx=10, pady=5)
info_frame.pack(fill=tk.X)
self.info_label = tk.Label(info_frame, text="Ready to start", bg="#34495e",
fg="white", font=("Arial", 9))
self.info_label.pack(side=tk.LEFT)
self.ip_label = tk.Label(info_frame, text=f"IP: {self.get_local_ip()}", bg="#34495e",
fg="#f39c12", font=("Arial", 9, "bold"))
self.ip_label.pack(side=tk.RIGHT)
# Video display
self.video_frame = tk.Frame(self.root, bg="black")
self.video_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
self.canvas = tk.Canvas(self.video_frame, bg="black", highlightthickness=0)
self.canvas.pack(fill=tk.BOTH, expand=True)
self.canvas_image = None # Store canvas image reference
# Bottom stats panel
stats_frame = tk.Frame(self.root, bg="#2c3e50", padx=10, pady=5)
stats_frame.pack(fill=tk.X)
self.stats_label = tk.Label(stats_frame, text="FPS: 0 | Hands Detected: 0 | Clients: 0",
bg="#2c3e50", fg="white", font=("Arial", 9))
self.stats_label.pack()
def populate_cameras(self):
"""Detect available cameras"""
cameras = []
for i in range(5): # Check first 5 camera indices
cap = cv2.VideoCapture(i)
if cap.isOpened():
cameras.append(f"Camera {i}")
cap.release()
if cameras:
self.camera_combo['values'] = cameras
self.camera_combo.current(0)
else:
self.camera_combo['values'] = ["No cameras found"]
self.camera_combo.current(0)
def change_camera(self, event=None):
"""Change the active camera"""
if self.is_running:
camera_str = self.camera_var.get()
if "Camera" in camera_str:
new_camera = int(camera_str.split()[-1])
if new_camera != self.current_camera:
self.current_camera = new_camera
if self.cap:
self.cap.release()
self.cap = cv2.VideoCapture(self.current_camera)
self.info_label.config(text=f"Switched to {camera_str}")
def toggle_camera(self):
"""Start or stop the camera"""
if not self.is_running:
camera_str = self.camera_var.get()
if "Camera" in camera_str:
self.current_camera = int(camera_str.split()[-1])
self.cap = cv2.VideoCapture(self.current_camera)
if self.cap.isOpened():
self.is_running = True
self.start_button.config(text="Stop Camera", bg="#e74c3c")
self.info_label.config(text="Camera running")
threading.Thread(target=self.update_frame, daemon=True).start()
else:
self.info_label.config(text="Failed to open camera")
else:
self.is_running = False
self.start_button.config(text="Start Camera", bg="#27ae60")
self.info_label.config(text="Camera stopped")
if self.cap:
self.cap.release()
def update_frame(self):
"""Main loop to capture and process frames"""
import time
fps_counter = 0
fps_time = time.time()
fps = 0
while self.is_running:
ret, frame = self.cap.read()
if not ret:
continue
# Flip frame horizontally for mirror effect
frame = cv2.flip(frame, 1)
# Convert BGR to RGB for MediaPipe
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Process hand tracking
results = self.hands.process(rgb_frame)
# Draw hand landmarks
self.hand_landmarks_list = []
hands_detected = 0
if results.multi_hand_landmarks:
hands_detected = len(results.multi_hand_landmarks)
for hand_landmarks in results.multi_hand_landmarks:
# Draw the hand skeleton in red
self.mp_drawing.draw_landmarks(
frame,
hand_landmarks,
self.mp_hands.HAND_CONNECTIONS,
self.mp_drawing.DrawingSpec(color=(0, 0, 255), thickness=2, circle_radius=3), # Red landmarks
self.mp_drawing.DrawingSpec(color=(0, 0, 255), thickness=2) # Red connections
)
# Store landmarks for socket transmission
landmarks_data = []
for landmark in hand_landmarks.landmark:
landmarks_data.append({
'x': landmark.x,
'y': landmark.y,
'z': landmark.z
})
self.hand_landmarks_list.append(landmarks_data)
# Store processed frame
self.frame = frame.copy()
# Send to clients if server is running
if self.server_running and self.clients:
self.broadcast_frame(frame, self.hand_landmarks_list)
# Display frame
self.display_frame(frame)
# Calculate FPS
fps_counter += 1
if time.time() - fps_time > 1:
fps = fps_counter
fps_counter = 0
fps_time = time.time()
# Update stats
self.stats_label.config(text=f"FPS: {fps} | Hands Detected: {hands_detected} | Clients: {len(self.clients)}")
def display_frame(self, frame):
"""Display frame on canvas"""
# Resize frame to fit canvas
canvas_width = self.canvas.winfo_width()
canvas_height = self.canvas.winfo_height()
if canvas_width > 1 and canvas_height > 1:
# Calculate aspect ratio
frame_height, frame_width = frame.shape[:2]
aspect_ratio = frame_width / frame_height
if canvas_width / canvas_height > aspect_ratio:
new_height = canvas_height
new_width = int(new_height * aspect_ratio)
else:
new_width = canvas_width
new_height = int(new_width / aspect_ratio)
frame = cv2.resize(frame, (new_width, new_height), interpolation=cv2.INTER_LINEAR)
# Convert to PhotoImage
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(frame_rgb)
photo = ImageTk.PhotoImage(image=img)
# Update canvas without clearing (prevents flicker)
if self.canvas_image is None:
self.canvas_image = self.canvas.create_image(
canvas_width // 2, canvas_height // 2,
image=photo, anchor=tk.CENTER
)
else:
self.canvas.itemconfig(self.canvas_image, image=photo)
self.canvas.coords(self.canvas_image, canvas_width // 2, canvas_height // 2)
self.canvas.image = photo # Keep a reference
def toggle_server(self):
"""Start or stop the socket server"""
if not self.server_running:
self.server_running = True
self.server_button.config(text="Stop Server", bg="#e74c3c")
self.server_status_label.config(text=f"Server: ON (Port {self.server_port})", fg="#27ae60")
threading.Thread(target=self.run_server, daemon=True).start()
else:
self.server_running = False
self.server_button.config(text="Start Server", bg="#3498db")
self.server_status_label.config(text="Server: OFF", fg="#e74c3c")
if self.server_socket:
self.server_socket.close()
self.clients = []
def run_server(self):
"""Run the socket server"""
try:
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) # Disable Nagle's algorithm
self.server_socket.bind(('0.0.0.0', self.server_port))
self.server_socket.listen(5)
self.info_label.config(text=f"Server listening on {self.get_local_ip()}:{self.server_port}")
while self.server_running:
try:
self.server_socket.settimeout(1.0)
client_socket, address = self.server_socket.accept()
client_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) # Disable Nagle's for client too
self.clients.append(client_socket)
print(f"Client connected: {address}")
threading.Thread(target=self.handle_client, args=(client_socket,), daemon=True).start()
except socket.timeout:
continue
except Exception as e:
if self.server_running:
print(f"Server error: {e}")
break
except Exception as e:
self.info_label.config(text=f"Server error: {e}")
self.server_running = False
self.server_button.config(text="Start Server", bg="#3498db")
self.server_status_label.config(text="Server: OFF", fg="#e74c3c")
def handle_client(self, client_socket):
"""Handle individual client connection"""
try:
while self.server_running and client_socket in self.clients:
pass # Client handling is done in broadcast_frame
except:
pass
finally:
if client_socket in self.clients:
self.clients.remove(client_socket)
try:
client_socket.close()
except:
pass
def broadcast_frame(self, frame, landmarks):
"""Send frame and landmarks to all connected clients"""
try:
# Resize frame before encoding to reduce bandwidth
height, width = frame.shape[:2]
max_width = 640 # Reduce resolution for faster transmission
if width > max_width:
scale = max_width / width
new_width = max_width
new_height = int(height * scale)
frame = cv2.resize(frame, (new_width, new_height), interpolation=cv2.INTER_LINEAR)
# Encode frame as JPEG with lower quality for speed
_, buffer = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 60])
data = pickle.dumps({
'frame': buffer,
'landmarks': landmarks
})
# Send to all clients
message = struct.pack("Q", len(data)) + data
disconnected_clients = []
for client in self.clients:
try:
client.sendall(message)
except:
disconnected_clients.append(client)
# Remove disconnected clients
for client in disconnected_clients:
if client in self.clients:
self.clients.remove(client)
try:
client.close()
except:
pass
except Exception as e:
print(f"Broadcast error: {e}")
def get_local_ip(self):
"""Get local IP address"""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
except:
return "127.0.0.1"
def on_closing(self):
"""Cleanup on application close"""
self.is_running = False
self.server_running = False
if self.cap:
self.cap.release()
if self.server_socket:
self.server_socket.close()
for client in self.clients:
try:
client.close()
except:
pass
self.root.destroy()
if __name__ == "__main__":
root = tk.Tk()
app = HandTrackingApp(root)
root.protocol("WM_DELETE_WINDOW", app.on_closing)
root.mainloop()