From 6b83118b91b40c4bcba7bb4916ecd71ffa3ee84e Mon Sep 17 00:00:00 2001 From: Rob Hallam <0504004h@student.gla.ac.uk> Date: Tue, 13 Aug 2024 11:20:39 +0100 Subject: [PATCH] feat: add to_json() to Source, Interval & Feature This lets us serialise properly --- pipeline/utils.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pipeline/utils.py b/pipeline/utils.py index ee4ec19..944a351 100644 --- a/pipeline/utils.py +++ b/pipeline/utils.py @@ -92,6 +92,13 @@ class Source(): self._duration = 0.0 or float(subprocess.check_output(["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", file])) return self._duration + def to_json(self): + """Return a dict representation of the source for JSON encoding + + @see video_producers.py:PipelineJSONEncoder + """ + return dict(source=self.source, path=self.path, provider=self.provider) + class Interval(): @@ -113,6 +120,7 @@ class Interval(): # TODO: have default duration for intervals set by config # TODO: consider if we want to permit adjusting intervals (eg, start time, end time, duration) [probably yes] # NOTE: if we have more ways of defining, we could consider multipledispatch? + # TODO: consider if we want to keep a reference to the media file (source) in the interval DEFAULT_DURATION = 5 # seconds DEFAUT_PRECISION = 3 # decimal places @@ -171,6 +179,13 @@ class Interval(): return self.end < other.end return self.start < other.start + def to_json(self): + """Return a dict representation of the interval for JSON encoding + + @see video_producers.py:PipelineJSONEncoder + """ + return dict(start=self.start, end=self.end, duration=self.duration) + # -------------------------------------------------------------- # TODO: handle bad cases, eg negative duration, start > end, etc # -------------------------------------------------------------- @@ -261,3 +276,10 @@ class Feature(): return self.score < other.score return self.source < other.source return self.interval < other.interval + def to_json(self): + """Return a dict representation of the feature for JSON encoding + + @see video_producers.py:PipelineJSONEncoder + """ + return dict(interval=self.interval.to_json(), source=self.source.to_json(), + feature_extractor=self.feature_extractor, score=self.score)