def process(self, data_el): img_desc, ann_orig = data_el img_wh = ann_orig.image_size_wh if 'random_part' in self.settings: rect_to_crop = random_rect_from_bounds( self.settings['random_part'], *img_wh) elif 'sides' in self.settings: rect_to_crop = rect_from_bounds(self.settings['sides'], *img_wh) else: raise NotImplemented('Crop layer: wrong params.') rect_img = Rect.from_size(img_wh) if rect_to_crop.is_empty: # tiny image (left >= right etc) logger.warning('Crop layer produced empty crop.') return # no yield if not rect_img.contains(rect_to_crop): # some negative params raise RuntimeError( 'Crop layer: result crop bounds are outside of source image.') ann = deepcopy(ann_orig) ann.apply_to_figures(lambda x: x.crop(rect_to_crop)) yield img_desc, ann
def all_filtered_bbox_rois(ann, included_classes, padding_settings, img_wh): for src_obj in ann['objects']: if is_object_title_included(src_obj.class_title, included_classes): bbox = src_obj.get_bbox().round() roi = rect_from_bounds(padding_settings, img_w=bbox.width, img_h=bbox.height, shift_inside=False) roi = roi.move( (bbox.left, bbox.top)).intersection(Rect.from_size(img_wh)) if not roi.is_empty: yield src_obj, roi
def feed(self, img, ann, inference_cback): src_objects, model_objects, interm_objects, img_wh = self._init_feed( ann) roi = rect_from_bounds(self._mode_conf['bounds'], *img_wh) rect_img = Rect.from_size(img_wh) if roi.is_empty or (not rect_img.contains(roi)): raise RuntimeError('Mode "roi": result crop bounds are invalid.') model_objects = get_objects_for_bbox(img, roi, inference_cback, self._renamer_model) if self._intermediate_bbox_class is not None: interm_objects.extend( FigureRectangle.from_rect( self._intermediate_bbox_class['title'], img_wh, roi)) ann['objects'] = src_objects + interm_objects + model_objects return ann
def process(self, data_el): img_desc, ann_orig = data_el imsize_wh = ann_orig.image_size_wh rect_img = Rect.from_size(imsize_wh) padding_dct = self.settings['pad']['sides'] img_orig = None for idx, src_fig in enumerate(ann_orig['objects']): if src_fig.class_title not in self.classes_to_crop: continue src_fig_bbox = src_fig.get_bbox().round() if src_fig_bbox.is_empty: continue # tiny object new_img_rect = rect_from_bounds(padding_dct, img_w=src_fig_bbox.width, img_h=src_fig_bbox.height, shift_inside=False) rect_to_crop = new_img_rect.move(src_fig_bbox.point0) rect_to_crop = rect_to_crop.intersection(rect_img).round() if rect_to_crop.is_empty: continue # let's crop if img_orig is None: img_orig = img_desc.read_image() img = crop_image_with_rect(img_orig, rect_to_crop) new_img_desc = img_desc.clone_with_img(img) ann = deepcopy(ann_orig) ann['objects'] = [ x for i, x in enumerate(ann['objects']) if i == idx or x.class_title in self.classes_to_save ] ann.apply_to_figures(lambda x: x.crop(rect_to_crop)) delta = (-rect_to_crop.left, -rect_to_crop.top) for fig in ann['objects']: fig.shift(delta) # to new coords of image ann.update_image_size(img) yield new_img_desc, ann
def process(self, data_el): img_desc, ann_orig = data_el img_wh = ann_orig.image_size_wh if 'random_part' in self.settings: rect_to_crop = random_rect_from_bounds( self.settings['random_part'], *img_wh) elif 'sides' in self.settings: rect_to_crop = rect_from_bounds(self.settings['sides'], *img_wh) else: raise NotImplemented('Crop layer: wrong params.') # rect_to_crop has 'true' coordinates, e.g. single pixel crop will be defined as (0, 0, 1, 1) rect_img = Rect.from_size(img_wh) if rect_to_crop.is_empty: # tiny image (left >= right etc) logger.warning('Crop layer produced empty crop.') return # no yield if not rect_img.contains(rect_to_crop): # some negative params raise RuntimeError( 'Crop layer: result crop bounds are outside of source image.') img = img_desc.read_image() img = crop_image_with_rect(img, rect_to_crop) new_img_desc = img_desc.clone_with_img(img) ann = deepcopy(ann_orig) ann.apply_to_figures(lambda x: x.crop(rect_to_crop)) delta = (-rect_to_crop.left, -rect_to_crop.top) for fig in ann['objects']: fig.shift(delta) # to new coords of image ann.update_image_size(img) yield new_img_desc, ann