76 lines
2.8 KiB
Python
76 lines
2.8 KiB
Python
import cv2
|
|
import mediapipe as mp
|
|
import json
|
|
import time
|
|
|
|
# === Grundeinstellungen ===
|
|
CAM_INDEX = 0 # Wähle die Kamera für den Tisch (z. B. Laptop-Kamera oder USB-Kamera)
|
|
NUM_POINTS = 4 # Anzahl der Kalibrierpunkte (vier Ecken)
|
|
OUTPUT_FILE = "calibration.json"
|
|
|
|
mp_hands = mp.solutions.hands
|
|
mp_draw = mp.solutions.drawing_utils
|
|
|
|
def main():
|
|
cap = cv2.VideoCapture(CAM_INDEX)
|
|
hands = mp_hands.Hands(max_num_hands=1, min_detection_confidence=0.6)
|
|
points = []
|
|
print("📸 Starte Kalibrierung...")
|
|
print("➡️ Zeige mit dem Zeigefinger nacheinander auf die 4 Ecken der Projektion.")
|
|
print(" - Oben links, oben rechts, unten rechts, unten links")
|
|
print(" - Halte den Finger kurz still, bis Punkt erkannt wird.")
|
|
|
|
while True:
|
|
ok, frame = cap.read()
|
|
if not ok:
|
|
print("⚠️ Kamera nicht verfügbar.")
|
|
break
|
|
|
|
frame = cv2.flip(frame, 1)
|
|
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
results = hands.process(rgb)
|
|
h, w, _ = frame.shape
|
|
|
|
if results.multi_hand_landmarks:
|
|
lm = results.multi_hand_landmarks[0]
|
|
mp_draw.draw_landmarks(frame, lm, mp_hands.HAND_CONNECTIONS)
|
|
|
|
# Zeigefingerspitze (Index 8)
|
|
fx, fy = int(lm.landmark[8].x * w), int(lm.landmark[8].y * h)
|
|
cv2.circle(frame, (fx, fy), 12, (0, 255, 0), -1)
|
|
cv2.putText(frame, "Finger", (fx + 10, fy - 10),
|
|
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
|
|
|
# Wenn Finger nahe der Fläche bleibt, erfasse Punkt
|
|
if len(points) < NUM_POINTS:
|
|
if lm.landmark[8].y > 0.8: # Finger tief genug
|
|
print(f"✅ Punkt {len(points)+1} erfasst: ({fx},{fy})")
|
|
points.append((fx, fy))
|
|
time.sleep(1.5) # kurz warten, um Doppel-Erfassung zu vermeiden
|
|
|
|
# Punkte visualisieren
|
|
for i, (x, y) in enumerate(points):
|
|
cv2.circle(frame, (x, y), 10, (255, 255, 0), -1)
|
|
cv2.putText(frame, f"P{i+1}", (x + 10, y - 10),
|
|
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 0), 2)
|
|
|
|
cv2.putText(frame, f"Punkte: {len(points)}/{NUM_POINTS}",
|
|
(20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
|
|
|
|
cv2.imshow("Calibration", frame)
|
|
if cv2.waitKey(5) & 0xFF == 27: # ESC drücken zum Abbrechen
|
|
break
|
|
|
|
# Wenn alle Punkte gesetzt wurden → speichern
|
|
if len(points) == NUM_POINTS:
|
|
with open(OUTPUT_FILE, "w") as f:
|
|
json.dump(points, f)
|
|
print(f"💾 Kalibrierung gespeichert in {OUTPUT_FILE}: {points}")
|
|
break
|
|
|
|
cap.release()
|
|
cv2.destroyAllWindows()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|