1515class StreamGrid :
1616 """Ultra-fast multi-stream video display with object detection."""
1717
18- def __init__ (self , sources = None , model = None , save = True , device = "cpu" , analytics = False ):
18+ def __init__ (
19+ self , sources = None , model = None , save = True , device = "cpu" , analytics = False
20+ ):
1921 # Initialize components
2022 self .stream_manager = StreamManager (sources )
2123 self .model = model
@@ -32,7 +34,9 @@ def __init__(self, sources=None, model=None, save=True, device="cpu", analytics=
3234 self .plotter = StreamPlotter (self .cell_w , self .cell_h , self .max_sources )
3335
3436 # Display state
35- self .grid = np .zeros ((self .rows * self .cell_h , self .cols * self .cell_w , 3 ), dtype = np .uint8 )
37+ self .grid = np .zeros (
38+ (self .rows * self .cell_h , self .cols * self .cell_w , 3 ), dtype = np .uint8
39+ )
3640 self .frames = {}
3741 self .show_stats = True
3842 self .running = False
@@ -51,8 +55,10 @@ def setup_video_writer(self):
5155 """Setup video writer for saving output."""
5256 fourcc = cv2 .VideoWriter_fourcc (* "mp4v" )
5357 return cv2 .VideoWriter (
54- f"streamgrid_output_{ self .max_sources } _streams.mp4" , fourcc , 30 ,
55- (self .cols * self .cell_w , self .rows * self .cell_h )
58+ f"streamgrid_output_{ self .max_sources } _streams.mp4" ,
59+ fourcc ,
60+ 30 ,
61+ (self .cols * self .cell_w , self .rows * self .cell_h ),
5662 )
5763
5864 def process_batch (self ):
@@ -79,7 +85,9 @@ def process_batch(self):
7985
8086 # Run inference if model available
8187 if self .model :
82- results = self .model .predict (frames , conf = 0.25 , verbose = False , device = self .device )
88+ results = self .model .predict (
89+ frames , conf = 0.25 , verbose = False , device = self .device
90+ )
8391 for source_id , frame , result in zip (ids , frames , results ):
8492 self .update_source (source_id , frame , result )
8593 else :
@@ -110,10 +118,14 @@ def update_source(self, source_id, frame, results=None):
110118 detections = 0
111119 if results and results .boxes is not None :
112120 detections = len (results .boxes )
113- resized = self .plotter .draw_detections (resized , results , frame .shape [:2 ])
121+ resized = self .plotter .draw_detections (
122+ resized , results , frame .shape [:2 ]
123+ )
114124
115125 # Add source label
116- resized = self .plotter .draw_source_label (resized , source_id , self .show_stats )
126+ resized = self .plotter .draw_source_label (
127+ resized , source_id , self .show_stats
128+ )
117129
118130 # Store processed frame
119131 self .frames [source_id ] = resized
@@ -140,8 +152,10 @@ def update_display(self):
140152 # Add FPS overlay
141153 if self .show_stats :
142154 self .grid = self .plotter .draw_fps_overlay (
143- self .grid , self .prediction_fps ,
144- self .cols * self .cell_w , self .rows * self .cell_h
155+ self .grid ,
156+ self .prediction_fps ,
157+ self .cols * self .cell_w ,
158+ self .rows * self .cell_h ,
145159 )
146160
147161 # Display and save
@@ -166,7 +180,7 @@ def run(self):
166180 key = cv2 .waitKey (1 ) & 0xFF
167181 if key == 27 : # ESC
168182 break
169- elif key == ord ('s' ):
183+ elif key == ord ("s" ):
170184 self .show_stats = not self .show_stats
171185 finally :
172186 self .stop ()
@@ -185,7 +199,9 @@ def stop(self):
185199 self .analytics .summary ()
186200 if self .video_writer :
187201 self .video_writer .release ()
188- LOGGER .info (f"β
Video saved: streamgrid_output_{ self .max_sources } _streams.mp4" )
202+ LOGGER .info (
203+ f"β
Video saved: streamgrid_output_{ self .max_sources } _streams.mp4"
204+ )
189205
190206 with self .lock :
191207 self .frames .clear ()
0 commit comments