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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -25
app.py CHANGED
@@ -8,16 +8,12 @@ import insightface
8
  # Load Models (CPU mode)
9
  # ----------------------------
10
 
11
- yolo = YOLO("yolov8n.pt") # lightweight model
12
 
13
  face_model = insightface.app.FaceAnalysis(name="buffalo_l")
14
- face_model.prepare(ctx_id=-1) # -1 forces CPU (important for HF free tier)
15
 
16
 
17
- # ----------------------------
18
- # Utility: Normalize embedding
19
- # ----------------------------
20
-
21
  def normalize(vec):
22
  vec = np.array(vec, dtype=np.float32)
23
  norm = np.linalg.norm(vec)
@@ -26,33 +22,23 @@ def normalize(vec):
26
  return (vec / norm).tolist()
27
 
28
 
29
- # ----------------------------
30
- # Main Processing Function
31
- # ----------------------------
32
-
33
  def process_image(image):
34
  image_np = np.array(image)
35
-
36
  results = yolo(image_np)
37
-
38
  faces_output = []
39
 
40
  for r in results:
41
  boxes = r.boxes
42
 
43
  for box, cls, conf in zip(boxes.xyxy, boxes.cls, boxes.conf):
44
-
45
- # YOLO class 0 = person
46
  if int(cls) != 0:
47
  continue
48
-
49
  if float(conf) < 0.4:
50
  continue
51
 
52
  xmin, ymin, xmax, ymax = box.cpu().numpy()
53
  xmin, ymin, xmax, ymax = map(int, [xmin, ymin, xmax, ymax])
54
 
55
- # Safety check for valid crop
56
  h, w, _ = image_np.shape
57
  xmin = max(0, xmin)
58
  ymin = max(0, ymin)
@@ -60,17 +46,13 @@ def process_image(image):
60
  ymax = min(h, ymax)
61
 
62
  person_crop = image_np[ymin:ymax, xmin:xmax]
63
-
64
  if person_crop.size == 0:
65
  continue
66
 
67
- # Detect face inside person crop
68
  detected_faces = face_model.get(person_crop)
69
 
70
  for face in detected_faces:
71
  embedding = normalize(face.embedding)
72
-
73
- # Adjust face bbox to original image coordinates
74
  fxmin, fymin, fxmax, fymax = face.bbox.astype(int)
75
 
76
  faces_output.append({
@@ -89,14 +71,18 @@ def process_image(image):
89
  return faces_output
90
 
91
 
92
- # ----------------------------
93
- # Gradio Interface
94
- # ----------------------------
95
-
96
  iface = gr.Interface(
97
  fn=process_image,
98
  inputs=gr.Image(type="pil"),
99
  outputs="json"
100
  )
101
 
102
- iface.launch()
 
 
 
 
 
 
 
 
 
8
  # Load Models (CPU mode)
9
  # ----------------------------
10
 
11
+ yolo = YOLO("yolov8n.pt")
12
 
13
  face_model = insightface.app.FaceAnalysis(name="buffalo_l")
14
+ face_model.prepare(ctx_id=-1)
15
 
16
 
 
 
 
 
17
  def normalize(vec):
18
  vec = np.array(vec, dtype=np.float32)
19
  norm = np.linalg.norm(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
 
36
  if float(conf) < 0.4:
37
  continue
38
 
39
  xmin, ymin, xmax, ymax = box.cpu().numpy()
40
  xmin, ymin, xmax, ymax = map(int, [xmin, ymin, xmax, ymax])
41
 
 
42
  h, w, _ = image_np.shape
43
  xmin = max(0, xmin)
44
  ymin = max(0, ymin)
 
46
  ymax = min(h, ymax)
47
 
48
  person_crop = image_np[ymin:ymax, xmin:xmax]
 
49
  if person_crop.size == 0:
50
  continue
51
 
 
52
  detected_faces = face_model.get(person_crop)
53
 
54
  for face in detected_faces:
55
  embedding = normalize(face.embedding)
 
 
56
  fxmin, fymin, fxmax, fymax = face.bbox.astype(int)
57
 
58
  faces_output.append({
 
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
+ )