|
12 | 12 | convert = lambda text: int(text) if text.isdigit() else text.lower()
|
13 | 13 | natrual_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
|
14 | 14 |
|
15 |
| -def resize_array(array, new_size): |
16 |
| - image = Image.fromarray(array) |
| 15 | +def resize_binary_mask(array, new_size): |
| 16 | + image = Image.fromarray(array.astype(np.uint8)*255) |
17 | 17 | image = image.resize(new_size)
|
18 |
| - return np.asarray(image) |
| 18 | + return np.asarray(image).astype(np.bool_) |
19 | 19 |
|
20 | 20 | def close_contour(contour):
|
21 | 21 | if not np.array_equal(contour[0], contour[-1]):
|
@@ -76,15 +76,21 @@ def create_image_info(image_id, file_name, image_size,
|
76 | 76 |
|
77 | 77 | return image_info
|
78 | 78 |
|
79 |
| -def create_annotation_info(annotation_id, image_id, category_info, binary_mask, image_size, tolerance=0): |
80 |
| - binary_mask = resize_array(binary_mask, image_size) |
| 79 | +def create_annotation_info(annotation_id, image_id, category_info, binary_mask, |
| 80 | + image_size=None, tolerance=2, bounding_box=None): |
| 81 | + |
| 82 | + if image_size is not None: |
| 83 | + binary_mask = resize_binary_mask(binary_mask, image_size) |
| 84 | + |
81 | 85 | binary_mask_encoded = mask.encode(np.asfortranarray(binary_mask.astype(np.uint8)))
|
82 |
| - bounding_box = mask.toBbox(binary_mask_encoded) |
83 |
| - area = mask.area(binary_mask_encoded) |
84 | 86 |
|
| 87 | + area = mask.area(binary_mask_encoded) |
85 | 88 | if area < 1:
|
86 | 89 | return None
|
87 | 90 |
|
| 91 | + if bounding_box is None: |
| 92 | + bounding_box = mask.toBbox(binary_mask_encoded) |
| 93 | + |
88 | 94 | if category_info["is_crowd"]:
|
89 | 95 | is_crowd = 1
|
90 | 96 | segmentation = binary_mask_to_rle(binary_mask)
|
|
0 commit comments