|
| 1 | +from PIL import Image, ImageDraw |
| 2 | +from imageio import imread |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +class DiffgramDatasetIterator: |
| 6 | + |
| 7 | + def __init__(self, project, diffgram_file_id_list): |
| 8 | + """ |
| 9 | +
|
| 10 | + :param project (sdk.core.core.Project): A Project object from the Diffgram SDK |
| 11 | + :param diffgram_file_list (list): An arbitrary number of file ID's from Diffgram. |
| 12 | + """ |
| 13 | + self.diffgram_file_id_list = diffgram_file_id_list |
| 14 | + |
| 15 | + self.project = project |
| 16 | + self._internal_file_list = [] |
| 17 | + self.__validate_file_ids() |
| 18 | + self.current_file_index = 0 |
| 19 | + |
| 20 | + def __iter__(self): |
| 21 | + self.current_file_index = 0 |
| 22 | + return self |
| 23 | + |
| 24 | + def __len__(self): |
| 25 | + return len(self.diffgram_file_id_list) |
| 26 | + |
| 27 | + def __getitem__(self, idx): |
| 28 | + diffgram_file = self.project.file.get_by_id(self.diffgram_file_id_list[idx], with_instances = True) |
| 29 | + instance_data = self.get_file_instances(diffgram_file) |
| 30 | + return instance_data |
| 31 | + |
| 32 | + def __next__(self): |
| 33 | + file_id = self.diffgram_file_id_list[self.current_file_index] |
| 34 | + diffgram_file = self.project.file.get_by_id(file_id, with_instances = True) |
| 35 | + instance_data = self.get_file_instances(diffgram_file) |
| 36 | + self.current_file_index += 1 |
| 37 | + return instance_data |
| 38 | + |
| 39 | + def __validate_file_ids(self): |
| 40 | + result = self.project.file.file_list_exists(self.diffgram_file_id_list) |
| 41 | + if not result: |
| 42 | + raise Exception( |
| 43 | + 'Some file IDs do not belong to the project. Please provide only files from the same project.') |
| 44 | + |
| 45 | + def get_image_data(self, diffgram_file): |
| 46 | + if hasattr(diffgram_file, 'image'): |
| 47 | + image = imread(diffgram_file.image.get('url_signed')) |
| 48 | + return image |
| 49 | + else: |
| 50 | + raise Exception('Pytorch datasets only support images. Please provide only file_ids from images') |
| 51 | + |
| 52 | + def get_file_instances(self, diffgram_file): |
| 53 | + if diffgram_file.type not in ['image', 'frame']: |
| 54 | + raise NotImplementedError('File type "{}" is not supported yet'.format(diffgram_file['type'])) |
| 55 | + |
| 56 | + image = self.get_image_data(diffgram_file) |
| 57 | + instance_list = diffgram_file.instance_list |
| 58 | + instance_types_in_file = set([x['type'] for x in instance_list]) |
| 59 | + # Process the instances of each file |
| 60 | + sample = {'image': image, 'diffgram_file': diffgram_file} |
| 61 | + has_boxes = False |
| 62 | + has_poly = False |
| 63 | + if 'box' in instance_types_in_file: |
| 64 | + has_boxes = True |
| 65 | + x_min_list, x_max_list, y_min_list, y_max_list = self.extract_bbox_values(instance_list, diffgram_file) |
| 66 | + sample['x_min_list'] = x_min_list |
| 67 | + sample['x_max_list'] = x_max_list |
| 68 | + sample['y_min_list'] = y_min_list |
| 69 | + sample['y_max_list'] = y_max_list |
| 70 | + |
| 71 | + if 'polygon' in instance_types_in_file: |
| 72 | + has_poly = True |
| 73 | + mask_list = self.extract_masks_from_polygon(instance_list, diffgram_file) |
| 74 | + sample['polygon_mask_list'] = mask_list |
| 75 | + |
| 76 | + if len(instance_types_in_file) > 2 and has_boxes and has_boxes: |
| 77 | + raise NotImplementedError( |
| 78 | + 'SDK only supports boxes and polygon types currently. If you want a new instance type to be supported please contact us!' |
| 79 | + ) |
| 80 | + |
| 81 | + label_id_list, label_name_list = self.extract_labels(instance_list) |
| 82 | + sample['label_id_list'] = label_id_list |
| 83 | + sample['label_name_list'] = label_name_list |
| 84 | + |
| 85 | + return sample |
| 86 | + |
| 87 | + def extract_masks_from_polygon(self, instance_list, diffgram_file, empty_value = 0): |
| 88 | + nx, ny = diffgram_file.image['width'], diffgram_file.image['height'] |
| 89 | + mask_list = [] |
| 90 | + for instance in instance_list: |
| 91 | + if instance['type'] != 'polygon': |
| 92 | + continue |
| 93 | + poly = [(p['x'], p['y']) for p in instance['points']] |
| 94 | + |
| 95 | + img = Image.new(mode = 'L', size = (nx, ny), color = 0) # mode L = 8-bit pixels, black and white |
| 96 | + draw = ImageDraw.Draw(img) |
| 97 | + draw.polygon(poly, outline = 1, fill = 1) |
| 98 | + mask = np.array(img).astype('float32') |
| 99 | + # mask[np.where(mask == 0)] = empty_value |
| 100 | + mask_list.append(mask) |
| 101 | + return mask_list |
| 102 | + |
| 103 | + def extract_labels(self, instance_list, allowed_instance_types = None): |
| 104 | + label_file_id_list = [] |
| 105 | + label_names_list = [] |
| 106 | + |
| 107 | + for inst in instance_list: |
| 108 | + if allowed_instance_types and inst['type'] in allowed_instance_types: |
| 109 | + continue |
| 110 | + |
| 111 | + label_file_id_list.append(inst['label_file']['id']) |
| 112 | + label_names_list.append(inst['label_file']['label']['name']) |
| 113 | + |
| 114 | + return label_file_id_list, label_names_list |
| 115 | + |
| 116 | + def extract_bbox_values(self, instance_list, diffgram_file): |
| 117 | + """ |
| 118 | + Creates a pytorch tensor based on the instance type. |
| 119 | + For now we are assuming shapes here, but we can extend it |
| 120 | + to accept custom shapes specified by the user. |
| 121 | + :param instance: |
| 122 | + :return: |
| 123 | + """ |
| 124 | + x_min_list = [] |
| 125 | + x_max_list = [] |
| 126 | + y_min_list = [] |
| 127 | + y_max_list = [] |
| 128 | + |
| 129 | + for inst in instance_list: |
| 130 | + if inst['type'] != 'box': |
| 131 | + continue |
| 132 | + x_min_list.append(inst['x_min'] / diffgram_file.image['width']) |
| 133 | + x_max_list.append(inst['x_max'] / diffgram_file.image['width']) |
| 134 | + y_min_list.append(inst['y_min'] / diffgram_file.image['width']) |
| 135 | + y_max_list.append(inst['y_max'] / diffgram_file.image['width']) |
| 136 | + |
| 137 | + return x_min_list, x_max_list, y_min_list, y_max_list |
0 commit comments