|
| 1 | +import logging |
| 2 | + |
1 | 3 | from PIL import Image, ImageDraw
|
2 | 4 | from imageio import imread
|
3 | 5 | import numpy as np
|
@@ -42,9 +44,14 @@ def __init__(self,
|
42 | 44 | diffgram_file_id_list = diffgram_file_id_list,
|
43 | 45 | validate_ids = validate_ids,
|
44 | 46 | max_size_cache = max_size_cache,
|
45 |
| - max_num_concurrent_fetches = max_num_concurrent_fetches) |
46 |
| - |
47 |
| - def start_iterator(self, project, diffgram_file_id_list, validate_ids = True, max_size_cache = 1073741824, |
| 47 | + max_num_concurrent_fetches = max_num_concurrent_fetches |
| 48 | + ) |
| 49 | + |
| 50 | + def start_iterator(self, |
| 51 | + project, |
| 52 | + diffgram_file_id_list, |
| 53 | + validate_ids = True, |
| 54 | + max_size_cache = 1073741824, |
48 | 55 | max_num_concurrent_fetches = 25):
|
49 | 56 | self.diffgram_file_id_list = diffgram_file_id_list
|
50 | 57 | self.max_size_cache = max_size_cache
|
@@ -107,7 +114,11 @@ def __getitem__(self, idx):
|
107 | 114 |
|
108 | 115 | def __next__(self):
|
109 | 116 | if self.file_cache.get(self.current_file_index):
|
110 |
| - return self.file_cache.get(self.current_file_index) |
| 117 | + result = self.file_cache.get(self.current_file_index) |
| 118 | + self.current_file_index += 1 |
| 119 | + return result |
| 120 | + if self.current_file_index >= len(self.diffgram_file_id_list): |
| 121 | + raise StopIteration |
111 | 122 | instance_data = self.__get_file_data_for_index(self.current_file_index)
|
112 | 123 | self.current_file_index += 1
|
113 | 124 | return instance_data
|
@@ -177,13 +188,16 @@ def gen_tag_instances(self, instance_list):
|
177 | 188 | return result
|
178 | 189 |
|
179 | 190 | def get_file_instances(self, diffgram_file):
|
180 |
| - if diffgram_file.type not in ['image', 'frame']: |
181 |
| - raise NotImplementedError('File type "{}" is not supported yet'.format(diffgram_file['type'])) |
182 |
| - image = self.get_image_data(diffgram_file) |
| 191 | + sample = {'diffgram_file': diffgram_file} |
| 192 | + if diffgram_file.type not in ['image', 'frame', 'compound']: |
| 193 | + logging.warning('File type "{}" is not supported yet'.format(diffgram_file.type)) |
| 194 | + return sample |
| 195 | + if diffgram_file.type in ['image', 'frame']: |
| 196 | + sample['image'] = self.get_image_data(diffgram_file) |
183 | 197 | instance_list = diffgram_file.instance_list
|
184 | 198 | instance_types_in_file = set([x['type'] for x in instance_list])
|
185 | 199 | # Process the instances of each file
|
186 |
| - sample = {'image': image, 'diffgram_file': diffgram_file} |
| 200 | + |
187 | 201 | has_boxes = False
|
188 | 202 | has_poly = False
|
189 | 203 | has_tags = False
|
@@ -231,11 +245,13 @@ def get_file_instances(self, diffgram_file):
|
231 | 245 | def extract_masks_from_polygon(self, instance_list, diffgram_file, empty_value = 0):
|
232 | 246 | nx, ny = diffgram_file.image['width'], diffgram_file.image['height']
|
233 | 247 | mask_list = []
|
| 248 | + if nx is None or ny is None: |
| 249 | + return mask_list |
| 250 | + |
234 | 251 | for instance in instance_list:
|
235 | 252 | if instance['type'] != 'polygon':
|
236 | 253 | continue
|
237 | 254 | poly = [(p['x'], p['y']) for p in instance['points']]
|
238 |
| - |
239 | 255 | img = Image.new(mode = 'L', size = (nx, ny), color = 0) # mode L = 8-bit pixels, black and white
|
240 | 256 | draw = ImageDraw.Draw(img)
|
241 | 257 | draw.polygon(poly, outline = 1, fill = 1)
|
|
0 commit comments