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 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 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 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 visualise_policy( image_path: str, save_path: str, bounding_boxes: List[List[int]], labels: Iterable[int], policy: List[List[POLICY_TUPLE_TYPE]], name_to_augmentation: Dict[str, Callable] = NAME_TO_AUGMENTATION) -> None: """ Visualise a single policy on an image :type image_path: str :param image_path: Path of the image :type save_path: str :param save_path: Directory where to save the images :type bounding_boxes: List[List[int]] :param bounding_boxes: Bounding boxes for the image :type labels: Iterable[int] :param labels: Iterable containing class labels as integers :type policy: List[List[POLICY_TUPLE_TYPE]] :param policy: The policy set to apply to the image :type name_to_augmentation: Dict[str, Callable] :param name_to_augmentation: Dictionary mapping of the augmentation name to the augmentation method :rtype: None """ policy_container = PolicyContainer( policy, name_to_augmentation=name_to_augmentation) for i, pol in enumerate(policy): fig, axes = plt.subplots(1, 3, figsize=(15, 4)) image = imageio.imread(image_path) [ax.axis('off') for ax in axes] for ax in range(3): img_aug, bbs_aug = policy_container.apply_augmentation( pol, image, bounding_boxes, labels, ) bbs_aug = BoundingBoxesOnImage( [BoundingBox(*box[1:], label=box[0]) for box in bbs_aug], shape=image.shape) axes[ax].imshow(bbs_aug.draw_on_image(img_aug, size=2)) fig.suptitle(pol) fig.tight_layout() fig.savefig(f'{save_path}/sub_policy_{i}.png')
def chapter_examples_bounding_boxes_simple(): import imgaug as ia import imgaug.augmenters as iaa from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage ia.seed(1) image = ia.quokka(size=(256, 256)) bbs = BoundingBoxesOnImage([ BoundingBox(x1=65, y1=100, x2=200, y2=150), BoundingBox(x1=150, y1=80, x2=200, y2=130) ], 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) # print coordinates before/after augmentation (see below) # use .x1_int, .y_int, ... to get integer coordinates for i in range(len(bbs.bounding_boxes)): before = bbs.bounding_boxes[i] after = bbs_aug.bounding_boxes[i] print("BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)" % (i, before.x1, before.y1, before.x2, before.y2, after.x1, after.y1, after.x2, after.y2)) # 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]) # ------------ save("examples_bounding_boxes", "simple.jpg", grid([image_before, image_after], cols=2, rows=1), quality=90)
def plot(image, output, color=[255, 0, 0]): bbs = BoundingBoxesOnImage([ BoundingBox(x1=x[0], x2=x[2], y1=x[1], y2=x[3]) for x in output['boxes'] ], shape=image.shape) image_bbs = bbs.draw_on_image(image, size=2, color=color) ret_key = False while True: cv2.imshow('Inference', cv2.cvtColor(image_bbs, cv2.COLOR_RGB2BGR)) # np.clip(image_bbs, 0, 1) k = cv2.waitKey(0) & 0xFF if k == ord('w') or k == ord('q'): ret_key = k == ord('w') break return ret_key
def draw_2d_boxes_from_objects_advanced( objects, calib, img, default_color=(0, 1, 0), #! default color is green color_func=None, text_func=None, size=3 ): color_dict = dict() for label_ind, label in enumerate(objects): if color_func is not None: color = color_func(label_ind, label) if color is None: continue else: color = default_color if text_func is not None: text = text_func(label_ind, label) if text is "": text = None else: text = None bbox = label.get_imgaug_bbox() bbox.label = text if color not in color_dict.keys(): color_dict[color] = { "boxes": [] } color_dict[color]['boxes'].append(bbox) for color, val in color_dict.items(): bboxes = BoundingBoxesOnImage(val['boxes'], shape=img.shape[:2]) #! flip color tuple b/c img is bgr and provided color is rgb img = bboxes.draw_on_image(img, color=tuple(int(i * 255) for i in color)[::-1], size=size) return img
def main(): config, use_val = load_configurations() training_config = config["training_configuration"] data_config = config["data_configuration"] mode = "train" if use_val: mode = "val" dataset = ImageDataset( data_config=data_config, transform=True, mode=mode ) dataloader = DataLoader( dataset, batch_size=training_config["batch_size"], num_workers=0, shuffle=True ) images, labels = next(iter(dataloader)) bounding_boxes = convert_to_bounding_boxes(labels) bbs_applied_images = [] for image, bbs in zip(images, bounding_boxes): bbs = BoundingBoxesOnImage(bbs, shape=image.shape) bbs_applied_images.append( bbs.draw_on_image(image, size=2, color=[0, 0, 255]) ) batch_image = cv2.vconcat(bbs_applied_images) file_name = "image.jpg" cv2.imwrite(file_name, batch_image)
def main(): config = load_configurations() encoder_config = config['simple_model_configuration'] data_encoder = DataEncoder(encoder_config) image_path = "data/val_images/000000086220.jpg" image = cv2.imread(image_path) aug = iaa.Resize({"height": 300, "width": 300}) resized_image = aug(image=image) default_boxes = data_encoder.default_boxes box_groups = get_box_groups(encoder_config, default_boxes) set_of_boxes = random.choice(box_groups[10]) set_of_boxes = (set_of_boxes * 300) print(set_of_boxes) vis_boxes = [] for box in set_of_boxes: vis_boxes.append(BoundingBox( x1=box[0], y1=box[1], x2=box[2], y2=box[3] ) ) bbs = BoundingBoxesOnImage(vis_boxes, shape=resized_image.shape) image_with_box = bbs.draw_on_image(resized_image, size=2) cv2.imwrite("example.png", image_with_box)
def test_plot(image, bbox=None): fig = plt.figure() image_ = image.clone().detach() image = denormalize(image) if bbox is None: plt.imshow(image) else: bbs = BoundingBoxesOnImage( [BoundingBox(x1=x[0], x2=x[2], y1=x[1], y2=x[3]) for x in bbox], shape=image.shape) image_bbs = bbs.draw_on_image(image, size=2, color=[255, 0, 0]) print(image, image_bbs) ax1 = fig.add_subplot(121) ax2 = fig.add_subplot(122) ax1.imshow(image) ax2.imshow(image_bbs) plt.show() plt.pause(0.1)
bbox_saved.append(line.strip().split()) count += 1 for bbox_index in range(0, count): bbox_center_x = float(bbox_saved[bbox_index][1]) * width bbox_center_y = float(bbox_saved[bbox_index][2]) * height bbox_width = float(bbox_saved[bbox_index][3]) * width bbox_height = float(bbox_saved[bbox_index][4]) * height bbox_top_left_x = bbox_center_x - bbox_width / 2 bbox_top_left_y = bbox_center_y - bbox_height / 2 bbox_bottom_right_x = bbox_center_x + bbox_width / 2 bbox_bottom_right_y = bbox_center_y + bbox_height / 2 bbox.append( BoundingBox(bbox_top_left_x, bbox_top_left_y, bbox_bottom_right_x, bbox_bottom_right_y)) bbs = BoundingBoxesOnImage(bbox, shape=image.shape) ia.imshow(bbs.draw_on_image(image, size=2)) del lines[:] del bbox_saved[:] del bbox_image[:] del bbox[:] #bbox 좌푣ㄹ ''' bbox_top_left_x = line[1]*height bbox_top_left_y = line[2]*width bbox_bottom_right_x = line[3]*height bbox_bottom_right_y = line[4]*width bbs = BoundingBoxesOnImage([BoundingBox(bbox_top_left_x, bbox_top_left_y, bbox_bottom_right_x, bbox_bottom_right_y)], shape = image.shape) ia.imshow(bbs.draw_on_image(image, size=1)) ''' #resize는 생략할것
def rotate(img, objs, imageFileName): newImg = img newObjects = objs BBoxs = [ BoundingBox(x1=0, x2=0, y1=0, y2=0), BoundingBox(x1=0, x2=0, y1=0, y2=0), BoundingBox(x1=0, x2=0, y1=0, y2=0), BoundingBox(x1=0, x2=0, y1=0, y2=0) ] p1 = [0, 0] p2 = [0, 0] for inde, obj in enumerate(objs): if obj["tags"][0] in classes: x1 = obj["box"]["x1"] y1 = obj["box"]["y1"] x2 = obj["box"]["x2"] y2 = obj["box"]["y2"] if obj["tags"][0] in ["top", "2"]: BBoxs[0] = BoundingBox(x1=x1, x2=x2, y1=y1, y2=y2, label="top") p1 = returnCentre([x1, y1, x2, y2]) elif obj["tags"][0] in ["bottom", "7"]: BBoxs[1] = BoundingBox(x1=x1, x2=x2, y1=y1, y2=y2, label="bottom") p2 = returnCentre([x1, y1, x2, y2]) elif obj["tags"][0] in ["1"]: BBoxs[2] = BoundingBox(x1=x1, x2=x2, y1=y1, y2=y2, label="influenza") p3 = returnCentre([x1, y1, x2, y2]) BBoxs[3] = BoundingBox(x1=0, x2=obj["width"], y1=0, y2=obj["height"], label="entireimage") if max(p1) > 0 and max(p2) > 0: p1 = np.array(p1) p2 = np.array(p2) angleToRotate, im0, scale_percent, quad, _ = angle_with_yaxis( p1, p2, img, [], 0) angleToRotate = angleToRotate + 90 seqAug = returnAugmentationObj(angleToRotate, scale_percent) bbs = BoundingBoxesOnImage(BBoxs, shape=img.shape) newshape = [img.shape[0] * scale_percent, img.shape[1] * scale_percent] image_aug, bbs_aug = seqAug(image=img, bounding_boxes=bbs) Imagebb = bbs_aug.bounding_boxes[-1] topBB = bbs_aug.bounding_boxes[0] bottBB = bbs_aug.bounding_boxes[1] p1 = returnCentre([topBB.x1, topBB.y1, topBB.x2, topBB.y2]) p2 = returnCentre([bottBB.x1, bottBB.y1, bottBB.x2, bottBB.y2]) centreRDT = returnCentre([p2[0], p2[1], p1[0], p1[1]]) # print(centreRDT) leftLim = centreRDT[0] - 640 rightLim = centreRDT[0] + 640 topLim = centreRDT[1] - 360 botLim = centreRDT[1] + 360 crop_top = int(topLim) #int(Imagebb.y1) crop_right = int( -rightLim + image_aug.shape[1]) #int(-Imagebb.x2+image_aug.shape[1]) crop_bott = int( -botLim + image_aug.shape[0]) #int(-Imagebb.y2+image_aug.shape[0]) crop_left = int(leftLim) #int(Imagebb.x1) X = max(bbs_aug.bounding_boxes[-1].x1, bbs_aug.bounding_boxes[-1].x2) x1_inf = bbs_aug.bounding_boxes[0].x1 + 152 y1_inf = bbs_aug.bounding_boxes[0].y1 x2_inf = min(bbs_aug.bounding_boxes[0].x2 + 170, X) y2_inf = bbs_aug.bounding_boxes[0].y2 listOfbb = bbs_aug.bounding_boxes imggrayaug = cv2.cvtColor(image_aug, cv2.COLOR_BGR2GRAY) infCalculatedFlag = False if (y2_inf - y1_inf) > 90: for i in range(3): yy = listOfbb[i].y2 y = listOfbb[i].y1 offset = ((yy - y) - 90) / 2 listOfbb[i].y2 -= offset listOfbb[i].y1 += offset offset = ((y2_inf - y1_inf) - 90) / 2 y2_inf -= offset y1_inf += offset if listOfbb[2].label == None: listOfbb[2] = (BoundingBox(x1=x1_inf, x2=x2_inf, y1=y1_inf, y2=y2_inf)) bbs_aug = BoundingBoxesOnImage(listOfbb, shape=image_aug.shape) infCalculatedFlag = True listOfbb[2].label = "influenza" histg = cv2.calcHist([ imggrayaug[int(y1_inf):int(y2_inf), int(x1_inf):int(bbs_aug.bounding_boxes[0].x2 + 170)] ], [0], None, [2], [0, 256]) # print(X) factor = 2 if histg[1] != 0: factor = histg[0] / histg[1] else: pass if infCalculatedFlag: if factor < 2: # listOfbb[2]=BoundingBox(x1=x1_inf,y1=y1_inf,x2=x2_inf,y2=y2_inf) bbs_aug = BoundingBoxesOnImage(listOfbb, shape=image_aug.shape) seqAug2 = returnAugmentationObjCrop(crop_top, crop_right, crop_bott, crop_left) image_aug, bbs_aug = seqAug2(image=image_aug, bounding_boxes=bbs_aug) image_with_bbs = bbs_aug.draw_on_image(image_aug) cv2.imwrite(os.path.join(outdirectory, "rot" + imageFileName), image_with_bbs) else: listOfbb[2] = BoundingBox(x1=0, y1=0, x2=0, y2=0, label=None) bbs_aug = BoundingBoxesOnImage(listOfbb, shape=image_aug.shape) seqAug2 = returnAugmentationObjCrop(crop_top, crop_right, crop_bott, crop_left) image_aug, bbs_aug = seqAug2(image=image_aug, bounding_boxes=bbs_aug) image_with_bbs = bbs_aug.draw_on_image(image_aug) cv2.imwrite(os.path.join(outdirectory, "rot" + imageFileName), image_with_bbs) else: # bbs_aug =BoundingBoxesOnImage(listOfbb, shape=image_aug.shape) seqAug2 = returnAugmentationObjCrop(crop_top, crop_right, crop_bott, crop_left) image_aug, bbs_aug = seqAug2(image=image_aug, bounding_boxes=bbs_aug) image_with_bbs = bbs_aug.draw_on_image(image_aug) cv2.imwrite(os.path.join(outdirectory, "rot" + imageFileName), image_with_bbs) # image_with_bbs = bbs.draw_on_image(img) ###########UNCOMMENT FOR CREATING YOLO TRAINING FILES lock.acquire() with open("rdt_train_crop.txt", "a") as fout: boxes = bbs_aug.bounding_boxes fout.write(os.path.join(horizontalImages, imageFileName) + " ") for bb in boxes: if bb.label == "influenza": annots = [str(x) for x in [bb.x1, bb.y1, bb.x2, bb.y2]] annots = ",".join(annots) + ",0 " fout.write(annots) if bb.label == "top": annots = [str(x) for x in [bb.x1, bb.y1, bb.x2, bb.y2]] annots = ",".join(annots) + ",1 " fout.write(annots) if bb.label == "bottom": annots = [str(x) for x in [bb.x1, bb.y1, bb.x2, bb.y2]] annots = ",".join(annots) + ",2 " fout.write(annots) fout.write("\n") cv2.imwrite(os.path.join(horizontalImages, imageFileName), image_aug) with open("anchors.txt", "a") as fout: for bb in bbs_aug.bounding_boxes[:-1]: w = bb.x2 - bb.x1 h = bb.y2 - bb.y1 # bbox_size.append(np.array([w,h])) fout.write(str(w) + "," + str(h) + "\n") lock.release() ###################END BLOCK return newImg, newObjects
def main(): filename = '/home/morgan/Documents/FROMTHINKPAD_IMAGES/both_images_and_xml/racecar00034' xml = '.xml' jpg = '.jpg' jpeg = '.jpeg' png = '.png' tree = ET.parse(filename + xml) root = tree.getroot() image = imageio.imread(filename + jpg) # gets all bounding boxes from a certain file and creates bbs with them allboxes = getAllBoundingBoxes(filename + xml, root) allbbs = [] for box in allboxes: box_x1 = box[0] box_y1 = box[1] box_x2 = box[2] box_y2 = box[3] newBox = BoundingBox(x1=box_x1, x2=box_x2, y1=box_y1, y2=box_y2) allbbs.append(newBox) # store all bounding boxes bbs = BoundingBoxesOnImage(allbbs, shape=image.shape) # augmentation sequence # seq = iaa.Sequential([ #iaa.GammaContrast(0.25) # add contrast #iaa.MotionBlur(k=50, angle=[-45, 135]), # motion blur #iaa.GaussianBlur(sigma=0.0) # gaussian blur #iaa.MeanShiftBlur() # meanshift blur #iaa.Affine(translate_percent={"x": 0.1}, scale=0.7), # translate the image #iaa.Affine(translate_percent={"y": 0.0}, scale=0.8), #iaa.Fliplr(p = 1.0), # apply horizontal flip #iaa.ElasticTransformation(alpha=(7.0), sigma=0.5), #iaa.PiecewiseAffine(scale=(0.2, 0.125)) #iaa.AveragePooling(((1, 7), (1, 7))) ]) seq2 = iaa.Sequential([ # can only be applied to images not bounding boxes # does not change position of pixels much so can use bounds from original image iaa.imgcorruptlike.ZoomBlur(severity=2), iaa.imgcorruptlike.Brightness(severity=2), iaa.imgcorruptlike.ElasticTransform(severity=2) ]) # apply augmentations image_aug, bbs_aug = seq(image=image, bounding_boxes=bbs) # apply augmentations image_aug = seq2(image=image) # creates new augmented bounding boxes aug_boxes = [] for i in range(len(bbs_aug.bounding_boxes)): aug_boxes.append(getAugBoundingBoxes(bbs_aug.bounding_boxes[i])) # add bounding boxes to images side_by_side = np.hstack([ bbs.draw_on_image( image, size=2), # blend the original image with bounding box bbs.draw_on_image( image_aug, size=2) # blend the augmented image with bounding box ]) # Plot with matplotlib imshow() fig, ax = plt.subplots(figsize=(10, 7)) ax.axis('off') plt.title('Augmentations for bounding boxes') ax.imshow(side_by_side) plt.show()
x1, y1 = cx - w / 2, cy - h / 2 x2, y2 = cx + w / 2, cy + h / 2 return (x1 * IMG_WIDTH), (y1 * IMG_HEIGHT), (x2 * IMG_WIDTH), (y2 * IMG_HEIGHT) file_name_base_name = '202103041451520961' base_folder = '/mnt/AI_2TB/drop_inlet/new_dataset/merged/' subfolder = 'val/labels/val' predicted_image_folder = 'YOLO_V4/val_test/' img_file = os.path.join(predicted_image_folder, '{}.jpg'.format(file_name_base_name)) img = imageio.imread(img_file) label_file_name = os.path.join(base_folder, subfolder, '{}.txt'.format(file_name_base_name)) file_handler = open(label_file_name, 'r') _, xc, yc, h, w = file_handler.readline().split(' ') x2, y2, x1, y1 = yolo_to_box2d( [float(xc), float(yc), float(h), float(w)], img.shape[1], img.shape[0]) bbs_final = BoundingBoxesOnImage([BoundingBox(x1=x1, x2=x2, y1=y1, y2=y2)], shape=img.shape) test = bbs_final.draw_on_image(img, size=2) imageio.imwrite('{}_IoU.jpg'.format(file_name_base_name), test)
import cv2 import numpy as np import imgaug as ia import imgaug.augmenters as iaa from imgaug.augmentables.bbs import BoundingBoxesOnImage, BoundingBox image = cv2.imread('input_image.jpg', 1) bbs = BoundingBoxesOnImage([ BoundingBox(x1=125.5, y1=485.5, x2=400.5, y2=680.5), BoundingBox(x1=400.5, y1=750.5, x2=700.5, y2=800.5) ], shape=image.shape) image_before = bbs.draw_on_image(image, size=2) cv2.imwrite("before_augmentation.jpg", image_before) seq = iaa.Sequential([ iaa.AdditiveGaussianNoise(scale=0.05*255), iaa.Fliplr(0.9) ]) image_aug, bbs_aug = seq(image=image, bounding_boxes=bbs) # print coordinates before/after augmentation (see below) # use .x1_int, .y_int, ... to get integer coordinates for i in range(len(bbs.bounding_boxes)): before = bbs.bounding_boxes[i] after = bbs_aug.bounding_boxes[i] print("BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)" % ( i,