Khalida1w commited on
Commit
cde5d08
1 Parent(s): 28476c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -12
app.py CHANGED
@@ -7,6 +7,15 @@ from ultralytics import YOLO
7
  # Initialize YOLO model
8
  model = YOLO('./best.pt')
9
 
 
 
 
 
 
 
 
 
 
10
  def check_ffmpeg():
11
  """Check if ffmpeg is installed."""
12
  if shutil.which("ffmpeg") is None:
@@ -46,25 +55,28 @@ def process_video(video_path, output_option):
46
  if not matched_mp4_files:
47
  raise Exception(f"No .mp4 video files found in directory {output_dir} after conversion for {video_name}.")
48
 
49
- # Initialize object count
50
- object_count = 0
51
 
52
- # Calculate object count based on results
53
  for result in results:
54
  if hasattr(result, 'boxes'):
55
- object_count += len(result.boxes)
 
 
 
 
56
 
57
  # Set default outputs
58
  video_output = matched_mp4_files[0] if matched_mp4_files else None
59
- count_output = object_count
60
 
61
  # Determine the output based on user's choice
62
  if output_option == "Count":
63
- return count_output, None
64
  elif output_option == "Video":
65
  return None, video_output
66
  elif output_option == "Both":
67
- return count_output, video_output
68
 
69
  # Define Gradio inputs
70
  video_input = gr.Video()
@@ -78,14 +90,14 @@ examples = [
78
  video_interface = gr.Interface(
79
  fn=process_video,
80
  inputs=[video_input, output_option_input],
81
- outputs=[gr.Textbox(label="Object Count"), "video"], # Ensure two outputs are defined
82
- title="YOLO Video Tracking Application",
83
- description="A simple application to track objects in a video using YOLO model. Upload your own video, or click one of the examples to load them.",
84
  article="""<div>
85
- <p style="text-align: center">Upload a video file and select the type of output you want: object count, processed video, or both. Then, hit submit to process the video.</p>
86
  </div>""",
87
  examples=examples,
88
- cache_examples=True # Disable caching to speed up launch
89
  )
90
 
91
  # Deploy the interface with share enabled
 
7
  # Initialize YOLO model
8
  model = YOLO('./best.pt')
9
 
10
+ # Class to color mapping
11
+ ClassToColorMapping = {
12
+ 0: "black",
13
+ 1: "gray",
14
+ 2: "green",
15
+ 3: "purple",
16
+ 4: "red"
17
+ }
18
+
19
  def check_ffmpeg():
20
  """Check if ffmpeg is installed."""
21
  if shutil.which("ffmpeg") is None:
 
55
  if not matched_mp4_files:
56
  raise Exception(f"No .mp4 video files found in directory {output_dir} after conversion for {video_name}.")
57
 
58
+ # Initialize object count dictionary based on colors
59
+ color_counts = {color: 0 for color in ClassToColorMapping.values()}
60
 
61
+ # Calculate object count based on results and map to colors
62
  for result in results:
63
  if hasattr(result, 'boxes'):
64
+ for box in result.boxes:
65
+ class_id = int(box.cls) # Assuming `cls` is the class index
66
+ color = ClassToColorMapping.get(class_id, "unknown")
67
+ if color in color_counts:
68
+ color_counts[color] += 1
69
 
70
  # Set default outputs
71
  video_output = matched_mp4_files[0] if matched_mp4_files else None
 
72
 
73
  # Determine the output based on user's choice
74
  if output_option == "Count":
75
+ return color_counts, None
76
  elif output_option == "Video":
77
  return None, video_output
78
  elif output_option == "Both":
79
+ return color_counts, video_output
80
 
81
  # Define Gradio inputs
82
  video_input = gr.Video()
 
90
  video_interface = gr.Interface(
91
  fn=process_video,
92
  inputs=[video_input, output_option_input],
93
+ outputs=[gr.JSON(label="Color Counts"), "video"], # Ensure two outputs are defined, using JSON for color counts
94
+ title="YOLO Video Tracking Application with Color Counting",
95
+ description="A simple application to track objects in a video using YOLO model and count objects by color. Upload your own video, or click one of the examples to load them.",
96
  article="""<div>
97
+ <p style="text-align: center">Upload a video file and select the type of output you want: object color counts, processed video, or both. Then, hit submit to process the video.</p>
98
  </div>""",
99
  examples=examples,
100
+ cache_examples=False # Disable caching to speed up launch
101
  )
102
 
103
  # Deploy the interface with share enabled