Skip to content

Instantly share code, notes, and snippets.

@doleron
Created March 29, 2023 18:24
Show Gist options
  • Save doleron/d2f19e48b2ccb6d530344e8fdf0e560d to your computer and use it in GitHub Desktop.
Save doleron/d2f19e48b2ccb6d530344e8fdf0e560d to your computer and use it in GitHub Desktop.
plt.figure(figsize=(12, 10))
test_list = list(test_ds.take(20).as_numpy_iterator())
image, labels = test_list[0]
for i in range(len(test_list)):
ax = plt.subplot(4, 5, i + 1)
image, labels = test_list[i]
predictions = model(image)
predicted_box = predictions[1][0] * input_size
predicted_box = tf.cast(predicted_box, tf.int32)
predicted_label = predictions[0][0]
image = image[0]
actual_label = labels[0][0]
actual_box = labels[1][0] * input_size
actual_box = tf.cast(actual_box, tf.int32)
image = image.astype("float") * 255.0
image = image.astype(np.uint8)
image_color = cv.cvtColor(image, cv.COLOR_GRAY2RGB)
color = (255, 0, 0)
# print box red if predicted and actual label do not match
if (predicted_label[0] > 0.5 and actual_label[0] > 0) or (predicted_label[0] < 0.5 and actual_label[0] == 0):
color = (0, 255, 0)
img_label = "unmasked"
if predicted_label[0] > 0.5:
img_label = "masked"
predicted_box_n = predicted_box.numpy()
cv.rectangle(image_color, predicted_box_n, color, 2)
cv.rectangle(image_color, actual_box.numpy(), (0, 0, 255), 2)
cv.rectangle(image_color, (predicted_box_n[0], predicted_box_n[1] + predicted_box_n[3] - 20), (predicted_box_n[0] + predicted_box_n[2], predicted_box_n[1] + predicted_box_n[3]), color, -1)
cv.putText(image_color, img_label, (predicted_box_n[0] + 5, predicted_box_n[1] + predicted_box_n[3] - 5), cv.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0))
IoU = intersection_over_union(predicted_box.numpy(), actual_box.numpy())
plt.title("IoU:" + format(IoU, '.4f'))
plt.imshow(image_color)
plt.axis("off")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment