in detection-function/src/run.py [0:0]
def extract_bounding_boxes(image):
response = sm.invoke_endpoint(
EndpointName=SAGEMAKER_SSD_ENDPOINT,
Body=image,
ContentType='image/jpeg'
)
result = json.loads(response['Body'].read())
# list for bounding boxes
bounding_boxes = []
# Remove bounding boxes above the confidence ssd treshold
for prediction in result["prediction"]:
if prediction[1] >= CONFIDENCE_THRESHOLD_SSD:
bounding_boxes.append(prediction)
print("Bounding box quantity before NMS: {}".format(len(bounding_boxes)))
i = 0
while i < len(bounding_boxes):
boundbox = bounding_boxes[i]
len_bounding_boxes = len(bounding_boxes)
i+=1
if boundbox[5] - boundbox[3] < SMALL_BOX_FILTER_SCORE:
print("Size Removing, Y small: " + str(boundbox[5] - boundbox[3]))
bounding_boxes.remove(boundbox)
i=0
continue
if boundbox[4] - boundbox[2] < SMALL_BOX_FILTER_SCORE:
print("Size Removing, x small: " + str(boundbox[4] - boundbox[2]))
bounding_boxes.remove(boundbox)
i=0
continue
count = 0
len_bounding_boxes = len(bounding_boxes)
while count < len_bounding_boxes:
if boundbox != bounding_boxes[count]:
check = get_iou(boundbox[2:],bounding_boxes[count][2:])
if check >= CONFIDENCE_THRESHOLD_IOU:
# Best Score
if boundbox[1] < bounding_boxes[count][1]:
print("Removing: " + str(boundbox[1]))
bounding_boxes.remove(boundbox)
else:
print("Removing: " + str(bounding_boxes[count][1]))
bounding_boxes.remove(bounding_boxes[count])
print("Item Deleted: {}".format(count))
len_bounding_boxes -= 1
# Raise Counter
count += 1
print("Bounding box quantity after NMS: {}".format(len(bounding_boxes)))
items = []
for p in bounding_boxes:
(klass, score, x0, y0, x1, y1) = p
if score < CONFIDENCE_THRESHOLD_SSD:
continue
photo = Image.open(io.BytesIO(image))
shape = OBJECT_CATEGORIES[int(klass)]
items.append({
'product_id': shape,
'object_detection_score': score,
'position': get_object_boundary_box(photo, (x0, y0, x1, y1)),
})
print('extract_bounding_boxes: {}'.format(items))
return items