Manar312 commited on
Commit
aac2b7e
·
verified ·
1 Parent(s): 9f38370

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +1 -3
  2. main.py +42 -33
  3. requirements.txt +3 -3
Dockerfile CHANGED
@@ -1,11 +1,9 @@
1
- FROM python:3.10-slim
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, 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)
 
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
- opencv-python-headless
4
- mediapipe==0.10.9
5
  numpy
6
- python-multipart
 
1
  fastapi
2
  uvicorn
3
+ python-multipart
4
+ mediapipe
5
  numpy
6
+ pillow