-
Notifications
You must be signed in to change notification settings - Fork 0
/
predictions.py
52 lines (45 loc) · 1.54 KB
/
predictions.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
# Loads best model from freeze for output predictions API.
import tensorflow as tf
import keras_cv
from keras_cv import visualization
from bounding_boxes import render_to_list
bbxf = "xyxy"
def get_predictions(
image, model, confidence, iou, class_mapping, render_img=False, rescale_boxes=False
):
print("Confidence: ", confidence)
print("IOU: ", iou)
model.prediction_decoder = keras_cv.layers.NonMaxSuppression(
bounding_box_format=bbxf,
from_logits=True,
iou_threshold=iou,
confidence_threshold=confidence,
)
predictions = model.predict(image)
if not predictions:
print("No predictions")
return
y_pred = predictions.copy()
# convert to numpy arrays
if isinstance(y_pred["boxes"], tf.Tensor):
y_pred["boxes"] = y_pred["boxes"].numpy()
if isinstance(y_pred["classes"], tf.Tensor):
y_pred["classes"] = y_pred["classes"].numpy()
if render_img is True:
images_pred = visualization.draw_bounding_boxes(
image,
y_pred,
bounding_box_format=bbxf,
color=(255, 255, 59),
class_mapping=class_mapping,
font_scale=1,
)
image = images_pred[0]
bboxes, labels, labels_encoded = render_to_list(y_pred, class_mapping)
if rescale_boxes:
for i, box in enumerate(bboxes):
bboxes[i] = [box[0] * 640, box[1] * 640, box[2] * 640, box[3] * 640]
print(bboxes)
print(labels)
print(labels_encoded)
return image, bboxes, labels, labels_encoded