def uied():
    start = time.clock()
    models, input_path, output_root, note_success_file, note_fail_file, uied_params = params.get_params(
    )
    print("UIED Running on:", input_path, " Write to:", output_root)

    resized_height = resize_height_by_longest_edge(input_path)

    if is_ocr:
        import ocr_east as ocr
        os.makedirs(pjoin(output_root, 'ocr'), exist_ok=True)
        ocr.east(input_path,
                 output_root,
                 models['text'],
                 resize_by_height=resized_height,
                 show=False)

    if is_ip:
        os.makedirs(pjoin(output_root, 'ip'), exist_ok=True)
        ip.compo_detection(input_path,
                           output_root,
                           uied_params=uied_params,
                           classifier=models['compo'],
                           resize_by_height=resized_height,
                           show=False)

    if is_merge:
        # os.makedirs(pjoin(output_root, 'merge'), exist_ok=True)
        import merge
        name = input_path.split('/')[-1][:-4]
        compo_path = pjoin(output_root, 'ip', str(name) + '.json')
        ocr_path = pjoin(output_root, 'ocr', str(name) + '.json')
        merge.incorporate(input_path,
                          compo_path,
                          ocr_path,
                          output_root,
                          resize_by_height=resized_height,
                          show=False)

    open(note_success_file, 'a').write(output_root + '\n')
    print("[UIED complete in %.3fs]" % (time.clock() - start))
    print(time.ctime(), '\n')
예제 #2
0
파일: run_east.py 프로젝트: zergmk2/UIED
import glob
import time
import json
from tqdm import tqdm
from os.path import join as pjoin, exists

ROOT_INPUT = 'E:\\Mulong\\Datasets\\rico\\combined'
ROOT_OUTPUT = 'E:\\Mulong\\Result\\east'

# set input root directory and sort all images by their indices
data = json.load(
    open('E:\\Mulong\\Datasets\\rico\\instances_test_org.json', 'r'))
input_paths_img = [
    pjoin(ROOT_INPUT, img['file_name'].split('/')[-1])
    for img in data['images']
]
input_paths_img = sorted(
    input_paths_img,
    key=lambda x: int(x.split('\\')[-1][:-4]))  # sorted by index

start_index = 9122
end_index = 100000
for i, input_path_img in enumerate(input_paths_img):
    print(i, '/', len(input_paths_img))
    index = input_path_img.split('\\')[-1][:-4]
    if int(index) < start_index:
        continue
    if int(index) > end_index:
        break
    ocr.east(input_path_img, ROOT_OUTPUT, resize_by_height=None, show=True)
C.build_output_folders(is_clip)
resize_by_height = 600

# set input root directory and sort all images by their indices
input_paths_img = glob.glob(pjoin(C.ROOT_INPUT, '*.jpg'))
input_paths_img = sorted(
    input_paths_img,
    key=lambda x: int(x.split('\\')[-1][:-4]))  # sorted by index
# set the range of target inputs' indices
start_index = 24
end_index = 50
for input_path_img in input_paths_img:
    index = input_path_img.split('\\')[-1][:-4]
    if int(index) < start_index:
        continue
    if int(index) > end_index:
        break

    # *** start processing ***
    start = time.clock()

    if is_ocr:
        ocr.east(input_path_img, C.ROOT_OCR, resize_by_height)
    if is_ip:
        ip.compo_detection(input_path_img, C.ROOT_IP, resize_by_height)
    if is_merge:
        merge.incorporate(input_path_img, C.ROOT_OCR, C.ROOT_IP, C.ROOT_MERGE,
                          resize_by_height, is_clip)

    print('*** Total Time Taken:%.3f s ***\n' % (time.clock() - start))
예제 #4
0
    # set the range of target inputs' indices
    num = 0
    start_index = 30800  # 61728
    end_index = 100000
    for input_path_img in input_paths_img:
        index = input_path_img.split('\\')[-1][:-4]
        if int(index) < start_index:
            continue
        if int(index) > end_index:
            break

        if is_ocr:
            import ocr_east as ocr
            ocr.east(input_path_img,
                     output_root,
                     resize_by_height=None,
                     show=False,
                     write_img=True)

        if is_ip:
            ip.compo_detection(input_path_img,
                               output_root,
                               num,
                               resize_by_height=resize_by_height,
                               show=True,
                               classifier=classifier)

        if is_merge:
            import merge
            compo_path = pjoin(output_root, 'ip', str(index) + '.json')
            ocr_path = pjoin(output_root, 'ocr', str(index) + '.json')
import time

is_ocr = False
is_ip = True
is_merge = False
resize_by_height = 800

# set input image path
PATH_IMG_INPUT = 'data\\input\\1.jpg'
PATH_OUTPUT_ROOT = 'data\\output'

start = time.clock()
if is_ocr:
    # import ocr_ctpn as ocr
    # ocr.ctpn(PATH_IMG_INPUT, PATH_OUTPUT_ROOT, resize_by_height)
    import ocr_east as ocr
    ocr.east(PATH_IMG_INPUT, PATH_OUTPUT_ROOT, resize_by_height)
if is_ip:
    import ip
    ip.compo_detection(PATH_IMG_INPUT, PATH_OUTPUT_ROOT, resize_by_height)
if is_merge:
    import merge
    merge.incorporate(PATH_IMG_INPUT, PATH_OUTPUT_ROOT, resize_by_height)
print('Time Taken:%.3f s\n' % (time.clock() - start))
    resized_height = resize_height_by_longest_edge(input_path_img)

    is_ip = True
    is_clf = False
    is_ocr = False
    is_merge = False

    if is_ocr:
        os.makedirs(pjoin(output_root, 'ocr'), exist_ok=True)
        import ocr_east as ocr
        import lib_east.eval as eval
        models = eval.load()
        ocr.east(input_path_img,
                 output_root,
                 models,
                 resize_by_height=resized_height,
                 show=False)

    if is_ip:
        os.makedirs(pjoin(output_root, 'ip'), exist_ok=True)
        # switch of the classification func
        classifier = None
        if is_clf:
            classifier = {}
            from cnn.CNN import CNN
            # classifier['Image'] = CNN('Image')
            classifier['Elements'] = CNN('Elements')
            # classifier['Noise'] = CNN('Noise')
        ip.compo_detection(input_path_img,
                           output_root,
    # set the range of target inputs' indices
    num = 0
    start_index = 30800  # 61728
    end_index = 100000
    for input_img in input_imgs:
        resized_height = resize_height_by_longest_edge(input_img)
        index = input_img.split('/')[-1][:-4]
        if int(index) < start_index:
            continue
        if int(index) > end_index:
            break

        if is_ocr:
            ocr.east(input_img,
                     output_root,
                     ocr_model,
                     resize_by_height=resized_height,
                     show=False)

        if is_ip:
            ip.compo_detection(input_img,
                               output_root,
                               classifier=compo_classifier,
                               resize_by_height=resized_height,
                               show=True)

        if is_merge:
            import merge
            compo_path = pjoin(output_root, 'ip', str(index) + '.json')
            ocr_path = pjoin(output_root, 'ocr', str(index) + '.json')
            merge.incorporate(input_img,
예제 #8
0
def uied(input_path,
         output_root,
         params=None,
         is_ip=True,
         is_clf=False,
         is_ocr=True,
         is_merge=True):
    '''
            ele:min-grad: gradient threshold to produce binary map
            ele:ffl-block: fill-flood threshold
            ele:min-ele-area: minimum area for selected elements
            ele:merge-contained-ele: if True, merge elements contained in others
            text:max-word-inline-gap: words with smaller distance than the gap are counted as a line
            text:max-line-gap: lines with smaller distance than the gap are counted as a paragraph

            Tips:
            1. Larger *min-grad* produces fine-grained binary-map while prone to over-segment element to small pieces
            2. Smaller *min-ele-area* leaves tiny elements while prone to produce noises
            3. If not *merge-contained-ele*, the elements inside others will be recognized, while prone to produce noises
            4. The *max-word-inline-gap* and *max-line-gap* should be dependent on the input image size and resolution

            mobile: {'min-grad':4, 'ffl-block':5, 'min-ele-area':25, 'max-word-inline-gap':6, 'max-line-gap':1}
            web   : {'min-grad':3, 'ffl-block':5, 'min-ele-area':25, 'max-word-inline-gap':4, 'max-line-gap':4}
        '''
    if params is None:
        params = {
            'min-grad': 4,
            'ffl-block': 5,
            'min-ele-area': 25,
            'merge-contained-ele': True,
            'max-word-inline-gap': 4,
            'max-line-gap': 4
        }
    else:
        params = json.loads(params)
        for i in params:
            if params[i] == 'True':
                params[i] = True
            elif params[i] == 'False':
                params[i] = False
            else:
                params[i] = int(params[i])
    print(params)

    start = time.clock()
    resized_height = resize_height_by_longest_edge(input_path)

    if is_ocr:
        import ocr_east as ocr
        import lib_east.eval as eval
        os.makedirs(pjoin(output_root, 'ocr'), exist_ok=True)
        models = eval.load()
        ocr.east(input_path,
                 output_root,
                 models,
                 params['max-word-inline-gap'],
                 resize_by_height=resized_height,
                 show=False)

    if is_ip:
        import detect_compo.ip_region_proposal as ip
        os.makedirs(pjoin(output_root, 'ip'), exist_ok=True)
        # switch of the classification func
        classifier = None
        if is_clf:
            classifier = {}
            from CNN import CNN
            # classifier['Image'] = CNN('Image')
            classifier['Elements'] = CNN('Elements')
            # classifier['Noise'] = CNN('Noise')
        ip.compo_detection(input_path,
                           output_root,
                           uied_params=params,
                           classifier=classifier,
                           resize_by_height=resized_height,
                           show=False)

    if is_merge:
        import merge
        # os.makedirs(pjoin(output_root, 'merge'), exist_ok=True)
        name = input_path.split('/')[-1][:-4]
        compo_path = pjoin(output_root, 'ip', str(name) + '.json')
        ocr_path = pjoin(output_root, 'ocr', str(name) + '.json')
        merge.incorporate(input_path,
                          compo_path,
                          ocr_path,
                          output_root,
                          params=params,
                          resize_by_height=resized_height,
                          show=False)

    print("[UIED complete in %.3fs]" % (time.clock() - start))
    print(time.ctime(), '\n')