-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
540 lines (456 loc) · 20 KB
/
Copy pathmain.py
File metadata and controls
540 lines (456 loc) · 20 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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import cv2
import numpy as np
import tkinter as tk
from tkinter import ttk, messagebox
import random
import threading
import time
from PIL import Image, ImageTk
import mediapipe as mp
class EmojiMimickGame:
def __init__(self):
self.root = tk.Tk()
self.root.title("Emoji Mimick Game 😀")
self.root.geometry("1200x800") # Much bigger window!
self.root.configure(bg='#2c3e50')
# Game state
self.score = 0
self.round_count = 0
self.max_rounds = 10
self.current_emoji = None
self.game_active = False
self.detection_start_time = None
self.hold_duration = 2.0 # seconds to hold expression
# Emoji mappings with facial features
self.emojis = {
'😀': {'name': 'Happy', 'mouth_open': True, 'smile': True},
'🙂': {'name': 'Smile', 'mouth_open': False, 'smile': True},
'😉': {'name': 'Wink', 'mouth_open': False, 'smile': True, 'wink': True},
'😗': {'name': 'Kiss', 'mouth_open': False, 'smile': False, 'kiss': True},
'😔': {'name': 'Sad', 'mouth_open': False, 'smile': False, 'sad': True},
'☹️': {'name': 'Frown', 'mouth_open': False, 'smile': False, 'frown': True},
'😫': {'name': 'Tired', 'mouth_open': False, 'smile': False, 'tired': True},
'😡': {'name': 'Angry', 'mouth_open': False, 'smile': False, 'angry': True},
'😳': {'name': 'Surprised', 'mouth_open': True, 'smile': False, 'surprised': True},
'😱': {'name': 'Shocked', 'mouth_open': True, 'smile': False, 'shocked': True},
'😐': {'name': 'Neutral', 'mouth_open': False, 'smile': False, 'neutral': True},
'😬': {'name': 'Grimace', 'mouth_open': False, 'smile': False, 'grimace': True},
'🤢': {'name': 'Sick', 'mouth_open': False, 'smile': False, 'sick': True},
'😮': {'name': 'Oh', 'mouth_open': True, 'smile': False, 'oh': True}
}
# MediaPipe setup
self.mp_face_mesh = mp.solutions.face_mesh
self.mp_drawing = mp.solutions.drawing_utils
self.face_mesh = self.mp_face_mesh.FaceMesh(
static_image_mode=False,
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5
)
# Camera setup
self.cap = None
self.camera_active = False
self.setup_ui()
def load_emoji_images(self):
"""Load emoji images from the emojis folder"""
import os
emoji_images = {}
emoji_folder = "emojis"
# Mapping of emoji characters to file names
emoji_filenames = {
'😀': 'Happy.png',
'🙂': 'Smile.png',
'😉': 'Wink.png',
'😗': 'Kiss.png',
'😔': 'Sad.png',
'☹️': 'Frown.png',
'😫': 'Tired.png',
'😡': 'Angry.png',
'😳': 'Surprised.png',
'😱': 'Shocked.png',
'😐': 'Neutral.png',
'😬': 'Grimace.png',
'🤢': 'Sick.png',
'😮': 'Oh.png'
}
try:
if os.path.exists(emoji_folder):
for emoji, filename in emoji_filenames.items():
filepath = os.path.join(emoji_folder, filename)
if os.path.exists(filepath):
try:
# Load and resize image
img = Image.open(filepath)
img = img.resize((200, 200), Image.Resampling.LANCZOS)
emoji_images[emoji] = ImageTk.PhotoImage(img)
print(f"Loaded emoji image: {filename}")
except Exception as e:
print(f"Error loading {filename}: {e}")
emoji_images[emoji] = None
else:
print(f"Image not found: {filepath}")
emoji_images[emoji] = None
else:
print(f"Emoji folder '{emoji_folder}' not found. Using text emojis.")
except Exception as e:
print(f"Error loading emoji images: {e}")
return emoji_images
def setup_ui(self):
# Title
title_label = tk.Label(
self.root,
text="🎭 Emoji Mimick Game 🎭",
font=("Arial", 24, "bold"),
bg='#2c3e50',
fg='#ecf0f1'
)
title_label.pack(pady=10)
# Score and round display
self.info_frame = tk.Frame(self.root, bg='#2c3e50')
self.info_frame.pack(pady=5)
self.score_label = tk.Label(
self.info_frame,
text=f"Score: {self.score}",
font=("Arial", 16),
bg='#2c3e50',
fg='#e74c3c'
)
self.score_label.pack(side=tk.LEFT, padx=20)
self.round_label = tk.Label(
self.info_frame,
text=f"Round: {self.round_count}/{self.max_rounds}",
font=("Arial", 16),
bg='#2c3e50',
fg='#3498db'
)
self.round_label.pack(side=tk.RIGHT, padx=20)
# Main game area with camera and emoji
self.game_frame = tk.Frame(self.root, bg='#2c3e50')
self.game_frame.pack(pady=10)
# Camera feed
self.camera_frame = tk.Frame(self.game_frame, bg='#34495e', relief='raised', bd=2)
self.camera_frame.pack(side=tk.LEFT, padx=10)
self.camera_label = tk.Label(
self.camera_frame,
text="Camera will appear here",
font=("Arial", 14),
bg='#34495e',
fg='#95a5a6'
)
self.camera_label.pack(padx=5, pady=5)
# Current emoji display
self.emoji_frame = tk.Frame(self.game_frame, bg='#34495e', relief='raised', bd=2)
self.emoji_frame.pack(side=tk.RIGHT, padx=10)
self.emoji_label = tk.Label(
self.emoji_frame,
text="Press Start to Play!",
font=("Arial", 36),
bg='#34495e',
fg='#ecf0f1',
padx=20,
pady=20
)
self.emoji_label.pack()
# Emoji name label
self.emoji_name_label = tk.Label(
self.emoji_frame,
text="",
font=("Arial", 18, "bold"),
bg='#34495e',
fg='#e74c3c'
)
self.emoji_name_label.pack(pady=(0, 10))
# Load emoji images
self.emoji_images = self.load_emoji_images()
# Status label
self.status_label = tk.Label(
self.root,
text="Make sure your camera is working and you're well lit!",
font=("Arial", 12),
bg='#2c3e50',
fg='#95a5a6'
)
self.status_label.pack(pady=10)
# Progress bar for holding expression
self.progress = ttk.Progressbar(
self.root,
length=400,
mode='determinate'
)
self.progress.pack(pady=5)
# Control buttons
self.button_frame = tk.Frame(self.root, bg='#2c3e50')
self.button_frame.pack(pady=15)
self.start_button = tk.Button(
self.button_frame,
text="Start Game",
font=("Arial", 14, "bold"),
bg='#27ae60',
fg='white',
padx=20,
pady=10,
command=self.start_game
)
self.start_button.pack(side=tk.LEFT, padx=10)
self.stop_button = tk.Button(
self.button_frame,
text="Stop Game",
font=("Arial", 14, "bold"),
bg='#e74c3c',
fg='white',
padx=20,
pady=10,
command=self.stop_game,
state=tk.DISABLED
)
self.stop_button.pack(side=tk.LEFT, padx=10)
self.next_button = tk.Button(
self.button_frame,
text="Skip Emoji",
font=("Arial", 14),
bg='#f39c12',
fg='white',
padx=20,
pady=10,
command=self.next_emoji,
state=tk.DISABLED
)
self.next_button.pack(side=tk.LEFT, padx=10)
def calculate_expression_score(self, landmarks):
"""Calculate expression matching score based on facial landmarks"""
if not landmarks or not self.current_emoji:
return 0
# Get key facial points
left_eye = landmarks[33] # Left eye outer corner
right_eye = landmarks[133] # Right eye outer corner
nose_tip = landmarks[1]
mouth_left = landmarks[61]
mouth_right = landmarks[291]
mouth_top = landmarks[13]
mouth_bottom = landmarks[14]
# Calculate features
mouth_width = abs(mouth_right.x - mouth_left.x)
mouth_height = abs(mouth_top.y - mouth_bottom.y)
mouth_aspect_ratio = mouth_height / mouth_width if mouth_width > 0 else 0
# Eye aspect ratios (for detecting winks, surprise, etc.)
left_eye_height = abs(landmarks[159].y - landmarks[145].y)
right_eye_height = abs(landmarks[386].y - landmarks[374].y)
eye_width = abs(right_eye.x - left_eye.x)
left_eye_ratio = left_eye_height / eye_width if eye_width > 0 else 0
right_eye_ratio = right_eye_height / eye_width if eye_width > 0 else 0
# Mouth corners relative to center
mouth_center_y = (mouth_top.y + mouth_bottom.y) / 2
left_corner_lift = mouth_center_y - mouth_left.y
right_corner_lift = mouth_center_y - mouth_right.y
smile_indicator = (left_corner_lift + right_corner_lift) / 2
emoji_features = self.emojis[self.current_emoji]
score = 0
# Check mouth opening
if emoji_features.get('mouth_open', False):
if mouth_aspect_ratio > 0.03: # Mouth is open
score += 30
else:
if mouth_aspect_ratio < 0.02: # Mouth is closed
score += 20
# Check smile
if emoji_features.get('smile', False):
if smile_indicator > 0.005: # Smiling
score += 40
elif emoji_features.get('frown', False) or emoji_features.get('sad', False):
if smile_indicator < -0.002: # Frowning
score += 40
# Check for wink
if emoji_features.get('wink', False):
if abs(left_eye_ratio - right_eye_ratio) > 0.01: # One eye more closed
score += 30
# Check for surprise/shock (wide eyes)
if emoji_features.get('surprised', False) or emoji_features.get('shocked', False):
if left_eye_ratio > 0.02 and right_eye_ratio > 0.02: # Both eyes wide
score += 35
# Neutral expression
if emoji_features.get('neutral', False):
if abs(smile_indicator) < 0.003 and mouth_aspect_ratio < 0.02:
score += 40
return min(score, 100) # Cap at 100
def camera_loop(self):
"""Main camera processing loop"""
self.cap = cv2.VideoCapture(0)
if not self.cap.isOpened():
messagebox.showerror("Error", "Could not open camera!")
return
self.camera_active = True
match_threshold = 70 # Score needed to match emoji
while self.camera_active:
ret, frame = self.cap.read()
if not ret:
break
# Flip frame horizontally for mirror effect
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Create a copy for display
display_frame = rgb_frame.copy()
# Process with MediaPipe
results = self.face_mesh.process(rgb_frame)
score = 0
if results.multi_face_landmarks and self.game_active:
for face_landmarks in results.multi_face_landmarks:
# Draw face landmarks on display frame
self.mp_drawing.draw_landmarks(
display_frame,
face_landmarks,
self.mp_face_mesh.FACEMESH_CONTOURS,
None,
self.mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=1, circle_radius=1)
)
# Calculate expression score
score = self.calculate_expression_score(face_landmarks.landmark)
# Update status only if game is active
if score >= match_threshold:
if self.detection_start_time is None:
self.detection_start_time = time.time()
elapsed = time.time() - self.detection_start_time
progress = min((elapsed / self.hold_duration) * 100, 100)
self.root.after(0, lambda p=progress: self.progress.config(value=p))
self.root.after(0, lambda e=elapsed: self.status_label.config(
text=f"Great! Hold it... {e:.1f}s/{self.hold_duration}s",
fg='#27ae60'
))
if elapsed >= self.hold_duration:
self.root.after(0, self.expression_matched)
break
else:
self.detection_start_time = None
self.root.after(0, lambda: self.progress.config(value=0))
self.root.after(0, lambda s=score: self.status_label.config(
text=f"Match score: {s}% - Try harder!",
fg='#e67e22'
))
elif self.game_active:
self.detection_start_time = None
self.root.after(0, lambda: self.progress.config(value=0))
self.root.after(0, lambda: self.status_label.config(
text="No face detected - make sure you're in view!",
fg='#e74c3c'
))
# Add score overlay to frame
if self.game_active and score > 0:
cv2.putText(display_frame, f"Score: {score}%", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2)
# Convert frame to PhotoImage and display (MUCH BIGGER!)
display_frame = cv2.resize(display_frame, (640, 480)) # Increased from 480x360
img = Image.fromarray(display_frame)
photo = ImageTk.PhotoImage(image=img)
# Update camera display
self.root.after(0, lambda p=photo: self.update_camera_display(p))
time.sleep(0.03) # ~30 FPS
if self.cap:
self.cap.release()
def update_camera_display(self, photo):
"""Update the camera display with new frame"""
self.camera_label.config(image=photo, text="")
self.camera_label.image = photo # Keep a reference
def expression_matched(self):
"""Called when expression is successfully matched"""
self.score += 10
self.status_label.config(text="Perfect! 🎉", fg='#27ae60')
self.score_label.config(text=f"Score: {self.score}")
self.progress.config(value=100)
# Brief pause before next emoji
self.root.after(1500, self.next_emoji)
def next_emoji(self):
"""Move to next emoji"""
if not self.game_active:
return
self.round_count += 1
self.detection_start_time = None
self.progress.config(value=0)
if self.round_count >= self.max_rounds:
self.end_game()
return
# Select random emoji
self.current_emoji = random.choice(list(self.emojis.keys()))
emoji_name = self.emojis[self.current_emoji]['name']
# Display emoji image if available, otherwise use text
if self.current_emoji in self.emoji_images and self.emoji_images[self.current_emoji]:
self.emoji_label.config(
image=self.emoji_images[self.current_emoji],
text="",
compound='center'
)
self.emoji_label.image = self.emoji_images[self.current_emoji] # Keep reference
else:
# Fallback to text emoji
self.emoji_label.config(
text=self.current_emoji,
image="",
compound='center'
)
self.emoji_name_label.config(text=emoji_name)
self.round_label.config(text=f"Round: {self.round_count}/{self.max_rounds}")
self.status_label.config(text="Make this expression!", fg='#3498db')
def start_game(self):
"""Start the game"""
self.game_active = True
self.score = 0
self.round_count = 0
self.start_button.config(state=tk.DISABLED)
self.stop_button.config(state=tk.NORMAL)
self.next_button.config(state=tk.NORMAL)
# Start camera in separate thread
if not self.camera_active:
self.camera_thread = threading.Thread(target=self.camera_loop, daemon=True)
self.camera_thread.start()
# Start first round
self.next_emoji()
def stop_game(self):
"""Stop the game"""
self.game_active = False
self.start_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
self.next_button.config(state=tk.DISABLED)
self.emoji_label.config(text="Game Stopped")
self.status_label.config(text="Camera is still on - Press Start to play again!", fg='#95a5a6')
self.progress.config(value=0)
def end_game(self):
"""End the game and show results"""
self.game_active = False
final_score = (self.score / (self.max_rounds * 10)) * 100
result_text = f"Game Over!\n\nFinal Score: {self.score}/{self.max_rounds * 10}\nAccuracy: {final_score:.1f}%"
if final_score >= 80:
result_text += "\n\n🏆 Excellent! You're an emoji master!"
elif final_score >= 60:
result_text += "\n\n😊 Good job! Keep practicing!"
else:
result_text += "\n\n😅 Not bad! Try again for a better score!"
messagebox.showinfo("Game Results", result_text)
self.start_button.config(state=tk.NORMAL)
self.stop_button.config(state=tk.DISABLED)
self.next_button.config(state=tk.DISABLED)
self.emoji_label.config(text="Press Start to Play Again!", image="")
self.emoji_name_label.config(text="")
self.status_label.config(text="Camera is still on - Ready for another round?", fg='#95a5a6')
def run(self):
"""Start the application"""
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
self.root.mainloop()
def on_closing(self):
"""Clean up when closing the application"""
self.game_active = False
self.camera_active = False
if self.cap:
self.cap.release()
self.root.destroy()
if __name__ == "__main__":
# Check if required libraries are available
try:
import cv2
import mediapipe as mp
print("All required libraries found!")
print("Starting Emoji Mimick Game...")
game = EmojiMimickGame()
game.run()
except ImportError as e:
print(f"Missing required library: {e}")
print("\nPlease install required packages:")
print("pip install opencv-python mediapipe pillow")