From a1e43b8e37d0c3389658910a1d28fa282c6a97e4 Mon Sep 17 00:00:00 2001 From: mht Date: Thu, 6 Mar 2025 15:03:56 +0330 Subject: [PATCH] added capture card --- app.py | 70 ++++++++++++++++----- core.py | 6 +- message_queue/proto/SetCameraMessage.proto | 1 + message_queue/proto/SetCameraMessage_pb2.py | 11 +++- 4 files changed, 67 insertions(+), 21 deletions(-) diff --git a/app.py b/app.py index 06f2dfc..a300407 100755 --- a/app.py +++ b/app.py @@ -8,6 +8,7 @@ import os import time import threading # Import threading module +from cvStreamer import cvStreamer from detector import Detector # 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() 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): m = Message() m.msgType = MessageType.MESSAGE_TYPE_CAMERA_CONNECTION_STATUS @@ -64,7 +74,7 @@ class ConnectionTracker: self.last_client_ip = client_ip # Update last known client 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 # Create an instance of ConnectionTracker connection_tracker = ConnectionTracker() @@ -96,7 +106,7 @@ class ConnectionThread(threading.Thread): 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) print(f"New client found: {cl_ip}") @@ -125,6 +135,7 @@ class ConnectionThread(threading.Thread): print(f"Received response from {addr}: {data.decode()}") # Debugging if data.decode() == "HEARTBEAT_ACK": + connection_tracker.update_last_message_time() # Update the last message time if debug: print(f"Client {client_ip} is still responding.") @@ -165,8 +176,14 @@ if __name__ == '__main__': QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling) 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): videoStreamer = GstVideoStreamer(rtsp_link, [1920, 1080, 3], str(idx), fps=15) videoStreamer.cameraStatus.connect(handle_camera_status) @@ -175,12 +192,17 @@ if __name__ == '__main__': tracker = Tracker() 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): msg = Message() 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.track_cmd.command == TrackCommand.TRACK_COMMAND_STOP: @@ -192,18 +214,34 @@ if __name__ == '__main__': elif msg.msgType == MessageType.MESSAGE_TYPE_TRACK_SETTINGS: core.set_thickness(msg.track_settings.thickness) 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) 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 diff --git a/core.py b/core.py index 66c6e26..4db36f2 100755 --- a/core.py +++ b/core.py @@ -19,13 +19,13 @@ import ctypes from ctypes import c_int64 -showTrack = False +showTrack = True class Core(QThread): newFrame = pyqtSignal(object, int, bool, ctypes.c_int64) coordsUpdated = pyqtSignal(int, object, bool) - def __init__(self, video_sources: List[VideoStreamer], tracker=None, detector=None, parent=None): + def __init__(self, video_sources, tracker=None, detector=None, parent=None): super(QThread, self).__init__(parent) self.__detector = detector @@ -59,7 +59,7 @@ class Core(QThread): self.__processing_source = self.__video_sources[source_id] self.__processing_id = source_id - def set_video_sources(self, video_sources: List[VideoStreamer]): + def set_video_sources(self, video_sources): self.__video_sources = video_sources self.set_source(0) diff --git a/message_queue/proto/SetCameraMessage.proto b/message_queue/proto/SetCameraMessage.proto index eb69063..435cc98 100755 --- a/message_queue/proto/SetCameraMessage.proto +++ b/message_queue/proto/SetCameraMessage.proto @@ -3,4 +3,5 @@ syntax = "proto3"; message SetCameraMessage { string ip = 1; int32 port = 2; + int32 cameraSource = 3; } diff --git a/message_queue/proto/SetCameraMessage_pb2.py b/message_queue/proto/SetCameraMessage_pb2.py index 854c36e..9050267 100755 --- a/message_queue/proto/SetCameraMessage_pb2.py +++ b/message_queue/proto/SetCameraMessage_pb2.py @@ -19,7 +19,7 @@ DESCRIPTOR = _descriptor.FileDescriptor( syntax='proto3', serialized_options=None, create_key=_descriptor._internal_create_key, - serialized_pb=b'\n\x16SetCameraMessage.proto\",\n\x10SetCameraMessage\x12\n\n\x02ip\x18\x01 \x01(\t\x12\x0c\n\x04port\x18\x02 \x01(\x05\x62\x06proto3' + serialized_pb=b'\n\x16SetCameraMessage.proto\"B\n\x10SetCameraMessage\x12\n\n\x02ip\x18\x01 \x01(\t\x12\x0c\n\x04port\x18\x02 \x01(\x05\x12\x14\n\x0c\x63\x61meraSource\x18\x03 \x01(\x05\x62\x06proto3' ) @@ -47,6 +47,13 @@ _SETCAMERAMESSAGE = _descriptor.Descriptor( message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), + _descriptor.FieldDescriptor( + name='cameraSource', full_name='SetCameraMessage.cameraSource', index=2, + number=3, type=5, cpp_type=1, label=1, + has_default_value=False, default_value=0, + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR, create_key=_descriptor._internal_create_key), ], extensions=[ ], @@ -60,7 +67,7 @@ _SETCAMERAMESSAGE = _descriptor.Descriptor( oneofs=[ ], serialized_start=26, - serialized_end=70, + serialized_end=92, ) DESCRIPTOR.message_types_by_name['SetCameraMessage'] = _SETCAMERAMESSAGE