Ejemplo n.º 1
0
                        for extra in range(3):
                            center = np.array([
                                int(image.shape[0] / 2),
                                int(image.shape[1] / 2)
                            ])
                            deg = np.random.randint(-10, 10)
                            #deg = np.random.normal()*30
                            newimg = aug.salt_and_pepper(
                                aug.gaussian_blur(image))

                            #.3 probability for each of shifting vs rotating vs shift(rotate(image))
                            p = np.random.randint(0, 3)
                            if p == 0:
                                newimg, nb = aug.shift_image(newimg, box[idx])
                            elif p == 1:
                                newimg, nb = aug.rotate_image_and_boxes(
                                    newimg, deg, center, box[idx])
                            elif p == 2:
                                newimg, nb = aug.rotate_image_and_boxes(
                                    newimg, deg, center, box[idx])
                                newimg, nb = aug.shift_image(newimg, nb)

                            newimg = (newimg).astype(np.uint8)

                            if idx % 1000 == 0 and SAVE_IMAGES:
                                Image.fromarray(newimg).save(
                                    'process/img_%s_%s_%s.png' %
                                    (name, extra, it[0]))

                            if len(nb) > 0:
                                tf_example = tfr.to_tf_example(
                                    newimg, nb, classes_final[idx])
                            # remove and gaussian blur

                            newimg = aug.gaussian_blur(image)
                            #newimg = image

                            #.3 probability for each of shifting vs rotating vs shift(rotate(image))
                            p = np.random.randint(0, 3)
                            # debug
                            # modified to use the removed cloud version of bboxes
                            # image, new_coords, new_classes
                            if p == 0:
                                newimg, nb = aug.shift_image(
                                    newimg, new_coords)
                                #newimg,nb = aug.shift_image(newimg,box[idx])
                            elif p == 1:
                                newimg, nb = aug.rotate_image_and_boxes(
                                    newimg, deg, center, new_coords)
                                #newimg,nb = aug.rotate_image_and_boxes(newimg,deg,center,box[idx])
                            elif p == 2:
                                newimg, nb = aug.rotate_image_and_boxes(
                                    newimg, deg, center, new_coords)
                                #newimg,nb = aug.rotate_image_and_boxes(newimg,deg,center,box[idx])
                                newimg, nb = aug.shift_image(newimg, nb)

                            newimg = (newimg).astype(np.uint8)

                            if idx % 100 == 0 and SAVE_IMAGES:
                                #debug
                                # changed save dir
                                Image.fromarray(newimg).save(
                                    './augmented_img_60/img_%s_%s_%s.png' %
                                    (name, extra, it[0]))
                tot_box += len(box[idx])
                if name in train_fs:
                    img_dict, annos, ann_id = output_img(
                        image, box[idx], classes_final[idx], out_train_dir,
                        fname, idx, ind_chips, ann_id, cat_list)
                    train_ann['annotations'].extend(annos)
                    train_ann['images'].append(img_dict)
                    train_chips += 1

                    if AUGMENT:
                        degs = [10, 90, 180, 270]
                        center = (int(image.shape[0] / 2),
                                  int(image.shape[1] / 2))
                        for deg in degs:
                            ind_chips += 1
                            nimage, nbox = aug.rotate_image_and_boxes(
                                image, deg, center, box[idx])
                            img_dict, annos, ann_id = output_img(
                                nimage,
                                nbox,
                                classes_final[idx],
                                out_train_dir,
                                fname,
                                idx,
                                ind_chips,
                                ann_id,
                                cat_list,
                                aug=True)
                            train_ann['annotations'].extend(annos)
                            train_ann['images'].append(img_dict)
                            train_chips += 1
                elif name in val_fs:
    num_chips = len(c_img)
    print("\tNum Chips: ", num_chips)

    # For each image chip (i in range(num_chips))
    for i in range(num_chips):

        print("\t\tChip #: ", i)

        # Calculate the center of the chip
        center = (int(c_img[i].shape[0] / 2), int(c_img[i].shape[1] / 2))

        # For each of the desired rotation degrees
        for deg in DEGREES:

            # Rotate the original chip and get the updated image/boxes/classes
            tmp_img, tmp_box, tmp_cls = aug.rotate_image_and_boxes(
                c_img[i], deg, center, c_box[i], c_cls[i])

            # Git rid of very small boxes that are artifacts of chipping
            final_boxes = []
            final_classes = []
            final_classes_simple = []

            # Clip boxes correctly!!
            clipped_boxes = []
            for box in tmp_box:
                xMin, yMin, xMax, yMax = box
                xMin = clip(xMin, 0, CHIP_SHAPE[0] - 1)
                yMin = clip(yMin, 0, CHIP_SHAPE[1] - 1)
                xMax = clip(xMax, 0, CHIP_SHAPE[0] - 1)
                yMax = clip(yMax, 0, CHIP_SHAPE[1] - 1)
                clipped_boxes.append([xMin, yMin, xMax, yMax])
#								...
#							])
#					1: array([ [...], [...], ... ])
#					...
#				}
#
# c_cls = dictionary keyed by integer and values are arrays of ints which are class labels.
#   i.e. c_cls = {
#					0: [10, 15, 21, 29, 71, 20]
#					1: [30, 55, 22, 19, 61, 30]
#					...
#				}
# The keys of the dictionaries match up with len(c_img). The details for chip
#   c_img[0] are stored in c_box[0] and c_cls[0]. So, len(c_img) == len(c_box) == len(c_cls)
c_img, c_box, c_cls = wv.chip_image(img=arr,
                                    coords=coords,
                                    classes=classes,
                                    shape=(700, 700))

# Augment the data
ind = np.random.choice(range(c_img.shape[0]))
center = (int(c_img[ind].shape[0] / 2), int(c_img[ind].shape[1] / 2))

for deg in range(0, 360, 45):
    rot_im, rot_boxes = aug.rotate_image_and_boxes(c_img[ind], deg, center,
                                                   c_box[ind])
    pim = aug.draw_bboxes(rot_im, rot_boxes)
    plt.imshow(pim)
    plt.title("deg: {}".format(deg))
    plt.show()