hb-setosys commited on
Commit
c3cdf4b
·
verified ·
1 Parent(s): 6ea78d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -8,16 +8,17 @@ from ultralytics import YOLO
8
  MODEL_PATH = "yolov12x.pt" # Ensure the model is uploaded to the Hugging Face Space
9
  model = YOLO(MODEL_PATH)
10
 
11
- # COCO dataset class ID for trucks
12
- TRUCK_CLASS_ID = 7 # "truck"
 
13
 
14
- def count_trucks(video_path):
15
  cap = cv2.VideoCapture(video_path)
16
  if not cap.isOpened():
17
  return "Error: Unable to open video file."
18
 
19
  frame_count = 0
20
- truck_counts = []
21
  frame_skip = 5 # Process every 5th frame for efficiency
22
 
23
  while True:
@@ -32,38 +33,42 @@ def count_trucks(video_path):
32
  # Run YOLOv12x inference
33
  results = model(frame, verbose=False)
34
 
35
- truck_count = 0
36
  for result in results:
37
  for box in result.boxes:
38
  class_id = int(box.cls.item()) # Get class ID
39
  confidence = float(box.conf.item()) # Get confidence score
40
 
41
- # Count only trucks
42
- if class_id == TRUCK_CLASS_ID and confidence > 0.5:
 
 
43
  truck_count += 1
44
 
45
- truck_counts.append(truck_count)
 
46
 
47
  cap.release()
48
 
49
  return {
50
- "Trucks in a Frame": int(np.max(truck_counts)) if truck_counts else 0
 
51
  }
52
 
53
  # Gradio UI function
54
  def analyze_video(video_file):
55
- result = count_trucks(video_file)
56
  return "\n".join([f"{key}: {value}" for key, value in result.items()])
57
 
58
  # Define Gradio interface
59
  iface = gr.Interface(
60
  fn=analyze_video,
61
  inputs=gr.Video(label="Upload Video"),
62
- outputs=gr.Textbox(label="Truck Count"),
63
- title="YOLOv12x Truck Counter",
64
- description="Upload a video to count trucks using YOLOv12x."
65
  )
66
 
67
  # Launch the Gradio app
68
  if __name__ == "__main__":
69
- iface.launch()
 
8
  MODEL_PATH = "yolov12x.pt" # Ensure the model is uploaded to the Hugging Face Space
9
  model = YOLO(MODEL_PATH)
10
 
11
+ # COCO dataset class IDs
12
+ PERSON_CLASS_ID = 0 # "person"
13
+ TRUCK_CLASS_ID = 7 # "truck"
14
 
15
+ def count_objects(video_path):
16
  cap = cv2.VideoCapture(video_path)
17
  if not cap.isOpened():
18
  return "Error: Unable to open video file."
19
 
20
  frame_count = 0
21
+ object_counts = {"people": [], "trucks": []}
22
  frame_skip = 5 # Process every 5th frame for efficiency
23
 
24
  while True:
 
33
  # Run YOLOv12x inference
34
  results = model(frame, verbose=False)
35
 
36
+ people_count, truck_count = 0, 0
37
  for result in results:
38
  for box in result.boxes:
39
  class_id = int(box.cls.item()) # Get class ID
40
  confidence = float(box.conf.item()) # Get confidence score
41
 
42
+ # Count objects based on their class IDs
43
+ if class_id == PERSON_CLASS_ID and confidence > 0.5:
44
+ people_count += 1
45
+ elif class_id == TRUCK_CLASS_ID and confidence > 0.5:
46
  truck_count += 1
47
 
48
+ object_counts["people"].append(people_count)
49
+ object_counts["trucks"].append(truck_count)
50
 
51
  cap.release()
52
 
53
  return {
54
+ "Max People in a Frame": int(np.max(object_counts["people"])) if object_counts["people"] else 0,
55
+ "Max Trucks in a Frame": int(np.max(object_counts["trucks"])) if object_counts["trucks"] else 0
56
  }
57
 
58
  # Gradio UI function
59
  def analyze_video(video_file):
60
+ result = count_objects(video_file)
61
  return "\n".join([f"{key}: {value}" for key, value in result.items()])
62
 
63
  # Define Gradio interface
64
  iface = gr.Interface(
65
  fn=analyze_video,
66
  inputs=gr.Video(label="Upload Video"),
67
+ outputs=gr.Textbox(label="Analysis Result"),
68
+ title="YOLOv12x Object Counter",
69
+ description="Upload a video to count people and trucks using YOLOv12x."
70
  )
71
 
72
  # Launch the Gradio app
73
  if __name__ == "__main__":
74
+ iface.launch()