| from fastapi import FastAPI, UploadFile, File, Form
|
| from PIL import Image
|
| import mediapipe as mp
|
| import numpy as np
|
| import io
|
|
|
| from mediapipe.tasks import python
|
| from mediapipe.tasks.python import vision
|
|
|
| app = FastAPI()
|
|
|
| MODEL_PATH = "gesture_recognizer.task"
|
|
|
| MAP = {
|
| "Thumb_Up": "YES",
|
| "Thumb_Down": "NO",
|
| "Open_Palm": "HELLO",
|
| "Closed_Fist": "STOP",
|
| "Victory": "GOOD JOB",
|
| "Pointing_Up": "LOOK",
|
| "ILoveYou": "LOVE",
|
| }
|
|
|
|
|
| base_options = python.BaseOptions(
|
| model_asset_path=MODEL_PATH
|
| )
|
|
|
| options = vision.GestureRecognizerOptions(
|
| base_options=base_options
|
| )
|
|
|
| recognizer = vision.GestureRecognizer.create_from_options(options)
|
|
|
|
|
| @app.get("/")
|
| def home():
|
| return {
|
| "message": "Realtime Gesture API Running"
|
| }
|
|
|
|
|
| @app.post("/detect")
|
| async def detect(
|
| file: UploadFile = File(...),
|
| target_sign: str = Form(...)
|
| ):
|
|
|
| contents = await file.read()
|
|
|
| image = Image.open(
|
| io.BytesIO(contents)
|
| ).convert("RGB")
|
|
|
| image_np = np.array(image)
|
|
|
| mp_image = mp.Image(
|
| image_format=mp.ImageFormat.SRGB,
|
| data=image_np
|
| )
|
|
|
| result = recognizer.recognize(mp_image)
|
|
|
| detected_sign = "UNKNOWN"
|
| confidence = 0.0
|
|
|
| if result.gestures:
|
| top = result.gestures[0][0]
|
|
|
| raw_name = top.category_name
|
| confidence = float(top.score)
|
|
|
| detected_sign = MAP.get(raw_name, raw_name)
|
|
|
| status = (
|
| "CORRECT"
|
| if detected_sign.upper() == target_sign.upper()
|
| else "WRONG"
|
| )
|
|
|
| return {
|
| "target_sign": target_sign,
|
| "detected_sign": detected_sign,
|
| "confidence": round(confidence, 2),
|
| "status": status
|
| } |