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
import json
from os import makedirs
from os.path import exists, join
from bunch import bunchify
from datetime import datetime
class JsonMeta(object):
HOURS = 4
MINUTES = 59
SECONDS = 59
import unittest
import sys
from datetime import datetime
from os import listdir
from time import sleep
from tempfile import mkdtemp
from unittest.case import TestCase
# from json_parser import JsonParser
def output(self):
output = {'video_details': self.video_details}
result = list(self.frames.values())
# Every bbox in each frame has to have `top_k_labels` number of labels otherwise error raises.
for frame in result:
for bbox in frame.bboxes:
if not bbox.labels_full(self.top_k_labels):
raise ValueError(
"labels in frame_id: {}, bbox_id: {} is not fulled before outputting.".format(frame.frame_id,
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)