Beispiel #1
0
 def replay(self, dev: device.Device, state, observer, tlib, states=None):
     for test in self.steps:
         try:
             ret = test.attempt(dev, observer, state, tlib)
         except:
             progress.report_progress("  STEP %s EXCEPTION" %
                                      test.short_name())
             raise
         if not ret:
             progress.report_progress("  STEP %s FAILED" %
                                      test.short_name())
             return False
         tlib.handle_sys_screen(state, dev, observer)
         if states is not None:
             states.append(copy.deepcopy(state))
     return True
Beispiel #2
0
    def pick_route(self, test, testinfo=None, unclean_change=True):
        picked = None
        target_state = None
        for screen in self.screens:
            if test.usable(screen) and (testinfo is None
                                        or not testinfo.finished(screen)):
                progress.report_progress("    consider screen %s for test %s" %
                                         (screen, test.short_name()))
                newpicked = self.pick_better_route(screen, picked,
                                                   unclean_change)
                if newpicked is not None:
                    progress.report_progress("      select %s for %s" %
                                             (newpicked, self.better_reason))
                    picked = newpicked
                    target_state = screen

        if picked is not None:
            return (picked.route, target_state)
        else:
            return (None, None)
Beispiel #3
0
 def report_prog(self, prog):
     progress.report_progress(prog)
Beispiel #4
0
 def report_prog(self, prog):
     self.progress = "R.%d %s" % (self.round_no, prog)
     progress.report_progress(self.progress)
def main(args):
    print(f'Source region extraction tool version {__version__}')
    a = ArgumentParser()
    a.add_argument(
        'input',
        help=
        'figure image file or image folder path or semicolon separated list of files or meta json lines file'
    )
    a.add_argument(
        '--demo',
        help='highlight source region on input images and save separately',
        default=False,
        action='store_true')
    a.add_argument('-out', help='source regions output dir', default='out')
    a.add_argument('-meta_out',
                   help='source regions output JSONL file',
                   default='regions.json')
    args = a.parse_args(args)
    demo_dir = args.out + '/demo'
    print('Building a list of source files...')
    if os.path.exists(demo_dir):
        shutil.rmtree(demo_dir, ignore_errors=True)
    if args.demo:
        os.makedirs(demo_dir + '/', exist_ok=True)
    if args.out:
        if os.path.exists(args.out):
            shutil.rmtree(args.out, ignore_errors=True)
        try:
            os.makedirs(args.out, exist_ok=True)
            if args.demo:
                os.makedirs(demo_dir + '/', exist_ok=True)  # andrey
        except:
            pass

    #if (args.input[-1]!='\\'):
    articles = get_articles(args.input)
    file_mapping = get_file_mapping(articles)
    #else:
    #    file_mapping = {}
    #    for (dirpath, dirnames, filenames) in os.walk(args.input):
    #        for fnam in filenames:
    #            file_mapping[args.input+fnam] = ArticleImage(id=1,chain_id='0',filename=args.input+fnam,title='',page=1,idx_on_page=1,regions=[])
    #        break
    if len(file_mapping) == 0:
        print('No files found in specified sources')
        exit(-1)
    reg_id = 0
    for i, filename in enumerate(file_mapping.keys()):
        src_img = cv2.imread(filename)
        if src_img is None:
            print(f'Error reading file, skipping: {filename}')
            continue
        cur_id = 1
        # extract with default settings
        boxes, boxes2, boxes3, boxes3a, boxes4 = [], [], [], [], []
        PerformDebug = False
        annotations, boxes = get_region_boxes(src_img,
                                              bin_thresh=5,
                                              debug=PerformDebug)
        cur_id = annotate_boxes(cur_id, boxes, (255, 0, 0))
        # preset 2 to extract bordered regions
        annotations2, boxes2 = get_region_boxes(src_img,
                                                erode_iters=0,
                                                bin_thresh=50,
                                                debug=PerformDebug)
        cur_id = annotate_boxes(cur_id, boxes2, (0, 255, 0))
        # preset 3 with another threshold
        annotations3, boxes3 = get_region_boxes(src_img,
                                                erode_iters=0,
                                                bin_thresh=100,
                                                debug=PerformDebug)
        cur_id = annotate_boxes(cur_id, boxes3, (0, 0, 255))
        # preset 3a with another threshold
        annotations3a, boxes3a = get_region_boxes(src_img,
                                                  erode_iters=0,
                                                  bin_thresh=150,
                                                  debug=PerformDebug)
        cur_id = annotate_boxes(cur_id, boxes3a, (0, 0, 255))
        # preset 4 - low threshold no morph
        annotations4, boxes4 = get_region_boxes(src_img,
                                                erode_iters=0,
                                                debug=PerformDebug)
        cur_id = annotate_boxes(cur_id, boxes4, (255, 0, 255))
        #boxes = filter_boxes(boxes + boxes2 + boxes3 + boxes3a + boxes4)
        all_boxes = boxes + boxes2 + boxes3 + boxes3a + boxes4
        # Check the number of all boxes
        if len(all_boxes) == 1:  # add the entire image as a box
            box = Box(0, 0, src_img.shape[1] - 1, src_img.shape[0] - 1)
            box.img_data = src_img
            all_boxes.append(box)
        PerformDebug = False
        updated_boxes = filter_boxes_updated(all_boxes,
                                             src_img,
                                             debug=PerformDebug)
        # boxes = boxes + boxes2 + boxes3
        if len(updated_boxes) == 0:
            print(
                f'Warning: {filename} no source regions detected, assuming single source region'
            )
            box = Box(0,
                      0,
                      src_img.shape[1] - 1,
                      src_img.shape[0] - 1,
                      id=cur_id)
            box.img_data = src_img
            updated_boxes.append(box)

        # remove borders from regions
        print(f'{filename} number of source regions: {len(updated_boxes)}')
        for b in updated_boxes:
            src_reg = b.img_data
            assert src_reg.shape[:2] == b.img_data.shape[:2]
            # crop borders on normalized reg (white/black borders)

            PerformDebug = False
            x1, x2, y1, y2 = crop_border(preprocess_image(b.img_data,
                                                          150,
                                                          otsu=True),
                                         src_reg,
                                         debug=PerformDebug)
            sr = src_reg[y1:y2, x1:x2]
            # sr = src_img[b.y1:b.y2, b.x1:b.x2]
            reg_path = os.path.join(
                args.out,
                '.'.join(os.path.basename(filename).split('.')[:-1]) + '_' +
                'R' + str(b.id) + '.png')
            article_image = file_mapping[filename]
            article_image.regions.append(
                Region(id=reg_id,
                       chain_id=article_image.chain_id,
                       filename=os.path.abspath(reg_path),
                       box=(b.x1, b.y1, b.x2, b.y2),
                       matches=[],
                       title=os.path.basename(reg_path)))
            reg_id += 1
            #cv2.imshow('subimage', sr)  # andrey
            #cv2.waitKey(0)
            cv2.imwrite(reg_path, sr)

        if args.demo:
            ref = src_img.copy()
            for b in updated_boxes:
                cv2.rectangle(ref, (b.x1, b.y1), (b.x2, b.y2),
                              color=(b.meta if b.meta is not None else
                                     (255, 128, 100)),
                              thickness=2)
            ResultImagePathToSave = os.path.join(
                demo_dir,
                '.'.join(os.path.basename(filename).split('.')[:-1]) + '.png')
            cv2.imwrite(ResultImagePathToSave, ref)
            cv2.imshow('image with bounding box', ref)  # andrey
            cv2.waitKey(0)
        progress.report_progress(i + 1, len(file_mapping),
                                 'Extracting regions')
        #show(ref_imgs=annotations, title=f'{filename}')
        #show(ref_imgs=annotations2, title=f'{filename} pass 2')
        #show(ref_imgs=annotations3, title=f'{filename} pass 3')
        #show(ref_imgs=annotations4, title=f'{filename} pass 4')
    with open(args.meta_out, mode='w') as meta_file:
        for a in articles:
            meta_file.write(a.toJSON() + os.linesep)