-
Notifications
You must be signed in to change notification settings - Fork 138
/
detect.py
237 lines (189 loc) · 6.77 KB
/
detect.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# Lint as: python3
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Functions to work with a detection model."""
import collections
from pycoral.adapters import common
Object = collections.namedtuple('Object', ['id', 'score', 'bbox'])
"""Represents a detected object.
.. py:attribute:: id
The object's class id.
.. py:attribute:: score
The object's prediction score.
.. py:attribute:: bbox
A :obj:`BBox` object defining the object's location.
"""
class BBox(collections.namedtuple('BBox', ['xmin', 'ymin', 'xmax', 'ymax'])):
"""The bounding box for a detected object.
.. py:attribute:: xmin
X-axis start point
.. py:attribute:: ymin
Y-axis start point
.. py:attribute:: xmax
X-axis end point
.. py:attribute:: ymax
Y-axis end point
"""
__slots__ = ()
@property
def width(self):
"""The bounding box width."""
return self.xmax - self.xmin
@property
def height(self):
"""The bounding box height."""
return self.ymax - self.ymin
@property
def area(self):
"""The bound box area."""
return self.width * self.height
@property
def valid(self):
"""Indicates whether bounding box is valid or not (boolean).
A valid bounding box has xmin <= xmax and ymin <= ymax (equivalent
to width >= 0 and height >= 0).
"""
return self.width >= 0 and self.height >= 0
def scale(self, sx, sy):
"""Scales the bounding box.
Args:
sx (float): Scale factor for the x-axis.
sy (float): Scale factor for the y-axis.
Returns:
A :obj:`BBox` object with the rescaled dimensions.
"""
return BBox(
xmin=sx * self.xmin,
ymin=sy * self.ymin,
xmax=sx * self.xmax,
ymax=sy * self.ymax)
def translate(self, dx, dy):
"""Translates the bounding box position.
Args:
dx (int): Number of pixels to move the box on the x-axis.
dy (int): Number of pixels to move the box on the y-axis.
Returns:
A :obj:`BBox` object at the new position.
"""
return BBox(
xmin=dx + self.xmin,
ymin=dy + self.ymin,
xmax=dx + self.xmax,
ymax=dy + self.ymax)
def map(self, f):
"""Maps all box coordinates to a new position using a given function.
Args:
f: A function that takes a single coordinate and returns a new one.
Returns:
A :obj:`BBox` with the new coordinates.
"""
return BBox(
xmin=f(self.xmin),
ymin=f(self.ymin),
xmax=f(self.xmax),
ymax=f(self.ymax))
@staticmethod
def intersect(a, b):
"""Gets a box representing the intersection between two boxes.
Args:
a: :obj:`BBox` A.
b: :obj:`BBox` B.
Returns:
A :obj:`BBox` representing the area where the two boxes intersect
(may be an invalid box, check with :func:`valid`).
"""
return BBox(
xmin=max(a.xmin, b.xmin),
ymin=max(a.ymin, b.ymin),
xmax=min(a.xmax, b.xmax),
ymax=min(a.ymax, b.ymax))
@staticmethod
def union(a, b):
"""Gets a box representing the union of two boxes.
Args:
a: :obj:`BBox` A.
b: :obj:`BBox` B.
Returns:
A :obj:`BBox` representing the unified area of the two boxes
(always a valid box).
"""
return BBox(
xmin=min(a.xmin, b.xmin),
ymin=min(a.ymin, b.ymin),
xmax=max(a.xmax, b.xmax),
ymax=max(a.ymax, b.ymax))
@staticmethod
def iou(a, b):
"""Gets the intersection-over-union value for two boxes.
Args:
a: :obj:`BBox` A.
b: :obj:`BBox` B.
Returns:
The intersection-over-union value: 1.0 meaning the two boxes are
perfectly aligned, 0 if not overlapping at all (invalid intersection).
"""
intersection = BBox.intersect(a, b)
if not intersection.valid:
return 0.0
area = intersection.area
return area / (a.area + b.area - area)
def get_objects(interpreter,
score_threshold=-float('inf'),
image_scale=(1.0, 1.0)):
"""Gets results from a detection model as a list of detected objects.
Args:
interpreter: The ``tf.lite.Interpreter`` to query for results.
score_threshold (float): The score threshold for results. All returned
results have a score greater-than-or-equal-to this value.
image_scale (float, float): Scaling factor to apply to the bounding boxes as
(x-scale-factor, y-scale-factor), where each factor is from 0 to 1.0.
Returns:
A list of :obj:`Object` objects, which each contains the detected object's
id, score, and bounding box as :obj:`BBox`.
"""
# If a model has signature, we use the signature output tensor names to parse
# the results. Otherwise, we parse the results based on some assumption of the
# output tensor order and size.
# pylint: disable=protected-access
signature_list = interpreter._get_full_signature_list()
# pylint: enable=protected-access
if signature_list:
if len(signature_list) > 1:
raise ValueError('Only support model with one signature.')
signature = signature_list[next(iter(signature_list))]
count = int(interpreter.tensor(signature['outputs']['output_0'])()[0])
scores = interpreter.tensor(signature['outputs']['output_1'])()[0]
class_ids = interpreter.tensor(signature['outputs']['output_2'])()[0]
boxes = interpreter.tensor(signature['outputs']['output_3'])()[0]
elif common.output_tensor(interpreter, 3).size == 1:
boxes = common.output_tensor(interpreter, 0)[0]
class_ids = common.output_tensor(interpreter, 1)[0]
scores = common.output_tensor(interpreter, 2)[0]
count = int(common.output_tensor(interpreter, 3)[0])
else:
scores = common.output_tensor(interpreter, 0)[0]
boxes = common.output_tensor(interpreter, 1)[0]
count = (int)(common.output_tensor(interpreter, 2)[0])
class_ids = common.output_tensor(interpreter, 3)[0]
width, height = common.input_size(interpreter)
image_scale_x, image_scale_y = image_scale
sx, sy = width / image_scale_x, height / image_scale_y
def make(i):
ymin, xmin, ymax, xmax = boxes[i]
return Object(
id=int(class_ids[i]),
score=float(scores[i]),
bbox=BBox(xmin=xmin, ymin=ymin, xmax=xmax,
ymax=ymax).scale(sx, sy).map(int))
return [make(i) for i in range(len(scores)) if scores[i] >= score_threshold]