Skip to content

Instantly share code, notes, and snippets.

@masouduut94
Created July 10, 2020 07:49
Show Gist options
  • Save masouduut94/5e496e7d3566f18fd7968fb89bb86516 to your computer and use it in GitHub Desktop.
Save masouduut94/5e496e7d3566f18fd7968fb89bb86516 to your computer and use it in GitHub Desktop.
class BaseJsonParser(object):
"""
This is the base class that returns __dict__ of its own
it also returns the dicts of objects in the attributes that are list instances
"""
def dic(self):
# returns dicts of objects
out = {}
for k, v in self.__dict__.items():
if hasattr(v, 'dic'):
out[k] = v.dic()
elif isinstance(v, list):
out[k] = self.list(v)
else:
out[k] = v
return out
@staticmethod
def list(values):
# applies the dic method on items in the list
return [v.dic() if hasattr(v, 'dic') else v for v in values]
class Label(BaseJsonParser):
"""
For each bounding box there are various categories with confidences. Label class keeps track of that information.
"""
def __init__(self, category: str, confidence: float):
self.category = category
self.confidence = confidence
class Bbox(BaseJsonParser):
"""
This class keeps the information of each bounding box in the frame.
"""
def __init__(self, bbox_id, top, left, width, height):
self.labels = []
self.bbox_id = bbox_id
self.top = top
self.left = left
self.width = width
self.height = height
class Frame(BaseJsonParser):
def __init__(self, frame_id: int):
self.frame_id = frame_id
self.bboxes = []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment