示例#1
0
def DOTA2COCO(srcpath, destfile):
    imageparent = os.path.join(srcpath, 'images')
    labelparent = os.path.join(srcpath, 'labelTxt')

    data_dict = {}
    info = {
        'contributor': 'captain group',
        'data_created': '2019',
        'description': 'This is 1.5 version of DOTA dataset.',
        'url': 'http://captain.whu.edu.cn/DOTAweb/',
        'version': '1.5',
        'year': 2019
    }
    data_dict['info'] = info
    data_dict['images'] = []
    data_dict['categories'] = []
    data_dict['annotations'] = []
    for idex, name in enumerate(wordname_16):
        single_cat = {'id': idex + 1, 'name': name, 'supercategory': name}
        data_dict['categories'].append(single_cat)

    inst_count = 1
    image_id = 1
    with open(destfile, 'w') as f_out:
        filenames = util.GetFileFromThisRootDir(labelparent)
        for file in 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:
                single_obj = {}
                single_obj['area'] = obj['area']
                single_obj['category_id'] = wordname_16.index(obj['name']) + 1
                single_obj['segmentation'] = []
                single_obj['segmentation'].append(obj['poly'])
                single_obj['iscrowd'] = 0
                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)
示例#2
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-v1.0')

    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, 'w') as f_out:
        filenames = util.GetFileFromThisRootDir(labelparent)
        for file in filenames:
            basename = util.custombasename(file)
            # image_id = int(basename[1:])

            imagepath = os.path.join(imageparent, basename + '.png')
            img = Image.open(imagepath)
            height = img.height
            width = img.width
            # 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
                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 SplitSingle(self, name, rate, extent):
        """
            split a single image and ground truth
        :param name: image name
        :param rate: the resize scale for the image
        :param extent: the image format
        :return:
        """
        try:
            img = cv2.imread(os.path.join(self.imagepath, name + extent))
            print('img name:', name)
        except:
            print('img name:', name)
        if np.shape(img) == ():
            return
        fullname = os.path.join(self.labelpath, name + '.txt')
        objects = util.parse_dota_poly2(fullname)
        for obj in objects:
            obj['poly'] = list(map(lambda x: rate * x, obj['poly']))
            #obj['poly'] = list(map(lambda x: ([2 * y for y in x]), obj['poly']))

        if (rate != 1):
            resizeimg = cv2.resize(img,
                                   None,
                                   fx=rate,
                                   fy=rate,
                                   interpolation=cv2.INTER_CUBIC)
        else:
            resizeimg = img
        outbasename = name + '__' + str(rate) + '__'
        weight = np.shape(resizeimg)[1]
        height = np.shape(resizeimg)[0]

        # if (max(weight, height) < self.subsize):
        #     return

        left, up = 0, 0
        while (left < weight):
            if (left + self.subsize >= weight):
                left = max(weight - self.subsize, 0)
            up = 0
            while (up < height):
                if (up + self.subsize >= height):
                    up = max(height - self.subsize, 0)
                right = min(left + self.subsize, weight - 1)
                down = min(up + self.subsize, height - 1)
                subimgname = outbasename + str(left) + '___' + str(up)
                # self.f_sub.write(name + ' ' + subimgname + ' ' + str(left) + ' ' + str(up) + '\n')
                self.savepatches(resizeimg, objects, subimgname, left, up,
                                 right, down)
                if (up + self.subsize >= height):
                    break
                else:
                    up = up + self.slide
            if (left + self.subsize >= weight):
                break
            else:
                left = left + self.slide
示例#4
0
    def SplitSingle(self, name, rate, extent):
        """
            split a single image and ground truth   把一张原图和GT分割成子图
        :param name: image name
        :param rate: the resize scale for the image
        :param extent: the image format
        :return:
        """
        img = cv2.imread(os.path.join(self.imagepath,
                                      name + extent))  # 加载原图  imread 不能有中文路径
        if np.shape(img) == ():
            return
        fullname = os.path.join(self.labelpath, name + '.txt')
        objects = util.parse_dota_poly2(fullname)
        for obj in objects:
            obj['poly'] = list(map(lambda x: rate * x, obj['poly']))
            # obj['poly'] = list(map(lambda x: ([2 * y for y in x]), obj['poly']))

        if (rate != 1):  # 如果输入的图不为1,则对原图进行缩放处理
            resizeimg = cv2.resize(img,
                                   None,
                                   fx=rate,
                                   fy=rate,
                                   interpolation=cv2.INTER_CUBIC)
        else:
            resizeimg = img
        outbasename = name + '__' + str(rate) + '__'  # eg:P0003__2__
        weight = np.shape(resizeimg)[1]  #获取宽和高
        height = np.shape(resizeimg)[0]

        left, up = 0, 0  # 这个循环是先宽后高的,要注意一下
        while (left < weight):
            if (left + self.subsize >=
                    weight):  # 如果右边界大于图片宽度,就把图左定义为刚好可以放下最后一个子图或者子图大于原图情况下取0
                left = max(weight - self.subsize, 0)
            up = 0
            while (up < height):
                if (up + self.subsize >= height):
                    up = max(height - self.subsize, 0)  #不能超过上边
                right = min(left + self.subsize, weight - 1)
                down = min(up + self.subsize, height - 1)
                subimgname = outbasename + str(left) + '___' + str(
                    up)  # eg:P0003__2__0__0
                # self.f_sub.write(name + ' ' + subimgname + ' ' + str(left) + ' ' + str(up) + '\n')
                self.savepatches(resizeimg, objects, subimgname, left, up,
                                 right, down)
                if (up + self.subsize >= height):
                    break
                else:
                    up = up + self.slide  #窗口滑动
            if (left + self.subsize >= weight):
                break
            else:
                left = left + self.slide  # 窗口滑动
示例#5
0
    def transfer_process(self, original_dir, target_dir):
        inst_count = len(self.annotations) + 1
        image_id = len(self.images) + 1
        for file_name in self.file_list:
            original_img_path = os.path.join(original_dir, 'images',
                                             file_name + '.png')
            target_img_path = os.path.join(target_dir, 'images',
                                           file_name + '.png')
            shutil.copyfile(original_img_path, target_img_path)
            original_anno_path = os.path.join(original_dir, 'labelTxt',
                                              file_name + '.txt')
            target_anno_path = os.path.join(target_dir, 'labelTxt',
                                            file_name + '.txt')
            shutil.copyfile(original_anno_path, target_anno_path)

            objects = util.parse_dota_poly2(target_anno_path)
            if not len(objects):
                continue
            for obj in objects:
                single_obj = {}
                single_obj['area'] = obj['area']
                single_obj['category_id'] = wordname_18.index(obj['name'])
                single_obj['segmentation'] = []
                single_obj['segmentation'].append(obj['poly'])
                single_obj['iscrowd'] = 0
                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
                single_obj['id'] = inst_count
                self.annotations.append(single_obj)
                inst_count = inst_count + 1
            # images
            # basename = util.custombasename(file)
            # # image_id = int(basename[1:])
            #
            # imagepath = os.path.join(imageparent, basename + '.png')
            img = cv2.imread(target_img_path)
            height, width, c = img.shape

            single_image = {}
            single_image['file_name'] = file_name + '.png'
            single_image['id'] = image_id
            single_image['width'] = width
            single_image['height'] = height
            self.images.append(single_image)
            image_id = image_id + 1
def rotate_single_run(name, srcpath, dstpath):
    """
    only support 0, 90, 180, 270 now
    :param img:
    :param boxes:
    :param angle:
    :return:
    """

    src_imgpath = os.path.join(srcpath, 'images')
    dst_imgpath = os.path.join(dstpath, 'images')

    src_labelTxt = os.path.join(srcpath, 'labelTxt')
    dst_labelTxt = os.path.join(dstpath, 'labelTxt')

    objs = util.parse_dota_poly2(os.path.join(src_labelTxt, name + '.txt'))
    img = cv2.imread(os.path.join(src_imgpath, name + '.png'))
    angle = [np.pi / 2, np.pi, np.pi / 2 * 3]

    img_90 = np.rot90(img, 1)
    img_180 = np.rot90(img, 2)
    img_270 = np.rot90(img, 3)

    # cv2.imwrite(os.path.join(dst_imgpath, name + '_90.png'), img_90)
    # cv2.imwrite(os.path.join(dst_imgpath, name + '_180.png'), img_180)
    # cv2.imwrite(os.path.join(dst_imgpath, name + '_270.png'), img_270)

    h, w, c = img.shape
    print('h:', h, 'w:', w, 'c:', c)

    angles = [np.pi / 2, np.pi, np.pi / 2 * 3]

    rotate_90 = rotate_matrix(np.pi / 2)
    rotate_180 = rotate_matrix(np.pi)
    rotate_270 = rotate_matrix(np.pi / 2 * 3)

    rotate_90_polys = []
    rotate_180_polys = []
    rotate_270_polys = []

    for obj in objs:
        poly = np.array(obj['poly'])
        poly = np.reshape(poly, newshape=(2, 4), order='F')
        centered_poly = poly - np.array([[w / 2.], [h / 2.]])
        rotated_poly_90 = np.matmul(rotate_90, centered_poly) + np.array(
            [[h / 2.], [w / 2.]])
        rotated_poly_180 = np.matmul(rotate_180, centered_poly) + np.array(
            [[w / 2.], [h / 2.]])
        rotated_poly_270 = np.matmul(rotate_270, centered_poly) + np.array(
            [[h / 2.], [w / 2.]])

        rotate_90_polys.append(
            np.reshape(rotated_poly_90, newshape=(8), order='F'))
        rotate_180_polys.append(
            np.reshape(rotated_poly_180, newshape=(8), order='F'))
        rotate_270_polys.append(
            np.reshape(rotated_poly_270, newshape=(8), order='F'))

    with open(os.path.join(dst_labelTxt, name + '_90.txt'), 'w') as f_out:
        for index, poly in enumerate(rotate_90_polys):
            cls = objs[index]['name']
            diff = objs[index]['difficult']
            outline = ' '.join(map(str, list(poly))) + ' ' + cls + ' ' + diff
            f_out.write(outline + '\n')

    with open(os.path.join(dst_labelTxt, name + '_180.txt'), 'w') as f_out:
        for index, poly in enumerate(rotate_180_polys):
            cls = objs[index]['name']
            diff = objs[index]['difficult']
            outline = ' '.join(map(str, list(poly))) + ' ' + cls + ' ' + diff
            f_out.write(outline + '\n')

    with open(os.path.join(dst_labelTxt, name + '_270.txt'), 'w') as f_out:
        for index, poly in enumerate(rotate_270_polys):
            cls = objs[index]['name']
            diff = objs[index]['difficult']
            outline = ' '.join(map(str, list(poly))) + ' ' + cls + ' ' + diff
            f_out.write(outline + '\n')
def HRSC2COCOTrain(srcpath, destfile, cls_names, train_set_file):
    imageparent = os.path.join(srcpath, 'images')
    labelparent = os.path.join(srcpath, 'labelTxt_L1')

    data_dict = {}
    info = { 'contributor': 'Jian Ding',
             'data_created': '2019',
             'description': 'This is the L1 of HRSC',
             'url': 'sss',
             'version': '1.0',
             'year': 2019}
    data_dict['info'] = info
    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, 'w') as f_out:
        # filenames = util.GetFileFromThisRootDir(labelparent)
        with open(train_set_file, 'r') as f_in:
            lines = f_in.readlines()
            filenames = [os.path.join(labelparent, x.strip()) + '.txt' for x in lines]

        for file in filenames:
            basename = util.custombasename(file)
            # image_id = int(basename[1:])

            imagepath = os.path.join(imageparent, basename + '.bmp')

            img = Image.open(imagepath)
            height = img.height
            width = img.width

            print('height: ', height)
            print('width: ', width)

            single_image = {}
            single_image['file_name'] = basename + '.bmp'
            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:
                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
                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)
示例#8
0
    def SplitSingle(self, name, rate, extent):
        """
            split a single image and ground truth
        :param name: image name
        :param rate: the resize scale for the image
        :param extent: the image format
        :return:
        """
        img = cv2.imread(os.path.join(self.imagepath, name + extent))
        if np.shape(img) == ():
            return
        fullname = os.path.join(self.labelpath, name + '.txt')
        objects = util.parse_dota_poly2(fullname)
        for obj in objects:
            obj['poly'] = list(map(lambda x: rate * x, obj['poly']))
            #obj['poly'] = list(map(lambda x: ([2 * y for y in x]), obj['poly']))

        if (rate != 1):
            resizeimg = cv2.resize(img,
                                   None,
                                   fx=rate,
                                   fy=rate,
                                   interpolation=cv2.INTER_CUBIC)
        else:
            resizeimg = img
        # img.shape:(H,W,C) e.g. (600,1000,3)
        if resizeimg.shape[0] < self.subsize_h:
            # top bottom left right
            resizeimg = cv2.copyMakeBorder(resizeimg,
                                           0,
                                           self.subsize_h - resizeimg.shape[0],
                                           0,
                                           0,
                                           cv2.BORDER_CONSTANT,
                                           value=0)
        if resizeimg.shape[1] < self.subsize_w:
            resizeimg = cv2.copyMakeBorder(resizeimg,
                                           0,
                                           0,
                                           0,
                                           self.subsize_w - resizeimg.shape[1],
                                           cv2.BORDER_CONSTANT,
                                           value=0)

        outbasename = name + '__' + str(rate) + '__'
        weight = np.shape(resizeimg)[1]
        height = np.shape(resizeimg)[0]

        left, up = 0, 0
        while (left < weight):
            if (left + self.subsize_w >= weight):
                left = max(weight - self.subsize_w, 0)
            up = 0
            while (up < height):
                if (up + self.subsize_h >= height):
                    up = max(height - self.subsize_h, 0)
                right = min(left + self.subsize_w, weight - 1)
                down = min(up + self.subsize_h, height - 1)
                subimgname = outbasename + str(left) + '___' + str(up)
                # self.f_sub.write(name + ' ' + subimgname + ' ' + str(left) + ' ' + str(up) + '\n')
                self.savepatches(resizeimg, objects, subimgname, left, up,
                                 right, down)
                if (up + self.subsize_h >= height):
                    break
                else:
                    up = up + self.slide_h
            if (left + self.subsize_w >= weight):
                break
            else:
                left = left + self.slide_w
示例#9
0
def DOTA2RBOXES(srcpath, destfile):
    imageparent = os.path.join(srcpath, 'images')
    labelparent = os.path.join(srcpath, 'labelTxt')

    data_dict = {}
    info = {
        'contributor': 'captain group',
        'data_created': '2019',
        'description': 'Object detection for aerial pictures.',
        'url': 'http://rscup.bjxintong.com.cn/#/theme/2',
        'version': 'preliminary contest',
        'year': 2019
    }
    data_dict['info'] = info
    data_dict['images'] = []
    data_dict['categories'] = []
    data_dict['annotations'] = []
    for idex, name in enumerate(wordname_18):
        if name == '__background__':
            continue
        single_cat = {'id': idex, 'name': name, 'supercategory': name}
        data_dict['categories'].append(single_cat)
    data_dict['annotations'] = []

    inst_count = 1
    image_id = 1
    with open(destfile, 'w') as f_out:
        filenames = util.GetFileFromThisRootDir(labelparent)
        for file in filenames:
            # annotations
            objects = util.parse_dota_poly2(file)
            if not len(objects):
                continue
            for obj in objects:
                single_obj = {}
                single_obj['area'] = obj['area']
                single_obj['category_id'] = wordname_18.index(obj['name']) + 1
                single_obj['segmentation'] = []
                single_obj['segmentation'].append(obj['poly'])
                single_obj['iscrowd'] = 0
                single_obj['image_id'] = image_id

                # print(obj['poly'])
                single_obj['bbox'], single_obj['urbox'], single_obj[
                    'orbox'] = BOXES_cpt(obj['poly'])

                data_dict['annotations'].append(single_obj)
                single_obj['id'] = inst_count
                inst_count = inst_count + 1
                # break
            # images
            basename = util.custombasename(file)
            # image_id = int(basename[1:])

            imagepath = os.path.join(imageparent, basename + '.png')
            # print('name:', imagepath)
            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)

            image_id += 1
            # break
        json.dump(data_dict, f_out)
示例#10
0
def DOTA2COCO(srcpath, destfile):
    imageparent = os.path.join(srcpath, 'images')
    labelparent = os.path.join(srcpath, 'labelTxt')

    data_dict = {}
    info = {
        'contributor': 'captain group',
        'data_created': '2019',
        'description': 'Object detection for aerial pictures.',
        'url': 'http://rscup.bjxintong.com.cn/#/theme/2',
        'version': 'preliminary contest',
        'year': 2019
    }
    data_dict['info'] = info
    data_dict['images'] = []
    data_dict['categories'] = []
    data_dict['annotations'] = []
    for idex, name in enumerate(wordname_18):
        if name == '__background__':
            continue
        single_cat = {'id': idex, 'name': name, 'supercategory': name}
        data_dict['categories'].append(single_cat)

    inst_count = 1
    image_id = 1
    # file_list = []
    with open(destfile, 'w') as f_out:
        filenames = util.GetFileFromThisRootDir(labelparent)
        for file in filenames:
            # annotations
            objects = util.parse_dota_poly2(file)
            if not len(objects):
                continue
            for obj in objects:
                single_obj = {}
                single_obj['area'] = obj['area']
                single_obj['category_id'] = wordname_18.index(obj['name'])
                single_obj['segmentation'] = []
                # print('1', obj['poly'])
                # poly = [[float(obj['poly'][0]), float(obj['poly'][1])]]
                # poly = [float(obj['poly'][0]), float(obj['poly'][1]), float(obj['poly'][2]),
                #                 float(obj['poly'][3]), float(obj['poly'][4]), float(obj['poly'][5]),
                #                 float(obj['poly'][6]), float(obj['poly'][7]) ]
                # print('2', poly)
                single_obj['segmentation'].append(obj['poly'])
                single_obj['iscrowd'] = 0
                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
            # images
            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)

            image_id = image_id + 1
            # file_list.append(basename + '.png')
            # if image_id>=200:
            #     break
        print('img:{}, ins:{}'.format(image_id - 1, inst_count - 1))
        json.dump(data_dict, f_out)
示例#11
0
def DOTA2COCO(srcpath, dest_dir):
    multi_mkdir(dest_dir)
    labelparent = os.path.join(srcpath, 'labelTxt')

    inst_count = 0
    image_id = 0
    categories_dict = {}
    data_dict = {}
    for category in wordname_18:
        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
            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])
            data_dict[obj['name']].append(single_obj)
            # data_dict['annotations']
            # single_obj['id'] = inst_count

        # if image_id>=2:
        #     break
    # print(data_dict)
    if 'val' in dest_dir:
        print('val/name:'.ljust(24), 'T:'.ljust(24), 'S:'.ljust(24), 'M:'.ljust(24), 'L:'.ljust(24))
    else:
        print('train/name:'.ljust(24), 'T:'.ljust(24), 'S:'.ljust(24), 'M:'.ljust(24), 'L:'.ljust(24))
    for category in wordname_18:
        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'])
                line = line.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('img:{}, ins:{}'.format(image_id, inst_count))
示例#12
0
def minicategory2COCO(srcpath, destfile):
    imageparent = os.path.join(srcpath, 'images')
    labelparent = os.path.join(srcpath, 'labelTxt')

    data_dict = {}
    info = {
        'contributor': 'captain group',
        'data_created': '2019',
        'description': 'Object detection for aerial pictures.',
        'url': 'http://rscup.bjxintong.com.cn/#/theme/2',
        'version': 'preliminary contest',
        'year': 2019
    }
    data_dict['info'] = info
    data_dict['images'] = []
    data_dict['categories'] = []
    data_dict['annotations'] = []
    for idex, name in enumerate(mininame_3):
        if name == '__background__':
            continue
        single_cat = {'id': idex, 'name': name, 'supercategory': name}
        data_dict['categories'].append(single_cat)

    inst_count = 1
    image_id = 1
    with open(destfile, 'w') as f_out:
        filenames = util.GetFileFromThisRootDir(labelparent)
        for file in filenames:
            # annotations
            objects = util.parse_dota_poly2(file)
            if not len(objects):
                continue
            box_num = 0
            for obj in objects:
                if obj['name'] not in mininame_3:
                    continue
                box_num += 1
                single_obj = {}
                single_obj['area'] = obj['area']
                single_obj['category_id'] = mininame_3.index(obj['name'])
                single_obj['segmentation'] = []
                single_obj['segmentation'].append(obj['poly'])
                single_obj['iscrowd'] = 0
                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
            if not box_num:
                continue
            # images
            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)

            image_id = image_id + 1
            # print(data_dict)
            # break
        json.dump(data_dict, f_out)
示例#13
0
def ICDAR2COCOTrain(srcpath, destfile, cls_names):
    imageparent = os.path.join(srcpath, 'images')
    labelparent = os.path.join(srcpath, 'labels')

    data_dict = {}
    info = {
        'contributor': 'ming71',
        'data_created': '2020',
        'description': 'ICDAR2015 dataset',
        'url': 'sss',
        'version': '1.0',
        'year': 2020
    }
    data_dict['info'] = info
    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, 'w') as f_out:
        # filenames = util.GetFileFromThisRootDir(labelparent)
        filenames = os.listdir(labelparent)

        for file in filenames:
            basename = util.custombasename(file)
            import ipdb
            ipdb.set_trace()
            # image_id = int(basename[1:])

            imagepath = os.path.join(imageparent, basename + '.jpg')

            img = Image.open(imagepath)
            height = img.height
            width = img.width

            print('height: ', height)
            print('width: ', width)

            single_image = {}
            single_image['file_name'] = basename + '.jpg'
            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:
                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
                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)