Beispiel #1
0
def draw_bbox_on_tiff(chip_path, coords, chips, classes, save_path):
    #Load an image
    #path = '/home/ubuntu/anyan/harvey_data/converted_sample_tiff/'
    
    
    # big tiff name: chip name
    # init to {big_tiff_name : []}
    #big_tiff_dict = dict((k, []) for k in big_tiff_set)
    big_tiff_dict = dict()

    fnames = glob.glob(chip_path + "*.tif")
   # i = 0
    for f in fnames:
        
        chip_name = f.split('/')[-1]
        chip_big_tiff_id_list = chip_name.split('_')[1:3]
        chip_big_tiff_id = '_'.join(chip_big_tiff_id_list)
        print(chip_big_tiff_id)
        if chip_big_tiff_id not in set(big_tiff_dict.keys()):
            big_tiff_dict[chip_big_tiff_id] = list()
        else:
            if len(big_tiff_dict[chip_big_tiff_id]) < 5:
                big_tiff_dict[chip_big_tiff_id].append(chip_name)
                arr = wv.get_image(f)
#             print(arr.shape)
    #         plt.figure(figsize=(10,10))
    #         plt.axis('off')
    #         plt.imshow(arr)
                coords_chip = coords[chips==chip_name]
            #print(coords_chip.shape)
                if coords_chip.shape[0] == 0:
                    continue
                classes_chip = classes[chips==chip_name].astype(np.int64)
    #         #We can chip the image into 500x500 chips
    #         c_img, c_box, c_cls = wv.chip_image(img = arr, coords= coords, classes=classes, shape=(500,500))
    #         print("Num Chips: %d" % c_img.shape[0])
                labelled = aug.draw_bboxes(arr,coords_chip[classes_chip ==1])
                print(chip_name)
#             plt.figure(figsize=(15,15))
#             plt.axis('off')
#             plt.imshow(labelled)
                subdir_name = save_path + chip_big_tiff_id
                if os.path.isdir(subdir_name):
                    save_name = subdir_name +'/' + chip_name + '.png'
                    print(save_name)
                    labelled.save(save_name)
                else:
                    os.mkdir(subdir_name)
                    save_name = subdir_name +'/' + chip_name + '.png'
                    print(save_name)
                    labelled.save(save_name)
           
            
            else:
                continue
Beispiel #2
0
    def parseChip(self,
                  c_img,
                  c_box,
                  c_cls,
                  c_ids,
                  img_num,
                  c_dir,
                  res_out=30,
                  showImg=False):
        # Parses chips, saves chip image, and also saves corresponding labels
        fnames = []

        outputImgDir = "{}labels/".format(c_dir)
        outputLabelDir = "{}images/".format(c_dir)
        self.checkDir(outputImgDir)
        self.checkDir(outputLabelDir)

        images = []
        sboxes, sclasses, simgs = [], [], []

        for c_idx in range(c_img.shape[0]):
            c_name = "{:06}_{:02}".format(int(img_num), c_idx)
            sbox,scls,simg,sid = \
                c_box[c_idx],c_cls[c_idx],c_img[c_idx], c_ids[c_idx]
            #print(sid)
            if showImg:
                labelled = aug.draw_bboxes(simg, sbox)
                plt.figure(figsize=(10, 10))
                plt.imshow(labelled)
                #print(c_box[:,4])
                plt.title(str(c_name) + " " + str(simg.shape))
                plt.axis('off')
                break

            # Change chip into darknet format, and save
            result = self.toDarknetFmt(sbox, scls, simg, sid)
            ff_l = "{}labels/{}.txt".format(c_dir, c_name)
            np.savetxt(ff_l, result, fmt='%i %1.6f %1.6f %1.6f %1.6f %i')
            # Save image to specified dir
            ff_i = "{}images/{}.jpg".format(c_dir, c_name)

            Image.fromarray(simg).save(ff_i)
            # Append file name to list
            fnames.append("{}images/{}.jpg".format(c_dir, c_name))
            pass
        return fnames
Beispiel #3
0
                            continue
                    '''

                    if local_class1 > 0:
                        num_class1_chip += 1
                    if local_class2 > 0:
                        num_class2_chip += 1

                    #else:
                    train_writer.write(tf_example.SerializeToString())
                    train_chips += 1
                    if SAVE_IMAGES and idx % 5 == 0:
                        # debug: changed save dir
                        #aug.draw_bboxes(image, new_coords[new_classes ==1]).save('./harvey_ms_img_inspect_train_2class_noclean/img_%s_%s.png'%(name,str(idx)))
                        aug.draw_bboxes(image, new_coords).save(
                            './tomnod_2class_train_inspect/img_%s_%s.png' %
                            (name, str(idx)))

                    ind_chips += 1

                    # debug
                    # store the training and validation images with bboxes for inspection
                    '''
                    if SAVE_IMAGES:
                                    # debug: changed save dir
                        aug.draw_bboxes(image, new_coords).save('./harvey_img_inspect/img_%s_%s.png'%(name,str(idx)))
                    '''

                    #Make augmentation probability proportional to chip size.  Lower chip size = less chance.
                    #This makes the chip-size imbalance less severe.
                    #prob = np.random.randint(0,np.max(res))
Beispiel #4
0
                                    (name, extra, it[0]))

                            if len(nb) > 0:
                                tf_example = tfr.to_tf_example(
                                    newimg, nb, classes_final[idx])

                                #Don't count augmented chips for chip indices
                                if idx < split_ind:
                                    test_writer.write(
                                        tf_example.SerializeToString())
                                    test_chips += 1
                                else:
                                    train_writer.write(
                                        tf_example.SerializeToString())
                                    train_chips += 1
                            else:
                                if SAVE_IMAGES:
                                    aug.draw_bboxes(newimg, nb).save(
                                        'process/img_nobox_%s_%s_%s.png' %
                                        (name, extra, it[0]))
        if res_ind == 0:
            max_chips_per_res = int(ind_chips * 1.5)
            logging.info("Max chips per resolution: %s " % max_chips_per_res)

        logging.info("Tot Box: %d" % tot_box)
        logging.info("Chips: %d" % ind_chips)

    logging.info("saved: %d train chips" % train_chips)
    logging.info("saved: %d test chips" % test_chips)
    train_writer.close()
    test_writer.close()
                    if local_class1 > 0:
                        num_class1_chip += 1
                    if local_class2 > 0:
                        num_class2_chip += 1

                    #if idx < split_ind:
                    test_writer.write(tf_example.SerializeToString())
                    test_chips += 1
                    if SAVE_IMAGES and idx % 5 == 0:
                        # debug: changed save dir
                        #debug
                        # draw only DAMAGED buildings
                        #aug.draw_bboxes(image, new_coords[new_classes ==1]).save('./harvey_ms_img_inspect_val_2class_noclean/img_%s_%s.png'%(name,str(idx)))
                        aug.draw_bboxes(image, new_coords).save(
                            './tomnod_valtest_2class_inspect/img_%s_%s.png' %
                            (name, str(idx)))

                    #else:
                    #   train_writer.write(tf_example.SerializeToString())
                    #  train_chips += 1
                    # if SAVE_IMAGES:
                    # debug: changed save dir
                    #aug.draw_bboxes(image, new_coords[new_classes ==1]).save('./harvey_ms_img_inspect_train_2class_noclean/img_%s_%s.png'%(name,str(idx)))
                    #    aug.draw_bboxes(image, new_coords).save('./harvey_ms_img_inspect_train_2class_noclean/img_%s_%s.png'%(name,str(idx)))

                    ind_chips += 1

                    # debug
                    # store the training and validation images with bboxes for inspection
                    '''
Beispiel #6
0
                                    train_writer.write(
                                        tf_example_aug.SerializeToString())
                                    num_aug = num_aug + 1
                                    train_chips += 1
                                    num_aug_per_class[
                                        class_id] = num_aug_per_class[
                                            class_id] + 1
                                    #           num_aug_this_class=num_aug_this_class + 1
                                    # debug
                                    if aug_idx % 10 == 0 and SAVE_IMAGES:
                                        # debug: changed save dir
                                        aug_image = (aug_image).astype(
                                            np.uint8)
                                        aug.draw_bboxes(
                                            aug_image, boxes_aug[aug_idx]
                                        ).save(
                                            './expand_aug_random_256/img_aug_%s_%s_%s_%s.png'
                                            % (name, str(idx), str(aug_idx),
                                               str(class_id)))
                            # debug
                            print('augmenting class: ', class_id)
                            print('number of augmentation: ', num_aug)
                        #num_aug_per_class[class_id] = num_aug_this_class

                    # it: iterator for different resolutions
                    # start to augment the rest
                    if AUGMENT and prob < it[0] and MINOR_CLASS_FLAG == False:

                        for extra in range(3):
                            center = np.array([
                                int(image.shape[0] / 2),
                                int(image.shape[1] / 2)
                if local_class1 > 0:
                    num_class1_chip += 1
                if local_class2 > 0:
                    num_class2_chip += 1

                    #if idx < split_ind:
                test_writer.write(tf_example.SerializeToString())
                test_chips += 1
                if SAVE_IMAGES and idx % 5 == 0:
                    # debug: changed save dir
                    #debug
                    # draw only DAMAGED buildings
                    #aug.draw_bboxes(image, new_coords[new_classes ==1]).save('./harvey_ms_img_inspect_val_2class_noclean/img_%s_%s.png'%(name,str(idx)))
                    aug.draw_bboxes(image, new_coords[new_classes == 1]).save(
                        './tomnod_valtest_1class_inspect/img_%s_%s.png' %
                        (name, str(idx)))

                    #else:
                    #   train_writer.write(tf_example.SerializeToString())
                    #  train_chips += 1
                    # if SAVE_IMAGES:
                    # debug: changed save dir
                    #aug.draw_bboxes(image, new_coords[new_classes ==1]).save('./harvey_ms_img_inspect_train_2class_noclean/img_%s_%s.png'%(name,str(idx)))
                    #    aug.draw_bboxes(image, new_coords).save('./harvey_ms_img_inspect_train_2class_noclean/img_%s_%s.png'%(name,str(idx)))

                ind_chips += 1

                # debug
                # store the training and validation images with bboxes for inspection
                '''
                            continue
                    '''

                    if local_class1 > 0:
                        num_class1_chip += 1
                    if local_class2 > 0:
                        num_class2_chip += 1

                    #else:
                    train_writer.write(tf_example.SerializeToString())
                    train_chips += 1
                    if SAVE_IMAGES and idx % 5 == 0:
                        # debug: changed save dir
                        #aug.draw_bboxes(image, new_coords[new_classes ==1]).save('./harvey_ms_img_inspect_train_2class_noclean/img_%s_%s.png'%(name,str(idx)))
                        aug.draw_bboxes(image, new_coords).save(
                            './noaa_train_2class_inspect/img_%s_%s.png' %
                            (name, str(idx)))

                    ind_chips += 1

                    # debug
                    # store the training and validation images with bboxes for inspection
                    '''
                    if SAVE_IMAGES:
                                    # debug: changed save dir
                        aug.draw_bboxes(image, new_coords).save('./harvey_img_inspect/img_%s_%s.png'%(name,str(idx)))
                    '''

                    #Make augmentation probability proportional to chip size.  Lower chip size = less chance.
                    #This makes the chip-size imbalance less severe.
                    #prob = np.random.randint(0,np.max(res))
#								...
#							])
#					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()
Beispiel #10
0
    def exportChipImages(self,
                         image_paths,
                         c_dir,
                         set_str,
                         res_out=30,
                         showImg=False,
                         shape=(600, 600),
                         chipImage=False):
        fnames = []
        #image_paths = sorted(image_paths)
        for img_pth in image_paths:
            try:
                img_pth = self.input_dir + 'train_images/' + img_pth
                img_name = img_pth.split("/")[-1]
                img_num = img_name.split(".")[0]
                arr = wv.get_image(img_pth)
                chip_coords = self.coords[self.chips == img_name]
                chip_classes = self.classes[self.chips == img_name].astype(
                    np.int64)
                chip_coords,chip_classes = \
                    self.filterClasses(chip_coords,chip_classes,self.grouped_classes)
                # Code for downsampling the image
                scale = self.res_native / res_out
                if res_out != 30:
                    arr = self.downsampleImage(arr, res_out=res_out)
                    chip_coords[:, :4] = chip_coords[:, :4] * scale
                # Chip the tif image into tiles
                c_img, c_box, c_cls, c_ids = chip_image(img=arr,
                                                        coords=chip_coords,
                                                        classes=chip_classes,
                                                        shape=shape,
                                                        chipImage=chipImage)
                if showImg:
                    result = []
                    for key in c_cls.keys():
                        result.extend(c_cls[key])
                    print("number of classes: {}".format(len(result)))
                    for i, img in enumerate(c_img[:5]):
                        labelled = aug.draw_bboxes(c_img[i], c_box[i])
                        plt.imshow(labelled)
                        plt.axis('off')
                        plt.show()
                        pass
                    break
                    pass
                c_fnames = self.parseChip(c_img, c_box, c_cls, c_ids, img_num,
                                          c_dir, res_out)
                fnames.extend(c_fnames)

            except FileNotFoundError as e:
                print(e)
                pass
            pass

        lines = sorted(fnames)

        if not os.path.exists(self.output_dir):
            os.makedirs(self.output_dir)
            pass

        outputTxtPath = self.output_dir + "xview_img_{}.txt".format(set_str)

        print(outputTxtPath)
        if os.path.exists(outputTxtPath):
            os.remove(outputTxtPath)
            pass

        with open(outputTxtPath, mode='w', encoding='utf-8') as myfile:
            myfile.write('\n'.join(lines))
        pass