-
Notifications
You must be signed in to change notification settings - Fork 4
/
objtect.py
170 lines (114 loc) · 4 KB
/
objtect.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import cv2
import numpy as np
class ObjectDetectorInterface:
def infer(self, image, types=None):
pass
def start(self):
pass
def stop(self):
pass
def getInPipe(self):
pass
def getOutPipe(self):
pass
class InstanceType:
__type = None
__detectorId = None
def __init__(self, type, detectorId=None):
self.__type = type
self.__detectorId = detectorId
def getType(self):
return self.__type
def getDetectorId(self):
return self.__detectorId
class InferenceBounds:
__y_tl = None
__x_tl = None
__y_br = None
__x_br = None
def __init__(self, x_tl, y_tl, x_br, y_br):
self.__y_tl, self.__x_tl, self.__y_br, self.__x_br = y_tl, x_tl, y_br, x_br
def getTop(self):
return self.__y_tl
def getBottom(self):
return self.__y_br
def getLeft(self):
return self.__x_tl
def getRight(self):
return self.__x_br
def getBoundingBox(self):
return [[self.__x_tl, self.__y_tl],
[self.__x_tl, self.__y_br],
[self.__x_br, self.__y_br],
[self.__x_br, self.__y_tl]]
def getBoundingCoordinates(self):
return self.__y_tl , self.__x_tl, self.__y_br, self.__x_br, self.__y_br
class Inference:
__image = None
__decisionInstances = None
__annotatedImage = None
__crowdCount = None
def __init__(self, image, decisionInstances, annotatedImage, crowdCount):
self.__image = image
self.__decisionInstances= decisionInstances
self.__annotatedImage = annotatedImage
self.__crowdCount = crowdCount
def getAnnotatedImage(self):
return self.__annotatedImage
def getImage(self):
return self.__image.copy()
def getDecisionInstances(self):
return self.__decisionInstances
def getCrowdCount(self):
return self.__crowdCount
class DecisionInstance:
__objectClass = None
__boundingPath = None
__confidenceScore = None
__decisionMask = None
def __init__(self, decisionClass, confidenceScore, boundingPath, decisionMask=None):
self.__objectClass = decisionClass
self.__boundingPath = boundingPath
self.__confidenceScore = confidenceScore
self.__decisionMask = decisionMask
def getClass(self):
return self.__objectClass
def getBox(self):
return self.__boundingPath
def getScore(self):
return self.__confidenceScore
class ObjectDetector:
__image = None
__detector = None
__inferences = None
def __to_np_array(self, image):
im_width, im_height, _ = image.shape
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
def __init__(self, image, detector):
self.__image = image.copy()
self.__detector = detector
self.__inferences = detector.infer(self.__image.copy())
def getInference(self, index):
return self.__inferences[index]
def length(self):
return len(self.__inferences)
def getInstanceImage(self, index, types=None, threshold = None):
if threshold is None:
threshold = 0.3
inference = self.getInference(index)
if types is None or (inference.getClass().getType() in types and inference.getScore() > threshold):
bbox = inference.getBox()
return self.__image[int(bbox.getTop()):int(bbox.getBottom()), int(bbox.getLeft()):int(bbox.getRight())]
else:
return None
def getAnnotatedImage(self, types=None, threshold=None):
if threshold is None:
threshold = 0.3
img = self.__image.copy()
for i in range(self.length()):
inference = self.getInference(i)
if inference.getClass().getType()in types and inference.getScore() > threshold:
bbox = inference.getBox()
img = cv2.polylines(img, [np.int32(bbox.getBoundingBox())], 1, (0, 255, 0), 3)
return img