-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentities.py
96 lines (80 loc) · 2.53 KB
/
entities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
structures to hold domain objects
"""
# pylint: disable=C0103,R0903
from typing import List
import datetime
import json
import numpy as np
class EntityTag:
"""
Tag class for EntitiesJsonSerializer
"""
class EntitiesJsonSerializer(json.JSONEncoder):
"""
Serialization for Entity classes (derived from EntityTag)
but mostly for shitty python datetime support
"""
def default(self, o):
if isinstance(o, (float)):
return round(o, 2)
if isinstance(o, (np.float32)):
return round(float(o), 2)
if isinstance(o, EntityTag):
return o.__dict__
if isinstance(o, (datetime.date, datetime.datetime)):
return o.isoformat()
return json.JSONEncoder.default(self, o)
class CapturedFrame(EntityTag):
"""
Image with its source and timestamp
"""
def __init__(self, videostreamid, timestamp, img):
self.vsid = videostreamid
self.timestamp = timestamp
self.img = img
def __str__(self):
return f"[{self.vsid}@{self.timestamp}] {self.img}"
class BoundingBox(EntityTag):
"""
Detected object bounding box
"""
def __init__(self, left, top, right, bottom):
self.l = left
self.t = top
self.r = right
self.b = bottom
def __str__(self):
return f"({self.t},{self.l}):({self.r},{self.b})"
class DetectedObject(EntityTag):
"""
Detected object structure (class, probability, bounding box)
"""
def __init__(self, clazz, probability, bbox: BoundingBox):
self.c = clazz
self.p = probability
self.bbox = bbox
def __str__(self):
return f"c:{self.c},p:{self.p},bbox:{self.bbox}"
class DetectedObjectSet(EntityTag):
"""
List of detected objects with their source and timestamp
"""
def __init__(self, videostreamid, timestamp, detectedobjs: List[DetectedObject]):
self.vsid = videostreamid
self.ts = timestamp
self.dobjs = detectedobjs
def __str__(self):
dobjs = '; '.join(str(x) for x in self.dobjs)
return f"[{self.vsid}@{self.ts}] [{dobjs}]"
class DetectedObjectsFrame(EntityTag):
"""
List of all detected objects from all sources for a given timestamp
"""
def __init__(self, srvid, timestamp, detectedsets: List[DetectedObjectSet]):
self.srvid = srvid
self.ts = timestamp
self.dsets = detectedsets
def __str__(self):
dsets = '; '.join(str(x) for x in self.dsets)
return f"[{self.srvid}@{self.ts}] [{dsets}]"