def compo_detection(input_img_path, output_root, resize_by_height=600): start = time.clock() print("Compo Detection for %s" % input_img_path) name = input_img_path.split('\\')[-1][:-4] # *** Step 1 *** pre-processing: read img -> get binary map org, grey = pre.read_img(input_img_path, resize_by_height) binary_org = pre.preprocess(org, write_path=pjoin(output_root, name + '_binary.png')) # *** Step 2 *** block processing: detect block -> detect components in block blocks_corner = blk.block_division(grey, write_path=pjoin(output_root, name + '_block.png')) compo_in_blk_boundary, compo_in_blk_corner, compo_in_blk_class = processing_block(org, binary_org, blocks_corner, cnn) # *** Step 3 *** non-block processing: erase blocks from binary -> detect left components binary_non_block = blk.block_erase(binary_org, blocks_corner) compo_non_blk_boundary, compo_non_blk_corner, compo_non_blk_class = processing(org, binary_non_block, cnn, True) # *** Step 4 *** merge results # compos_boundary = compo_in_blk_boundary + compo_non_blk_boundary compos_corner = compo_in_blk_corner + compo_non_blk_corner compos_class = compo_in_blk_class + compo_non_blk_class # *** Step 5 *** save results: save text label -> save drawn image draw.draw_bounding_box_class(org, compos_corner, compos_class, write_path=pjoin(output_root, name + '_ip.png')) file.save_corners_json(pjoin(output_root, name + '_ip.json'), compos_corner, compos_class) print("[Compo Detection Completed in %.3f s]" % (time.clock() - start))
def block_detection(input_img_path, output_root, num=0, resize_by_height=800, show=False): start = time.clock() name = input_img_path.split('\\')[-1][:-4] # *** Step 1 *** pre-processing: read img -> get binary map org, grey = pre.read_img(input_img_path, resize_by_height) # *** Step 2 *** block processing: detect block -> calculate hierarchy -> detect components in block blocks = blk.block_division(grey) blocks = blk.block_add_bkg(blocks, org, grey.shape, show=show) blk.block_hierarchy(blocks) file.save_blocks(pjoin(output_root, name + '.json'), blocks) draw.draw_region(blocks, grey.shape, show=show, write_path=pjoin(output_root, name + '_blk.png')) cv2.imwrite(pjoin(output_root, name + '.png'), org) print("[Compo Detection Completed in %.3f s] %d %s" % (time.clock() - start, num, input_img_path))
def nesting_inspection(org, grey, compos): nesting_compos = [] for compo in compos: if compo.height > 50: clip_org = compo.compo_clipping(org) clip_grey = compo.compo_clipping(grey) n_compos = blk.block_division(clip_grey, org, show=False) Compo.cvt_compos_relative_pos(n_compos, compo.bbox.col_min, compo.bbox.row_min) nesting_compos += n_compos # cv2.imshow('clip', clip_org) # cv2.waitKey() return nesting_compos
def nesting_inspection(org, grey, compos): nesting_compos = [] for i, compo in enumerate(compos): if compo.height > 50: replace = False clip_org = compo.compo_clipping(org) clip_grey = compo.compo_clipping(grey) n_compos = blk.block_division(clip_grey, org, show=False) Compo.cvt_compos_relative_pos(n_compos, compo.bbox.col_min, compo.bbox.row_min) for n_compo in n_compos: if n_compo.redundant: compos[i] = n_compo replace = True break if not replace: nesting_compos += n_compos # cv2.imshow('clip', clip_org) # cv2.waitKey() return nesting_compos
def compo_detection(input_img_path, output_root, num=0, resize_by_height=600, block_pad=4, classifier=None, show=False, write_img=True): start = time.clock() name = input_img_path.split('\\')[-1][:-4] ip_root = file.build_directory(pjoin(output_root, "ip")) # *** Step 1 *** pre-processing: read img -> get binary map org, grey = pre.read_img(input_img_path, resize_by_height) binary = pre.binarization( org, show=show, write_path=pjoin(ip_root, name + '_binary.png') if write_img else None) binary_org = binary.copy() # *** Step 2 *** block processing: detect block -> calculate hierarchy -> detect components in block blocks = blk.block_division( grey, org, show=show, write_path=pjoin(ip_root, name + '_block.png') if write_img else None) blk.block_hierarchy(blocks) uicompos_in_blk = processing_block(org, binary, blocks, block_pad) # *** Step 3 *** non-block part processing: remove lines -> erase blocks from binary -> detect left components det.rm_line(binary, show=show) blk.block_bin_erase_all_blk(binary, blocks, block_pad) uicompos_not_in_blk = det.component_detection(binary) uicompos = uicompos_in_blk + uicompos_not_in_blk # *** Step 4 *** results refinement: remove top and bottom compos -> merge words into line uicompos = det.rm_top_or_bottom_corners(uicompos, org.shape) file.save_corners_json(pjoin(ip_root, name + '_all.json'), uicompos) uicompos = det.merge_text(uicompos, org.shape) draw.draw_bounding_box(org, uicompos, show=show) # uicompos = det.merge_intersected_corner(uicompos, org.shape) Compo.compos_containment(uicompos) # draw.draw_bounding_box(org, uicompos, show=show, write_path=pjoin(ip_root, name + '_ip.png') if write_img else None) # # *** Step 5 *** Image Inspection: recognize image -> remove noise in image -> binarize with larger threshold and reverse -> rectangular compo detection # if classifier is not None: # classifier['Image'].predict(seg.clipping(org, uicompos), uicompos) # draw.draw_bounding_box_class(org, uicompos, show=show) # uicompos = det.rm_noise_in_large_img(uicompos, org) # draw.draw_bounding_box_class(org, uicompos, show=show) # det.detect_compos_in_img(uicompos, binary_org, org) # draw.draw_bounding_box(org, uicompos, show=show) # if classifier is not None: # classifier['Noise'].predict(seg.clipping(org, uicompos), uicompos) # draw.draw_bounding_box_class(org, uicompos, show=show) # uicompos = det.rm_noise_compos(uicompos) # *** Step 6 *** element classification: all category classification if classifier is not None: classifier['Elements'].predict(seg.clipping(org, uicompos), uicompos) draw.draw_bounding_box_class(org, uicompos, show=show, write_path=pjoin(ip_root, name + '_cls.png')) # uicompos = det.compo_filter(uicompos, org) draw.draw_bounding_box(org, uicompos, show=show) file.save_corners_json(pjoin(ip_root, name + '.json'), uicompos) print("[Compo Detection Completed in %.3f s] %d %s" % (time.clock() - start, num, input_img_path)) # Record run time open('time.txt', 'a').write(str(round(time.clock() - start, 3)) + '\n') if show: cv2.destroyAllWindows()