def makeDir(imFolder, outFolder, labelFile, dirFile, classRef, res=(300, 300)): coords, imNames, classes = get_labels(labelFile) print('Got labels') imFiles = listdir(imFolder) with open(dirFile, 'w') as outFile: for im in imFiles: if im.endswith('.tif'): arr = np.array(Image.open(path.join(imFolder, im))) chips, boxes, boxClasses = chip_image(arr, coords[imNames == im], classes[imNames == im], res) for i in range(len(chips)): chip = Image.fromarray(chips[i]) newName = path.join(outFolder, "{}_{}.jpg".format(im[:-4], i)) chip.save(newName) boxStr = [] emptyChip = False for j in range(len(boxes[i])): box = boxes[i][j] boxClass = boxClasses[i][j] if boxClass == 0: emptyChip = True classID = getRealID(int(boxClasses[i][j]), classRef) boxStr.append("{},{},{},{},{}".format( int(box[0]), int(box[1]), int(box[2]), int(box[3]), classID)) if not emptyChip: infoStr = '{} {}\n'.format(newName, " ".join(boxStr)) outFile.write(infoStr) # print(newName) print(im)
def get_labels_for_chip(src, json_file): """ Get the bbox and class for a chip :param src: path to chip image :param json_file: path to json_file with labels :return: bbox coords and classes """ coords, chips, classes = wv.get_labels(json_file) chip_name = image_name(src) coords = coords[chips == chip_name] classes = classes[chips == chip_name].astype(np.int64) return coords, classes
type=str) parser.add_argument('-d', '--id2name', help='the id2name txt file', type=str) parser.add_argument('-o', '--output', help='output directory', type=str, default='./') parser.add_argument('-r', '--resolution', help='output image resolution', type=int, default=672) args = parser.parse_args() ## load all annotations coords, img_fn, classes = wv.get_labels(args.geojson) img_files = list(set(img_fn)) print("total image files: ", len(img_files), "first 5 image files = ", img_files[:5]) cls_cnt = Counter(classes).most_common() ## Load id2name cid2name = {} with open(args.id2name, 'r') as f: for line in f: x, y = line.strip().split(':') cid2name[int(x)] = y # map id to consecutive ids idremap = {x: y for x, y in zip(sorted(cid2name.keys()), range(len(cid2name)))} img_dir = args.images ## divide the TIF images into smaller jpg images
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 for chip_name in tqdm.tqdm(all_images[:300]): chip_name = (chip_name.split("/")[-1]) img_name = chip_name.split(".")[0]
parser = argparse.ArgumentParser( description='Create Annotations and images for xview chips') parser.add_argument('chip_size', type=int) args = parser.parse_args() ######### Inputs CHIP_SIZE = args.chip_size OLD_ROOT = "../xView" NEW_ROOT = "../xView-voc-{}".format(CHIP_SIZE) IMAGESETS_PATH = os.path.join(NEW_ROOT, "ImageSets", "Main") CHIP_SHAPE = (CHIP_SIZE, CHIP_SIZE) OVERLAP = 0.20 ######### # Load all of the labels from .geojson all_coords, all_chips, all_classes = wv.get_labels(OLD_ROOT + "/xView_train.geojson") print("Full Dataset Stats:") print(all_coords.shape) print(all_chips.shape) print(all_classes.shape) # Get all of the unique .tif names from all_chips tif_names = np.unique(all_chips) rows = [] xyz = 0 # For each unique .tif for unique_tif in tif_names:
#resolutions should be largest -> smallest. We take the number of chips in the largest resolution and make #sure all future resolutions have less than 1.5times that number of images to prevent chip size imbalance. #res = [(500,500),(400,400),(300,300),(200,200)] res = [(args.resolution, args.resolution)] images = {} boxes = {} train_chips = 0 test_chips = 0 skip_chips = 0 #Parameters max_chips_per_res = 100000 coords, chips, classes = wv.get_labels(args.json_filepath) 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") if args.images: fimages = args.images.split(',') image_filter = [ '%s%s.tif' % (args.image_folder, f) for f in fimages ] fnames = [f for f in fnames if f in image_filter] print(fnames)
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 coords = coords[chips == chip_name] classes = classes[chips == chip_name].astype(np.int64) print("# Objects In Image: ", len(classes)) # Create a Class # -> Class Label Map labels = {} with open('xview_class_labels.txt') as f: for row in csv.reader(f):