Esempio n. 1
0
def ms_eval(ms_res_dirs, cfg):

    merge_box = cfg.TEST.MERGE_BOX
    '''
    for scale in ms_res_dirs["scales"]:

        print("*************** Scale:", scale, " ***************")

        result_dir = ms_res_dirs["result_prefix"] + "_" + str(scale)

        model = RRPNDemo(
        cfg,
        min_image_size=scale,
        confidence_threshold=0.6,
        )

        image_list = os.listdir(ms_res_dirs['ori_im_dir'])
        num_images = len(image_list)
        cnt = 0
        for idx in range(len(image_list)):
            image = 'img_' + str(idx+1) + '.jpg'
            impath = os.path.join(ms_res_dirs['ori_im_dir'], image)
            # print('image:', impath)
            img = cv2.imread(impath)
            cnt += 1
            tic = time.time()
            predictions, bounding_boxes = model.run_on_opencv_image(img)
            toc = time.time()

            print('time cost:', str(toc - tic)[:6], '|', str(cnt) + '/' + str(num_images))

            # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            bboxes_np = bounding_boxes.bbox.data.cpu().numpy()
            scores = bounding_boxes.get_field("scores").data.cpu().numpy()
            # ms_scores = bounding_boxes.get_field("mask_score").data.cpu().numpy()
            # bboxes_np[:, 2:4] /= cfg.MODEL.RRPN.GT_BOX_MARGIN

            # if merge_box:
            #    bboxes_np_reverse = bboxes_np.copy()
            #    bboxes_np_reverse[:, 2:4] = bboxes_np_reverse[:, 3:1:-1]
            #   bboxes_np_reverse = merge(bboxes_np_reverse)
            #   bboxes_np_reverse[:, 2:4] = bboxes_np_reverse[:, 3:1:-1]
            #    bboxes_np = bboxes_np_reverse

            width, height = bounding_boxes.size

            # if vis:
            #     pil_image = vis_image(Image.fromarray(img), bboxes_np)
            #     pil_image.save(os.path.join(image_vis_dir, image))
                # pil_image.show()
                # time.sleep(20)
            # else:
            write_result_ICDAR_RRPN2polys(image[:-4],
                                          bboxes_np,
                                          threshold=0.7,
                                          result_dir=result_dir,
                                          height=height,
                                          width=width,
                                          cls_prob=scores,
                                          # ms_scores=ms_scores,
                                          scale=[scale for i in range(bboxes_np.shape[0])])

        del model
    '''
    f_list = os.listdir(ms_res_dirs["result_prefix"] + "_" +
                        str(ms_res_dirs["scales"][0]))

    nms_thres = 0.1

    cnt = 0

    if len(os.listdir(fuse_resdir)) > 0:
        print(os.popen("rm -rf " + fuse_resdir).read())
        os.makedirs(fuse_resdir)

    for resname in f_list:
        if resname[-4:] == '.txt':
            # print('resname:', resname)
            all_boxes = []
            for scale in ms_res_dirs["scales"]:
                result_dir = ms_res_dirs["result_prefix"] + "_" + str(scale)
                all_boxes.append(get_rboxes(os.path.join(result_dir, resname)))
            # sboxes = get_rboxes(os.path.join(ms_res_dirs['S'], resname)).reshape(-1, 6)
            # mboxes = get_rboxes(os.path.join(ms_res_dirs['M'], resname)).reshape(-1, 6)
            # lboxes = get_rboxes(os.path.join(ms_res_dirs['L'], resname)).reshape(-1, 6)
            # print('shape:', sboxes.shape, mboxes.shape, lboxes.shape)

            # idx = int(((int(resname[-9:-4]) - 1) / 5000)) + 1
            imdir = os.path.join(ms_res_dirs['ori_im_dir'], resname[4:-4])
            # imdir = os.path.join(ms_res_dirs['ori_im_dir']) + '/tr_' + resname[4:-4]

            # print('resname:', resname[-9:-4])
            im = cv2.imread(imdir + '.jpg')
            '''
            
            if im is None:
                im = cv2.imread(imdir + '.JPG')
            if im is None:
                im = cv2.imread(imdir + '.png')
            if im is None:
                tmp = imageio.mimread(imdir + '.gif')
                im_tmp = np.array(tmp)
                im_tmp = im_tmp[0]
                im = im_tmp[:, :, 0:3]
            '''
            height, width = im.shape[:2]

            all_boxes = np.concatenate(all_boxes, axis=0)
            # all_boxes[:, -1] = 1 - all_boxes[:, -1]

            # cls_score, ms_score, scales

            arg = np.argsort(-all_boxes[:, 6])
            all_boxes = all_boxes[arg]
            keep = rnms(
                np.concatenate([all_boxes[:, :5], all_boxes[:, 5:6]], axis=1),
                nms_thres)
            # print(keep)
            # all_boxes = all_boxes[keep]

            # merge_keep = merge_closest(all_boxes[:, :5], all_boxes[:, 5], 0.85)
            # all_boxes = all_boxes[merge_keep]

            all_boxes = all_boxes[all_boxes[:, 5] > 0.8]
            # all_boxes = all_boxes[all_boxes[:, 6] > 0.55]
            # print('all_boxes:', all_boxes.shape)
            cnt += 1
            print('cnt:', str(cnt) + '/' + str(len(f_list)), resname)

            write_result_ICDAR_RRPN2polys(
                resname[4:-4],
                all_boxes,
                0.7,
                fuse_resdir_4save,
                height,
                width,
                cls_prob=all_boxes[:, 5],
                # ms_scores=all_boxes[:, 6],
                scale=all_boxes[:, 6])
            write_result_ICDAR_RRPN2polys(
                resname[4:-4],
                all_boxes,
                0.7,
                fuse_resdir,
                height,
                width,
                # cls_prob=all_boxes[:, 5],
                # ms_scores=all_boxes[:, 6],
                # scale=all_boxes[:, 7]
            )

    zipfilename = os.path.join(fuse_resdir, 'submit_MS_test.zip')
    if os.path.isfile(zipfilename):
        print('Zip file exists, removing it...')
        os.remove(zipfilename)
    zip_dir(fuse_resdir, zipfilename)
    # res_dict = compute_hmean(zipfilename)

    # res_samples = res_dict["per_sample"]
    # print("res_sample:", res_samples.keys())

    pkl_cache = "res_cache.pkl"

    # if not os.path.isfile(pkl_cache):
    res_dict = compute_hmean(zipfilename)
    # res_samples = res_dict["per_sample"]
    res_samples = res_dict["per_sample"]

    pkl_f = open(pkl_cache, "wb")
    pickle.dump(res_samples, pkl_f)
    pkl_f.close()
    # else:
    #    pkl_f = open(pkl_cache, "rb")
    #    res_samples = pickle.load(pkl_f)

    # print("res_sample:", res_samples.keys())
    '''
    comm = 'curl -i -F "submissionFile=@' + zipfilename + '" http://127.0.0.1:8080/evaluate'
    # print(comm)
    print(os.popen(comm, 'r'))
    '''

    vis_eval(res_samples, ms_res_dirs['ori_im_dir'], fuse_resdir_4save)
Esempio n. 2
0
    width, height = bounding_boxes.size

    if vis:
        pil_image = vis_image(Image.fromarray(img), bboxes_np)
        pil_image.show()
        time.sleep(10)
    write_result_ICDAR_RRPN2polys(image[:-4],
                                  bboxes_np,
                                  threshold=0.7,
                                  result_dir=result_dir,
                                  height=height,
                                  width=width)
    #im_file, dets, threshold, result_dir, height, width
    #cv2.imshow('win', predictions)
    #cv2.waitKey(0)

if dataset_name == 'IC15':
    zipfilename = os.path.join(
        result_dir, 'submit_' + config_file.split('/')[-1].split('.')[0] +
        '_' + cfg.MODEL.WEIGHT.split('/')[-1].split('.')[0] + '.zip')
    if os.path.isfile(zipfilename):
        print('Zip file exists, removing it...')
        os.remove(zipfilename)
    zip_dir(result_dir, zipfilename)
    comm = 'curl -i -F "submissionFile=@' + zipfilename + '" http://127.0.0.1:8080/evaluate'
    # print(comm)
    print(os.popen(comm, 'r'))
else:
    pass
def gotest(setting_weights, config_file, remove_output_dir=False):

    # config_file = "configs/arpn/e2e_rrpn_R_50_C4_1x_train_AFPN_RT.yaml" #'configs/rrpn/e2e_rrpn_R_50_C4_1x_LSVT_test_RFPN.yaml' #'#"configs/ICDAR2019_det_RRPN/e2e_rrpn_R_50_C4_1x_LSVT_val_4scales_angle_norm.yaml" #e2e_rrpn_R_50_C4_1x_ICDAR13_15_trial_test.yaml

    # update the config options with the config file
    cfg.merge_from_file(config_file)
    # manual override some options
    cfg.merge_from_list(["MODEL.DEVICE", "cuda"])
    if remove_output_dir:
        cfg.merge_from_list(["OUTPUT_DIR", ""])
    opts = ["MODEL.WEIGHT", setting_weights]
    cfg.merge_from_list(opts)
    # cfg.MODEL.WEIGHT = setting_weights
    # cfg.freeze()

    vis = True
    merge_box = cfg.TEST.MERGE_BOX
    result_dir = os.path.join('results',
                              config_file.split('/')[-1].split('.')[0],
                              cfg.MODEL.WEIGHT.split('/')[-1].split('.')[0])

    if merge_box:
        result_dir += '_merge_box'

    if not os.path.isdir(result_dir):
        os.makedirs(result_dir)
    else:
        print(os.popen("rm -rf " + result_dir).read())
        os.makedirs(result_dir)
    coco_demo = RRPNDemo(
        cfg,
        min_image_size=1260,
        confidence_threshold=0.4,
    )
    #
    dataset_name = cfg.TEST.DATASET_NAME

    print("dataset_name:", dataset_name)

    testing_dataset = {
        'LSVT': {
            'testing_image_dir':
            '../datasets/LSVT/train_full_images_0/train_full_images_0/',
            'off': [0, 3000]
        },
        'ArT': {
            'testing_image_dir':
            '../datasets/ArT/ArT_detect_train/train_images',
            'off': [4000, 5603]
        },
        'ReCTs': {
            'testing_image_dir': '',
            'off': [0, 1000000]
        },
        'MLT': {
            'testing_image_dir': '/mnt/nvme/Dataset/MLT/val/image/',
            'off': [0, 1000000]
        },
        'MLT_test': {
            'testing_image_dir': [
                '/mnt/nvme/Dataset/MLT/test/MLT19_TestImagesPart1/TestImagesPart1',
                '/mnt/nvme/Dataset/MLT/test/MLT19_TestImagesPart2/TestImagesPart2'
            ],
            'off': [0, 1000000]
        },
        'IC15': {
            'testing_image_dir': '../dataset/ICDAR15/ch4_test_images/',
            'off': [0, 500],
            'gt_dir': "../dataset/ICDAR15/Challenge4_Test_Task4_GT"
        },
        'docVQA': {
            'testing_image_dir':
            '../dataset/docVQA/docvqa_train_v0.1/documents/',
            'off': [4000, 4330],
            # 'gt_dir':"../dataset/ICDAR15/Challenge4_Test_Task4_GT"
        },
        'docVQA_TEST': {
            'testing_image_dir': '../dataset/docVQA/test/documents/',
            'off': [0, 1000000]  # [4000, 4330],
            # 'gt_dir':"../dataset/ICDAR15/Challenge4_Test_Task4_GT"
        },
        'docVQA_VAL': {
            'testing_image_dir': '../dataset/docVQA/val/documents/',
            'off': [0, 1000000]
        }
    }

    image_dir = testing_dataset[dataset_name]['testing_image_dir']
    # vocab_dir = testing_dataset[dataset_name]['test_vocal_dir']
    off_group = testing_dataset[dataset_name]['off']
    # load image and then run prediction
    # image_dir = '../datasets/ICDAR13/Challenge2_Test_Task12_Images/'
    # imlist = os.listdir(image_dir)[off_group[0]:off_group[1]]

    image_vis_dir = "vis_results_" + dataset_name + "/"
    if not os.path.isdir(image_vis_dir):
        os.makedirs(image_vis_dir)

    print('************* META INFO ***************')
    print('config_file:', config_file)
    print('result_dir:', result_dir)
    print('image_dir:', image_dir)
    print('weights:', cfg.MODEL.WEIGHT)
    print('merge_box:', merge_box)
    print('***************************************')

    imlist = []

    if type(image_dir) == list:
        for dir in image_dir:
            tmp_list = os.listdir(dir)
            for im_name in tmp_list:
                imlist.append(os.path.join(dir, im_name))
    else:
        imlist = os.listdir(image_dir)

    imlist.sort()

    num_images = len(imlist)
    cnt = 0

    if dataset_name in ['ReCTs', 'MLT']:
        for image in imlist:
            # image = 'gt_' + str(idx) + '.jpg'
            if type(image_dir) == list:
                impath = image
            else:
                impath = os.path.join(image_dir, image)

            # print('image:', impath)
            img = cv2.imread(impath)
            cnt += 1
            tic = time.time()
            predictions, bounding_boxes = coco_demo.run_on_opencv_image(img)
            toc = time.time()

            print('time cost:',
                  str(toc - tic)[:6], '|',
                  str(cnt) + '/' + str(num_images))

            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            bboxes_np = bounding_boxes.bbox.data.cpu().numpy()
            # bboxes_np[:, 2:4] /= cfg.MODEL.RRPN.GT_BOX_MARGIN

            if merge_box:
                bboxes_np_reverse = bboxes_np.copy()
                bboxes_np_reverse[:, 2:4] = bboxes_np_reverse[:, 3:1:-1]
                bboxes_np_reverse = merge(bboxes_np_reverse)
                bboxes_np_reverse[:, 2:4] = bboxes_np_reverse[:, 3:1:-1]
                bboxes_np = bboxes_np_reverse

            width, height = bounding_boxes.size

            if vis:
                pil_image = vis_image(Image.fromarray(img), bboxes_np)
                # pil_image.show()

                # time.sleep(20)
            else:

                results_prefix = image[:-4].replace(
                    'tr_', '') if dataset_name == 'MLT' else image[:-4]

                write_result_ICDAR_RRPN2polys(results_prefix,
                                              bboxes_np,
                                              threshold=0.7,
                                              result_dir=result_dir,
                                              height=height,
                                              width=width)
            # im_file, dets, threshold, result_dir, height, width
            # cv2.imshow('win', predictions)
            # cv2.waitKey(0)
    else:
        num_images = min(off_group[1], len(imlist)) - off_group[0]
        for idx in range(off_group[0], min(off_group[1], len(imlist))):

            if dataset_name == "IC15":
                image = 'img_' + str(idx + 1) + '.jpg'  #idx
            else:
                image = imlist[idx]
            impath = os.path.join(image_dir, image)
            # print('image:', impath)
            img = cv2.imread(impath)
            # img = padding32(img)

            cnt += 1
            tic = time.time()
            predictions, bounding_boxes = coco_demo.run_on_opencv_image(img)
            toc = time.time()

            print('time cost:',
                  str(toc - tic)[:6], '|',
                  str(cnt) + '/' + str(num_images), image)

            bboxes_np = bounding_boxes.bbox.data.cpu().numpy()
            # bboxes_np[:, 2:4] /= cfg.MODEL.RRPN.GT_BOX_MARGIN

            # bboxes_np[:, :2] = bboxes_np[:, :2] - 16.

            if merge_box:
                bboxes_np_reverse = bboxes_np.copy()
                bboxes_np_reverse[:, 2:4] = bboxes_np_reverse[:, 3:1:-1]
                bboxes_np_reverse = merge(bboxes_np_reverse)
                bboxes_np_reverse[:, 2:4] = bboxes_np_reverse[:, 3:1:-1]
                bboxes_np = bboxes_np_reverse

            width, height = bounding_boxes.size

            scores = bounding_boxes.get_field("scores").data.cpu().numpy()

            if bboxes_np.shape[0] > 0:
                # merge_keep = merge_closest(bboxes_np, scores, 0.81)
                # bboxes_np = bboxes_np[merge_keep]
                # scores = scores[merge_keep]
                pass
            if vis:
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                pil_image = Image.fromarray(img)
                # print("bboxes_np:", bboxes_np)

                if "gt_dir" in testing_dataset[dataset_name]:
                    gt_file = os.path.join(
                        testing_dataset[dataset_name]["gt_dir"],
                        "gt_" + image.split(".")[0] + ".txt")

                    gt_lines = open(gt_file, "r").readlines()
                    gt_boxes = []
                    for line in gt_lines:
                        splts = line.replace("\r", "").replace("\n",
                                                               "").split(",")
                        if len(splts) > 8:
                            gt_boxes.append(
                                np.array(splts[:8]).astype(np.int32))

                        pil_image = vis_image(pil_image,
                                              np.array(gt_boxes),
                                              mode=1,
                                              repre="poly")

                    # gt_image.save(os.path.join(image_vis_dir, "gt_" + image))
                pil_image = vis_image(pil_image, bboxes_np, mode=2)

                pil_image.save(os.path.join(image_vis_dir, image))
                # cv2.imwrite(os.path.join(image_vis_dir, "mask_" + image), predictions)
                # pil_image.show()
                # time.sleep(20)
            # else:

            # print("bboxes_np", bboxes_np.shape)

            write_result_ICDAR_RRPN2polys(image[:-4],
                                          bboxes_np,
                                          threshold=0.7,
                                          result_dir=result_dir,
                                          height=height,
                                          width=width)
            #im_file, dets, threshold, result_dir, height, width
            # print("predictions:", predictions.shape)
            # cv2.imshow('win', predictions)
            # cv2.waitKey(0)

    del coco_demo

    if dataset_name == 'IC15':

        zipfilename = os.path.join(
            result_dir, 'submit_' + config_file.split('/')[-1].split('.')[0] +
            '_' + cfg.MODEL.WEIGHT.split('/')[-1].split('.')[0] + '.zip')
        if os.path.isfile(zipfilename):
            print('Zip file exists, removing it...')
            os.remove(zipfilename)
        zip_dir(result_dir, zipfilename)

        comm = 'curl -i -F "submissionFile=@' + zipfilename + '" http://127.0.0.1:8080/evaluate'
        print(comm)
        print(os.popen(comm, 'r'))

        res_dict = compute_hmean(zipfilename)
        del res_dict["per_sample"]
        print(res_dict)

        return res_dict

    elif dataset_name == 'MLT':
        zipfilename = os.path.join(
            result_dir, 'submit_' + config_file.split('/')[-1].split('.')[0] +
            '_' + cfg.MODEL.WEIGHT.split('/')[-1].split('.')[0] + '.zip')
        if os.path.isfile(zipfilename):
            print('Zip file exists, removing it...')
            os.remove(zipfilename)
        zip_dir(result_dir, zipfilename)
        comm = 'curl -i -F "submissionFile=@' + zipfilename + '" http://127.0.0.1:8080/evaluate'
        # print(comm)
        print(os.popen(comm, 'r'))

    elif dataset_name == 'LSVT':
        # input_json_path = 'results/e2e_rrpn_R_50_C4_1x_LSVT_val/model_0190000/res.json'
        gt_json_path = '../datasets/LSVT/train_full_labels.json'
        # to json
        input_json_path = res2json(result_dir)
        eval_func(input_json_path, gt_json_path)
    elif dataset_name == 'ArT':
        # input_json_path = 'results/e2e_rrpn_R_50_C4_1x_LSVT_val/model_0190000/res.json'
        gt_json_path = '../datasets/ArT/ArT_detect_train/train_labels.json'
        # to json
        input_json_path = res2json(result_dir)
        eval_func(input_json_path, gt_json_path)
    else:
        pass

    return None