Skip to content

Commit

Permalink
refactoring(data): Refactoring inter-process data passing WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
DarwinsBuddy committed Oct 27, 2024
1 parent a19688f commit facf30f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 36 deletions.
13 changes: 6 additions & 7 deletions foosball/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,21 @@ def to_string(self) -> str:
return f'{self.title} {self.value}'


@dataclass
class InfoLog:
infos: [Info]
def __init__(self, infos=None):
self.infos: [Info] = [] if infos is None else infos

def __iter__(self):
return (i for i in self.infos)

def append(self, info: Info):
self.infos.append(info)

def concat(self, info_log):
self.infos = self.infos + info_log.infos
def extend(self, info_log):
self.infos.extend(info_log.infos)

def filter(self, infoVerbosity: Verbosity = Verbosity.TRACE):
return InfoLog(
infos=[i for i in self.infos if infoVerbosity is not None and infoVerbosity.value <= i.verbosity.value])
def filter(self, infoVerbosity: Verbosity = Verbosity.TRACE) -> [Info]:
return [i for i in self.infos if infoVerbosity is not None and infoVerbosity.value <= i.verbosity.value]

def to_string(self):
return " - ".join([i.to_string() for i in self.infos])
Expand Down
38 changes: 21 additions & 17 deletions foosball/pipe/BaseProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,33 @@
# TODO: check why merging into one Msg is having a huge impact on FPS
@dataclass
class Msg:
args: list[any]
kwargs: dict
info: InfoLog = None
data: dict
info: InfoLog
timestamp: dt.datetime = dt.datetime.now()

def add(self, name: str, data: any, info=InfoLog([])):
self.kwargs[name] = data
def __init__(self, data=None, info=InfoLog(), timestamp=dt.datetime.now()):
if data is None:
data = dict()
self.data = data
self.info = info
self.timestamp = timestamp

def __getitem__(self, key):
return self.data[key]

def __setitem__(self, key, value):
self.data[key] = value

def add(self, name: str, data: any, info: InfoLog = None):
self.data[name] = data
info_log = InfoLog() if info is None else info
if self.info is not None:
self.info.concat(info)
self.info.extend(info_log)
else:
self.info = InfoLog([])
self.info = InfoLog()

def remove(self, name) -> any:
return self.kwargs.pop(name)

def __init__(self, args=None, kwargs=None, timestamp=dt.datetime.now()):
if kwargs is None:
kwargs = dict()
if args is None:
args = list()
self.kwargs = kwargs
self.args = args
self.timestamp = timestamp
return self.data.pop(name)


class BaseProcess(multiprocessing.Process):
Expand Down
2 changes: 1 addition & 1 deletion foosball/source/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def output(self) -> multiprocessing.Queue:
return self.Q

def send_frame(self, frame: Frame) -> None:
msg = Msg(timestamp=dt.datetime.now(), kwargs={'frame': frame}) if frame is not None else None
msg = Msg(timestamp=dt.datetime.now(), data={'frame': frame}) if frame is not None else None
while True:
try:
# try to put it into the queue
Expand Down
2 changes: 1 addition & 1 deletion foosball/tracking/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def step_frame():
self.logger.debug("received SENTINEL")
break
self.fps.update()
result = msg.kwargs.get('Renderer', None)
result = msg.data.get('Renderer', None)
frame = result.frame if result is not None else None
info: InfoLog = msg.info
self.fps.stop()
Expand Down
6 changes: 3 additions & 3 deletions foosball/tracking/analyzer/ScoreAnalyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ def goal_shot(self, goals: Goals, track: Track) -> Optional[Team]:
return None

def analyze(self, msg: Msg, timestamp: dt.datetime) -> [ScoreAnalyzerResult, InfoLog]:
goals = msg.kwargs["Tracker"].goals
track = msg.kwargs["Tracker"].ball_track
info = InfoLog([])
goals = msg.data["Tracker"].goals
track = msg.data["Tracker"].ball_track
info = InfoLog()
team_scored = None
try:
self.check_reset_score()
Expand Down
4 changes: 2 additions & 2 deletions foosball/tracking/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ def mask_frame(self, frame: Frame) -> Frame:
return frame if self.mask is None else cv2.bitwise_and(frame, frame, mask=self.mask)

def process(self, msg: Msg) -> Msg:
frame = msg.kwargs['frame']
frame = msg.data['frame']
frame = self.proc(frame)
frame = scale(frame, self.dims, ScaleDirection.DOWN)
preprocessed = frame
info: InfoLog = InfoLog(infos=[])
info: InfoLog = InfoLog()
try:
if self.goals_calibration:
try:
Expand Down
6 changes: 3 additions & 3 deletions foosball/tracking/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ def __init__(self, dims: FrameDimensions, headless=False, useGPU: bool = False,
[self.proc, self.iproc] = generate_processor_switches(useGPU)

def process(self, msg: Msg) -> Msg:
score_analyzer = msg.kwargs["ScoreAnalyzer"]
tracker = msg.kwargs["Tracker"]
score_analyzer = msg.data["ScoreAnalyzer"]
tracker = msg.data["Tracker"]
info: InfoLog = msg.info
try:
if not self.headless:
Expand All @@ -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, info.filter(self.infoVerbosity), text_scale=0.5, thickness=1)
msg.add("Renderer", RendererResult(frame=self.iproc(f)), info=InfoLog([]))
msg.add("Renderer", RendererResult(frame=self.iproc(f)), info=InfoLog())
except Exception as e:
logger.error(f"Error in renderer {e}")
traceback.print_exc()
Expand Down
4 changes: 2 additions & 2 deletions foosball/tracking/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ def config_input(self, config: BallColorConfig) -> None:
self.bounds_in.put_nowait(config)

def process(self, msg: Msg) -> Msg:
preprocess_result = msg.kwargs['Preprocessor']
preprocess_result = msg.data['Preprocessor']
data = preprocess_result
ball = None
goals = data.goals
ball_track = None
tracker_info = InfoLog([])
tracker_info = InfoLog()
try:
if not self.off:
if self.calibration:
Expand Down

0 comments on commit facf30f

Please sign in to comment.