def process_augmentations(aug_seq, image, bbs, threshold): """ Applying data augmentation. Removing the bbox when it is outside the image or the fraction of bbox area outside of the image plane > threshold Parameters ---------- aug_seq: imgaug.augmenters.Sequential image: image numpy array (H,W,C) bbs: imgaug.augmentables.bbs,BoundingBoxesOnImage threshold: float Deleting the bbox when the fraction of bbox area outside of the image plane > threshold Returns image, bbs ------- """ if isinstance(aug_seq, (iaa.Sequential, iaa.Augmenter)): image_aug, bbs_aug = aug_seq(image=image, bounding_boxes=bbs) bbs_fraction_outside = [ x.compute_out_of_image_fraction(image) for x in bbs_aug ] if_reserved = np.less(bbs_fraction_outside, threshold) bbs_aug = BoundingBoxesOnImage( [bbs_aug[i] for i in range(len(if_reserved)) if if_reserved[i]], shape=image_aug.shape) bbs_aug = bbs_aug.remove_out_of_image().clip_out_of_image() return image_aug, bbs_aug else: raise TypeError( 'Aug_seq must be an instance of iaa.Sequential or iaa.Augmenter')
def chapter_examples_bounding_boxes_projection(): import imgaug as ia from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage ia.seed(1) # Define image with two bounding boxes image = ia.quokka(size=(256, 256)) bbs = BoundingBoxesOnImage([ BoundingBox(x1=25, x2=75, y1=25, y2=75), BoundingBox(x1=100, x2=150, y1=25, y2=75) ], shape=image.shape) # Rescale image and bounding boxes image_rescaled = ia.imresize_single_image(image, (512, 512)) bbs_rescaled = bbs.on(image_rescaled) # Draw image before/after rescaling and with rescaled bounding boxes image_bbs = bbs.draw_on_image(image, size=2) image_rescaled_bbs = bbs_rescaled.draw_on_image(image_rescaled, size=2) # ------------ save("examples_bounding_boxes", "projection.jpg", grid([image_bbs, image_rescaled_bbs], cols=2, rows=1), quality=90)
def examine_aug_images(filename: str, resized_images_df: pd.DataFrame, augmented_images_df: pd.DataFrame): grouped_resized = resized_images_df.groupby('filename') grouped_augmented = augmented_images_df.groupby('filename') group_r_df = grouped_resized.get_group(filename) group_r_df = group_r_df.reset_index() group_r_df = group_r_df.drop(['index'], axis=1) bb_r_array = group_r_df.drop(['filename', 'width', 'height', 'class'], axis=1).values resized_img = imageio.imread('images_train/' + filename) bbs_r = BoundingBoxesOnImage.from_xyxy_array(bb_r_array, shape=resized_img.shape) group_a_df = grouped_augmented.get_group('aug1_' + filename) group_a_df = group_a_df.reset_index() group_a_df = group_a_df.drop(['index'], axis=1) bb_a_array = group_a_df.drop(['filename', 'width', 'height', 'class'], axis=1).values augmented_img = imageio.imread('images_aug/' + 'aug1_' + filename) bbs_a = BoundingBoxesOnImage.from_xyxy_array(bb_a_array, shape=augmented_img.shape) ia.imshow( np.hstack([ bbs_r.draw_on_image(resized_img, size=2), bbs_a.draw_on_image(augmented_img, size=2) ])) # Examine random example filename = resized_images_df['filename'].unique()[np.random.randint( 0, len(resized_images_df['filename'].unique()), 1)][0] examine_aug_images(filename, resized_images_df, augmented_images_df)
def augument(impath): ia.seed(1) image = imageio.imread(impath) bbs = BoundingBoxesOnImage([ BoundingBox(x1=170, y1=130, x2=252, y2=248, label='18'), BoundingBox(x1=100, y1=154, x2=120, y2=175, label='1') ], shape=image.shape) ia.imshow(bbs.draw_on_image(image, size=2)) # apply augumentation # We choose a simple contrast augmentation (affects only the image) and an affine transformation (affects image and bounding boxes). seq = iaa.Sequential([ iaa.GammaContrast(1.5), iaa.Affine(translate_percent={"x": 0.1}, scale=0.8) ]) image_aug, bbs_aug = seq(image=image, bounding_boxes=bbs) ia.imshow(bbs_aug.draw_on_image(image_aug, size=2)) # we apply an affine transformation consisting only of rotation. image_aug, bbs_aug = iaa.Affine(rotate=50)(image=image, bounding_boxes=bbs) ia.imshow(bbs_aug.draw_on_image(image_aug))
def get_image_with_box(img: np.ndarray, img_name: str, df: pd.DataFrame) -> np.ndarray: """ This function will: 1. get all bounding boxes that relate to the image for the DataFrame 2. concatenate them with imgaug package to a BoundingBoxesOnImage object 3. draw the Bounding boxes onto the image :param img: image as np array :param img_name: filename to locate the bounding boxes in the df :param df: DataFrame that holds the information about all bounding boxes :return: image with bounding boxes drawn onto it """ # create bounding box with imgaug img_boxs = df[df.filename == img_name] bbs = list() for _, row in img_boxs.iterrows(): x1 = row.xmin y1 = row.ymin x2 = row.xmax y2 = row.ymax bbs.append(BoundingBox(x1=x1, y1=y1, x2=x2, y2=y2)) # convert single bounding boxes to BOundingBoxOnImage instance to draw it on the picture bbs = BoundingBoxesOnImage(bbs, img.shape[:-1]) # draw image img = bbs.draw_on_image(img) return img
def Resize(it): """ Resizes images according to the resize dimensions image: (array) Image resize: (tuple of integers) New dimensions target: (Dictionary) Containing BBox, Area, Labels and Index Returns normalized_image, target """ image, target = it resize = res new_target = target.copy() bbs = BoundingBoxesOnImage([ BoundingBox(x1=new_target['boxes'][0].item(), y1=new_target['boxes'][1].item(), x2=new_target['boxes'][2].item(), y2=new_target['boxes'][3].item()) ], image.shape) img = ia.imresize_single_image(np.array(image), resize) new_bbs = bbs.on(img) bbox = torch.tensor( [new_bbs[0].x1, new_bbs[0].y1, new_bbs[0].x2, new_bbs[0].y2]) new_target['area'] = torch.tensor([ (new_bbs[0].x2 - new_bbs[0].x1) * (new_bbs[0].y2 - new_bbs[0].y1) ]) new_target['boxes'] = bbox.unsqueeze(0) return img, [new_target]
def test_plot_all(orig, normalized, target, denormalize): # plot image, normalized images with boxes, and denormalized image fig = plt.figure() ax1 = fig.add_subplot(221) ax2 = fig.add_subplot(222) ax3 = fig.add_subplot(223) bbs = BoundingBoxesOnImage([ BoundingBox(x1=x[0], x2=x[2], y1=x[1], y2=x[3]) for x in target['boxes'] ], shape=normalized.shape) bbs_args = {'size': 2, 'color': [255, 0, 0]} image_bbs = bbs.draw_on_image(normalized.permute(1, 2, 0), **bbs_args) denormed = denormalize(normalized) denormed_bbs = bbs.draw_on_image(denormed.permute(1, 2, 0), **bbs_args) print(orig) print(image_bbs) print(denormed) ax1.set_title('Input') ax2.set_title('Normalized') ax3.set_title('Denormalized') ax1.imshow(orig.permute(1, 2, 0)) ax2.imshow(np.clip(image_bbs, 0, 1)) ax3.imshow(np.clip(denormed_bbs, 0, 1)) plt.show() plt.pause(0.1)
def transform(): ia.seed(1) image = ia.quokka(size=(256, 256)) bbs = BoundingBoxesOnImage([ BoundingBox(x1=65, y1=100, x2=200, y2=150), ], shape=image.shape) seq = iaa.Sequential([ iaa.Multiply((1.2, 1.5)), # change brightness, doesn't affect BBs iaa.Affine( translate_px={ "x": 40, "y": 60 }, scale=(0.5, 0.7) ) # translate by 40/60px on x/y axis, and scale to 50-70%, affects BBs ]) # Augment BBs and images. image_aug, bbs_aug = seq(image=image, bounding_boxes=bbs) # image with BBs before/after augmentation (shown below) image_before = bbs.draw_on_image(image, size=2) image_after = bbs_aug.draw_on_image(image_aug, size=2, color=[0, 0, 255]) cv2.imshow('before', image_before) cv2.imshow('after', image_after) cv2.waitKey()
def chapter_examples_bounding_boxes_shift(): import imgaug as ia from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage ia.seed(1) # Define image and two bounding boxes image = ia.quokka(size=(256, 256)) bbs = BoundingBoxesOnImage([ BoundingBox(x1=25, x2=75, y1=25, y2=75), BoundingBox(x1=100, x2=150, y1=25, y2=75) ], shape=image.shape) # Move both BBs 25px to the right and the second BB 25px down bbs_shifted = bbs.shift(left=25) bbs_shifted.bounding_boxes[1] = bbs_shifted.bounding_boxes[1].shift(top=25) # Draw images before/after moving BBs image = bbs.draw_on_image(image, color=[0, 255, 0], size=2, alpha=0.75) image = bbs_shifted.draw_on_image(image, color=[0, 0, 255], size=2, alpha=0.75) # ------------ save("examples_bounding_boxes", "shift.jpg", grid([image], cols=1, rows=1), quality=90)
def output_preview_image(annotation): xml = etree.fromstring(open(annotation).read().encode()) annotation = recursive_parse_xml_to_dict(xml) image_file = Path(FLAGS.input_dir).joinpath('images').joinpath( annotation['annotation']['filename']) image = Image.open(image_file) bndboxes = [x['bndbox'] for x in annotation['annotation']['object']] bbs = BoundingBoxesOnImage([ BoundingBox(int(b['xmin']), int(b['ymin']), int(b['xmax']), int(b['ymax'])) for b in bndboxes ], shape=(image.width, image.height)) image_bbs = bbs.draw_on_image(np.array(image)) output_dir = Path(FLAGS.output_dir) if not output_dir.exists(): output_dir.mkdir(parents=True) output_file = output_dir.joinpath(image_file.name) image_bbs = Image.fromarray(image_bbs) image_bbs.save(output_file)
def test_augment_bounding_boxes__kernel_size_is_two__no_keep_size(self): from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage bbs = [BoundingBox(x1=1.5, y1=2.5, x2=3.5, y2=4.5)] bbsoi = BoundingBoxesOnImage(bbs, shape=(6, 6, 3)) expected = BoundingBoxesOnImage( [BoundingBox(x1=1.5 / 2, y1=2.5 / 2, x2=3.5 / 2, y2=4.5 / 2)], shape=(3, 3, 3)) self._test_augment_cbaoi__kernel_size_is_two__no_keep_size( bbsoi, expected, "augment_bounding_boxes")
def test_bounding_boxes_alignment(self): from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage bbs = [BoundingBox(x1=10, y1=10, x2=30, y2=30)] bbsoi = BoundingBoxesOnImage(bbs, shape=(40, 40, 1)) bbsoi_empty = BoundingBoxesOnImage([], shape=(40, 40, 1)) self._test_cbaoi_alignment(bbsoi, bbsoi_empty, [[(10 / 2, 10 / 2), (30 / 2, 30 / 2)]], [[(10, 10), (30, 30)]], "augment_bounding_boxes")
def test_empty_bounding_boxes(self): from imgaug.augmentables.bbs import BoundingBoxesOnImage bbsoi = BoundingBoxesOnImage([], shape=(5, 6, 3)) aug = self.augmenter(3, keep_size=False) bbsoi_aug = aug.augment_bounding_boxes(bbsoi) expected = bbsoi.deepcopy() expected.shape = (2, 2, 3) assert_cbaois_equal(bbsoi_aug, expected)
def show_bbs(img, anno): gts = anno[1].numpy() np_img = np.asarray(img) bbs = [] for gt in gts: bbs.append(BoundingBox(x1=gt[0], y1=gt[1], x2=gt[2], y2=gt[3])) bbs_on_img = BoundingBoxesOnImage(bbs, shape=np_img.shape) draw_img = bbs_on_img.draw_on_image(np_img, size=2) ia.imshow(draw_img)
def quokka_bounding_boxes(size=None, extract=None): """Return example bounding boxes on the standard example quokke image. Currently only a single bounding box is returned that covers the quokka. Added in 0.5.0. (Moved from ``imgaug.imgaug``.) Parameters ---------- size : None or float or tuple of int or tuple of float, optional Size of the output image on which the BBs are placed. If ``None``, then the BBs are not projected to any new size (positions on the original image are used). ``float`` s lead to relative size changes, ``int`` s to absolute sizes in pixels. extract : None or 'square' or tuple of number or imgaug.augmentables.bbs.BoundingBox or imgaug.augmentables.bbs.BoundingBoxesOnImage Subarea to extract from the image. See :func:`~imgaug.imgaug.quokka`. Returns ------- imgaug.augmentables.bbs.BoundingBoxesOnImage Example BBs on the quokka image. """ # TODO get rid of this deferred import from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage left, top = 0, 0 if extract is not None: bb_extract = _quokka_normalize_extract(extract) left = bb_extract.x1 top = bb_extract.y1 with open(_QUOKKA_ANNOTATIONS_FP, "r") as f: json_dict = json.load(f) bbs = [] for bb_dict in json_dict["bounding_boxes"]: bbs.append( BoundingBox( x1=bb_dict["x1"] - left, y1=bb_dict["y1"] - top, x2=bb_dict["x2"] - left, y2=bb_dict["y2"] - top ) ) if extract is not None: shape = (bb_extract.height, bb_extract.width, 3) else: shape = (643, 960, 3) bbsoi = BoundingBoxesOnImage(bbs, shape=shape) if size is not None: shape_resized = _compute_resized_shape(shape, size) bbsoi = bbsoi.on(shape_resized) return bbsoi
def test_augment_bounding_boxes__kernel_size_is_two__no_keep_size(self): from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage bbs = [BoundingBox(x1=1.5, y1=2.5, x2=3.5, y2=4.5)] bbsoi = BoundingBoxesOnImage(bbs, shape=(6, 6, 3)) aug = self.augmenter(2, keep_size=False) bbsoi_aug = aug.augment_bounding_boxes(bbsoi) expected = BoundingBoxesOnImage( [BoundingBox(x1=1.5 / 2, y1=2.5 / 2, x2=3.5 / 2, y2=4.5 / 2)], shape=(3, 3, 3)) assert_cbaois_equal(bbsoi_aug, expected)
def visualize_bounding_box(annot_path,images_path,name="images_with_bounding_boxes",save=True): """ Annotations are in xml. """ save_path=os.path.join(os.path.dirname(images_path),name) if not os.path.exists(save_path): os.makedirs(save_path) count=0 for xml in os.listdir(annot_path): bounding_box_list=[] full_path=os.path.join(annot_path,xml) tree=ET.parse(full_path) root=tree.getroot() for object in root.iter("object"): voc_list=[] name=str(object.find("name").text) box= object.find("bndbox") xmin=int(box.find("xmin").text) ymin=int(box.find("ymin").text) xmax=int(box.find("xmax").text) ymax=int(box.find("ymax").text) bb_list=[xmin,ymin,xmax,ymax] bounding_box_list.append(BoundingBox(*bb_list)) #print(images_path) image_path=os.path.join(images_path,os.path.splitext(xml)[0]+".jpg") image=Image.open(image_path) bbs = BoundingBoxesOnImage(bounding_box_list, shape=image.size) with_boxes=bbs.draw_on_image(image,size=2) count+=1 if count<3: ia.imshow(with_boxes) if save: im=Image.fromarray(with_boxes) im.save(os.path.join(save_path,os.path.splitext(xml)[0]+".jpg"))
def boxes_numpy2imgaug(annotations, image_shape): boxes = [] for annot in annotations: x1, y1, x2, y2, label = annot box = BoundingBox(x1, y1, x2, y2, label) boxes.append(box) return BoundingBoxesOnImage(boxes, image_shape)
def check(): image = imageio.imread("img1.jpg") bbs = BoundingBoxesOnImage([ BoundingBox(x1=0, x2=0, y1=0, y2=0) ], shape=image.shape) for i in range(1): rotate=iaa.Affine(rotate=(-12, 12)) image_aug, bbs_aug = rotate(image=image, bounding_boxes=bbs) for j in range(1): r="AddToHue" aug = iaa.AddToBrightness((-30, 30)) image_aug, bbs_aug = aug(image=image_aug, bounding_boxes=bbs_aug) aug = iaa.AddToHue((-15, 15)) image_aug, bbs_aug = aug(image=image_aug, bounding_boxes=bbs_aug) # cv2.imwrite("a.jpg",image_aug) print(bbs_aug[0].x1) ia.imshow(bbs_aug.draw_on_image(image_aug, size=2))
def _read_labelimg(_path_list, _pos): for i, name in enumerate(_path_list): pos = (_pos + i) with open(os.path.join(self.label_path, name[:name.rfind(".")] + ".xml"), encoding=self.encoding) as f: soup = BeautifulSoup(f.read(), "xml") img = Image.open(os.path.join(self.img_path, name)) img, zoom_r = _process_img(img, self.size) bbs = [] labels = [] for obj in soup.select("object"): if obj.select_one("name").text in self.class_names: label_text = obj.select_one("name").text label = self.class_names.index(label_text) labels.append(label) xmin = int(obj.select_one("xmin").text) / zoom_r[0] xmax = int(obj.select_one("xmax").text) / zoom_r[0] ymin = int(obj.select_one("ymin").text) / zoom_r[1] ymax = int(obj.select_one("ymax").text) / zoom_r[1] bbs.append( BoundingBox(x1=xmin, y1=ymin, x2=xmax, y2=ymax)) bbs = BoundingBoxesOnImage(bbs, shape=img.shape) _imgaug_to_array(img, bbs, self.grid_shape, pos, labels)
def transforms_visualization(img_root, csv_path, plot_num): """ :param img_root: root to the images (positive included) :param csv_path: path to the csv generated from the xml annotations :param plot_num: number of images to plot """ df = pd.read_csv(csv_path) img_names = sorted(list(set(df['img_name']))) for name in img_names[:plot_num]: objects = df.loc[df['img_name'] == name] image = cv2.imread(os.path.join(img_root, name)) bboxes_list = [] for index, row in objects.iterrows(): bboxes_list.append( BoundingBox(x1=row['xmin'], x2=row['xmax'], y1=row['ymin'], y2=row['ymax'])) bbs = BoundingBoxesOnImage(bboxes_list, shape=image.shape) img_aug, bbs_aug = transforms(image=image, bounding_boxes=bbs) image_before = draw_bbs(image, bbs) cv2.imshow('Before', image_before) cv2.waitKey(0) image_after = draw_bbs(img_aug, bbs_aug) cv2.imshow('After', np.array(image_after)) cv2.waitKey(0) cv2.destroyAllWindows()
def augment(record): record = deepcopy(record) image = plt.imread(record["filepath"]) annotations = record["annotations"] boxes = [annotation["bbox"] for annotation in annotations] classes = [annotation["category_id"] for annotation in annotations] boxes = BoundingBoxesOnImage( [ BoundingBox(*box, label=class_) for box, class_ in zip(boxes, classes) ], shape=image.shape, ) image, boxes = AUGMENTER(image=image, bounding_boxes=boxes) classes = [bbox.label for bbox in boxes.bounding_boxes] boxes = np.array([[box.x1, box.y1, box.x2, box.y2] for box in boxes.items]) image = image[..., [2, 1]] image = image.transpose(2, 0, 1).astype(np.float32) annotations = [{ "bbox": box, "bbox_mode": BoxMode.XYXY_ABS, "category_id": class_ } for box, class_ in zip(boxes, classes)] record["image"] = torch.as_tensor(image) instances = utils.annotations_to_instances(annotations, image.shape[1:]) record["instances"] = utils.filter_empty_instances(instances) return record
def __call__(self, img, anno): gts = anno[1].numpy() np_img = np.asarray(img) scale = self.h / np_img.shape[0] # get BoundingBoxesOnImage bbs = [] for gt in gts: bbs.append(BoundingBox(x1=gt[0], y1=gt[1], x2=gt[2], y2=gt[3])) bbs_on_img = BoundingBoxesOnImage(bbs, shape=np_img.shape) # draw_img = bbs_on_img.draw_on_image(np_img, size=2) self.seq = iaa.Sequential( [iaa.Resize({ "height": self.h, "width": self.w })]) # apply augment image_aug = self.seq.augment_image(np_img) bbs_aug = self.seq.augment_bounding_boxes(bbs_on_img).bounding_boxes gts = [] for bb in bbs_aug: gts.append([bb.x1, bb.y1, bb.x2, bb.y2]) gts = torch.from_numpy(np.array(gts)) anno[2]["scale"] = scale return image_aug, (anno[0], gts, anno[2])
def __call__(self, img, anno): gts = anno[1].numpy() np_img = np.asarray(img) # get BoundingBoxesOnImage bbs = [] for gt in gts: bbs.append(BoundingBox(x1=gt[0], y1=gt[1], x2=gt[2], y2=gt[3])) bbs_on_img = BoundingBoxesOnImage(bbs, shape=np_img.shape) # draw_img = bbs_on_img.draw_on_image(np_img, size=2) x = np.random.randint(-self.x, self.x) y = np.random.randint(-self.y, self.y) self.seq = iaa.Sequential( [iaa.Affine(translate_percent={ "x": x, "y": y })]) # apply augment image_aug = self.seq.augment_image(np_img) bbs_aug = self.seq.augment_bounding_boxes(bbs_on_img).bounding_boxes gts = [] for bb in bbs_aug: gts.append([bb.x1, bb.y1, bb.x2, bb.y2]) gts = torch.from_numpy(np.array(gts)) return image_aug, (anno[0], gts, anno[2])
def __call__(self, img, anno): labels = anno[0].numpy() gts = anno[1].numpy() np_img = np.asarray(img) # get BoundingBoxesOnImage bbs = [] for gt in gts: bbs.append(BoundingBox(x1=gt[0], y1=gt[1], x2=gt[2], y2=gt[3])) bbs_on_img = BoundingBoxesOnImage(bbs, shape=np_img.shape) # draw_img = bbs_on_img.draw_on_image(np_img, size=2) r = np.random.sample() p = -self.percent + 2 * self.percent * r self.seq = iaa.Sequential([iaa.CropAndPad(percent=p, keep_size=False)]) # apply augment image_aug = self.seq.augment_image(np_img) bbs_aug = self.seq.augment_bounding_boxes(bbs_on_img).bounding_boxes gts = [] for bb in bbs_aug: gts.append([bb.x1, bb.y1, bb.x2, bb.y2]) gts = torch.from_numpy(np.array(gts)) return image_aug, (anno[0], gts, anno[2])
def augment_transform(image, bbox, text, random_remove_box=0.15): image_h, image_w = image.shape[-2:] ty = random.uniform(-random_y_translation, random_y_translation) tx = random.uniform(-random_x_translation, random_x_translation) aug_bbs = [] for i in range(bbox.shape[0]): x1, y1, x2, y2 = bbox[i] x1, y1, x2, y2 = x1 * image_w, y1 * image_h, x2 * image_w, y2 * image_h aug_bbs.append(BoundingBox(x1=x1, y1=y1, x2=x2, y2=y2)) bbs = BoundingBoxesOnImage(aug_bbs, shape=image.shape) seq = iaa.Sequential([ iaa.Affine( cval=255, translate_px={ "x": int(tx * image.shape[1]), "y": int(ty * image.shape[0]) }, scale=(0.9, 1.1), ), iaa.Crop(percent=(0., 0., .3, 0.)) ]) image_aug, bbs_aug = seq(image=image, bounding_boxes=bbs) bbox_out = [] for bb_aug in bbs_aug: x1, y1, x2, y2 = bb_aug.x1, bb_aug.y1, bb_aug.x2, bb_aug.y2 x1, y1, x2, y2 = x1 / image_w, y1 / image_h, x2 / image_w, y2 / image_h bbox_out.append([x1, y1, x2, y2]) bbox_out = np.array(bbox_out) return transform(image_aug, bbox_out, text)
def _read_labelme(_path_list, _pos): for i, name in enumerate(_path_list): pos = (_pos + i) with open(os.path.join(self.label_path, name[:name.rfind(".")] + ".json"), encoding=self.encoding) as f: jdata = f.read() data = json.loads(jdata) if self.img_path is None: img64 = data['imageData'] img = Image.open(BytesIO(base64.b64decode(img64))) else: img = Image.open(os.path.join(self.img_path, name)) img, zoom_r = _process_img(img, self.size) bbs = [] labels = [] for i in range(len(data['shapes'])): if data['shapes'][i]['label'] in self.class_names: label_text = data['shapes'][i]['label'] label = self.class_names.index(label_text) labels.append(label) point = np.array(data['shapes'][i]['points']) point = point / zoom_r bbs.append( BoundingBox(x1=point[0, 0], y1=point[0, 1], x2=point[1, 0], y2=point[1, 1])) bbs = BoundingBoxesOnImage(bbs, shape=img.shape) _imgaug_to_array(img, bbs, self.grid_shape, pos, labels)
def __call__(self, img, anno): if np.random.random_sample() > self.probability: return img, anno gts = anno[1].numpy() np_img = np.asarray(img) # get BoundingBoxesOnImage bbs = [] for gt in gts: bbs.append(BoundingBox(x1=gt[0], y1=gt[1], x2=gt[2], y2=gt[3])) bbs_on_img = BoundingBoxesOnImage(bbs, shape=np_img.shape) # draw_img = bbs_on_img.draw_on_image(np_img, size=2) self.seq = iaa.Sequential([iaa.Flipud(1)]) # apply augment image_aug = self.seq.augment_image(np_img) bbs_aug = self.seq.augment_bounding_boxes(bbs_on_img).bounding_boxes gts = [] for bb in bbs_aug: gts.append([bb.x1, bb.y1, bb.x2, bb.y2]) gts = torch.from_numpy(np.array(gts)) return image_aug, (anno[0], gts, anno[2])
def deep_data_aug(image, bb_list): import imgaug as ia import imgaug.augmenters as iaa from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage bbs = BoundingBoxesOnImage([ BoundingBox(x1=bb_list[0][0], y1=bb_list[0][1], x2=bb_list[1][0], y2=bb_list[1][1]), ], shape=image.shape) seq = iaa.Sometimes( 0.5, iaa.SomeOf( (0, None), [ iaa.Fliplr(0.3), iaa.Flipud(0.2), iaa.GaussianBlur(sigma=(0.0, 3.0)), iaa.Multiply( (1.2, 1.5)), # change brightness, doesn't affect BBs iaa.AdditiveGaussianNoise(scale=(0, 0.05 * 255)) ], random_order=True)) # Augment BBs and images. image_aug, bbs_aug = seq(image=image, bounding_boxes=bbs) formated_bbs_aug = [[ bbs_aug.bounding_boxes[0].x1, bbs_aug.bounding_boxes[0].y1, bbs_aug.bounding_boxes[0].x2, bbs_aug.bounding_boxes[0].y2 ]] return image_aug, formated_bbs_aug
def updateBoundingBoxes(row, augmentor, image): ia.seed(random.randrange(1,100,1)) #image = ia.quokka(size=(WIDTH, HEIGHT)) # Bounding box coordinates start at row[2] # and go in the order Xmin, Ymin, Xmax, Ymax # there are 7 bounding boxes per image & a class label between each one bbs = BoundingBoxesOnImage([ BoundingBox(x1 = float(row[2]), y1=float(row[3]), x2=float(row[4]), y2=float(row[5])), BoundingBox(x1 = float(row[7]), y1=float(row[8]), x2=float(row[9]), y2=float(row[10])), BoundingBox(x1 = float(row[12]), y1=float(row[13]), x2=float(row[14]), y2=float(row[15])), BoundingBox(x1 = float(row[17]), y1=float(row[18]), x2=float(row[19]), y2=float(row[20])), BoundingBox(x1 = float(row[22]), y1=float(row[23]), x2=float(row[24]), y2=float(row[25])), BoundingBox(x1 = float(row[27]), y1=float(row[28]), x2=float(row[29]), y2=float(row[30])), BoundingBox(x1 = float(row[32]), y1=float(row[33]), x2=float(row[34]), y2=float(row[35])) ], shape = image.shape) image_aug, bbs_aug = augmentor(image = image, bounding_boxes=bbs) # update new_row with new BB Values offset = 2 #modified = bbs_aug.remove_out_of_image().clip_out_of_image() #print(len(bbs.bounding_boxes)) for i in range(len(bbs_aug.bounding_boxes)): #print(offset, offset+3) current = bbs_aug.bounding_boxes[i] row[offset] = str(current.x1) row[offset+1] = str(current.y1) row[offset+2] = str(current.x2) row[offset+3] = str(current.y2) offset = offset + 5 # For debug #image_aug = bbs_aug.draw_on_image(image_aug, size=2, color=[0,0,255]) #imshow(image_aug) return image_aug, row
def normalize_bounding_boxes(inputs, shapes=None): # TODO get rid of this deferred import from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage shapes = _preprocess_shapes(shapes) ntype = estimate_bounding_boxes_norm_type(inputs) _assert_exactly_n_shapes_partial = functools.partial( _assert_exactly_n_shapes, from_ntype=ntype, to_ntype="List[BoundingBoxesOnImage]", shapes=shapes) if ntype == "None": return None elif ntype in ["array[float]", "array[int]", "array[uint]"]: _assert_single_array_ndim(inputs, 3, "(N,B,4)", "BoundingBoxesOnImage") _assert_single_array_last_dim_exactly(inputs, 4, "BoundingBoxesOnImage") _assert_exactly_n_shapes_partial(n=len(inputs)) return [ BoundingBoxesOnImage.from_xyxy_array(attr_i, shape=shape) for attr_i, shape in zip(inputs, shapes) ] elif ntype == "tuple[number,size=4]": _assert_exactly_n_shapes_partial(n=1) return [ BoundingBoxesOnImage( [BoundingBox( x1=inputs[0], y1=inputs[1], x2=inputs[2], y2=inputs[3])], shape=shapes[0]) ] elif ntype == "BoundingBox": _assert_exactly_n_shapes_partial(n=1) return [BoundingBoxesOnImage([inputs], shape=shapes[0])] elif ntype == "BoundingBoxesOnImage": return [inputs] elif ntype == "iterable[empty]": return None elif ntype in ["iterable-array[float]", "iterable-array[int]", "iterable-array[uint]"]: _assert_many_arrays_ndim(inputs, 2, "(B,4)", "BoundingBoxesOnImage") _assert_many_arrays_last_dim_exactly(inputs, 4, "BoundingBoxesOnImage") _assert_exactly_n_shapes_partial(n=len(inputs)) return [ BoundingBoxesOnImage.from_xyxy_array(attr_i, shape=shape) for attr_i, shape in zip(inputs, shapes) ] elif ntype == "iterable-tuple[number,size=4]": _assert_exactly_n_shapes_partial(n=1) return [ BoundingBoxesOnImage( [BoundingBox(x1=x1, y1=y1, x2=x2, y2=y2) for x1, y1, x2, y2 in inputs], shape=shapes[0]) ] elif ntype == "iterable-BoundingBox": _assert_exactly_n_shapes_partial(n=1) return [BoundingBoxesOnImage(inputs, shape=shapes[0])] elif ntype == "iterable-BoundingBoxesOnImage": return inputs elif ntype == "iterable-iterable[empty]": return None elif ntype == "iterable-iterable-tuple[number,size=4]": _assert_exactly_n_shapes_partial(n=len(inputs)) return [ BoundingBoxesOnImage.from_xyxy_array( np.array(attr_i, dtype=np.float32), shape=shape) for attr_i, shape in zip(inputs, shapes) ] else: assert ntype == "iterable-iterable-BoundingBox", ( "Got unknown normalization type '%s'." % (ntype,)) _assert_exactly_n_shapes_partial(n=len(inputs)) return [BoundingBoxesOnImage(attr_i, shape=shape) for attr_i, shape in zip(inputs, shapes)]