99import requests
1010from tqdm import tqdm
1111from streamgrid .utils import LOGGER , get_optimal_grid_size
12+ from streamgrid .analytics import StreamAnalytics
1213from ultralytics .utils .plotting import Annotator , colors
1314
1415
@@ -39,7 +40,7 @@ class StreamGrid:
3940 video_writer: OpenCV video writer object.
4041 """
4142
42- def __init__ (self , sources = None , model = None , save = True , device = "cpu" ):
43+ def __init__ (self , sources = None , model = None , save = True , device = "cpu" , analytics = False ):
4344 """Initialize StreamGrid with video sources and configuration.
4445
4546 Args:
@@ -48,6 +49,7 @@ def __init__(self, sources=None, model=None, save=True, device="cpu"):
4849 model (optional): YOLO model instance for object detection.
4950 save (bool, optional): Save output video. Output will be saved as "streamgrid_output_{N}_streams.mp4".
5051 device (str, optional): Wheather to run inference on GPU or CPU device.
52+ analytics (bool, optional): Wheather to store streams results in CSV file.
5153 """
5254 # GitHub repository URLs for default videos
5355 self .GITHUB_ASSETS_BASE = "https://github.com/RizwanMunawar/streamgrid/releases/download/v1.0.0/"
@@ -111,6 +113,7 @@ def __init__(self, sources=None, model=None, save=True, device="cpu"):
111113 self .auto_shutdown = True # Control auto-shutdown behavior
112114 self .shutdown_delay = 3.0 # Seconds to wait after streams end before shutdown
113115
116+ self .analytics = StreamAnalytics () if analytics else None # Enable analytics storage.
114117 self .run ()
115118
116119 def get_default_videos (self ):
@@ -324,6 +327,9 @@ def update_source(self, source_id, frame, yolo_results=None):
324327 detections = len (yolo_results .boxes )
325328 resized = self .draw_boxes (resized , yolo_results , frame .shape [:2 ])
326329
330+ if self .analytics :
331+ self .analytics .log (source_id , detections , self .prediction_fps )
332+
327333 # Store processed frame and statistics
328334 self .frames [source_id ] = resized
329335 self .stats [source_id ] = {'detections' : detections , 'time' : time .time ()}
@@ -531,6 +537,8 @@ def stop(self):
531537 for i , thread in enumerate (self .stream_threads ): # Wait for threads to finish (with timeout)
532538 thread .join (timeout = 0.5 )
533539
540+ if self .analytics :
541+ self .analytics .summary ()
534542
535543 if self .save and self .video_writer :
536544 self .video_writer .release ()
0 commit comments