Skip to content

Commit facca7b

Browse files
authored
feat: adds support for compound file (#55)
adds support for compound file on dataset iterator and fixes small issues with caching and __next__() method
1 parent 65e3cc4 commit facca7b

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

sdk/diffgram/core/diffgram_dataset_iterator.py

+25-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import logging
2+
13
from PIL import Image, ImageDraw
24
from imageio import imread
35
import numpy as np
@@ -42,9 +44,14 @@ def __init__(self,
4244
diffgram_file_id_list = diffgram_file_id_list,
4345
validate_ids = validate_ids,
4446
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,
4855
max_num_concurrent_fetches = 25):
4956
self.diffgram_file_id_list = diffgram_file_id_list
5057
self.max_size_cache = max_size_cache
@@ -107,7 +114,11 @@ def __getitem__(self, idx):
107114

108115
def __next__(self):
109116
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
111122
instance_data = self.__get_file_data_for_index(self.current_file_index)
112123
self.current_file_index += 1
113124
return instance_data
@@ -177,13 +188,16 @@ def gen_tag_instances(self, instance_list):
177188
return result
178189

179190
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)
183197
instance_list = diffgram_file.instance_list
184198
instance_types_in_file = set([x['type'] for x in instance_list])
185199
# Process the instances of each file
186-
sample = {'image': image, 'diffgram_file': diffgram_file}
200+
187201
has_boxes = False
188202
has_poly = False
189203
has_tags = False
@@ -231,11 +245,13 @@ def get_file_instances(self, diffgram_file):
231245
def extract_masks_from_polygon(self, instance_list, diffgram_file, empty_value = 0):
232246
nx, ny = diffgram_file.image['width'], diffgram_file.image['height']
233247
mask_list = []
248+
if nx is None or ny is None:
249+
return mask_list
250+
234251
for instance in instance_list:
235252
if instance['type'] != 'polygon':
236253
continue
237254
poly = [(p['x'], p['y']) for p in instance['points']]
238-
239255
img = Image.new(mode = 'L', size = (nx, ny), color = 0) # mode L = 8-bit pixels, black and white
240256
draw = ImageDraw.Draw(img)
241257
draw.polygon(poly, outline = 1, fill = 1)

sdk/diffgram/core/directory.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ def all_file_ids(self, query = None):
118118
limit = 5000,
119119
page_num = page_num,
120120
file_view_mode = 'ids_only',
121-
query = query)
121+
query = query,
122+
with_children_files = True)
122123

123124
if diffgram_ids is False:
124125
raise Exception('Error Fetching Files: Please check you are providing a valid query.')
@@ -242,7 +243,8 @@ def list_files(
242243
limit = 100,
243244
search_term: str = None,
244245
file_view_mode: str = 'annotation',
245-
query: str = None):
246+
query: str = None,
247+
with_children_files = False):
246248
"""
247249
"""
248250
if self.id:
@@ -262,6 +264,7 @@ def list_files(
262264
'page': page_num,
263265
'file_view_mode': file_view_mode,
264266
'search_term': search_term,
267+
'with_children_files': with_children_files,
265268
'query': query
266269
}
267270
}

0 commit comments

Comments
 (0)