Skip to content
Snippets Groups Projects
Commit a68f0243 authored by Mathieu Beligon's avatar Mathieu Beligon
Browse files

[Camera] Improve threading management

parent 42cddcd8
No related branches found
No related tags found
No related merge requests found
from threading import Thread
from typing import Any, Iterator
import cv2
from polystar.frame_generators.cv2_frame_generator_abc import CV2FrameGenerator
from polystar.models.image import Image
from polystar.utils.thread import MyThread
class CameraFrameGenerator(CV2FrameGenerator):
......@@ -17,25 +17,14 @@ class CameraFrameGenerator(CV2FrameGenerator):
while True:
yield self.camera_thread.current_frame.copy()
def __del__(self):
self.camera_thread.stop()
class CameraThread(Thread):
class CameraThread(MyThread):
def __init__(self, it: Iterator[Image]):
super().__init__()
self.it = it
self.running = True
self._get_next_frame()
def run(self):
while self.running:
self._get_next_frame()
def stop(self):
self.running = False
self.step()
def _get_next_frame(self):
def step(self):
try:
self.current_frame = next(self.it)
except StopIteration:
......
from threading import Thread
from typing import List
class MyThread(Thread):
THREADS: List["MyThread"] = []
def __init__(self):
super().__init__()
self.running = False
self.THREADS.append(self)
def run(self) -> None:
self.running = True
while self.running:
self.step()
def step(self):
pass
def stop(self):
self.running = False
@staticmethod
def stop_all():
for thread in MyThread.THREADS:
thread.stop()
......@@ -8,6 +8,7 @@ from polystar.models.image import Image
from polystar.target_pipeline.debug_pipeline import DebugTargetPipeline
from polystar.target_pipeline.target_pipeline import NoTargetFoundException
from polystar.utils.fps import FPS
from polystar.utils.thread import MyThread
from polystar.view.cv2_results_viewer import CV2ResultViewer
......@@ -49,3 +50,4 @@ class CameraPipelineDemo:
if __name__ == "__main__":
make_injector().get(CameraPipelineDemo).run()
MyThread().stop_all()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment