def draw_bbox_on_tiff(bad_big_tif_list, chip_path, coords, chips, classes,
                      uids, save_path):

    i = 0
    # f is the filename without path, chip_name is the filename + path
    for f in bad_big_tif_list:
        if len(f) < 5:
            print('invalid file name: ', f)
            continue
        chip_name = os.path.join(chip_path, f)
        print(chip_name)

        arr = wv.get_image(chip_name)
        coords_chip = coords[chips == f]
        if coords_chip.shape[0] == 0:
            print('no bounding boxes for this image')
            continue
        classes_chip = classes[chips == f].astype(np.int64)
        uids_chip = uids[chips == f].astype(np.int64)
        labelled = draw_bboxes_withindex(arr, coords_chip[classes_chip == 1],
                                         uids_chip)
        print(chip_name)
        #             plt.figure(figsize=(15,15))
        #             plt.axis('off')
        #             plt.imshow(labelled)
        subdir_name = save_path
        if os.path.isdir(subdir_name):
            save_name = subdir_name + '/' + f + '.png'
            print(save_name)
            labelled.save(save_name)
        else:
            os.mkdir(subdir_name)
            save_name = subdir_name + '/' + f + '.png'
            print(save_name)
            labelled.save(save_name)
Beispiel #2
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 #3
0
def get_images_from_filename_array(coords,
                                   chips,
                                   classes,
                                   folder_names,
                                   res=(250, 250)):
    """
    Gathers and chips all images within a given folder at a given resolution.

    Args:
        coords: an array of bounding box coordinates
        chips: an array of filenames that each coord/class belongs to.
        classes: an array of classes for each bounding box
        folder_names: a list of folder names containing images
        res: an (X,Y) tuple where (X,Y) are (width,height) of each chip respectively

    Output:
        images, boxes, classes arrays containing chipped images, bounding boxes, and classes, respectively.
    """

    images = []
    boxes = []
    clses = []

    k = 0
    bi = 0

    for folder in folder_names:
        fnames = glob.glob(folder + "*.tif")
        fnames.sort()
        for fname in tqdm(fnames):
            #Needs to be "X.tif" ie ("5.tif")
            name = fname.split("\\")[-1]
            arr = wv.get_image(fname)
            print(arr.shape)

            img, box, cls = wv.chip_image(arr, coords[chips == name],
                                          classes[chips == name], res)

            for im in img:
                images.append(im)
            for b in box:
                boxes.append(b)
            for c in cls:
                clses.append(cls)
            k = k + 1

    return images, boxes, clses
Beispiel #4
0
    for res_ind, it in enumerate(res):
        tot_box = 0
        logging.info("Res: %s" % str(it))
        ind_chips = 0

        fnames = glob.glob(args.image_folder + "*.tif")
        fnames.sort()

        for fname in tqdm(fnames):
            #Needs to be "X.tif", ie ("5.tif")
            #Be careful!! Depending on OS you may need to change from '/' to '\\'.  Use '/' for UNIX and '\\' for windows
            name = fname.split("/")[-1]
            # debug
            #print('file name: ', name)
            arr = wv.get_image(fname)

            # debug
            print('file name: ', name)
            #print('classes[chips==name], ', classes[chips==name])

            im1, box1, classes_final1 = wv.chip_image(arr,
                                                      coords[chips == name],
                                                      classes[chips == name],
                                                      it)

            #Shuffle images & boxes all at once. Comment out the line below if you don't want to shuffle images
            im1, box1, classes_final1 = shuffle_images_and_boxes_classes(
                im1, box1, classes_final1)

            if AUGMENT:
Beispiel #5
0
This class converts images from xView into chipped images
for input into Darknet format
"""
import aug_util as aug
import wv_util as wv
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import csv
import tqdm

fdir = '/data/zjc4/'
#Load an image
chip_name = fdir + 'train_images/104.tif'
cdir = "/data/zjc4/chipped/train/"
arr = wv.get_image(chip_name)

#Loading our labels
coords1, chips1, classes1 = wv.get_labels(fdir + 'xView_train.geojson')
import glob
all_images = glob.glob(fdir + 'train_images/*.tif')

#Load the class number -> class string label map
labels = {}
with open('xview_class_labels.txt') as f:
    for row in csv.reader(f):
        labels[int(row[0].split(":")[0])] = row[0].split(":")[1]
        pass
    pass

i = 0
import os
import math
import numpy as np
import random
import time
import matplotlib
matplotlib.use('tkagg')
import matplotlib.pyplot as plt
import wv_util as wv
import aug_util as aug

ff_name = '../xView/train_images/100.tif'
ff_id = ff_name.split('/')[-1].split('.')[0]
ff_arr = wv.get_image(ff_name)

plt.axis('off')
plt.imshow(ff_arr)
plt.savefig('../../xview_project/images/ff/ff_{}.png'.format(ff_id))
plt.show()
for unique_tif in tif_names:

    xyz += 1
    print("Working on: [{} / {}] {} ".format(xyz, len(tif_names), unique_tif))

    # Make sure the file exists
    if not os.path.isfile(OLD_ROOT + "/train_images/" + unique_tif):
        continue

    # Get the info relevant to this single full frame .tif
    ff_coords = all_coords[all_chips == unique_tif]
    ff_classes = all_classes[all_chips == unique_tif].astype(np.int64)
    print("\tTotal Num Targets: ", len(ff_classes))

    # Chip the image into smaller pieces
    arr = wv.get_image(OLD_ROOT + "/train_images/" + unique_tif)
    #c_img, c_box, c_cls = wv.chip_image(img=arr, coords=ff_coords, classes=ff_classes, shape=CHIP_SHAPE)
    c_img, c_box, c_cls, c_offsets = wv.chip_image_overlap(img=arr,
                                                           coords=ff_coords,
                                                           classes=ff_classes,
                                                           shape=CHIP_SHAPE,
                                                           overlap=OVERLAP)
    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)

        # Write offsets to offsets file
Beispiel #8
0
# Load all info from geojson
all_coords, all_chips, all_classes = wv.get_labels(OLD_ROOT +
                                                   "/xView_train.geojson")

max_gt_boxes = 0

# Iterate over each tif, and chip
for e, tif in enumerate(tif_list):
    print("{} / {}".format(e, len(tif_list)))
    # Get the info relevant to this single full frame .tif
    ff_coords = all_coords[all_chips == tif]
    ff_classes = all_classes[all_chips == tif].astype(np.int64)

    # Chip the image into smaller pieces
    arr = wv.get_image(OLD_ROOT + "/train_images/" + tif)
    c_img, c_box, c_cls = wv.chip_image(img=arr,
                                        coords=ff_coords,
                                        classes=ff_classes,
                                        shape=chip_shape)
    num_chips = len(c_img)

    # Iterate over chips for this tif
    for i in range(num_chips):
        # If chip has no gt boxes, skip
        if len(c_box[i]) == 1 and c_box[i].all() == 0:
            continue
        if len(c_box[i]) > max_gt_boxes:
            max_gt_boxes = len(c_box[i])
            max_gt_chip_num = i
            max_gt_tif = tif
# NAI
'''
This script is a utility to convert the original xView dataset to a VOC2007
  like layout. It will create a second xView dataset directory where it 
  will store the extracted image chips and annotations.
'''

import aug_util as aug
import wv_util as wv
import matplotlib.pyplot as plt
import numpy as np
import csv

chip_name = "1052.tif"

arr = wv.get_image("../xView/train_images/" + chip_name)

#### Load labels for whole dataset
# - coords.shape = (601937, 4)
# - chips.shape = (601937,)
# - classes.shape = (601937,)
# This makes three flat, index aligned lists, where all of the chips from all images are
#   assembled. If we want all of the info for one chip, we look up the chip name in chips
#   list and use those indexes to access the other lists.
coords, chips, classes = wv.get_labels("../xView/xView_train.geojson")

#print coords[0] # = [2712, 1145, 2746, 1177]
#print chips[0] # = 2355.tif
#print classes[0] # = 73.0

# Get info specific to our chip
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