notsahil commited on
Commit
6e46e91
·
verified ·
1 Parent(s): bfb34be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -10
app.py CHANGED
@@ -5,7 +5,7 @@ from ultralytics import ASSETS, YOLO
5
  model = YOLO("yolo12x.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,
@@ -13,16 +13,29 @@ def predict_image(img, conf_threshold, iou_threshold):
13
  show_labels=True,
14
  show_conf=True,
15
  imgsz=640,
16
- classes=[0]
17
  )
18
-
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,
@@ -33,10 +46,10 @@ iface = gr.Interface(
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__":
 
5
  model = YOLO("yolo12x.pt")
6
 
7
  def predict_image(img, conf_threshold, iou_threshold):
8
+ """Predicts persons and cars in an image and returns the image with detections and counts."""
9
  results = model.predict(
10
  source=img,
11
  conf=conf_threshold,
 
13
  show_labels=True,
14
  show_conf=True,
15
  imgsz=640,
16
+ classes=[0, 2] # 0 for person, 2 for car
17
  )
18
+
19
  for r in results:
20
  im_array = r.plot()
21
  im = Image.fromarray(im_array[..., ::-1])
22
+
23
+ # Count persons and cars separately
24
+ person_count = 0
25
+ car_count = 0
26
+
27
+ if results[0].boxes is not None:
28
+ for box in results[0].boxes:
29
+ class_id = int(box.cls[0])
30
+ if class_id == 0: # person
31
+ person_count += 1
32
+ elif class_id == 2: # car
33
+ car_count += 1
34
+
35
+ total_count = person_count + car_count
36
+ count_text = f"Persons: {person_count} | Cars: {car_count} | Total: {total_count}"
37
+
38
+ return im, count_text
39
 
40
  iface = gr.Interface(
41
  fn=predict_image,
 
46
  ],
47
  outputs=[
48
  gr.Image(type="pil", label="Result"),
49
+ gr.Textbox(label="Detection Count")
50
  ],
51
+ title="Person and Car Detection",
52
+ description="Upload images to detect persons and cars with individual counts",
53
  )
54
 
55
  if __name__ == "__main__":