-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
571 additions
and
477 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
__version__ = "0.0.4" | ||
__version__ = "0.1.1" | ||
|
||
from .inference import Inference | ||
from .trackers import FrameTracker, TrackerManager | ||
from .dispatcher import Dispatcher, DispatcherManager | ||
from .player import Player |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from multiprocessing.managers import BaseManager | ||
from collections import deque | ||
from typing import List, Any | ||
|
||
|
||
class Dispatcher: | ||
def __init__(self, max_size: int = 120): | ||
self.queue = deque(maxlen=max_size) | ||
self.current_time = 0 | ||
self.current_frame = 0 | ||
self.collect_results = {} | ||
|
||
def get_size(self) -> int: | ||
return len(self.queue) | ||
|
||
def add_frame(self, frame): | ||
self.queue.append(frame) | ||
self.current_frame += 1 | ||
|
||
def get_frames(self, count: int, step: int) -> List[Any]: | ||
if len(self.queue) < count * step: | ||
return [] | ||
|
||
return list(self.queue)[-count * step :][::-1][::step][::-1] | ||
|
||
def collect_result(self, inference_result): | ||
if inference_result is not None: | ||
time = str(inference_result[0]) | ||
name = inference_result[1] | ||
data = inference_result[2] | ||
if self.collect_results.get(name) is None: | ||
self.collect_results[name] = {} | ||
self.collect_results[name][time] = data | ||
|
||
def clear(self): | ||
self.queue.clear() | ||
self.collect_results.clear() | ||
self.current_time = 0 | ||
self.current_frame = 0 | ||
|
||
def increase_current_time(self): | ||
self.current_time += 1 | ||
|
||
def get_current_time(self) -> int: | ||
return self.current_time | ||
|
||
def get_current_frame(self) -> int: | ||
return self.current_frame | ||
|
||
|
||
class DispatcherManager: | ||
def __init__(self, obj=None): | ||
self._manager = None | ||
self._dispatcher = None | ||
self._obj = Dispatcher if obj is None else obj | ||
|
||
def create(self, max_size: int = 120): | ||
if self._manager is None: | ||
self._initialize_manager(max_size) | ||
return self._dispatcher | ||
|
||
def _initialize_manager(self, max_size): | ||
BaseManager.register("Dispatcher", self._obj) | ||
self._manager = BaseManager() | ||
self._manager.start() | ||
self._dispatcher = self._manager.Dispatcher(max_size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,59 @@ | ||
import threading as th | ||
|
||
from .timer import Timer | ||
from .log import logger | ||
|
||
|
||
class Inference: | ||
def __init__(self, frame_tracker, collector=None): | ||
self.frame_tracker = frame_tracker | ||
self.collector = collector | ||
def __init__(self, dispatcher): | ||
self.dispatcher = dispatcher | ||
self.inferences_info = [] | ||
self.timers = {} | ||
self.is_stop = False | ||
|
||
def load_algo(self, algo_instance, frame_count, frame_step, interval): | ||
self.inferences_info.append((algo_instance, frame_count, frame_step, interval)) | ||
self.timers[algo_instance.name] = Timer(interval) | ||
algo_instance.init() | ||
|
||
def start(self, player, fps: int = None, is_offline: bool = False): | ||
""" | ||
Easy to use function to start inference with realtime mode. | ||
""" | ||
if is_offline: | ||
for _, current_frame in player.play(fps): | ||
self.auto_run_specific_inference(player.fps, current_frame) | ||
else: | ||
player.play_realtime(fps) | ||
while player.is_active(): | ||
self.run_inference() | ||
|
||
def run_inference(self): | ||
# start_time = time.time() | ||
|
||
def run(self): | ||
for inference_info in self.inferences_info: | ||
algo_instance, _, _, _ = inference_info | ||
timer = self.timers[algo_instance.name] | ||
if timer.is_time(): | ||
self._inference_task(inference_info) | ||
self._infer(inference_info) | ||
|
||
# end_time = time.time() | ||
# elapsed_time = end_time - start_time | ||
def run_loop(self): | ||
while not self.is_stop: | ||
self.run() | ||
|
||
def auto_run_specific_inference(self, fps, current_frame) -> str: | ||
def run_async(self): | ||
thread = th.Thread(target=self.run_loop) | ||
thread.start() | ||
return thread | ||
|
||
def stop(self): | ||
self.is_stop = True | ||
|
||
def auto_run_specific(self, fps, current_frame) -> str: | ||
for algo_instance, _, _, frequency in self.inferences_info: | ||
if current_frame % int(frequency * fps) == 0: | ||
self.run_specific_inference(algo_instance.name) | ||
self.run_specific(algo_instance.name) | ||
return algo_instance.name | ||
|
||
def run_specific_inference(self, algo_name): | ||
def run_specific(self, algo_name): | ||
for inference_info in self.inferences_info: | ||
algo_instance, _, _, _ = inference_info | ||
if algo_instance.name == algo_name: | ||
self._inference_task(inference_info) | ||
|
||
def run_inference_loop(self): | ||
while True: | ||
self.run_inference() | ||
self._infer(inference_info) | ||
|
||
def _inference_task(self, inference_info): | ||
def _infer(self, inference_info): | ||
algo_instance, frame_count, frame_step, _ = inference_info | ||
frames = self.frame_tracker.get_frames(frame_count, frame_step) | ||
frames = self.dispatcher.get_frames(frame_count, frame_step) | ||
if not frames: | ||
return -1 | ||
result = algo_instance.run(frames) | ||
if self.collector: | ||
self.collector.collect( | ||
(self.frame_tracker.get_current_time(), algo_instance.name, result) | ||
) | ||
self.dispatcher.collect_result( | ||
(self.dispatcher.get_current_time(), algo_instance.name, result) | ||
) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.