|  | @ -8,6 +8,7 @@ import os | 
		
	
		
			
				|  |  | import time |  |  | import time | 
		
	
		
			
				|  |  | import threading  # Import threading module |  |  | import threading  # Import threading module | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  | from cvStreamer import cvStreamer | 
		
	
		
			
				|  |  | from detector import Detector |  |  | from detector import Detector | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  | # Add the proto directory to the Python path |  |  | # Add the proto directory to the Python path | 
		
	
	
		
			
				|  | @ -45,6 +46,15 @@ config_manager = ConfigManager('config.yaml') | 
		
	
		
			
				|  |  | rtsp_links = config_manager.configs['rtsp_links'].get() |  |  | rtsp_links = config_manager.configs['rtsp_links'].get() | 
		
	
		
			
				|  |  | debug = config_manager.configs['debug'].get() |  |  | debug = config_manager.configs['debug'].get() | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  | def findUsbCams(): | 
		
	
		
			
				|  |  |  |  |  |     cv_cameras = [] | 
		
	
		
			
				|  |  |  |  |  |     for i in range(50): | 
		
	
		
			
				|  |  |  |  |  |         cap = cvStreamer(i) | 
		
	
		
			
				|  |  |  |  |  |         if cap.isOpened(): | 
		
	
		
			
				|  |  |  |  |  |             cv_cameras.append(cap) | 
		
	
		
			
				|  |  |  |  |  |     return cv_cameras | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  | def handle_camera_status(status: int): |  |  | def handle_camera_status(status: int): | 
		
	
		
			
				|  |  |     m = Message() |  |  |     m = Message() | 
		
	
		
			
				|  |  |     m.msgType = MessageType.MESSAGE_TYPE_CAMERA_CONNECTION_STATUS |  |  |     m.msgType = MessageType.MESSAGE_TYPE_CAMERA_CONNECTION_STATUS | 
		
	
	
		
			
				|  | @ -64,7 +74,7 @@ class ConnectionTracker: | 
		
	
		
			
				|  |  |             self.last_client_ip = client_ip  # Update last known client |  |  |             self.last_client_ip = client_ip  # Update last known client | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |     def is_client_active(self): |  |  |     def is_client_active(self): | 
		
	
		
			
				|  |  |         """Check if the last client is still active within the last 30 seconds.""" |  |  |  | 
		
	
		
			
				|  |  |  |  |  |         """Check if the last client is still active within the last 15 seconds.""" | 
		
	
		
			
				|  |  |         return (time.time() - self.last_message_time) < self.timeout |  |  |         return (time.time() - self.last_message_time) < self.timeout | 
		
	
		
			
				|  |  | # Create an instance of ConnectionTracker |  |  | # Create an instance of ConnectionTracker | 
		
	
		
			
				|  |  | connection_tracker = ConnectionTracker() |  |  | connection_tracker = ConnectionTracker() | 
		
	
	
		
			
				|  | @ -96,7 +106,7 @@ class ConnectionThread(threading.Thread): | 
		
	
		
			
				|  |  |             if not connection_tracker.is_client_active(): |  |  |             if not connection_tracker.is_client_active(): | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |                 print("Client inactive for 30 seconds. Searching for another client...") |  |  |  | 
		
	
		
			
				|  |  |  |  |  |                 print("Client inactive for 15 seconds. Searching for another client...") | 
		
	
		
			
				|  |  |                 cl_ip, _ = self.start_discovery_service(12345) |  |  |                 cl_ip, _ = self.start_discovery_service(12345) | 
		
	
		
			
				|  |  |                 print(f"New client found: {cl_ip}") |  |  |                 print(f"New client found: {cl_ip}") | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
	
		
			
				|  | @ -125,6 +135,7 @@ class ConnectionThread(threading.Thread): | 
		
	
		
			
				|  |  |                 print(f"Received response from {addr}: {data.decode()}")  # Debugging |  |  |                 print(f"Received response from {addr}: {data.decode()}")  # Debugging | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |             if data.decode() == "HEARTBEAT_ACK": |  |  |             if data.decode() == "HEARTBEAT_ACK": | 
		
	
		
			
				|  |  |  |  |  |                 connection_tracker.update_last_message_time()  # Update the last message time | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |                 if debug: |  |  |                 if debug: | 
		
	
		
			
				|  |  |                     print(f"Client {client_ip} is still responding.") |  |  |                     print(f"Client {client_ip} is still responding.") | 
		
	
	
		
			
				|  | @ -165,8 +176,14 @@ if __name__ == '__main__': | 
		
	
		
			
				|  |  |     QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling) |  |  |     QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling) | 
		
	
		
			
				|  |  |     app = QCoreApplication(sys.argv) |  |  |     app = QCoreApplication(sys.argv) | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |     videoStreamers: List[GstVideoStreamer] = [] |  |  |  | 
		
	
		
			
				|  |  |     streamerThreads = [] |  |  |  | 
		
	
		
			
				|  |  |  |  |  |         # cv_cameras = [] | 
		
	
		
			
				|  |  |  |  |  |         # for i in range(3): | 
		
	
		
			
				|  |  |  |  |  |         #     cap = cvStreamer(i) | 
		
	
		
			
				|  |  |  |  |  |         #     if cap.isOpened(): | 
		
	
		
			
				|  |  |  |  |  |         #         cv_cameras.append(cap) | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  |     videoStreamers = [] | 
		
	
		
			
				|  |  |     for idx, rtsp_link in enumerate(rtsp_links): |  |  |     for idx, rtsp_link in enumerate(rtsp_links): | 
		
	
		
			
				|  |  |         videoStreamer = GstVideoStreamer(rtsp_link, [1920, 1080, 3], str(idx), fps=15) |  |  |         videoStreamer = GstVideoStreamer(rtsp_link, [1920, 1080, 3], str(idx), fps=15) | 
		
	
		
			
				|  |  |         videoStreamer.cameraStatus.connect(handle_camera_status) |  |  |         videoStreamer.cameraStatus.connect(handle_camera_status) | 
		
	
	
		
			
				|  | @ -175,12 +192,17 @@ if __name__ == '__main__': | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |     tracker = Tracker() |  |  |     tracker = Tracker() | 
		
	
		
			
				|  |  |     detector = Detector(classes=[0, 2, 5, 7]) |  |  |     detector = Detector(classes=[0, 2, 5, 7]) | 
		
	
		
			
				|  |  |     core = Core(videoStreamers,tracker,detector) |  |  |  | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |  |     sources = findUsbCams() | 
		
	
		
			
				|  |  |  |  |  |     if len(sources) >= 2: | 
		
	
		
			
				|  |  |  |  |  |         core = Core(sources,tracker,detector) | 
		
	
		
			
				|  |  |  |  |  |     else: | 
		
	
		
			
				|  |  |  |  |  |         core = Core(videoStreamers, tracker, detector) | 
		
	
		
			
				|  |  |  |  |  | 
 | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |     def manager_callback(msg_str): |  |  |     def manager_callback(msg_str): | 
		
	
		
			
				|  |  |         msg = Message() |  |  |         msg = Message() | 
		
	
		
			
				|  |  |         msg.ParseFromString(msg_str) |  |  |         msg.ParseFromString(msg_str) | 
		
	
		
			
				|  |  |         connection_tracker.update_last_message_time()  # Update the last message time |  |  |  | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |         if msg.msgType == MessageType.MESSAGE_TYPE_TOGGLE_TRACK: |  |  |         if msg.msgType == MessageType.MESSAGE_TYPE_TOGGLE_TRACK: | 
		
	
		
			
				|  |  |             if msg.track_cmd.command == TrackCommand.TRACK_COMMAND_STOP: |  |  |             if msg.track_cmd.command == TrackCommand.TRACK_COMMAND_STOP: | 
		
	
	
		
			
				|  | @ -192,18 +214,34 @@ if __name__ == '__main__': | 
		
	
		
			
				|  |  |         elif msg.msgType == MessageType.MESSAGE_TYPE_TRACK_SETTINGS: |  |  |         elif msg.msgType == MessageType.MESSAGE_TYPE_TRACK_SETTINGS: | 
		
	
		
			
				|  |  |             core.set_thickness(msg.track_settings.thickness) |  |  |             core.set_thickness(msg.track_settings.thickness) | 
		
	
		
			
				|  |  |         elif msg.msgType == MessageType.MESSAGE_TYPE_SWITCH_CAMERA: |  |  |         elif msg.msgType == MessageType.MESSAGE_TYPE_SWITCH_CAMERA: | 
		
	
		
			
				|  |  |  |  |  |             print(f"switch camera detected ,primary value is : {msg.cam_switch.primaryCamType}") | 
		
	
		
			
				|  |  |  |  |  |             print(f"switch camera detected ,secondary value is : {msg.cam_switch.secondaryCamType}") | 
		
	
		
			
				|  |  |             core.set_source(msg.cam_switch.primaryCamType) |  |  |             core.set_source(msg.cam_switch.primaryCamType) | 
		
	
		
			
				|  |  |         elif msg.msgType == MessageType.MESSAGE_TYPE_SET_CAMERA: |  |  |         elif msg.msgType == MessageType.MESSAGE_TYPE_SET_CAMERA: | 
		
	
		
			
				|  |  |             ip = msg.cam_set.ip |  |  |  | 
		
	
		
			
				|  |  |             videoStreamers = [] |  |  |  | 
		
	
		
			
				|  |  |             for src in range(2): |  |  |  | 
		
	
		
			
				|  |  |                 rtsp_link = f'rtsp://admin:Abc.12345@{ip}:554/ch{src}/stream0' |  |  |  | 
		
	
		
			
				|  |  |                 # ic(rtsp_link) |  |  |  | 
		
	
		
			
				|  |  |                 videoStreamer = GstVideoStreamer(rtsp_link, [1920, 1080, 3], str(idx), fps=15) |  |  |  | 
		
	
		
			
				|  |  |                 videoStreamer.cameraStatus.connect(handle_camera_status) |  |  |  | 
		
	
		
			
				|  |  |                 print(f'{videoStreamer.id} connected') |  |  |  | 
		
	
		
			
				|  |  |                 videoStreamers.append(videoStreamer) |  |  |  | 
		
	
		
			
				|  |  |             core.set_video_sources(videoStreamers) |  |  |  | 
		
	
		
			
				|  |  |  |  |  |             if msg.cam_set.cameraSource == 1: | 
		
	
		
			
				|  |  |  |  |  |                 print(f"set camera source to network") | 
		
	
		
			
				|  |  |  |  |  |                 ip = msg.cam_set.ip | 
		
	
		
			
				|  |  |  |  |  |                 videoStreamers = [] | 
		
	
		
			
				|  |  |  |  |  |                 for src in range(2): | 
		
	
		
			
				|  |  |  |  |  |                     rtsp_link = f'rtsp://admin:Abc.12345@{ip}:554/ch{src}/stream0' | 
		
	
		
			
				|  |  |  |  |  |                     # ic(rtsp_link) | 
		
	
		
			
				|  |  |  |  |  |                     videoStreamer = GstVideoStreamer(rtsp_link, [1920, 1080, 3], str(idx), fps=15) | 
		
	
		
			
				|  |  |  |  |  |                     videoStreamer.cameraStatus.connect(handle_camera_status) | 
		
	
		
			
				|  |  |  |  |  |                     print(f'{videoStreamer.id} connected') | 
		
	
		
			
				|  |  |  |  |  |                     videoStreamers.append(videoStreamer) | 
		
	
		
			
				|  |  |  |  |  |                 core.set_video_sources(videoStreamers) | 
		
	
		
			
				|  |  |  |  |  |                 # videoStreamers = [] | 
		
	
		
			
				|  |  |  |  |  |                 # for idx, rtsp_link in enumerate(rtsp_links): | 
		
	
		
			
				|  |  |  |  |  |                 #     videoStreamer = GstVideoStreamer(rtsp_link, [1920, 1080, 3], str(idx), fps=15) | 
		
	
		
			
				|  |  |  |  |  |                 #     videoStreamer.cameraStatus.connect(handle_camera_status) | 
		
	
		
			
				|  |  |  |  |  |                 #     print(f'{videoStreamer.id} connected') | 
		
	
		
			
				|  |  |  |  |  |                 #     videoStreamers.append(videoStreamer) | 
		
	
		
			
				|  |  |  |  |  |                 #     core.set_video_sources(videoStreamers) | 
		
	
		
			
				|  |  |  |  |  |             else: | 
		
	
		
			
				|  |  |  |  |  |                 sources = findUsbCams() | 
		
	
		
			
				|  |  |  |  |  |                 if len(sources) >= 2 : | 
		
	
		
			
				|  |  |  |  |  |                     print("set camera source to captutre card") | 
		
	
		
			
				|  |  |  |  |  |                     core.set_video_sources(findUsbCams()) | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
		
			
				|  |  |     import socket |  |  |     import socket | 
		
	
		
			
				|  |  | 
 |  |  | 
 | 
		
	
	
		
			
				|  | 
 |