notsahil commited on
Commit
304efa6
·
verified ·
1 Parent(s): 7baed24

Return person count

Browse files
Files changed (1) hide show
  1. app.py +10 -12
app.py CHANGED
@@ -1,13 +1,11 @@
1
  import gradio as gr
2
  import PIL.Image as Image
3
-
4
  from ultralytics import ASSETS, YOLO
5
 
6
  model = YOLO("yolo12n.pt")
7
 
8
-
9
  def predict_image(img, conf_threshold, iou_threshold):
10
- """Predicts objects in an image."""
11
  results = model.predict(
12
  source=img,
13
  conf=conf_threshold,
@@ -21,9 +19,10 @@ def predict_image(img, conf_threshold, iou_threshold):
21
  for r in results:
22
  im_array = r.plot()
23
  im = Image.fromarray(im_array[..., ::-1])
 
 
24
 
25
- return im
26
-
27
 
28
  iface = gr.Interface(
29
  fn=predict_image,
@@ -32,13 +31,12 @@ iface = gr.Interface(
32
  gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
33
  gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
34
  ],
35
- outputs=gr.Image(type="pil", label="Result"),
36
- title="Image",
37
- description="Upload images for inference",
38
- # examples=[
39
- # [ASSETS / "bus.jpg", 0.25, 0.45],
40
- # [ASSETS / "zidane.jpg", 0.25, 0.45],
41
- # ],
42
  )
43
 
44
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import PIL.Image as Image
 
3
  from ultralytics import ASSETS, YOLO
4
 
5
  model = YOLO("yolo12n.pt")
6
 
 
7
  def predict_image(img, conf_threshold, iou_threshold):
8
+ """Predicts persons in an image and returns the image with detections and count."""
9
  results = model.predict(
10
  source=img,
11
  conf=conf_threshold,
 
19
  for r in results:
20
  im_array = r.plot()
21
  im = Image.fromarray(im_array[..., ::-1])
22
+
23
+ person_count = len(results[0].boxes) if results[0].boxes is not None else 0
24
 
25
+ return im, f"Number of persons detected: {person_count}"
 
26
 
27
  iface = gr.Interface(
28
  fn=predict_image,
 
31
  gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
32
  gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
33
  ],
34
+ outputs=[
35
+ gr.Image(type="pil", label="Result"),
36
+ gr.Textbox(label="Person Count")
37
+ ],
38
+ title="Image Person Detection",
39
+ description="Upload images to detect persons and get a count",
 
40
  )
41
 
42
  if __name__ == "__main__":