def dota2darknet(imgpath, txtpath, dstpath, extractclassname):
    """
    :param imgpath: the path of images
    :param txtpath: the path of txt in dota format
    :param dstpath: the path of txt in YOLO format
    :param extractclassname: the category you selected
    :return:
    """
    filelist = util.GetFileFromThisRootDir(txtpath)
    for fullname in filelist:
        objects = util.parse_dota_poly(fullname)
        name = os.path.splitext(os.path.basename(fullname))[0]
        img_fullname = os.path.join(imgpath, name + '.png')
        img = Image.open(img_fullname)
        img_w, img_h = img.size
        # print img_w,img_h
        with open(os.path.join(dstpath, name + '.txt'), 'w') as f_out:
            for obj in objects:
                poly = obj['poly']
                bbox = np.array(util.dots4ToRecC(poly, img_w, img_h))
                if (sum(bbox <= 0) + sum(bbox >= 1)) >= 1:
                    continue
                if (obj['name'] in extractclassname):
                    id = extractclassname.index(obj['name'])
                else:
                    continue
                outline = str(id) + ' ' + ' '.join(list(map(str, bbox)))
                f_out.write(outline + '\n')
Esempio n. 2
0
def imageformatTrans(srcpath, dstpath, format):
    filelist = util.GetFileFromThisRootDir(srcpath)
    for fullname in filelist:
        img = cv2.imread(fullname)
        basename = util.custombasename(fullname)
        dstname = os.path.join(dstpath, basename + format)
        cv2.imwrite(dstname, img)
Esempio n. 3
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)
Esempio n. 4
0
def mergebase_parallel(srcpath, dstpath, nms, nms_thresh):
    pool = Pool(16)
    filelist = util.GetFileFromThisRootDir(srcpath)

    mergesingle_fn = partial(mergesingle, dstpath, nms, nms_thresh)
    # pdb.set_trace()
    pool.map(mergesingle_fn, filelist)
Esempio n. 5
0
def DOTA2COCOTest(srcpath, destfile, cls_names, ext='.png'):
    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 filenames:
            basename = util.custombasename(file)
            imagepath = os.path.join(imageparent, basename + ext)
            img = Image.open(imagepath)
            height = img.height
            width = img.width

            single_image = {}
            single_image['file_name'] = basename + ext
            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)
Esempio n. 6
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)
Esempio n. 7
0
 def splitdata(self, rate):
     imagelist = util.GetFileFromThisRootDir(self.srcpath)
     imagenames = [
         util.custombasename(x) for x in imagelist
         if (util.custombasename(x) != 'Thumbs')
     ]
     for name in imagenames:
         self.SplitSingle(name, rate, self.ext)
    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)
        worker = partial(split_single_warp, split_base=self, rate=rate, extent=self.ext)
        self.pool.map(worker, imagenames)
Esempio n. 9
0
def rotate(srcpath, dstpath, num_process=16):

    pool = Pool(num_process)
    imgnames = util.GetFileFromThisRootDir(os.path.join(srcpath, 'images'))
    names = [util.custombasename(x) for x in imgnames]

    rotate_fun = partial(rotate_single_run, srcpath=srcpath, dstpath=dstpath)

    pool.map(rotate_fun, names)
Esempio n. 10
0
def delete(imgpath, txtpath):
    filelist = util.GetFileFromThisRootDir(
        txtpath)  # fileist=['/.../P0005.txt', ..., /.../P000?.txt]
    for fullname in filelist:  # fullname='/.../P000?.txt'
        name = os.path.splitext(os.path.basename(fullname))[0]  # name='P000?'
        img_fullname = os.path.join(imgpath, name +
                                    '.png')  # img_fullname='/.../P000?.png'
        if not os.path.exists(img_fullname):  # 如果文件bu存在
            os.remove(fullname)
Esempio n. 11
0
 def __init__(self, basepath):
     self.basepath = basepath
     self.labelpath = os.path.join(basepath, 'labelTxt')
     self.imagepath = os.path.join(basepath, 'images')
     self.imgpaths = util.GetFileFromThisRootDir(self.labelpath)
     self.imglist = [util.custombasename(x) for x in self.imgpaths]
     self.catToImgs = defaultdict(list)
     self.ImgToAnns = defaultdict(list)
     self.createIndex()
Esempio n. 12
0
 def __init__(self, basepath):
     self.basepath = basepath
     self.labelpath = os.path.join(basepath, 'labelTxt')
     self.imagepath = os.path.join(basepath, 'images')
     self.imgpaths = util.GetFileFromThisRootDir(self.labelpath)
     self.imglist = [util.custombasename(x) for x in self.imgpaths]  # 去掉后缀
     self.catToImgs = defaultdict(
         list)  # defaultdict 接受工厂方法,list 为[] int 为0 str 为"",set为 set()
     self.ImgToAnns = defaultdict(list)
     self.createIndex()  # 创建种类到图片id 以及图像id 到对象列表的索引
def filecopy_v2(srcpath, dstpath, num_process=32):
    filenames = util.GetFileFromThisRootDir(srcpath)
    filenames = [os.path.basename(x.strip()) for x in filenames]
    path_pair_list = []
    for name in filenames:
        srcdir = os.path.join(srcpath, name)
        dstdir = os.path.join(dstpath, name)
        path_pair_list.append((srcdir, dstdir))

    copy_pool = Pool(num_process)
    copy_pool.map(filecopy_single, path_pair_list)
Esempio n. 14
0
def drawLongsideFormatimg(imgpath,
                          txtpath,
                          dstpath,
                          extractclassname,
                          thickness=2):
    """
    根据labels绘制边框(label_format:classid, x_c_normalized, y_c_normalized, longside_normalized, shortside_normalized, Θ)
    :param imgpath: the path of images
    :param txtpath: the path of txt in longside format
    :param dstpath: the path of image_drawed
    :param extractclassname: the category you selected
    """
    if os.path.exists(dstpath):
        shutil.rmtree(dstpath)  # delete output folder
    os.makedirs(dstpath)  # make new output folder
    # 设置画框的颜色    colors = [[178, 63, 143], [25, 184, 176], [238, 152, 129],....,[235, 137, 120]]随机设置RGB颜色
    colors = [[random.randint(0, 255) for _ in range(3)]
              for _ in range(len(extractclassname))]
    filelist = util.GetFileFromThisRootDir(
        txtpath)  # fileist=['/.../P0005.txt', ..., /.../P000?.txt]
    for fullname in filelist:  # fullname='/.../P000?.txt'
        objects = util.parse_longsideformat(fullname)
        '''
        objects[i] = [classid, x_c_normalized, y_c_normalized, longside_normalized, shortside_normalized, theta]
        '''
        name = os.path.splitext(os.path.basename(fullname))[0]  # name='P000?'
        img_fullname = os.path.join(imgpath, name +
                                    '.png')  # img_fullname='/.../P000?.png'
        img_savename = os.path.join(dstpath, name +
                                    '_.png')  # img_fullname='/.../_P000?.png'
        img = Image.open(img_fullname)  # 图像被打开但未被读取
        img_w, img_h = img.size
        img = cv2.imread(img_fullname)  # 读取图像像素
        for i, obj in enumerate(objects):
            # obj = [classid, x_c_normalized, y_c_normalized, longside_normalized, shortside_normalized, float:0-179]
            class_index = obj[0]
            # rect=[(x_c,y_c),(w,h),Θ] Θ:flaot[0-179]  -> (-180,0)
            rect = longsideformat2cvminAreaRect(obj[1], obj[2], obj[3], obj[4],
                                                (obj[5] - 179.9))
            # poly = [(x1,y1),(x2,y2),(x3,y3),(x4,y4)]
            poly = np.float32(cv2.boxPoints(rect))  # 返回rect对应的四个点的值 normalized

            # 四点坐标反归一化 取整
            poly[:, 0] = poly[:, 0] * img_w
            poly[:, 1] = poly[:, 1] * img_h
            poly = np.int0(poly)

            # 画出来
            cv2.drawContours(image=img,
                             contours=[poly],
                             contourIdx=-1,
                             color=colors[int(class_index)],
                             thickness=thickness)
        cv2.imwrite(img_savename, img)
Esempio n. 15
0
def Aerial2COCOTrain(srcpath, destfile, cls_names):

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

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

            img = cv2.imread(imagepath)
            height, width, c = img.shape

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

            # annotations
            objects = parse_aerial_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, indent=2)
Esempio n. 16
0
def mergebase(srcpath, dstpath, nms):
    if not os.path.exists(dstpath):
        os.makedirs(dstpath)
    img_result = {}
    filelist = util.GetFileFromThisRootDir(srcpath)
    for fullname in filelist:
        name = util.custombasename(fullname)
        #print('name:', name)
        dstname = os.path.join(dstpath, name + '.txt')
        with open(fullname, 'r') as f_in:
            nameboxdict = {}
            lines = f_in.readlines()
            splitlines = [x.strip().split(' ') for x in lines]
            for splitline in splitlines:
                subname = splitline[0]
                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:
                    if (imgname not in img_result):
                        img_result[imgname] = {}
                    if (name not in img_result[imgname]):
                        img_result[imgname][name] = []
                    for det in nameboxnmsdict[imgname]:
                        img_result[imgname][name].append(det)
                        #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')
    return img_result
Esempio n. 17
0
def mergebase_parallel(srcpath, dstpath, nms):
    """
        将源路径中所有的txt目标信息,经nms后存入目标路径中的同名txt
        @param srcpath: 合并前信息保存的txt源路径
        @param dstpath: 合并后信息保存的txt目标路径
        @param nms: NMS函数
    """
    pool = Pool(16)
    filelist = util.GetFileFromThisRootDir(srcpath)

    mergesingle_fn = partial(mergesingle, dstpath, nms)
    # pdb.set_trace()
    pool.map(mergesingle_fn, filelist)
Esempio n. 18
0
def mergebase(srcpath, dstpath, nms):
    """
    读入子图的检测结果,返回最终的检测结果,还是子图的检测结果名称还有下划线
    :param srcpath:
    :param dstpath:
    :param nms:
    :return:
    """
    filelist = util.GetFileFromThisRootDir(srcpath)
    for fullname in filelist:
        name = util.custombasename(fullname)  # 去掉拓展名
        # print('name:', name)
        dstname = os.path.join(dstpath, name + '.txt')  #形成dstname 的目标文件
        with open(fullname, 'r') as f_in:
            nameboxdict = {}
            lines = f_in.readlines()
            splitlines = [x.strip().split(' ') for x in lines]
            for splitline in splitlines:
                subname = splitline[0]    #子图的名称
                splitname = subname.split('__')
                oriname = splitname[0]  # 原图的名称
                pattern1 = re.compile(r'__\d+___\d+')
                # print('subname:', subname)
                x_y = re.findall(pattern1, subname)  # 返回的是一个只包含一个元素的list,所以下一步[0]
                x_y_2 = re.findall(r'\d+', x_y[0])
                x, y = int(x_y_2[0]), int(x_y_2[1])  # 解出 x,y的值

                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) # 还原出原来的poly
                det = origpoly
                det.append(confidence)   # [8个坐标,置信度]
                det = list(map(float, det))
                if (oriname not in nameboxdict):
                    nameboxdict[oriname] = []
                nameboxdict[oriname].append(det)   # 原图对应的多个检测结果
            nameboxnmsdict = nmsbynamedict(nameboxdict, nms, nms_thresh)   # 进行nms
            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')
Esempio n. 19
0
def mergebase_parallel(srcpath, dstpath, nms):
    """
    多线程转换为原图
    :param srcpath:
    :param dstpath:
    :param nms:
    :return:
    """
    pool = Pool(16)
    filelist = util.GetFileFromThisRootDir(srcpath)

    mergesingle_fn = partial(mergesingle, dstpath, nms)
    # pdb.set_trace()
    pool.map(mergesingle_fn, filelist)
Esempio n. 20
0
def dota2Darknet(imgpath, txtpath, dstpath, extractclassname):
    """
    :param imgpath: the path of images
    :param txtpath: the path of txt in dota format
    :param dstpath: the path of txt in YOLO format
    :param extractclassname: the category you selected
    :return:
           txt format: id x y w h
    """
    if os.path.exists(dstpath):
        shutil.rmtree(dstpath)  # delete output folder
    os.makedirs(dstpath)  # make new output folder
    filelist = util.GetFileFromThisRootDir(
        txtpath)  # fileist=['/.../P0005.txt', ..., /.../P000?.txt]
    for fullname in filelist:  # fullname='/.../P000?.txt'
        objects = util.parse_dota_poly(fullname)
        '''
        objects =
        [{'name': 'ship', 
          'difficult': '1', 
          'poly': [(1054.0, 1028.0), (1063.0, 1011.0), (1111.0, 1040.0), (1112.0, 1062.0)], 
          'area': 1159.5
          },
          ...
        ]
        '''
        name = os.path.splitext(os.path.basename(fullname))[0]  # name='P000?'
        img_fullname = os.path.join(imgpath, name +
                                    '.png')  # img_fullname='/.../P000?.png'
        img = Image.open(img_fullname)
        img_w, img_h = img.size
        # print img_w,img_h
        with open(os.path.join(dstpath, name + '.txt'), 'w') as f_out:
            for obj in objects:
                poly = obj['poly']  # poly=[(x1,y1),(x2,y2),(x3,y3),(x4,y4)]
                bbox = np.array(util.dots4ToRecC(poly, img_w,
                                                 img_h))  # bbox=[x y w h]
                if (sum(bbox <= 0) +
                        sum(bbox >= 1)) >= 1:  # 若bbox中有<=0或>= 1的元素则将该box排除
                    continue
                if (obj['name'] in extractclassname):
                    id = extractclassname.index(
                        obj['name'])  # id=类名的索引 比如'plane'对应id=0
                else:
                    continue
                outline = str(id) + ' ' + ' '.join(list(map(
                    str, bbox)))  # outline='id x y w h'
                f_out.write(outline + '\n')  # 写入txt文件中并加上换行符号 \n
Esempio n. 21
0
def HRSC2COCOTest(srcpath, destfile, cls_names):
    imageparent = os.path.join(srcpath, 'images')
    # labelparent = os.path.join(srcpath, 'labelTxt')
    data_dict = {}
    info = {'contributor': 'Jian Ding',
            'data_created': '2019',
            'description': 'This is HRSC.',
            'url': 'http://captain.whu.edu.cn/DOTAweb/',
            'version': '1.0',
            'year': 2018}
    data_dict['info'] = info
    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)

    inst_count = 1
    image_id = 1
    with open(destfile, 'w') as f_out:
        filenames = util.GetFileFromThisRootDir(imageparent)
        # with open(test_set_file, 'r') as f_in:
        #     lines = f_in.readlines()
        #     filenames = [os.path.join(imageparent, x.strip()) + '.bmp' 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 = cv2.imread(imagepath)
            img = Image.open(imagepath)
            # height, width, c = img.shape
            height = img.height
            width = img.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)

            image_id = image_id + 1
        json.dump(data_dict, f_out)
def rotate(srcpath, dstpath):

    pool = Pool(16)
    imgnames = util.GetFileFromThisRootDir(os.path.join(srcpath, 'images'))
    names = [util.custombasename(x) for x in imgnames]

    dst_imgpath = os.path.join(dstpath, 'images')
    dst_labelTxt = os.path.join(dstpath, 'labelTxt')

    if not os.path.exists(dst_imgpath):
        os.mkdir(dst_imgpath)

    if not os.path.exists(dst_labelTxt):
        os.mkdir(dst_labelTxt)

    rotate_fun = partial(rotate_single_run, srcpath=srcpath, dstpath=dstpath)

    pool.map(rotate_fun, names)
def extract_largesize_index(labelpath):
    filenames = util.GetFileFromThisRootDir(labelpath)
    large_size_index = []
    for name in filenames:
        objs = util.parse_dota_poly(name)
        flag = 0
        for obj in objs:
            poly = np.array(obj['poly'])
            xmin, ymin, xmax, ymax = np.min(poly[:, 0]), np.min(poly[:, 1]), np.max(poly[:, 0]), np.max(poly[:, 1])
            w = xmax - xmin
            h = ymax - ymin
            max_side = max(w, h)
            if max_side > 400:
                flag = 1
                break
        if flag:
            large_size_index.append(util.custombasename(name))
    # print('index:', large_size_index)
    # print('len:', len(large_size_index))

    return large_size_index
Esempio n. 24
0
def drwaBox_parallel(srcpath, imgpath, dstpath):
    pool = Pool(16)
    filelist = util.GetFileFromThisRootDir(srcpath)
    nameboxdict = {}
    for file in filelist:
        name = util.custombasename(file)
        with open(file, 'r') as f_in:
            lines = f_in.readlines()
            splitlines = [x.strip().split(' ') for x in lines]
            for splitline in splitlines:
                oriname = splitline[0]
                confidence = float(splitline[1])
                bbox = list(map(float, splitline[2:]))
                bbox.append(confidence)
                bbox.append(name.split('_')[-1])
                if (oriname not in nameboxdict):
                    nameboxdict[oriname] = []
                nameboxdict[oriname].append(bbox)

    drawBoxsingle_fn = partial(drawBoxsingle, imgpath, dstpath, nameboxdict)
    # pdb.set_trace()
    pool.map(drawBoxsingle_fn, nameboxdict)
Esempio n. 25
0
def mergebase(srcpath, dstpath, nms, isTask2=True):
    filelist = util.GetFileFromThisRootDir(srcpath)
    for fullname in filelist:
        name = util.custombasename(fullname)
        #print('name:', name)
        dstname = os.path.join(dstpath, name + '.txt')
        with open(fullname, 'r') as f_in:
            nameboxdict = {}
            lines = f_in.readlines()
            splitlines = [x.strip().split(' ') for x in lines]
            for splitline in splitlines:
                subname = splitline[0]
                '''
                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]
                '''
                splitname = subname.split('_')
                oriname = splitname[0]
                x = int(splitname[2])
                y = int(splitname[3])
                rate = int(splitname[1])

                confidence = splitline[1]
                poly = list(map(float, splitline[2:]))
                if isTask2:
                    x1 = np.round(poly[0])
                    y1 = np.round(poly[1])
                    x2 = np.round(poly[0] + poly[2] - 1)
                    y2 = np.round(poly[1] + poly[3] - 1)
                    poly = [x1, y1, x2, y1, x2, y2, x1, y2]
                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]
                        if not isTask2:
                            outline = imgname + ' ' + str(
                                confidence) + ' ' + ' '.join(map(str, bbox))
                        else:
                            outline = imgname + ' ' + str(
                                confidence) + ' ' + ' '.join(
                                    map(str,
                                        [bbox[0], bbox[1], bbox[4], bbox[5]]))
                        #print('outline:', outline)
                        f_out.write(outline + '\n')
Esempio n. 26
0
def dota2LongSideFormat(imgpath, txtpath, dstpath, extractclassname):
    """
    trans dota farmat to longside format
    :param imgpath: the path of images
    :param txtpath: the path of txt in dota format
    :param dstpath: the path of txt in YOLO format
    :param extractclassname: the category you selected
    """
    if os.path.exists(dstpath):
        shutil.rmtree(dstpath)  # delete output folder
    os.makedirs(dstpath)  # make new output folder
    filelist = util.GetFileFromThisRootDir(
        txtpath)  # fileist=['/.../P0005.txt', ..., /.../P000?.txt]
    for fullname in filelist:  # fullname='/.../P000?.txt'
        objects = util.parse_dota_poly(fullname)
        '''
        objects =
        [{'name': 'ship', 
          'difficult': '1', 
          'poly': [(1054.0, 1028.0), (1063.0, 1011.0), (1111.0, 1040.0), (1112.0, 1062.0)], 
          'area': 1159.5
          },
          ...
        ]
        '''
        name = os.path.splitext(os.path.basename(fullname))[0]  # name='P000?'
        img_fullname = os.path.join(imgpath, name +
                                    '.png')  # img_fullname='/.../P000?.png'
        img = Image.open(img_fullname)
        img_w, img_h = img.size
        # print img_w,img_h
        with open(os.path.join(dstpath, name + '.txt'), 'w') as f_out:
            num_gt = 0
            for i, obj in enumerate(objects):
                num_gt = num_gt + 1  # 为当前有效gt计数
                poly = obj['poly']  # poly=[(x1,y1),(x2,y2),(x3,y3),(x4,y4)]
                poly = np.float32(np.array(poly))
                # 四点坐标归一化
                poly[:, 0] = poly[:, 0] / img_w
                poly[:, 1] = poly[:, 1] / img_h

                rect = cv2.minAreaRect(poly)  # 得到最小外接矩形的(中心(x,y), (宽,高), 旋转角度)
                # box = np.float32(cv2.boxPoints(rect))  # 返回rect四个点的值

                c_x = rect[0][0]
                c_y = rect[0][1]
                w = rect[1][0]
                h = rect[1][1]
                theta = rect[-1]  # Range for angle is [-90,0)

                trans_data = cvminAreaRect2longsideformat(
                    c_x, c_y, w, h, theta)
                if not trans_data:
                    if theta != 90:  # Θ=90说明wh中有为0的元素,即gt信息不完整,无需提示异常,直接删除
                        print('opencv表示法转长边表示法出现异常,已将第%d个box排除,问题出现在该图片中:%s' %
                              (i, img_fullname))
                    num_gt = num_gt - 1
                    continue
                else:
                    # range:[-180,0)
                    c_x, c_y, longside, shortside, theta_longside = trans_data

                bbox = np.array((c_x, c_y, longside, shortside))

                if (sum(bbox <= 0) +
                        sum(bbox[:2] >= 1)) >= 1:  # 0<xy<1, 0<side<=1
                    print(
                        'bbox[:2]中有>= 1的元素,bbox中有<= 0的元素,已将第%d个box排除,问题出现在该图片中:%s'
                        % (i, img_fullname))
                    print(
                        '出问题的longside形式数据:[%.16f, %.16f, %.16f, %.16f, %.1f]' %
                        (c_x, c_y, longside, shortside, theta_longside))
                    num_gt = num_gt - 1
                    continue
                if (obj['name'] in extractclassname):
                    id = extractclassname.index(
                        obj['name'])  # id=类名的索引 比如'plane'对应id=0
                else:
                    print('预定类别中没有类别:%s;已将该box排除,问题出现在该图片中:%s' %
                          (obj['name'], fullname))
                    num_gt = num_gt - 1
                    continue
                theta_label = int(theta_longside +
                                  180.5)  # range int[0,180] 四舍五入
                if theta_label == 180:  # range int[0,179]
                    theta_label = 179
                # outline='id x y longside shortside Θ'

                # final check
                if id > 15 or id < 0:
                    print('id problems,问题出现在该图片中:%s' % (i, img_fullname))
                    print(
                        '出问题的longside形式数据:[%.16f, %.16f, %.16f, %.16f, %.1f]' %
                        (c_x, c_y, longside, shortside, theta_longside))
                if theta_label < 0 or theta_label > 179:
                    print('id problems,问题出现在该图片中:%s' % (i, img_fullname))
                    print(
                        '出问题的longside形式数据:[%.16f, %.16f, %.16f, %.16f, %.1f]' %
                        (c_x, c_y, longside, shortside, theta_longside))
                outline = str(id) + ' ' + ' '.join(list(map(
                    str, bbox))) + ' ' + str(theta_label)
                f_out.write(outline + '\n')  # 写入txt文件中并加上换行符号 \n

        if num_gt == 0:
            os.remove(os.path.join(dstpath, name + '.txt'))  #
            os.remove(img_fullname)
            os.remove(fullname)
            print('%s 图片对应的txt不存在有效目标,已删除对应图片与txt' % img_fullname)
    print('已完成文件夹内DOTA数据形式到长边表示法的转换')
Esempio n. 27
0
    import dota_utils as util
except:
    import dota_kit.dota_utils as util
srcpath1 = '/mnt/lustre/yanhongchang/project/one-rpn/mmdetection/demo/work_dirs/out_img/faster_x101/crop512/txt2'
srcpath2 = '/mnt/lustre/yanhongchang/project/one-rpn/mmdetection/demo/work_dirs/out_img/faster_x101/crop1024/txt2'
srcpath3 = '/mnt/lustre/yanhongchang/project/one-rpn/mmdetection/demo/work_dirs/out_img/faster_x101/origin/txt2'
dstpath = '/mnt/lustre/yanhongchang/project/one-rpn/mmdetection/demo/work_dirs/out_img/faster_x101/tr_merge3/txt'


def osp(savepath):
    if not os.path.exists(savepath):
        os.makedirs(savepath)


osp(dstpath)
filelist = util.GetFileFromThisRootDir(srcpath1)
for filename in filelist:
    name = util.custombasename(filename)
    print(name)
    file_1 = open(os.path.join(srcpath1, name + '.txt'), 'r')
    file_2 = open(os.path.join(srcpath2, name + '.txt'), 'r')
    file_3 = open(os.path.join(srcpath3, name + '.txt'), 'r')
    file_new = open(os.path.join(dstpath, name + '.txt'), 'w')

    list1 = []
    for line in file_1.readlines():
        ss = line.strip()
        list1.append(ss)
    file_1.close()

    list2 = []
def prepare():
    args = parse_args()
    data_root_path = args.data_path

    train_path = os.path.join(data_root_path, 'train')
    val_path = os.path.join(data_root_path, 'val')
    test_path = os.path.join(data_root_path, 'test')

    if not os.path.exists(os.path.join(data_root_path, 'trainval_large')):
        os.makedirs(os.path.join(data_root_path, 'trainval_large'))
    if not os.path.exists(os.path.join(data_root_path, 'trainval_large', 'images')):
        os.makedirs(os.path.join(data_root_path, 'trainval_large', 'images'))
    if not os.path.exists(os.path.join(data_root_path, 'trainval_large', 'labelTxt')):
        os.makedirs(os.path.join(data_root_path, 'trainval_large', 'labelTxt'))

    if not os.path.exists(os.path.join(data_root_path, 'trainval1024_1')):
        os.makedirs(os.path.join(data_root_path, 'trainval1024_1'))

    split_train = ImgSplit_multi_process.splitbase(train_path,
                       os.path.join(data_root_path, 'trainval1024_1'),
                      gap=200,
                      subsize=1024,
                      num_process=args.num_process
                      )
    split_train.splitdata(1)

    split_val = ImgSplit_multi_process.splitbase(val_path,
                        os.path.join(data_root_path, 'trainval1024_1'),
                         gap=200,
                         subsize=1024,
                         num_process=args.num_process
                        )
    split_val.splitdata(1)

    # extract train images contain large intances
    train_large_names = extract_largesize_index(os.path.join(data_root_path, 'train', 'labelTxt'))
    filecopy(os.path.join(data_root_path, 'train', 'labelTxt'),
             os.path.join(data_root_path, 'trainval_large', 'labelTxt'),
             train_large_names,
             '.txt',
             num_process=args.num_process)
    filecopy(os.path.join(data_root_path, 'train', 'images'),
             os.path.join(data_root_path, 'trainval_large', 'images'),
             train_large_names,
             '.png',
             num_process=args.num_process)

    # extract val images contain large instances
    val_large_names = extract_largesize_index(os.path.join(data_root_path, 'val', 'labelTxt'))
    filecopy(os.path.join(data_root_path, 'val', 'labelTxt'),
             os.path.join(data_root_path, 'trainval_large', 'labelTxt'),
             val_large_names,
             '.txt',
             num_process=args.num_process)
    filecopy(os.path.join(data_root_path, 'val', 'images'),
             os.path.join(data_root_path, 'trainval_large', 'images'),
             val_large_names,
             '.png',
             num_process=args.num_process)

    # split for images contin large size instances
    if not os.path.exists(os.path.join(data_root_path, 'trainval_large_1024_0.4')):
        os.makedirs(os.path.join(data_root_path, 'trainval_large_1024_0.4'))
    split_trainval_large = ImgSplit_multi_process.splitbase(os.path.join(data_root_path, 'trainval_large'),
                                    os.path.join(data_root_path, 'trainval_large_1024_0.4'),
                                    gap=512,
                                    subsize=1024,
                                    num_process=args.num_process)
    split_trainval_large.splitdata(0.4)

    # rotate augment for images contain large size instances
    rotate_augment(os.path.join(data_root_path, 'trainval_large_1024_0.4'),
                   os.path.join(data_root_path, 'trainval_large_1024_0.4_rotate'))

    # copy files to images and labelTxt
    if not os.path.exists(os.path.join(data_root_path, 'images')):
        os.makedirs(os.path.join(data_root_path, 'images'))
    if not os.path.exists(os.path.join(data_root_path, 'labelTxt')):
        os.makedirs(os.path.join(data_root_path, 'labelTxt'))

    filemove_v2(os.path.join(data_root_path, 'trainval1024_1', 'images'),
                os.path.join(data_root_path, 'images'),
                '.png',
                num_process=args.num_process
                )
    filemove_v2(os.path.join(data_root_path, 'trainval1024_1', 'labelTxt'),
                os.path.join(data_root_path, 'labelTxt'),
                '.txt',
                num_process=args.num_process
                )

    filemove_v2(os.path.join(data_root_path, 'trainval_large_1024_0.4', 'images'),
                os.path.join(data_root_path, 'images'),
                '.png',
                num_process=args.num_process
                )
    filemove_v2(os.path.join(data_root_path, 'trainval_large_1024_0.4', 'labelTxt'),
                os.path.join(data_root_path, 'labelTxt'),
                '.txt',
                num_process=args.num_process
                )

    filemove_v2(os.path.join(data_root_path, 'trainval_large_1024_0.4_rotate', 'images'),
                os.path.join(data_root_path, 'images'),
                '.png',
                num_process=args.num_process
                )
    filemove_v2(os.path.join(data_root_path, 'trainval_large_1024_0.4_rotate', 'labelTxt'),
                os.path.join(data_root_path, 'labelTxt'),
                '.txt',
                num_process=args.num_process
                )

    train_without_balance = util.GetFileFromThisRootDir(os.path.join(data_root_path, 'labelTxt'))
    train_without_balance_names = [util.custombasename(x.strip()) for x in train_without_balance]

    # data balance
    with open('train_balance_extend.txt', 'r') as f_in:
        train_balance_names = f_in.readlines()
        train_balance_names = [x.strip() for x in train_balance_names]
    train_names = train_without_balance_names + train_balance_names
    with open(os.path.join(data_root_path, 'train.txt'), 'w') as f_out:
        for index, name in enumerate(train_names):
            if index == (len(train_names) - 1):
                f_out.write(name)
            else:
                f_out.write(name + '\n')

    # prepare test data
    if not os.path.exists(os.path.join(data_root_path, 'test1024')):
        os.makedirs(os.path.join(data_root_path, 'test1024'))

    split_test = SplitOnlyImage_multi_process.splitbase(os.path.join(test_path, 'images'),
                       os.path.join(data_root_path, 'test1024', 'images'),
                      gap=512,
                      subsize=1024,
                      num_process=args.num_process
                      )
    split_test.splitdata(1)
    split_test.splitdata(0.5)

    test_names = util.GetFileFromThisRootDir(os.path.join(data_root_path, 'test1024', 'images'))
    test_names = [util.custombasename(x.strip()) for x in test_names]

    with open(os.path.join(data_root_path, 'test.txt'), 'w') as f_out:
        for index, name in enumerate(test_names):
            if index == (len(test_names) - 1):
                f_out.write(name)
            else:
                f_out.write(name + '\n')

    filemove_v2(os.path.join(data_root_path, 'test1024', 'images'),
                os.path.join(data_root_path, 'images'),
                '.png',
                num_process=args.num_process)

    shutil.rmtree(os.path.join(data_root_path, r'trainval_large_1024_0.4'))
    shutil.rmtree(os.path.join(data_root_path, r'trainval_large_1024_0.4_rotate'))
    shutil.rmtree(os.path.join(data_root_path, r'test1024'))
    shutil.rmtree(os.path.join(data_root_path, r'trainval1024_1'))
    shutil.rmtree(os.path.join(data_root_path, r'trainval_large'))
def filemove_v2(srcpath, dstpath, extent, num_process=32):
    filelist = util.GetFileFromThisRootDir(srcpath)
    filenames = [util.custombasename(x.strip()) for x in filelist]
    print('srcpath: ', srcpath)
    print('num: ', len(filenames))
    filemove(srcpath, dstpath, filenames, extent, num_process)
def mergebase(srcpath, dstpath, nms):
    filelist = util.GetFileFromThisRootDir(srcpath)
    for filename in filelist:
        mergesingle(dstpath, nms, filename)