From 962380c5a00bb4d5e3fe3da0a659725c7cd513ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Sp=C3=B6rk?= Date: Fri, 1 Nov 2024 13:01:08 +0100 Subject: [PATCH] refactoring(data): Refactoring inter-process data passing Remove msg passthrough in preprocess/tracker (too expensive) Fix logging in hooks --- foosball/hooks/__init__.py | 15 ++++++--------- foosball/tracking/preprocess.py | 3 ++- foosball/tracking/render.py | 2 +- foosball/tracking/tracker.py | 6 +++--- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/foosball/hooks/__init__.py b/foosball/hooks/__init__.py index b7f3f49..7cfe35f 100644 --- a/foosball/hooks/__init__.py +++ b/foosball/hooks/__init__.py @@ -1,17 +1,13 @@ -import dataclasses import logging import os import random import threading -import traceback from abc import ABC, abstractmethod from typing import Mapping, Self import urllib3 import yaml -logger = logging.getLogger(__name__) - class Hook(ABC): @abstractmethod @@ -34,7 +30,7 @@ def play_sound(sound_file: str): if os.path.isfile(sound_file): playsound(sound_file, block=False) else: - logger.warning(f"Audio not found: {sound_file}") + logging.warning(f"Audio not found: {sound_file}") @staticmethod def play_random_sound(folder: str, prefix: str = './assets/audio'): @@ -44,7 +40,9 @@ def play_random_sound(folder: str, prefix: str = './assets/audio'): class Webhook(Hook): + def __init__(self, method: str = None, url: str = None, json: dict = None, headers: Mapping[str, str] = None, *args, **kwargs): + super().__init__() self.method: str = method self.url: str = url self.json: dict = json @@ -70,10 +68,9 @@ def call(method: str, url: str, json: dict = None, headers: Mapping[str, str] = if json is not None and "content-type" not in headers: headers['content_type'] = 'application/json' response = urllib3.request(method, url, json=json, headers=headers) - logger.debug(f"webhook response: {response.status}") + logging.debug(f"webhook response: {response.status}") except Exception as e: - logger.error(f"Webhook failed - {e}") - traceback.print_exc() + logging.error(f"Webhook failed - {e}") @classmethod def load_webhook(cls, filename: str) -> Self: @@ -82,4 +79,4 @@ def load_webhook(cls, filename: str) -> Self: wh = yaml.safe_load(f) return Webhook(**wh) else: - logger.info("No goal webhook configured under 'goal_webhook.yaml'") + logging.info("No goal webhook configured under 'goal_webhook.yaml'") diff --git a/foosball/tracking/preprocess.py b/foosball/tracking/preprocess.py index ab01833..5f42579 100644 --- a/foosball/tracking/preprocess.py +++ b/foosball/tracking/preprocess.py @@ -148,7 +148,8 @@ def process(self, msg: Msg) -> Msg: except Exception as e: self.logger.error(f"Error in preprocess {e}") traceback.print_exc() - return Msg(msg=msg, info=info, data={ + # Not passing original msg due to performance impact (copying whole frames, etc.) + return Msg(info=info, data={ "Preprocessor": PreprocessorResult(original=self.iproc(frame), preprocessed=self.iproc(preprocessed), homography_matrix=self.homography_matrix, goals=self.goals) }) diff --git a/foosball/tracking/render.py b/foosball/tracking/render.py index fea5161..d0b47b2 100644 --- a/foosball/tracking/render.py +++ b/foosball/tracking/render.py @@ -132,7 +132,7 @@ def process(self, msg: Msg) -> Msg: r_score(f, score, text_scale=1, thickness=4) if self.infoVerbosity is not None: r_info(f, shape, filter_info(info, self.infoVerbosity), text_scale=0.5, thickness=1) - return Msg(msg=msg, info=None, data={ + return Msg(info=None, data={ "Renderer": RendererResult(frame=self.iproc(f)) }) except Exception as e: diff --git a/foosball/tracking/tracker.py b/foosball/tracking/tracker.py index 37ca493..3580f4e 100644 --- a/foosball/tracking/tracker.py +++ b/foosball/tracking/tracker.py @@ -102,8 +102,8 @@ def process(self, msg: Msg) -> Msg: except Exception as e: logger.error(f"Error in track {e}") traceback.print_exc() - # TODO: splitting into Preprocess and Tracker data maybe renders this obsolete + # Not passing original msg due to performance impact (copying whole frames, etc.) if not self.verbose: - return Msg(msg=msg, info=tracker_info, data={"Tracker": TrackerResult(frame=data.original, goals=goals, ball_track=ball_track, ball=ball)}) + return Msg(info=tracker_info, data={"Tracker": TrackerResult(frame=data.original, goals=goals, ball_track=ball_track, ball=ball)}) else: - return Msg(msg=msg, info=tracker_info, data={"Tracker": TrackerResult(frame=data.preprocessed, goals=goals, ball_track=ball_track, ball=ball)}) + return Msg(info=tracker_info, data={"Tracker": TrackerResult(frame=data.preprocessed, goals=goals, ball_track=ball_track, ball=ball)})