133 lines
3.7 KiB
Python
133 lines
3.7 KiB
Python
"""
|
|
Realtime Detection untuk Kata dengan visualisasi MediaPipe landmarks.
|
|
|
|
Proses sesuai README:
|
|
1. Webcam
|
|
2. MediaPipe
|
|
3. Landmark
|
|
4. Normalisasi
|
|
5. Window 30 Frame
|
|
6. Model TCN
|
|
7. Prediksi
|
|
8. Tampilkan hasil
|
|
"""
|
|
|
|
import sys
|
|
import cv2
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from utils.inference import GestureRecognizer
|
|
from utils.config import MODEL_DIR, WINDOW_SIZE
|
|
|
|
|
|
def run_realtime(category: str = "kata", camera_id: int = 0):
|
|
"""
|
|
Run real-time recognition dengan visualisasi landmarks.
|
|
|
|
Args:
|
|
category: Nama kategori
|
|
camera_id: Camera index
|
|
"""
|
|
model_path = MODEL_DIR / "kata" / "model_kata.pt"
|
|
encoder_path = MODEL_DIR / "kata" / "label_encoder.json"
|
|
|
|
if not model_path.exists():
|
|
print(f"Error: Model tidak ditemukan di {model_path}")
|
|
print("Jalankan training terlebih dahulu!")
|
|
return
|
|
|
|
# Initialize recognizer
|
|
recognizer = GestureRecognizer(
|
|
model_path,
|
|
encoder_path,
|
|
window_size=WINDOW_SIZE,
|
|
draw_landmarks=True
|
|
)
|
|
|
|
# Open camera
|
|
cap = cv2.VideoCapture(camera_id)
|
|
|
|
if not cap.isOpened():
|
|
print(f"Error: Tidak dapat membuka camera {camera_id}")
|
|
return
|
|
|
|
print("\n" + "="*60)
|
|
print(f"REALTIME RECOGNITION - {category.upper()}")
|
|
print("="*60)
|
|
print("Tekan 'q' untuk quit")
|
|
print("Tekan 'r' untuk reset buffer")
|
|
print("Tekan 'l' untuk toggle landmark visibility")
|
|
print("="*60 + "\n")
|
|
|
|
show_landmarks = True
|
|
|
|
try:
|
|
while True:
|
|
ret, frame = cap.read()
|
|
|
|
if not ret:
|
|
print("Error: Tidak dapat membaca frame")
|
|
break
|
|
|
|
# Draw landmarks
|
|
if show_landmarks:
|
|
frame = recognizer.draw_landmarks_on_frame(frame)
|
|
|
|
# Process frame
|
|
label, confidence = recognizer.process_frame(frame)
|
|
|
|
# Draw prediction
|
|
if label is not None:
|
|
text = f"{label} ({confidence*100:.1f}%)"
|
|
cv2.putText(frame, text, (10, 50),
|
|
cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0), 3)
|
|
|
|
# Draw buffer status
|
|
buffer_len = len(recognizer.buffer)
|
|
status = f"Buffer: {buffer_len}/{recognizer.window_size}"
|
|
cv2.putText(frame, status, (10, 100),
|
|
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
|
|
|
|
# Draw landmark status
|
|
landmark_status = "Landmarks: ON" if show_landmarks else "Landmarks: OFF"
|
|
cv2.putText(frame, landmark_status, (10, 130),
|
|
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 255), 2)
|
|
|
|
# Draw detection status
|
|
if label is None:
|
|
cv2.putText(frame, "Tunjukkan gestur isyarat...", (10, 160),
|
|
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
|
|
|
|
# Show frame
|
|
cv2.imshow(f"Realtime Recognition - {category}", frame)
|
|
|
|
# Check key
|
|
key = cv2.waitKey(1) & 0xFF
|
|
if key == ord('q'):
|
|
break
|
|
elif key == ord('r'):
|
|
recognizer.reset()
|
|
print("Buffer reset")
|
|
elif key == ord('l'):
|
|
show_landmarks = not show_landmarks
|
|
print(f"Landmarks: {'ON' if show_landmarks else 'OFF'}")
|
|
|
|
finally:
|
|
cap.release()
|
|
cv2.destroyAllWindows()
|
|
recognizer.close()
|
|
|
|
|
|
def main():
|
|
import argparse
|
|
parser = argparse.ArgumentParser(description="Realtime recognition kata")
|
|
parser.add_argument("--camera", type=int, default=0, help="Camera index")
|
|
args = parser.parse_args()
|
|
run_realtime("kata", args.camera)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|