prithivMLmods commited on
Commit
7c5b520
1 Parent(s): 1ee2bcb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")
7
+
8
+ def process_video(video_path, analytics_type):
9
+ cap = cv2.VideoCapture(video_path)
10
+ assert cap.isOpened(), "Error reading video file"
11
+
12
+ # Get video properties
13
+ w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
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
+
28
+ while cap.isOpened():
29
+ success, frame = cap.read()
30
+ if success:
31
+ frame_count += 1
32
+ results = model.track(frame, persist=True, verbose=False)
33
+
34
+ if results[0].boxes.id is not None:
35
+ boxes = results[0].boxes.xyxy.cpu()
36
+ clss = results[0].boxes.cls.cpu().tolist()
37
+
38
+ for box, cls in zip(boxes, clss):
39
+ if model.names[int(cls)] in clswise_count:
40
+ clswise_count[model.names[int(cls)]] += 1
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
+
57
+ else:
58
+ break
59
+
60
+ cap.release()
61
+ out.release()
62
+
63
+ return output_filename # Return the output video file
64
+
65
+
66
+ def gradio_app(video, analytics_type):
67
+ # Save uploaded video locally
68
+ video_path = video.name
69
+ output_video = process_video(video_path, analytics_type)
70
+
71
+ # Return processed video for display
72
+ return output_video
73
+
74
+
75
+ # Gradio interface
76
+ with gr.Blocks() as demo:
77
+ gr.Markdown("# YOLO Video Processing App")
78
+
79
+ with gr.Row():
80
+ video_input = gr.Video(label="Upload Video", type="file")
81
+ analytics_dropdown = gr.Dropdown(
82
+ ["line", "multiple_line", "pie", "area"],
83
+ label="Select Analytics Type",
84
+ value="line"
85
+ )
86
+
87
+ output_video = gr.Video(label="Processed Output")
88
+
89
+ # Button to start processing
90
+ submit_btn = gr.Button("Process Video")
91
+
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()