From 80f4a474410e8c10db097fdfa1132b4bb1821668 Mon Sep 17 00:00:00 2001 From: s_kiani Date: Wed, 9 Apr 2025 17:59:20 +0330 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A5=E2=9C=A8=20Refactor=20streaming=20?= =?UTF-8?q?logic=20to=20separate=20tracking=20and=20non-tracking=20frame?= =?UTF-8?q?=20updates?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🔄 Changed __stream method behavior: - If tracking is active, only the non-tracked source updates via update_frame - If not tracking, both sources stream frames as before This ensures the tracked source is exclusively handled in the __tracking thread. --- core.py | 52 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/core.py b/core.py index e9ab6ea..1bf912b 100755 --- a/core.py +++ b/core.py @@ -20,7 +20,7 @@ import ctypes from ctypes import c_int64 from server import run_server, RTSPServer, get_local_ip -showTrack = True +showTrack = False class Core(QThread): newFrame = pyqtSignal(object, int, bool, ctypes.c_int64) @@ -79,35 +79,32 @@ class Core(QThread): self.set_source(0) def __stream(self): - # return """Continuous streaming of the video source.""" while self.__is_streaming: try: - frame_0 = self.__video_sources[0].get_frame() frame_1 = self.__video_sources[1].get_frame() - if frame_1 is not None: - if self.__is_tracking and self.__tracker is not None and self.__processing_id == 1: - - if self.__tracker_roi is not None: - x, y, w, h = map(int, self.__tracker_roi) - box_color = (0, 255, 255) if self.__tracker__succ else (255, 0, 0) - cv2.rectangle(frame_1, (x, y), (x + w, y + h), box_color, 2) - print(frame_1.shape) - self.__rtspserver_1.update_frame(frame_1) - - if frame_0 is not None: - if self.__is_tracking and self.__tracker is not None and self.__processing_id == 0: - if self.__tracker_roi is not None: - x, y, w, h = map(int, self.__tracker_roi) - box_color = (0, 255, 255) if self.__tracker__succ else (255, 0, 0) - cv2.rectangle(frame_0, (x, y), (x + w, y + h), box_color, 2) - print(frame_0.shape) - self.__rtspserver_0.update_frame(frame_0) + if self.__is_tracking: + # Only update the non-tracking source + if self.__processing_id == 0: + if frame_1 is not None: + self.__rtspserver_1.update_frame(frame_1) + elif self.__processing_id == 1: + if frame_0 is not None: + self.__rtspserver_0.update_frame(frame_0) + else: + # Update both sources if not tracking + if frame_0 is not None: + self.__rtspserver_0.update_frame(frame_0) + if frame_1 is not None: + self.__rtspserver_1.update_frame(frame_1) sleep(0.03) - # self.__tracker_roi = None + + except Exception as e: + print(e) + sleep(0.1) @@ -167,6 +164,17 @@ class Core(QThread): self.__tracker_roi = bbox self.__tracker__succ = success + if frame is not None: + if self.__is_tracking and self.__tracker is not None and self.__tracker_roi is not None : + x, y, w, h = map(int, self.__tracker_roi) + box_color = (0, 255, 255) if self.__tracker__succ else (255, 0, 0) + cv2.rectangle(frame, (x, y), (x + w, y + h), box_color, 2) + + if self.__processing_id == 1: + self.__rtspserver_1.update_frame(frame) + elif self.__processing_id == 0: + self.__rtspserver_0.update_frame(frame) + if bbox is not None: center = bbox[:2] + bbox[2:] // 2 self.coordsUpdated.emit(self.__processing_id, center, success)