|
1 | 1 | import pytest |
2 | 2 |
|
| 3 | +from PIL import Image |
3 | 4 | import numpy as np |
4 | 5 |
|
5 | | -from unstructured_inference.visualize import draw_bounding_boxes |
| 6 | +from unstructured_inference.inference.elements import Rectangle |
| 7 | +from unstructured_inference.visualize import draw_bbox, draw_yolox_bounding_boxes |
6 | 8 |
|
7 | 9 |
|
8 | 10 | @pytest.mark.parametrize( |
|
20 | 22 | def test_visualize(y_coords, x_coords): |
21 | 23 | test_image = np.ones((100, 100, 3)) |
22 | 24 | boxes = [[1, 10, 50, 40]] |
23 | | - annotated_img = draw_bounding_boxes( |
| 25 | + annotated_img = draw_yolox_bounding_boxes( |
24 | 26 | test_image, boxes, scores=[0.8], cls_ids=[0], class_names=["thing"] |
25 | 27 | ) |
26 | 28 | assert annotated_img[y_coords, x_coords, 0].sum() == 0.0 |
| 29 | + |
| 30 | + |
| 31 | +def test_draw_bbox(): |
| 32 | + test_image_arr = np.ones((100, 100, 3), dtype="uint8") |
| 33 | + image = Image.fromarray(test_image_arr) |
| 34 | + x1, y1, x2, y2 = (1, 10, 7, 11) |
| 35 | + rect = Rectangle(x1, y1, x2, y2) |
| 36 | + annotated_image = draw_bbox(image=image, rect=rect) |
| 37 | + annotated_array = np.array(annotated_image) |
| 38 | + # Make sure the pixels on the edge of the box are red |
| 39 | + for i, expected in zip(range(3), [255, 0, 0]): |
| 40 | + assert all(annotated_array[y1, x1:x2, i] == expected) |
| 41 | + assert all(annotated_array[y2, x1:x2, i] == expected) |
| 42 | + assert all(annotated_array[y1:y2, x1, i] == expected) |
| 43 | + assert all(annotated_array[y1:y2, x2, i] == expected) |
| 44 | + # Make sure almost all the pixels are not changed |
| 45 | + assert ((annotated_array[:, :, 0] == 1).mean()) > 0.995 |
| 46 | + assert ((annotated_array[:, :, 1] == 1).mean()) > 0.995 |
| 47 | + assert ((annotated_array[:, :, 2] == 1).mean()) > 0.995 |
0 commit comments