Browse Source

made project thread safe by making detector and tracker objects out of core object and they are shared using pointers

client-connection
s_kiani 1 month ago
parent
commit
691a281bb0
  1. 7
      app.py
  2. 9
      core.py

7
app.py

@ -8,6 +8,7 @@ import os
import time import time
import threading # Import threading module import threading # Import threading module
from detector import Detector
# Add the proto directory to the Python path # Add the proto directory to the Python path
proto_dir = os.path.join(os.path.dirname(__file__), 'message_queue', 'proto') proto_dir = os.path.join(os.path.dirname(__file__), 'message_queue', 'proto')
@ -25,6 +26,7 @@ from icecream import ic
from configs import ConfigManager from configs import ConfigManager
from core import Core from core import Core
from tracker import Tracker
from message_queue.Bridge import Bridge from message_queue.Bridge import Bridge
from message_queue.Manager import Manager from message_queue.Manager import Manager
from message_queue.proto.ImageMessage_pb2 import ImageMessage, TrackMode from message_queue.proto.ImageMessage_pb2 import ImageMessage, TrackMode
@ -171,7 +173,9 @@ if __name__ == '__main__':
print(f'{videoStreamer.id} connected') print(f'{videoStreamer.id} connected')
videoStreamers.append(videoStreamer) videoStreamers.append(videoStreamer)
core = Core(videoStreamers)
tracker = Tracker()
detector = Detector(classes=[0, 2, 5, 7])
core = Core(videoStreamers,tracker,detector)
def manager_callback(msg_str): def manager_callback(msg_str):
msg = Message() msg = Message()
@ -226,6 +230,7 @@ if __name__ == '__main__':
manager.start(manager_callback) manager.start(manager_callback)
def gotNewFrame(bboxes, id_, isDetection, ctime): def gotNewFrame(bboxes, id_, isDetection, ctime):
#print(f"Got new frame, bboxes : {bboxes} Id: {id} Is detection {isDetection}")
m = Message() m = Message()
m.msgType = MessageType.MESSAGE_TYPE_IMAGE m.msgType = MessageType.MESSAGE_TYPE_IMAGE
m.image.timestamp = int(ctime.value) m.image.timestamp = int(ctime.value)

9
core.py

@ -25,11 +25,11 @@ class Core(QThread):
newFrame = pyqtSignal(object, int, bool,ctypes.c_int64) newFrame = pyqtSignal(object, int, bool,ctypes.c_int64)
coordsUpdated = pyqtSignal(int, object, bool) coordsUpdated = pyqtSignal(int, object, bool)
def __init__(self, video_sources: List[VideoStreamer], parent=None):
def __init__(self, video_sources: List[VideoStreamer], tracker = None, detector = None , parent=None):
super(QThread, self).__init__(parent) super(QThread, self).__init__(parent)
self.__detector = Detector(classes=[0, 2, 5, 7])
self.__tracker = Tracker()
self.__detector = detector
self.__tracker = tracker
self.__video_sources = video_sources self.__video_sources = video_sources
self.__processing_source = video_sources[0] self.__processing_source = video_sources[0]
@ -118,6 +118,7 @@ class Core(QThread):
self.__detection_thread = None self.__detection_thread = None
def start_track(self, x: int, y: int, w: int = 0, h: int = 0): def start_track(self, x: int, y: int, w: int = 0, h: int = 0):
print(f"start tracking: {x}, {y}, {w}, {h}")
try: try:
self.__is_detecting = False self.__is_detecting = False
self.__is_tracking = False self.__is_tracking = False
@ -142,12 +143,14 @@ class Core(QThread):
if self.__tracking_thread is not None: if self.__tracking_thread is not None:
self.__tracking_thread.join() self.__tracking_thread.join()
self.stop_track()
self.__is_tracking = True self.__is_tracking = True
self.__tracking_thread = Thread(target=self.__tracking) self.__tracking_thread = Thread(target=self.__tracking)
self.__tracking_thread.start() self.__tracking_thread.start()
sleep(0.03) sleep(0.03)
def stop_track(self): def stop_track(self):
print("stop tracking")
self.stop_detection() self.stop_detection()
self.__tracker.stop() self.__tracker.stop()
self.__is_tracking = False self.__is_tracking = False

Loading…
Cancel
Save