Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +9 -0
- main.py +50 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY . .
|
| 6 |
+
|
| 7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
+
|
| 9 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
import mediapipe as mp
|
| 6 |
+
from mediapipe.tasks import python
|
| 7 |
+
from mediapipe.tasks.python import vision
|
| 8 |
+
import tempfile
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# ===== Load Model =====
|
| 13 |
+
MODEL_PATH = "gesture_recognizer.task"
|
| 14 |
+
|
| 15 |
+
base_options = python.BaseOptions(model_asset_path=MODEL_PATH)
|
| 16 |
+
gesture_options = vision.GestureRecognizerOptions(base_options=base_options)
|
| 17 |
+
gesture_recognizer = vision.GestureRecognizer.create_from_options(gesture_options)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@app.get("/")
|
| 21 |
+
def home():
|
| 22 |
+
return {"message": "Gesture + Face API is running"}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# =========================
|
| 26 |
+
# GESTURE DETECTION API
|
| 27 |
+
# =========================
|
| 28 |
+
@app.post("/gesture")
|
| 29 |
+
async def detect_gesture(file: UploadFile = File(...)):
|
| 30 |
+
try:
|
| 31 |
+
contents = await file.read()
|
| 32 |
+
np_img = np.frombuffer(contents, np.uint8)
|
| 33 |
+
image = cv2.imdecode(np_img, cv2.IMREAD_COLOR)
|
| 34 |
+
|
| 35 |
+
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 36 |
+
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb)
|
| 37 |
+
|
| 38 |
+
result = gesture_recognizer.recognize(mp_image)
|
| 39 |
+
|
| 40 |
+
if result.gestures:
|
| 41 |
+
top = result.gestures[0][0]
|
| 42 |
+
return JSONResponse({
|
| 43 |
+
"gesture": top.category_name,
|
| 44 |
+
"confidence": float(top.score)
|
| 45 |
+
})
|
| 46 |
+
|
| 47 |
+
return JSONResponse({"gesture": "none", "confidence": 0})
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return JSONResponse({"error": str(e)}, status_code=500)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
opencv-python-headless
|
| 4 |
+
mediapipe==0.10.9
|
| 5 |
+
numpy
|
| 6 |
+
python-multipart
|