forked from rh-aiservices-bu/object-detection-rest
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprediction.py
66 lines (52 loc) · 1.82 KB
/
prediction.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
from os import environ
from pprint import pformat
from yaml import load, SafeLoader
from classes import default_class_labels
from object_detection import detect_objects
from preprocessing import preprocess_encoded_image
prediction_url = environ.get('PREDICTION_URL')
raw_class_labels = environ.get('CLASS_LABELS')
class_labels = (
load(raw_class_labels, Loader=SafeLoader)
if raw_class_labels else default_class_labels
)
print(f'Using class labels: {pformat(class_labels)}')
def predict(body):
print('Received prediction request.')
base64encoded_image = body.get('image')
transformed_image, scaling, padding = preprocess_encoded_image(
base64encoded_image
)
detections = detect_objects(
transformed_image, prediction_url, len(class_labels)
)
mapped_detections = map_(detections, class_labels, scaling, padding)
payload = {'detections': mapped_detections}
print(f'Prediction complete. Returning payload: {pformat(payload)}')
return payload
def map_(objects, class_labels, scaling, padding, edge_length=640):
cleaned = []
for object_ in objects:
bbox = object_[:4].tolist()
x0, y0, x1, y1 = [coord / scaling for coord in bbox]
x0 -= padding[0]
y0 -= padding[1]
x1 -= padding[0]
y1 -= padding[1]
x0 = max(x0, 0)
y0 = max(y0, 0)
x1 = min(x1, edge_length)
y1 = min(y1, edge_length)
d = {
'box': {
'yMin': y0 / edge_length,
'xMin': x0 / edge_length,
'yMax': y1 / edge_length,
'xMax': x1 / edge_length,
},
'class': class_labels[int(object_[5])],
'label': class_labels[int(object_[5])],
'score': float(object_[4]),
}
cleaned.append(d)
return cleaned