prithivMLmods commited on
Commit
063ca5e
1 Parent(s): 8505340

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  import cv2
3
- from ultralytics import YOLO, solutions
4
 
5
  # Initialize the YOLO model
6
  model = YOLO("yolov8s.pt")
@@ -14,14 +14,6 @@ def process_video(video_path, analytics_type):
14
  output_filename = f"{analytics_type}_output.avi"
15
  out = cv2.VideoWriter(output_filename, cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))
16
 
17
- # Set up analytics based on the selected type
18
- analytics = solutions.Analytics(
19
- type=analytics_type,
20
- writer=out,
21
- im0_shape=(w, h),
22
- view_img=False
23
- )
24
-
25
  clswise_count = {}
26
  frame_count = 0
27
 
@@ -41,16 +33,24 @@ def process_video(video_path, analytics_type):
41
  else:
42
  clswise_count[model.names[int(cls)]] = 1
43
 
44
- # Update analytics based on type
45
  if analytics_type == "line":
46
- total_counts = sum(clswise_count.values())
47
- analytics.update_line(frame_count, total_counts)
 
48
  elif analytics_type == "multiple_line":
49
- analytics.update_multiple_lines(clswise_count, list(clswise_count.keys()), frame_count)
 
 
 
50
  elif analytics_type == "pie":
51
- analytics.update_pie(clswise_count)
 
52
  elif analytics_type == "area":
53
- analytics.update_area(frame_count, clswise_count)
 
 
 
54
 
55
  clswise_count = {} # Reset for next frame
56
 
@@ -92,5 +92,5 @@ with gr.Blocks() as demo:
92
  # Define the output when the button is clicked
93
  submit_btn.click(gradio_app, inputs=[video_input, analytics_dropdown], outputs=output_video)
94
 
95
- # Launch the Gradio app
96
- demo.launch()
 
1
  import gradio as gr
2
  import cv2
3
+ from ultralytics import YOLO
4
 
5
  # Initialize the YOLO model
6
  model = YOLO("yolov8s.pt")
 
14
  output_filename = f"{analytics_type}_output.avi"
15
  out = cv2.VideoWriter(output_filename, cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))
16
 
 
 
 
 
 
 
 
 
17
  clswise_count = {}
18
  frame_count = 0
19
 
 
33
  else:
34
  clswise_count[model.names[int(cls)]] = 1
35
 
36
+ # Perform simple analytics based on type
37
  if analytics_type == "line":
38
+ # Display the number of detections on each frame (for example)
39
+ cv2.putText(frame, f"Frame {frame_count}: Detections - {sum(clswise_count.values())}",
40
+ (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
41
  elif analytics_type == "multiple_line":
42
+ # Display classwise counts
43
+ for i, (cls_name, count) in enumerate(clswise_count.items()):
44
+ cv2.putText(frame, f"{cls_name}: {count}",
45
+ (10, 30 + (i + 1) * 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
46
  elif analytics_type == "pie":
47
+ # Placeholder for pie chart (can implement later with matplotlib and overlay on frame)
48
+ pass
49
  elif analytics_type == "area":
50
+ # Placeholder for area graph (implement as needed)
51
+ pass
52
+
53
+ out.write(frame)
54
 
55
  clswise_count = {} # Reset for next frame
56
 
 
92
  # Define the output when the button is clicked
93
  submit_btn.click(gradio_app, inputs=[video_input, analytics_dropdown], outputs=output_video)
94
 
95
+ # Launch the Gradio app with a public link
96
+ demo.launch(share=True)