TIF_E4253362/backend/realtime/realtime_angka.py

96 lines
2.8 KiB
Python

"""
Realtime Detection untuk Angka dengan visualisasi MediaPipe landmarks.
"""
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 = "angka", camera_id: int = 0):
model_path = MODEL_DIR / "angka" / "model_angka.pt"
encoder_path = MODEL_DIR / "angka" / "label_encoder.json"
if not model_path.exists():
print(f"Error: Model tidak ditemukan di {model_path}")
return
recognizer = GestureRecognizer(
model_path,
encoder_path,
window_size=WINDOW_SIZE,
draw_landmarks=True
)
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, 'r' untuk reset, 'l' toggle landmarks")
print("="*60 + "\n")
show_landmarks = True
try:
while True:
ret, frame = cap.read()
if not ret:
break
if show_landmarks:
frame = recognizer.draw_landmarks_on_frame(frame)
label, confidence = recognizer.process_frame(frame)
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)
buffer_len = len(recognizer.buffer)
cv2.putText(frame, f"Buffer: {buffer_len}/{recognizer.window_size}",
(10, 100), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
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)
if label is None:
cv2.putText(frame, "Tunjukkan gestur isyarat...", (10, 160),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.imshow(f"Realtime - {category}", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('r'):
recognizer.reset()
elif key == ord('l'):
show_landmarks = not show_landmarks
finally:
cap.release()
cv2.destroyAllWindows()
recognizer.close()
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--camera", type=int, default=0)
args = parser.parse_args()
run_realtime("angka", args.camera)
if __name__ == "__main__":
main()