Example #1
0
def mergebase_parallel(srcpath, dstpath, nms):
    pool = Pool(16)
    filelist = util.GetFileFromThisRootDir(srcpath)

    mergesingle_fn = partial(mergesingle, dstpath, nms)
    # pdb.set_trace()
    pool.map(mergesingle_fn, filelist)
Example #2
0
def DOTA2COCOTest(srcpath, destfile, cls_names):
    imageparent = os.path.join(srcpath, 'images')
    data_dict = {}

    data_dict['images'] = []
    data_dict['categories'] = []
    for idex, name in enumerate(cls_names):
        single_cat = {'id': idex + 1, 'name': name, 'supercategory': name}
        data_dict['categories'].append(single_cat)

    image_id = 1
    with open(destfile, 'w') as f_out:
        filenames = util.GetFileFromThisRootDir(imageparent)
        for file in tqdm(filenames):
            basename = util.custombasename(file)
            imagepath = os.path.join(imageparent, basename + '.png')
            img = Image.open(imagepath)
            height = img.height
            width = img.width

            single_image = {}
            single_image['file_name'] = basename + '.png'
            single_image['id'] = image_id
            single_image['width'] = width
            single_image['height'] = height
            data_dict['images'].append(single_image)

            image_id = image_id + 1
        json.dump(data_dict, f_out)
Example #3
0
def DOTA2COCOTrain(srcpath, destfile, cls_names, difficult='2'):
    # set difficult to filter '2', '1', or do not filter, set '-1'

    imageparent = os.path.join(srcpath, 'images')
    labelparent = os.path.join(srcpath, 'labelTxt')

    data_dict = {}
    data_dict['images'] = []
    data_dict['categories'] = []
    data_dict['annotations'] = []
    for idex, name in enumerate(cls_names):
        single_cat = {'id': idex + 1, 'name': name, 'supercategory': name}
        data_dict['categories'].append(single_cat)

    inst_count = 1
    image_id = 1
    with open(destfile, 'wt+') as f_out:
        filenames = util.GetFileFromThisRootDir(labelparent)
        for file in tqdm(filenames):
            basename = util.custombasename(file)
            # image_id = int(basename[1:])

            imagepath = os.path.join(imageparent, basename + '.png')
            img = cv2.imread(imagepath)
            height, width, c = img.shape

            single_image = {}
            single_image['file_name'] = basename + '.png'
            single_image['id'] = image_id
            single_image['width'] = width
            single_image['height'] = height
            data_dict['images'].append(single_image)

            # annotations
            objects = util.parse_dota_poly2(file)
            for obj in objects:
                if obj['difficult'] == difficult:
                    print('difficult: ', difficult)
                    continue
                single_obj = {}
                single_obj['area'] = obj['area']
                single_obj['category_id'] = cls_names.index(obj['name']) + 1
                single_obj['segmentation'] = []
                single_obj['segmentation'].append(obj['poly'])
                single_obj['iscrowd'] = 0
                ############################################
                for i in range(len(obj['poly'])):
                    obj['poly'][i] = float(obj['poly'][i])
                #############################################
                xmin, ymin, xmax, ymax = min(obj['poly'][0::2]), min(obj['poly'][1::2]), \
                                         max(obj['poly'][0::2]), max(obj['poly'][1::2])

                width, height = xmax - xmin, ymax - ymin
                single_obj['bbox'] = xmin, ymin, width, height
                single_obj['image_id'] = image_id
                data_dict['annotations'].append(single_obj)
                single_obj['id'] = inst_count
                inst_count = inst_count + 1
            image_id = image_id + 1
        json.dump(data_dict, f_out)
    def splitdata(self, rate):

        imagelist = util.GetFileFromThisRootDir(self.srcpath)
        imagenames = [
            util.custombasename(x) for x in imagelist
            if (util.custombasename(x) != 'Thumbs')
        ]

        worker = partial(self.SplitSingle, rate=rate, extent=self.ext)

        self.pool.map(worker, imagenames)
Example #5
0
def mergebase(srcpath, dstpath, nms):
    filelist = util.GetFileFromThisRootDir(srcpath)
    print(filelist)
    # PLUS (0314)
    filelist = [file for file in filelist if file.split('.')[-1] == 'txt']
    for fullname in filelist:
        name = util.custombasename(fullname)
        #print('name:', name)
        dstname = os.path.join(dstpath, name + '.txt')
        print(dstname)
        with open(fullname, 'r', encoding='utf-8') as f_in:
            nameboxdict = {}
            lines = f_in.readlines()
            splitlines = [x.strip().split(' ') for x in lines]
            for splitline in splitlines:
                subname = splitline[0]
                # print(subname)
                splitname = subname.split('__')
                oriname = splitname[0]
                pattern1 = re.compile(r'__\d+___\d+')
                #print('subname:', subname)
                x_y = re.findall(pattern1, subname)
                x_y_2 = re.findall(r'\d+', x_y[0])
                x, y = int(x_y_2[0]), int(x_y_2[1])

                pattern2 = re.compile(r'__([\d+\.]+)__\d+___')

                rate = re.findall(pattern2, subname)[0]

                confidence = splitline[1]
                poly = list(map(float, splitline[2:]))
                origpoly = poly2origpoly(poly, x, y, rate)
                det = origpoly
                det.append(confidence)
                det = list(map(float, det))
                if (oriname not in nameboxdict):
                    nameboxdict[oriname] = []
                nameboxdict[oriname].append(det)
            nameboxnmsdict = nmsbynamedict(nameboxdict, nms, nms_thresh)
            with open(dstname, 'w') as f_out:
                for imgname in nameboxnmsdict:
                    for det in nameboxnmsdict[imgname]:
                        #print('det:', det)
                        confidence = det[-1]
                        bbox = det[0:-1]
                        outline = imgname + ' ' + str(
                            confidence) + ' ' + ' '.join(map(str, bbox))
                        #print('outline:', outline)
                        f_out.write(outline + '\n')
Example #6
0
def mergebase(srcpath, dstpath, nms):
    filelist = util.GetFileFromThisRootDir(srcpath)
    for filename in filelist:
        mergesingle(dstpath, nms, filename)
Example #7
0
def DOTA2COCO(srcpath, dest_dir, category_list):
    multi_mkdir(dest_dir)
    labelparent = os.path.join(srcpath, 'labelTxt-v1.0')

    inst_count = 0
    image_id = 0
    categories_dict = {}
    data_dict = {}
    for category in category_list:
        if category == '__background__':
            continue
        categories_dict[category] = collections.OrderedDict({
            'T': 0,
            'S': 0,
            'M': 0,
            'L': 0,
            'total': 0
        })
        data_dict[category] = []
    categories_dict['all'] = collections.OrderedDict({
        'T': 0,
        'S': 0,
        'M': 0,
        'L': 0,
        'total': 0
    })
    # with open(destfile, 'w') as f_out:
    filenames = util.GetFileFromThisRootDir(labelparent)
    for file in filenames:
        image_id = image_id + 1
        objects = util.parse_dota_poly2(file)
        if not len(objects):
            continue
        basename = util.custombasename(file)
        # data_dict[basename] = []
        for obj in objects:
            inst_count = inst_count + 1
            single_obj = {}
            single_obj['filename'] = basename
            # obj['poly'] = [1062.0, 1886.0, 1062.0, 1826.0, 1120.0, 1826.0000000000002, 1120.0, 1886.0000000000002]
            single_obj['bbox'] = pbox2poly(get_pbox(get_rbox(obj['poly'])))
            # pbox, obj['area'] = poly2pbox(obj['poly'])
            # single_obj['bbox'] = pbox2poly(pbox)
            if obj['area'] < 16 * 16:
                categories_dict[obj['name']]['T'] += 1
            elif obj['area'] < 32 * 32:
                categories_dict[obj['name']]['S'] += 1
            elif obj['area'] < 96 * 96:
                categories_dict[obj['name']]['M'] += 1
            else:
                categories_dict[obj['name']]['L'] += 1
            # xmin, ymin, xmax, ymax = min(obj['poly'][0::2]), min(obj['poly'][1::2]), \
            #                          max(obj['poly'][0::2]), max(obj['poly'][1::2])
            # single_obj['bbox'] = dots4ToRec8([xmin, ymin, xmax, ymax])
            # print(basename)
            # single_obj['bbox'] = pbox2poly(get_pbox(get_rbox(obj['poly'])))
            data_dict[obj['name']].append(single_obj)
            # data_dict['annotations']
            # single_obj['id'] = inst_count
            # break

        # if image_id>=2:
        # break
    # print(data_dict)
    print()
    if 'val' in dest_dir:
        print('| val/name:'.ljust(24), '| T:'.ljust(24), '| S:'.ljust(24),
              '| M:'.ljust(24), '| L:'.ljust(24), '| Total |'.ljust(26))
    else:
        print('| train/name:'.ljust(24), '| T:'.ljust(24), '| S:'.ljust(24),
              '| M:'.ljust(24), '| L:'.ljust(24), '| Total |'.ljust(26))
    print('| - | - | - | - | - | - |')
    for category in category_list:
        if category == '__background__':
            continue
        categories_dict[category]['total'] = categories_dict[category]['T'] + categories_dict[category]['S'] \
                                             + categories_dict[category]['M'] + categories_dict[category]['L']
        txt_file_path = os.path.join(dest_dir, category + '.txt')
        with open(txt_file_path, "w") as save_f:
            # print(category, len(data_dict[category]))
            for category_ins in data_dict[category]:
                # line = '{}'.format(category_ins['bbox']).replace(',', '')
                line = '{} {} {}'.format(category_ins['filename'], 1.0,
                                         category_ins['bbox'].tolist())
                line = line.replace('(', '').replace(',', '').replace(')', '') \
                    .replace('[', '').replace(']', '')
                save_f.writelines(line + '\n')
        save_f.close()
        print(
            '| {}'.format(category).ljust(24),
            '| {}'.format(100 * categories_dict[category]['T'] /
                          categories_dict[category]['total']).ljust(24),
            '| {}'.format(100 * categories_dict[category]['S'] /
                          categories_dict[category]['total']).ljust(24),
            '| {}'.format(100 * categories_dict[category]['M'] /
                          categories_dict[category]['total']).ljust(24),
            '| {}'.format(100 * categories_dict[category]['L'] /
                          categories_dict[category]['total']).ljust(24),
            '| {} |'.format(categories_dict[category]['total']))
        # print(line)
        # break
        categories_dict['all']['T'] += categories_dict[category]['T']
        categories_dict['all']['S'] += categories_dict[category]['S']
        categories_dict['all']['M'] += categories_dict[category]['M']
        categories_dict['all']['L'] += categories_dict[category]['L']
        categories_dict['all']['total'] += categories_dict[category]['total']
        # break
    print(
        '| {}'.format('all').ljust(24),
        '| {}'.format(100 * categories_dict['all']['T'] /
                      categories_dict['all']['total']).ljust(24),
        '| {}'.format(100 * categories_dict['all']['S'] /
                      categories_dict['all']['total']).ljust(24),
        '| {}'.format(100 * categories_dict['all']['M'] /
                      categories_dict['all']['total']).ljust(24),
        '| {}'.format(100 * categories_dict['all']['L'] /
                      categories_dict['all']['total']).ljust(24),
        '| {} |'.format(categories_dict['all']['total']))
    print()
    print('img:{}, ins:{}'.format(image_id, inst_count))