generated from fraukecharms/fastapi-rekognition-compvision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_sagemaker.py
156 lines (115 loc) · 4.48 KB
/
helper_sagemaker.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
from typing import Tuple, List
from PIL import ImageDraw, ImageFont, Image
import boto3
import json
import numpy as np
def parse_response(query_response: str) -> Tuple[list, list, list]:
"""extract bounding box coordinates, class labels, confidence values from sagemaker
response
Args:
query_response (str): response string
Returns:
tuple[list, list, list]: bounding box coordinates, class labels, confidence
values
"""
model_predictions = json.loads(query_response)
normalized_boxes, classes, scores, labels = (
model_predictions["normalized_boxes"],
model_predictions["classes"],
model_predictions["scores"],
model_predictions["labels"],
)
# Substitute the class index with the class name
class_names = [labels[int(idx)] for idx in classes]
return normalized_boxes, class_names, scores
def query_endpoint(
endpoint_name: str, input_img_rb: bytearray
) -> Tuple[list, list, list]:
"""send image to sagemaker endpoint
Args:
endpoint_name (str): endpoint name
input_img_rb (bytearray): image
Returns:
Tuple[list, list, list]: bounding box coordinates, class labels, confidence
values
"""
client = boto3.client("sagemaker-runtime")
# The MIME type of the input data in the request body.
content_type = "application/x-image"
# The desired MIME type of the inference in the response.
accept = "application/json;verbose;n_predictions=3"
# Payload for inference.
payload = input_img_rb
response = client.invoke_endpoint(
EndpointName=endpoint_name,
ContentType=content_type,
Accept=accept,
Body=payload,
)
response_readable = response["Body"].read().decode("utf-8")
normalized_boxes, class_names, scores = parse_response(response_readable)
return normalized_boxes, class_names, scores
def draw_all_boxes(
image: Image,
boxes: List[List[float]],
labels: List[str],
conf: List[float] = None,
threshold=0.9,
) -> Image:
"""draw bounding boxes on PIL image
Args:
image (Image): image to draw bounding boxes on
boxes (List[List[float]]): list of bounding box coordinates
labels (List[str]): list of object class labels
conf (List[float], optional): list of confidence values for each detection.
Defaults to None.
threshold (float, optional): optional confidence threshold. Defaults to 0.9.
Returns:
Image: PIL Image with boxes
"""
img_width, img_height = image.size
draw = ImageDraw.Draw(image, mode="RGBA")
# set bounding box linewidth based on image size
# for object bounding box and text bounding box
linewidth = max(int((img_width + img_height) // 250), 2)
linewidth_textbox = max(int(linewidth // 3), 1)
textsize = linewidth * 4
font = ImageFont.truetype("font/OpenSans-Regular.ttf", textsize)
# margins for text bounding box
shift_const = 3
shift = np.array([-1, -1, 1, 1]) * shift_const * linewidth_textbox
# scaling factors for rescaling normalized boxes
scale = np.array([img_width, img_height, img_width, img_height])
if conf:
inds = [i for i in range(len(conf)) if conf[i] >= threshold]
boxes = [boxes[i] for i in inds]
labels = [labels[i] for i in inds]
for i in range(len(boxes)):
# draw object bounding box
box = np.array(boxes[i])
left, top, right, bottom = box * scale
points = [(left, top), (right, bottom)]
draw.rectangle(points, outline="#c73286", width=linewidth)
label = labels[i]
textanchor = (left + 2 * linewidth, top + 2 * linewidth)
# draw label bounding box with added margins
textbb = draw.textbbox(textanchor, label, font=font, anchor="lt")
spaceybox = [sum(x) for x in zip(textbb, shift)]
draw.rectangle(spaceybox, width=linewidth_textbox, fill=(255, 255, 255, 128))
# draw label text
draw.text(textanchor, label, font=font, anchor="lt", fill = "#000000")
return image
def list_endpoints() -> List[str]:
"""list live Sagemaker endpoints
Returns:
List[str]: list of endpoint names
"""
client = boto3.client("sagemaker")
response = client.list_endpoints()
endpoints = response["Endpoints"]
n = len(endpoints)
endpointnames = []
for i in range(n):
name = endpoints[i]["EndpointName"]
endpointnames.append(name)
return endpointnames