File size: 1,785 Bytes
49152f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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",
}

# Load recognizer once
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
    }