Skip to content

Commit

Permalink
fix(goal-counting): Take frame timestamp instead of analyzer time for…
Browse files Browse the repository at this point in the history
… grace period comparison
  • Loading branch information
DarwinsBuddy committed Oct 6, 2024
1 parent b4b4b7b commit 980a3cc
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 11 deletions.
3 changes: 2 additions & 1 deletion foosball/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import collections
from dataclasses import dataclass
from datetime import datetime
from enum import Enum
from typing import Optional, Union

Expand Down Expand Up @@ -162,4 +163,4 @@ class PreprocessResult:
preprocessed: Optional[CPUFrame]
homography_matrix: Optional[np.ndarray] # 3x3 matrix used to warp the image and project points
goals: Optional[Goals]
info: Info
info: InfoLog
5 changes: 4 additions & 1 deletion foosball/pipe/BaseProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import multiprocessing
import traceback
import datetime as dt
from queue import Empty, Full

from foosball.pipe.Pipe import clear, SENTINEL
Expand All @@ -12,14 +13,16 @@
class Msg:
args: list[any]
kwargs: dict
timestamp: dt.datetime = dt.datetime.now()

def __init__(self, args=None, kwargs=None):
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


class BaseProcess(multiprocessing.Process):
Expand Down
5 changes: 3 additions & 2 deletions foosball/source/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import multiprocessing
import datetime as dt
from abc import abstractmethod
from multiprocessing import Queue
from queue import Full
Expand Down Expand Up @@ -53,8 +54,8 @@ def read_frame(self) -> (bool, Frame):
def output(self) -> multiprocessing.Queue:
return self.Q

def send_frame(self, frame) -> None:
msg = Msg(kwargs={'frame': frame}) if frame is not None else None
def send_frame(self, frame: Frame) -> None:
msg = Msg(timestamp=dt.datetime.now(), kwargs={'frame': frame}) if frame is not None else None
while True:
try:
# try to put it into the queue
Expand Down
6 changes: 2 additions & 4 deletions foosball/tracking/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ def process(self, msg: Msg) -> Msg:
info = track_result.info
try:
self.check_reset_score()
now = dt.datetime.now()
# TODO: define goal grace period in seconds of runtime not seconds in rendering!
now = msg.timestamp # take frame timestamp as now instead of dt.datetime.now (to prevent drift due to pushing/dragging pipeline)
no_track_sighting_in_grace_period = (now - self.last_track_sighting).total_seconds() >= self.goal_grace_period_sec if self.last_track_sighting is not None else None
if not self.is_track_empty(track):
# track is not empty, so we save our state and remove a potential goal (which was wrongly tracked)
Expand All @@ -84,8 +83,7 @@ def process(self, msg: Msg) -> Msg:
traceback.print_exc()
self.last_track = track
info.append(Info(verbosity=Verbosity.INFO, title="Score", value=self.score.to_string()))
return Msg(kwargs={"result": AnalyzeResult(score=self.score, ball=ball, goals=goals, frame=frame, info=info,
ball_track=track)})
return Msg(timestamp=msg.timestamp, kwargs={"result": AnalyzeResult(score=self.score, ball=ball, goals=goals, frame=frame, info=info, ball_track=track)})

def check_reset_score(self):
if self.score_reset.is_set():
Expand Down
2 changes: 1 addition & 1 deletion foosball/tracking/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def process(self, msg: Msg) -> Msg:
except Exception as e:
self.logger.error(f"Error in preprocess {e}")
traceback.print_exc()
return Msg(kwargs={"result": PreprocessResult(self.iproc(frame), self.iproc(preprocessed), self.homography_matrix, self.goals, info)})
return Msg(timestamp=msg.timestamp, kwargs={"result": PreprocessResult(self.iproc(frame), self.iproc(preprocessed), self.homography_matrix, self.goals, info)})

@staticmethod
def corners2pt(corners) -> [int, int]:
Expand Down
4 changes: 2 additions & 2 deletions foosball/tracking/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,6 @@ def process(self, msg: Msg) -> Msg:
logger.error(f"Error in track {e}")
traceback.print_exc()
if not self.verbose:
return Msg(kwargs={"result": TrackResult(preprocess_result.original, goals, ball_track, ball, info)})
return Msg(timestamp=msg.timestamp, kwargs={"result": TrackResult(preprocess_result.original, goals, ball_track, ball, info)})
else:
return Msg(kwargs={"result": TrackResult(preprocess_result.preprocessed, goals, ball_track, ball, info)})
return Msg(timestamp=msg.timestamp, kwargs={"result": TrackResult(preprocess_result.preprocessed, goals, ball_track, ball, info)})

0 comments on commit 980a3cc

Please sign in to comment.