Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +1 -3
- main.py +42 -33
- requirements.txt +3 -3
Dockerfile
CHANGED
|
@@ -1,11 +1,9 @@
|
|
| 1 |
-
FROM python:3.10
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
COPY requirements.txt .
|
| 6 |
|
| 7 |
-
RUN pip uninstall -y opencv-python opencv-contrib-python || true
|
| 8 |
-
|
| 9 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
|
| 11 |
COPY . .
|
|
|
|
| 1 |
+
FROM python:3.10
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
COPY requirements.txt .
|
| 6 |
|
|
|
|
|
|
|
| 7 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
|
| 9 |
COPY . .
|
main.py
CHANGED
|
@@ -1,50 +1,59 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
| 2 |
-
from
|
| 3 |
-
import cv2
|
| 4 |
import numpy as np
|
| 5 |
import mediapipe as mp
|
| 6 |
-
|
| 7 |
-
from mediapipe.tasks.python import vision
|
| 8 |
-
import tempfile
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
@app.get("/")
|
| 21 |
def home():
|
| 22 |
-
return {"message": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 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 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
return JSONResponse({
|
| 43 |
-
"gesture": top.category_name,
|
| 44 |
-
"confidence": float(top.score)
|
| 45 |
-
})
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
return JSONResponse({"error": str(e)}, status_code=500)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from PIL import Image
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import mediapipe as mp
|
| 5 |
+
import io
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
mp_face = mp.solutions.face_detection
|
| 10 |
+
mp_hands = mp.solutions.hands
|
| 11 |
|
| 12 |
+
face_detection = mp_face.FaceDetection(
|
| 13 |
+
min_detection_confidence=0.5
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
hands_detection = mp_hands.Hands(
|
| 17 |
+
static_image_mode=True,
|
| 18 |
+
max_num_hands=2,
|
| 19 |
+
min_detection_confidence=0.5
|
| 20 |
+
)
|
| 21 |
|
| 22 |
|
| 23 |
@app.get("/")
|
| 24 |
def home():
|
| 25 |
+
return {"message": "Correct Photo API Running"}
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
@app.post("/analyze")
|
| 29 |
+
async def analyze(file: UploadFile = File(...)):
|
| 30 |
+
|
| 31 |
+
contents = await file.read()
|
| 32 |
+
|
| 33 |
+
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 34 |
+
|
| 35 |
+
image_np = np.array(image)
|
| 36 |
|
| 37 |
+
results = {
|
| 38 |
+
"face_detected": False,
|
| 39 |
+
"hands_detected": False,
|
| 40 |
+
"status": "INVALID"
|
| 41 |
+
}
|
| 42 |
|
| 43 |
+
# Face Detection
|
| 44 |
+
face_result = face_detection.process(image_np)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
if face_result.detections:
|
| 47 |
+
results["face_detected"] = True
|
| 48 |
|
| 49 |
+
# Hands Detection
|
| 50 |
+
hands_result = hands_detection.process(image_np)
|
| 51 |
|
| 52 |
+
if hands_result.multi_hand_landmarks:
|
| 53 |
+
results["hands_detected"] = True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# Final Status
|
| 56 |
+
if results["face_detected"] and results["hands_detected"]:
|
| 57 |
+
results["status"] = "CORRECT"
|
| 58 |
|
| 59 |
+
return results
|
|
|
requirements.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
-
|
| 4 |
-
mediapipe
|
| 5 |
numpy
|
| 6 |
-
|
|
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
+
python-multipart
|
| 4 |
+
mediapipe
|
| 5 |
numpy
|
| 6 |
+
pillow
|