Skip to content

Instantly share code, notes, and snippets.

View masouduut94's full-sized avatar
πŸ‘¨β€πŸ‘©β€πŸ‘¦β€πŸ‘¦
Family comes before Work.

Masoud Masoumi Moghadam masouduut94

πŸ‘¨β€πŸ‘©β€πŸ‘¦β€πŸ‘¦
Family comes before Work.
View GitHub Profile
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
def add_label_to_bbox(self, frame_id: int, bbox_id: int, category: str, confidence: float):
bbox = self.find_bbox(frame_id, bbox_id)
if not bbox.labels_full(self.top_k_labels):
bbox.add_label(category, confidence)
else:
raise ValueError("labels in frame_id: {}, bbox_id: {} is fulled".format(frame_id, bbox_id))
def find_bbox(self, frame_id: int, bbox_id: int):
if not self.bbox_exists(frame_id, bbox_id):
raise ValueError("frame with id: {} does not contain bbox with id: {}".format(frame_id, bbox_id))
bboxes = {bbox.bbox_id: bbox for bbox in self.frames[frame_id].bboxes}
return bboxes.get(bbox_id)
def test_add_label_to_bbox(self):
# test if label exists in bbox
frame_one_id = 0
bbox_id = 1
bbox_xywh = (10, 20, 30, 40)
self.json_parser.add_frame(frame_one_id)
self.json_parser.add_bbox_to_frame(frame_one_id, bbox_id, *bbox_xywh)
categories = ['car', 'truck', 'bus']
confidences = [0.47, 0.85, 1]
def add_bbox(self, bbox_id: int, top: int, left: int, width: int, height: int):
bboxes_ids = [bbox.bbox_id for bbox in self.bboxes]
if bbox_id not in bboxes_ids:
self.bboxes.append(Bbox(bbox_id, top, left, width, height))
else:
raise ValueError("Frame with id: {} already has a Bbox with id: {}".format(self.frame_id, bbox_id))
def bbox_exists(self, frame_id, bbox_id):
bboxes = []
if self.frame_exists(frame_id=frame_id):
bboxes = [bbox.bbox_id for bbox in self.frames[frame_id].bboxes]
return bbox_id in bboxes
def add_bbox_to_frame(self, frame_id: int, bbox_id: int, top: int, left: int, width: int, height: int):
if self.frame_exists(frame_id):
frame = self.frames[frame_id]
if not self.bbox_exists(frame_id, bbox_id):
def test_add_bbox_to_frame(self):
# test if bbox is added to its own frame
frame_one_id = 0
bbox_id = 1
bbox_xywh = (10, 20, 30, 40)
top_k = 0
self.json_parser.set_top_k(top_k)
self.json_parser.add_frame(frame_one_id)
self.json_parser.add_bbox_to_frame(frame_one_id, bbox_id, *bbox_xywh)
def output(self):
output = {'video_details': self.video_details}
result = list(self.frames.values())
output['frames'] = [item.dic() for item in result]
return output
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():
def set_top_k(self, value):
self.top_k_labels = value
def frame_exists(self, frame_id: int):
return frame_id in self.frames.keys()
def add_frame(self, frame_id: int):
# Use this function to add frames with index.
if not self.frame_exists(frame_id):
self.frames[frame_id] = Frame(frame_id)