Added Mediapipe gesture detection and camera setup

This commit is contained in:
Elise Angela Bwemba 2025-11-12 15:19:21 +01:00
commit f1fc39dc05
4 changed files with 457 additions and 0 deletions

335
Finished_Memory_Mouse.py Normal file
View File

@ -0,0 +1,335 @@
import pygame
import random
import sys
import os
import time
from multiprocessing import Process, Queue
from gesture_input import run_gesture_input
# -------------------------------
# CountryCapital Memory Game + Gestensteuerung
# -------------------------------
CARD_BACK_COLOR = (100, 100, 200)
CARD_FRONT_COLOR = (230, 230, 250)
MATCH_COLOR = (120, 200, 120)
TEXT_COLOR = (0, 0, 0)
BG_COLOR = (50, 50, 80)
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
FPS = 30
class MemoryGame:
def __init__(self, gesture_queue):
self.deck = []
self.pair_map = {}
self.matched = []
self.revealed = []
self.scores = [0, 0]
self.current_player = 0
self.font = None
self.card_rects = []
self.selected = []
self.found_pairs = 0
self.total_pairs = 0
self.running = True
self.awaiting_confirmation = False
self.confirmation_result = None
self.correct_answer_expected = None
self.state = "continent" # continent → americas → difficulty → pairs → game
self.buttons = []
self.selected_continents = []
self.level = None
self.pair_count = 6 # Default
self.gesture_queue = gesture_queue
# -------------------------------
# Card Loading
# -------------------------------
def load_cards(self, filename):
"""Loads pairs from a text file."""
if not os.path.exists(filename):
print(f"⚠️ File not found: {filename}")
return []
pairs = []
with open(filename, "r", encoding="utf-8") as f:
for line in f:
parts = line.strip().split()
if len(parts) >= 2:
pairs.append((parts[0], parts[1]))
return pairs
def prepare_deck(self):
"""Loads all relevant continent + difficulty files."""
self.deck = []
for continent in self.selected_continents:
base = continent
if self.level == "Easy":
self.deck += self.load_cards(base + "-major.txt")
elif self.level == "Normal":
self.deck += self.load_cards(base + "-major.txt")
self.deck += self.load_cards(base + "-Minor.txt")
elif self.level == "Hard":
self.deck += self.load_cards(base + "-major.txt")
self.deck += self.load_cards(base + "-Minor.txt")
self.deck += self.load_cards(base + "-Dependent.txt")
if not self.deck:
print("⚠️ No cards loaded, check text files.")
sys.exit()
random.shuffle(self.deck)
# Limit to selected pair count
self.deck = self.deck[:self.pair_count]
def setup_game(self):
self.cards = []
self.pair_map = {}
for country, capital in self.deck:
self.cards.append(country)
self.cards.append(capital)
self.pair_map[country] = capital
self.pair_map[capital] = country
random.shuffle(self.cards)
self.matched = [False] * len(self.cards)
self.revealed = [False] * len(self.cards)
self.total_pairs = len(self.deck)
self.card_rects = []
self.selected = []
cols = 4
rows = (len(self.cards) + cols - 1) // cols
margin = 10
card_width = (SCREEN_WIDTH - (cols + 1) * margin) // cols
card_height = (SCREEN_HEIGHT - (rows + 1) * margin - 100) // rows
y_offset = 80
for i, _ in enumerate(self.cards):
col = i % cols
row = i // cols
x = margin + col * (card_width + margin)
y = y_offset + margin + row * (card_height + margin)
rect = pygame.Rect(x, y, card_width, card_height)
self.card_rects.append(rect)
# -------------------------------
# Drawing Menus
# -------------------------------
def draw_menu(self, screen, title, options):
screen.fill(BG_COLOR)
title_text = self.font.render(title, True, (255, 255, 255))
screen.blit(title_text, (SCREEN_WIDTH // 2 - title_text.get_width() // 2, 100))
self.buttons = []
for i, option in enumerate(options):
rect = pygame.Rect(SCREEN_WIDTH // 2 - 150, 200 + i * 70, 300, 50)
pygame.draw.rect(screen, (100, 100, 250), rect)
pygame.draw.rect(screen, (255, 255, 255), rect, 2)
text = self.font.render(option, True, (255, 255, 255))
screen.blit(text, (rect.centerx - text.get_width() // 2, rect.centery - text.get_height() // 2))
self.buttons.append((rect, option))
pygame.display.flip()
def draw_game(self, screen):
screen.fill(BG_COLOR)
title = self.font.render(f"Player {self.current_player + 1}'s turn", True, (255, 255, 255))
screen.blit(title, (20, 20))
score_text = self.font.render(f"Scores: P1={self.scores[0]} P2={self.scores[1]}", True, (200, 200, 200))
screen.blit(score_text, (20, 50))
for i, rect in enumerate(self.card_rects):
if self.matched[i]:
color = MATCH_COLOR
elif self.revealed[i]:
color = CARD_FRONT_COLOR
else:
color = CARD_BACK_COLOR
pygame.draw.rect(screen, color, rect)
pygame.draw.rect(screen, (0, 0, 0), rect, 2)
if self.revealed[i] or self.matched[i]:
text = self.font.render(self.cards[i], True, TEXT_COLOR)
text_rect = text.get_rect(center=rect.center)
screen.blit(text, text_rect)
if self.awaiting_confirmation:
self.draw_confirmation_box(screen)
pygame.display.flip()
def draw_confirmation_box(self, screen):
box_rect = pygame.Rect(SCREEN_WIDTH // 2 - 150, SCREEN_HEIGHT // 2 - 320, 300, 160)
pygame.draw.rect(screen, (250, 250, 250), box_rect)
pygame.draw.rect(screen, (0, 0, 0), box_rect, 3)
text = self.font.render("Is that correct?", True, (0, 0, 0))
screen.blit(text, (box_rect.centerx - text.get_width() // 2, box_rect.y + 20))
yes_rect = pygame.Rect(box_rect.x + 50, box_rect.y + 90, 80, 40)
no_rect = pygame.Rect(box_rect.x + 170, box_rect.y + 90, 80, 40)
pygame.draw.rect(screen, (0, 200, 0), yes_rect)
pygame.draw.rect(screen, (200, 0, 0), no_rect)
yes_text = self.font.render("Yes", True, (255, 255, 255))
no_text = self.font.render("No", True, (255, 255, 255))
screen.blit(yes_text, (yes_rect.centerx - yes_text.get_width() // 2, yes_rect.centery - yes_text.get_height() // 2))
screen.blit(no_text, (no_rect.centerx - no_text.get_width() // 2, no_rect.centery - no_text.get_height() // 2))
self.yes_rect, self.no_rect = yes_rect, no_rect
# -------------------------------
# Handling Clicks
# -------------------------------
def handle_click(self, pos):
if self.state in ["continent", "americas", "difficulty", "pairs"]:
for rect, option in self.buttons:
if rect.collidepoint(pos):
if self.state == "continent":
if option == "Americas":
self.state = "americas"
elif option == "All Continents":
self.selected_continents = ["Europe", "Asia", "Africa", "Oceania", "North-America", "South-America"]
self.state = "difficulty"
else:
self.selected_continents = [option]
self.state = "difficulty"
elif self.state == "americas":
if option == "North-America":
self.selected_continents = ["North-America"]
elif option == "South-America":
self.selected_continents = ["South-America"]
elif option == "Americas":
self.selected_continents = ["North-America", "South-America"]
self.state = "difficulty"
elif self.state == "difficulty":
self.level = option
self.state = "pairs"
elif self.state == "pairs":
self.pair_count = int(option)
self.prepare_deck()
self.setup_game()
self.state = "game"
return
elif self.state == "game":
if self.awaiting_confirmation:
if self.yes_rect.collidepoint(pos):
self.confirmation_result = "yes"
elif self.no_rect.collidepoint(pos):
self.confirmation_result = "no"
return
for i, rect in enumerate(self.card_rects):
if rect.collidepoint(pos) and not self.revealed[i] and not self.matched[i]:
self.revealed[i] = True
self.selected.append(i)
return
# -------------------------------
# Game Logic
# -------------------------------
def check_selected(self):
if self.state != "game":
return
if len(self.selected) == 2 and not self.awaiting_confirmation:
a, b = self.selected
is_match = self.pair_map.get(self.cards[a]) == self.cards[b]
self.correct_answer_expected = "yes" if is_match else "no"
self.awaiting_confirmation = True
elif self.awaiting_confirmation and self.confirmation_result:
a, b = self.selected
expected = self.correct_answer_expected
player_correct = self.confirmation_result == expected
if player_correct:
if expected == "yes":
self.matched[a] = self.matched[b] = True
self.scores[self.current_player] += 1
self.found_pairs += 1
else:
self.revealed[a] = self.revealed[b] = False
else:
self.scores[self.current_player] -= 1
self.revealed[a] = self.revealed[b] = False
self.awaiting_confirmation = False
self.confirmation_result = None
self.selected = []
self.current_player = 1 - self.current_player
# -------------------------------
# Main Loop
# -------------------------------
def run(self, gesture_queue=None):
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("CountryCapital Memory Game")
clock = pygame.time.Clock()
self.font = pygame.font.SysFont(None, 32)
while self.running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
self.handle_click(event.pos)
# 🔹 Gestenereignisse prüfen
if gesture_queue and not self.gesture_queue.empty():
event_type, data = self.gesture_queue.get()
if event_type == "touch":
self.handle_click(data)
elif event_type == "clap" and self.awaiting_confirmation:
self.confirmation_result = "yes"
if self.state == "continent":
self.draw_menu(screen, "Select Continent", ["Europe", "Americas", "Asia", "Africa", "Oceania", "All Continents"])
elif self.state == "americas":
self.draw_menu(screen, "Select Region", ["North-America", "South-America", "Americas"])
elif self.state == "difficulty":
self.draw_menu(screen, "Select Difficulty", ["Easy", "Normal", "Hard"])
elif self.state == "pairs":
self.draw_menu(screen, "Select Number of Pairs", ["4", "6", "8", "10", "12"])
elif self.state == "game":
self.draw_game(screen)
self.check_selected()
if self.found_pairs == self.total_pairs:
self.display_winner(screen)
pygame.display.flip()
pygame.time.wait(4000)
self.running = False
clock.tick(FPS)
pygame.quit()
sys.exit()
# -------------------------------
# Winner Display
# -------------------------------
def display_winner(self, screen):
if self.scores[0] > self.scores[1]:
text = "🏆 Player 1 Wins!"
elif self.scores[1] > self.scores[0]:
text = "🏆 Player 2 Wins!"
else:
text = "🤝 Draw!"
win_text = self.font.render(text, True, (255, 255, 0))
rect = win_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
screen.blit(win_text, rect)
# -------------------------------
# Run the Game
# -------------------------------
if __name__ == "__main__":
gesture_queue = Queue()
gesture_proc = Process(target=run_gesture_input, args=(gesture_queue,))
gesture_proc.start()
game = MemoryGame(gesture_queue)
try:
game.run(gesture_queue)
finally:
gesture_proc.terminate()

32
calibrate_touch.py Normal file
View File

@ -0,0 +1,32 @@
import cv2, mediapipe as mp, json
mp_hands=mp.solutions.hands; mp_draw=mp.solutions.drawing_utils
hands=mp_hands.Hands(max_num_hands=1,min_detection_confidence=0.6)
cap=cv2.VideoCapture(0)
points=[]; labels=["oben-links","oben-rechts","unten-rechts","unten-links"]
print("📏 Zeige nacheinander jede Ecke (Finger runter zum Bestätigen)")
while True:
ok,frame=cap.read()
if not ok: break
frame=cv2.flip(frame,1)
rgb=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
res=hands.process(rgb); h,w,_=frame.shape
if res.multi_hand_landmarks:
lm=res.multi_hand_landmarks[0]
mp_draw.draw_landmarks(frame,lm,mp_hands.HAND_CONNECTIONS)
x=int(lm.landmark[8].x*w); y=int(lm.landmark[8].y*h)
cv2.circle(frame,(x,y),10,(0,255,255),-1)
if len(points)<4:
cv2.putText(frame,f"Zeige {labels[len(points)]}",(40,40),
cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,0),2)
if lm.landmark[8].y>0.8:
print(f"{labels[len(points)]} gespeichert")
points.append((x,y)); cv2.waitKey(1000)
if len(points)==4:
json.dump(points,open("calibration.json","w"))
print("📄 calibration.json gespeichert ✅"); break
cv2.imshow("Kalibrierung",frame)
if cv2.waitKey(5)&0xFF==27: break
cap.release(); cv2.destroyAllWindows()

1
calibration.json Normal file
View File

@ -0,0 +1 @@
[[112, 403], [184, 463], [169, 447], [171, 448]]

89
gesture_input.py Normal file
View File

@ -0,0 +1,89 @@
import cv2, mediapipe as mp, json, time, math, numpy as np
from multiprocessing import Queue
# --------------- Hilfsfunktion: Projektionsfläche auf Bildschirm mappen ---------------
def map_to_screen(x, y, calib_points, screen_size=(800, 600)):
pts_src = np.array(calib_points, dtype=np.float32)
pts_dst = np.array([[0,0],[screen_size[0],0],
[screen_size[0],screen_size[1]],[0,screen_size[1]]],
dtype=np.float32)
M = cv2.getPerspectiveTransform(pts_src, pts_dst)
p = np.array([[[x,y]]], dtype=np.float32)
mapped = cv2.perspectiveTransform(p, M)[0][0]
return int(mapped[0]), int(mapped[1])
# --------------- Hauptfunktion ---------------
def run_gesture_input(queue: Queue,
touch_cam_index=0,
gesture_cam_index=1,
screen_size=(800,600)):
mp_hands = mp.solutions.hands
mp_draw = mp.solutions.drawing_utils
hands_touch = mp_hands.Hands(max_num_hands=1, min_detection_confidence=0.6)
hands_gesture = mp_hands.Hands(max_num_hands=2, min_detection_confidence=0.6)
# Kalibrierung laden
try:
calib_points = json.load(open("calibration.json"))
print("📄 Kalibrierung geladen:", calib_points)
except:
print("⚠️ Keine calibration.json zuerst calibrate_touch.py ausführen!")
return
cam_touch = cv2.VideoCapture(touch_cam_index)
cam_gesture = cv2.VideoCapture(gesture_cam_index)
prev_clap_time = 0
clap_cooldown = 1.5
while True:
ok1, frame_touch = cam_touch.read()
ok2, frame_gest = cam_gesture.read()
if not ok1 or not ok2:
print("⚠️ Kamera nicht verfügbar"); break
frame_touch = cv2.flip(frame_touch, 1)
frame_gest = cv2.flip(frame_gest, 1)
# ---------- Touch erkennen ----------
rgb_t = cv2.cvtColor(frame_touch, cv2.COLOR_BGR2RGB)
res_t = hands_touch.process(rgb_t)
h, w, _ = frame_touch.shape
if res_t.multi_hand_landmarks:
lm = res_t.multi_hand_landmarks[0]
mp_draw.draw_landmarks(frame_touch, lm, mp_hands.HAND_CONNECTIONS)
fx = int(lm.landmark[8].x * w)
fy = int(lm.landmark[8].y * h)
sx, sy = map_to_screen(fx, fy, calib_points, screen_size)
if lm.landmark[8].y > 0.8:
queue.put(("touch",(sx,sy)))
cv2.putText(frame_touch,f"Touch ({sx},{sy})",(40,60),
cv2.FONT_HERSHEY_SIMPLEX,0.8,(0,255,0),2)
# ---------- Klatschen / Bewegung ----------
rgb_g = cv2.cvtColor(frame_gest, cv2.COLOR_BGR2RGB)
res_g = hands_gesture.process(rgb_g)
gh, gw, _ = frame_gest.shape
if res_g.multi_hand_landmarks and len(res_g.multi_hand_landmarks)==2:
h1, h2 = res_g.multi_hand_landmarks
x1 = np.mean([p.x for p in h1.landmark])*gw
y1 = np.mean([p.y for p in h1.landmark])*gh
x2 = np.mean([p.x for p in h2.landmark])*gw
y2 = np.mean([p.y for p in h2.landmark])*gh
dist = math.hypot(x2-x1, y2-y1)
if dist < 100 and (time.time()-prev_clap_time)>clap_cooldown:
queue.put(("clap",None))
prev_clap_time=time.time()
cv2.putText(frame_gest,"👏",(int(gw/2)-20,80),
cv2.FONT_HERSHEY_SIMPLEX,2,(0,255,255),3)
# Anzeigen
cv2.imshow("Touch-Cam", frame_touch)
cv2.imshow("Gesture-Cam", frame_gest)
if cv2.waitKey(5)&0xFF==27:
break
cam_touch.release()
cam_gesture.release()
cv2.destroyAllWindows()