Skip to content

Commit

Permalink
refactoring(data): Refactoring inter-process data passing
Browse files Browse the repository at this point in the history
Remove msg passthrough in preprocess/tracker (too expensive)

Fix logging in hooks
  • Loading branch information
DarwinsBuddy committed Nov 1, 2024
1 parent aa05a34 commit 962380c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
15 changes: 6 additions & 9 deletions foosball/hooks/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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'):
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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'")
3 changes: 2 additions & 1 deletion foosball/tracking/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down
2 changes: 1 addition & 1 deletion foosball/tracking/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions foosball/tracking/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)})

0 comments on commit 962380c

Please sign in to comment.