-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworking_gui.py
More file actions
514 lines (423 loc) · 18.8 KB
/
working_gui.py
File metadata and controls
514 lines (423 loc) · 18.8 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#!/usr/bin/env python3
"""
Working GUI that forces camera feed display
"""
import tkinter as tk
from tkinter import ttk, messagebox
import cv2
import numpy as np
from PIL import Image, ImageTk
import threading
import time
import logging
from pathlib import Path
import sys
# Add src to path
sys.path.insert(0, str(Path(__file__).parent / 'src'))
from camera_manager import CameraManager
from face_detector import FaceDetector
from emotion_detector import EmotionDetector
from utils.preprocessing import ImagePreprocessor
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s:%(name)s:%(message)s')
logger = logging.getLogger(__name__)
class WorkingEmoticonGUI:
def __init__(self):
self.root = tk.Tk()
self.root.title("Emoticon - Working GUI")
self.root.geometry("1200x700")
self.root.configure(bg='#2c3e50')
# Variables
self.is_running = False
self.camera_manager = None
self.face_detector = None
self.emotion_detector = None
self.preprocessor = None
self.current_photo = None # Keep reference to prevent garbage collection
# Setup GUI
self.setup_gui()
# Initialize components
self.initialize_components()
# Protocol for closing
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
# Force window to front
self.root.lift()
self.root.attributes('-topmost', True)
self.root.attributes('-topmost', False)
def setup_gui(self):
"""Setup the GUI layout"""
# Main frame
main_frame = tk.Frame(self.root, bg='#2c3e50')
main_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
# Title
title_label = tk.Label(
main_frame,
text="Emoticon - Live Camera Feed",
font=('Arial', 24, 'bold'),
bg='#2c3e50',
fg='#ecf0f1'
)
title_label.pack(pady=(0, 20))
# Content frame
content_frame = tk.Frame(main_frame, bg='#2c3e50')
content_frame.pack(fill=tk.BOTH, expand=True)
# Left side - Camera feed
left_frame = tk.Frame(content_frame, bg='#34495e', relief=tk.RAISED, bd=2)
left_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(0, 10))
camera_label = tk.Label(
left_frame,
text="LIVE CAMERA FEED",
font=('Arial', 16, 'bold'),
bg='#34495e',
fg='#ecf0f1'
)
camera_label.pack(pady=10)
# Video display area with border
video_frame = tk.Frame(left_frame, bg='#2c3e50', relief=tk.SUNKEN, bd=3)
video_frame.pack(pady=20, padx=20, fill=tk.BOTH, expand=True)
self.video_label = tk.Label(
video_frame,
text="Camera not started\nClick 'Start Detection' to begin",
font=('Arial', 14),
bg='#2c3e50',
fg='#bdc3c7',
width=60,
height=20
)
self.video_label.pack(pady=20)
# Right side - Emotion display
right_frame = tk.Frame(content_frame, bg='#34495e', relief=tk.RAISED, bd=2)
right_frame.pack(side=tk.RIGHT, fill=tk.BOTH, padx=(10, 0))
emotion_label = tk.Label(
right_frame,
text="Emotion Detection",
font=('Arial', 16, 'bold'),
bg='#34495e',
fg='#ecf0f1'
)
emotion_label.pack(pady=10)
# Emotion icon
self.emotion_icon_label = tk.Label(
right_frame,
text="😐",
font=('Arial', 72),
bg='#34495e',
fg='#ecf0f1'
)
self.emotion_icon_label.pack(pady=20)
# Emotion text
self.emotion_text_label = tk.Label(
right_frame,
text="Neutral",
font=('Arial', 18, 'bold'),
bg='#34495e',
fg='#ecf0f1'
)
self.emotion_text_label.pack(pady=10)
# Confidence
self.confidence_label = tk.Label(
right_frame,
text="Confidence: 0%",
font=('Arial', 14),
bg='#34495e',
fg='#bdc3c7'
)
self.confidence_label.pack(pady=10)
# Debug info
self.debug_label = tk.Label(
right_frame,
text="Status: Ready",
font=('Arial', 10),
bg='#34495e',
fg='#95a5a6'
)
self.debug_label.pack(pady=10)
# Buttons frame
button_frame = tk.Frame(main_frame, bg='#2c3e50')
button_frame.pack(pady=20)
# Start button
self.start_button = tk.Button(
button_frame,
text="Start Detection",
command=self.start_detection,
font=('Arial', 14, 'bold'),
bg='#27ae60',
fg='white',
relief=tk.RAISED,
bd=3,
padx=20,
pady=10
)
self.start_button.pack(side=tk.LEFT, padx=10)
# Stop button
self.stop_button = tk.Button(
button_frame,
text="Stop Detection",
command=self.stop_detection,
font=('Arial', 14, 'bold'),
bg='#e74c3c',
fg='white',
relief=tk.RAISED,
bd=3,
padx=20,
pady=10,
state=tk.DISABLED
)
self.stop_button.pack(side=tk.LEFT, padx=10)
# Test camera button
self.test_button = tk.Button(
button_frame,
text="Test Camera",
command=self.test_camera,
font=('Arial', 12),
bg='#3498db',
fg='white',
relief=tk.RAISED,
bd=2,
padx=15,
pady=8
)
self.test_button.pack(side=tk.LEFT, padx=10)
# Status
self.status_label = tk.Label(
main_frame,
text="Ready to start - GUI loaded successfully",
font=('Arial', 12),
bg='#2c3e50',
fg='#ecf0f1'
)
self.status_label.pack(pady=10)
def initialize_components(self):
"""Initialize the emotion detection components"""
try:
config_dir = Path(__file__).parent / "config"
logger.info(f"Config directory: {config_dir}")
# Initialize camera manager
self.camera_manager = CameraManager(config_dir / "camera_config.yaml")
# Initialize face detector
self.face_detector = FaceDetector(config_dir / "model_config.yaml")
# Initialize emotion detector
self.emotion_detector = EmotionDetector(config_dir / "model_config.yaml")
# Initialize preprocessor
self.preprocessor = ImagePreprocessor(config_dir / "model_config.yaml")
self.status_label.config(text="Components initialized successfully")
self.debug_label.config(text="Status: Components ready")
logger.info("All components initialized successfully")
# Show a test image
self.show_test_image()
except Exception as e:
logger.error(f"Failed to initialize components: {e}")
messagebox.showerror("Error", f"Failed to initialize components: {e}")
self.status_label.config(text="Failed to initialize components")
def show_test_image(self):
"""Show a test image in the video label"""
try:
# Create a test image
test_image = np.zeros((480, 640, 3), dtype=np.uint8)
# Add some text to the test image
cv2.putText(test_image, "Camera Ready", (200, 240),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
cv2.putText(test_image, "Click 'Start Detection' to begin", (150, 280),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (200, 200, 200), 2)
# Convert to PIL and display
test_image_rgb = cv2.cvtColor(test_image, cv2.COLOR_BGR2RGB)
pil_image = Image.fromarray(test_image_rgb)
photo = ImageTk.PhotoImage(pil_image)
self.video_label.configure(image=photo, text="")
self.video_label.image = photo
self.current_photo = photo # Keep reference
self.debug_label.config(text="Status: Test image displayed")
except Exception as e:
logger.error(f"Error showing test image: {e}")
def test_camera(self):
"""Test camera functionality"""
try:
self.debug_label.config(text="Status: Testing camera...")
# Try to start camera
self.camera_manager.start()
# Get a test frame
test_frame = self.camera_manager.get_frame()
if test_frame is not None:
# Display the test frame
frame_rgb = cv2.cvtColor(test_frame, cv2.COLOR_BGR2RGB)
# Resize for display
height, width = frame_rgb.shape[:2]
max_width = 640
max_height = 480
if width > max_width or height > max_height:
scale = min(max_width / width, max_height / height)
new_width = int(width * scale)
new_height = int(height * scale)
frame_rgb = cv2.resize(frame_rgb, (new_width, new_height))
# Convert to PIL and display
pil_image = Image.fromarray(frame_rgb)
photo = ImageTk.PhotoImage(pil_image)
self.video_label.configure(image=photo, text="")
self.video_label.image = photo
self.current_photo = photo
self.debug_label.config(text="Status: Camera test successful!")
messagebox.showinfo("Camera Test", "Camera is working correctly!")
else:
raise Exception("No frame received from camera")
# Stop camera after test
self.camera_manager.stop()
except Exception as e:
logger.error(f"Camera test failed: {e}")
messagebox.showerror("Camera Test Failed",
f"Camera test failed: {e}\n\n"
"Please check camera permissions in System Preferences > Security & Privacy > Privacy > Camera")
self.debug_label.config(text="Status: Camera test failed")
self.show_test_image()
def start_detection(self):
"""Start the emotion detection"""
if not self.is_running:
try:
# Try to start camera
self.camera_manager.start()
# Test if camera is working
test_frame = self.camera_manager.get_frame()
if test_frame is None:
raise Exception("Camera not accessible. Please check camera permissions.")
self.is_running = True
self.start_button.config(state=tk.DISABLED)
self.stop_button.config(state=tk.NORMAL)
self.status_label.config(text="Detection started - Camera active")
self.debug_label.config(text="Status: Detection running")
# Start the detection thread
self.detection_thread = threading.Thread(target=self.detection_loop, daemon=True)
self.detection_thread.start()
except Exception as e:
logger.error(f"Failed to start detection: {e}")
messagebox.showerror("Camera Error",
f"Failed to start camera: {e}\n\n"
"Please check:\n"
"1. Camera permissions in System Preferences\n"
"2. Camera is not being used by another application\n"
"3. Camera is properly connected")
self.status_label.config(text="Camera error - check permissions")
self.debug_label.config(text="Status: Camera error")
def stop_detection(self):
"""Stop the emotion detection"""
if self.is_running:
self.is_running = False
self.camera_manager.stop()
self.start_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
self.status_label.config(text="Detection stopped")
self.debug_label.config(text="Status: Detection stopped")
self.show_test_image()
def detection_loop(self):
"""Main detection loop"""
frame_count = 0
start_time = time.time()
logger.info("Detection loop started")
while self.is_running:
try:
# Get frame from camera
frame = self.camera_manager.get_frame()
if frame is None:
logger.warning("No frame received from camera")
time.sleep(0.1)
continue
frame_count += 1
logger.info(f"Processing frame {frame_count}")
# Detect faces
faces = self.face_detector.detect_faces(frame)
logger.info(f"Detected {len(faces)} faces")
# Process each detected face
emotions = []
for face_bbox in faces:
# Extract face region
face_img = self.preprocessor.extract_face(frame, face_bbox)
if face_img is not None:
# Preprocess face image
processed_face = self.preprocessor.preprocess(face_img)
# Detect emotion
emotion_result = self.emotion_detector.detect_emotion(processed_face)
if emotion_result:
emotions.append({
'bbox': face_bbox,
'emotion': emotion_result['emotion'],
'confidence': emotion_result['confidence']
})
# Update GUI with results
self.root.after(0, self.update_gui, frame, emotions, frame_count, start_time)
if frame_count % 30 == 0: # Log every 30 frames
logger.info(f"Processed {frame_count} frames, {len(emotions)} emotions detected")
except Exception as e:
logger.error(f"Error in detection loop: {e}")
time.sleep(0.1)
continue
def update_gui(self, frame, emotions, frame_count, start_time):
"""Update the GUI with detection results"""
try:
logger.info(f"Updating GUI with frame {frame_count}, {len(emotions)} emotions")
# Update video frame
if frame is not None:
# Convert BGR to RGB
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Draw face detection boxes
for emotion in emotions:
bbox = emotion['bbox']
cv2.rectangle(frame_rgb, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 2)
# Draw emotion label
label = f"{emotion['emotion']}: {emotion['confidence']:.2f}"
cv2.putText(frame_rgb, label, (bbox[0], bbox[1] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Resize frame for display
height, width = frame_rgb.shape[:2]
max_width = 640
max_height = 480
if width > max_width or height > max_height:
scale = min(max_width / width, max_height / height)
new_width = int(width * scale)
new_height = int(height * scale)
frame_rgb = cv2.resize(frame_rgb, (new_width, new_height))
# Convert to PIL Image
pil_image = Image.fromarray(frame_rgb)
photo = ImageTk.PhotoImage(pil_image)
# Update video label
self.video_label.configure(image=photo, text="")
self.video_label.image = photo
self.current_photo = photo # Keep reference
logger.info(f"Updated video frame: {frame_rgb.shape}")
# Update emotion display
if emotions:
emotion = emotions[0] # Show first detected emotion
emotion_name = emotion['emotion']
confidence = emotion['confidence']
# Update emotion icon and text
emotion_icons = {
'happy': '😊',
'sad': '😢',
'angry': '😠',
'surprise': '😲',
'fear': '😨',
'disgust': '🤢',
'neutral': '😐'
}
icon = emotion_icons.get(emotion_name, "😐")
self.emotion_icon_label.config(text=icon)
self.emotion_text_label.config(text=emotion_name.title())
self.confidence_label.config(text=f"Confidence: {confidence*100:.1f}%")
logger.info(f"Updated emotion: {emotion_name} ({confidence*100:.1f}%)")
else:
# No emotions detected
self.emotion_icon_label.config(text="😐")
self.emotion_text_label.config(text="No Face")
self.confidence_label.config(text="Confidence: 0%")
# Update debug info
self.debug_label.config(text=f"Status: Frame {frame_count}, {len(emotions)} emotions")
except Exception as e:
logger.error(f"Error in update_gui: {e}")
def on_closing(self):
"""Handle window closing"""
if self.is_running:
self.stop_detection()
self.root.destroy()
def main():
"""Main entry point"""
app = WorkingEmoticonGUI()
app.root.mainloop()
if __name__ == "__main__":
main()