saketh-005 commited on
Commit
f97d8fd
·
verified ·
1 Parent(s): 6815378

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -18
app.py CHANGED
@@ -1,11 +1,12 @@
1
  import gradio as gr
2
  import numpy as np
3
- import cv2
4
  from ultralytics import YOLO
5
  import insightface
 
 
6
 
7
  # ----------------------------
8
- # Load Models (CPU mode)
9
  # ----------------------------
10
 
11
  yolo = YOLO("yolov8n.pt")
@@ -22,14 +23,12 @@ def normalize(vec):
22
  return (vec / norm).tolist()
23
 
24
 
25
- def process_image(image):
26
- image_np = np.array(image)
27
  results = yolo(image_np)
28
  faces_output = []
29
 
30
  for r in results:
31
  boxes = r.boxes
32
-
33
  for box, cls, conf in zip(boxes.xyxy, boxes.cls, boxes.conf):
34
  if int(cls) != 0:
35
  continue
@@ -71,18 +70,33 @@ def process_image(image):
71
  return faces_output
72
 
73
 
74
- iface = gr.Interface(
75
- fn=process_image,
76
- inputs=gr.Image(type="pil"),
77
- outputs="json"
78
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
- # 🔥 CRITICAL PART
81
- iface.queue(False)
82
 
83
- iface.launch(
84
- server_name="0.0.0.0",
85
- server_port=7860,
86
- ssr_mode=False,
87
- share=False
88
- )
 
1
  import gradio as gr
2
  import numpy as np
 
3
  from ultralytics import YOLO
4
  import insightface
5
+ from fastapi import FastAPI
6
+ from fastapi.responses import JSONResponse
7
 
8
  # ----------------------------
9
+ # Load models (CPU)
10
  # ----------------------------
11
 
12
  yolo = YOLO("yolov8n.pt")
 
23
  return (vec / norm).tolist()
24
 
25
 
26
+ def process_image_np(image_np):
 
27
  results = yolo(image_np)
28
  faces_output = []
29
 
30
  for r in results:
31
  boxes = r.boxes
 
32
  for box, cls, conf in zip(boxes.xyxy, boxes.cls, boxes.conf):
33
  if int(cls) != 0:
34
  continue
 
70
  return faces_output
71
 
72
 
73
+ # ----------------------------------------
74
+ # REAL FASTAPI APP (NO QUEUE)
75
+ # ----------------------------------------
76
+
77
+ app = FastAPI()
78
+
79
+ @app.post("/detect")
80
+ async def detect(file: bytes):
81
+ import cv2
82
+ import numpy as np
83
+
84
+ np_arr = np.frombuffer(file, np.uint8)
85
+ image_np = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
86
+
87
+ if image_np is None:
88
+ return JSONResponse({"error": "Invalid image"}, status_code=400)
89
+
90
+ result = process_image_np(image_np)
91
+ return result
92
+
93
+
94
+ # ----------------------------------------
95
+ # Gradio UI (optional)
96
+ # ----------------------------------------
97
 
98
+ with gr.Blocks() as demo:
99
+ gr.Markdown("Face Processing API Running")
100
 
101
+ demo.queue(False)
102
+ demo.launch(server_name="0.0.0.0", server_port=7860)