def clip_word(sentences, min_pix, output_path): if not os.path.exists(output_path): os.mkdir(output_path) index = 0 words = [] for sentence in sentences: prepros = pre.preprocess(sentence) hist = [int(h / 255) for h in np.sum(prepros, 0)] # project vertically front = -1 is_word = False for cur, h in enumerate(hist): if not is_word and h > min_pix: front = cur is_word = True continue if is_word and h < min_pix: word = sentence[:, front:cur] words.append(word) is_word = False cv2.imwrite(os.path.join(output_path, str(index) + '.png'), word) index += 1 return words
def main(): org = cv2.imread('data/4.jpg') org = shrink(org) grey = cv2.cvtColor(org, cv2.COLOR_BGR2GRAY) binary = pre.preprocess(grey) blocks_corner = block_division(grey, show=False) block_erase(binary, blocks_corner)
def refine_text(org, corners_text, max_line_gap, min_word_length): def refine(bin): head = 0 rear = 0 gap = 0 get_word = False for i in range(bin.shape[1]): # find head if not get_word and np.sum(bin[:, i]) != 0: head = i rear = i get_word = True continue if get_word and np.sum(bin[:, i]) != 0: rear = i continue if get_word and np.sum(bin[:, i]) == 0: gap += 1 if gap > max_line_gap: if (rear - head) > min_word_length: corners_text_refine.append( (head + col_min, row_min, rear + col_min, row_max)) gap = 0 get_word = False if get_word and (rear - head) > min_word_length: corners_text_refine.append( (head + col_min, row_min, rear + col_min, row_max)) corners_text_refine = [] pad = 1 for corner in corners_text: (col_min, row_min, col_max, row_max) = corner col_min = max(col_min - pad, 0) col_max = min(col_max + pad, org.shape[1]) row_min = max(row_min - pad, 0) row_max = min(row_max + pad, org.shape[0]) clip = org[row_min:row_max, col_min:col_max] clip_bin = preprocess(clip) refine(clip_bin) return corners_text_refine
def pre_processing(input_path): # *** Step 1 *** pre-processing: gray, gradient, binary org, gray = pre.read_img(input_path, (0, 3000)) # cut out partial img binary = pre.preprocess(gray) return org, binary
import cv2 import time # initialization start = time.clock() is_detect_line = False is_merge_img = False is_shrink_img = False is_ocr = True is_segment = False is_save = True # *** Step 1 *** pre-processing: gray, gradient, binary org, gray = pre.read_img('input/5.png', (0, 600)) # cut out partial img binary = pre.preprocess(gray, 1) # *** Step 2 *** object detection: get connected areas -> get boundary -> get corners boundary_all, boundary_rec, boundary_nonrec = det.boundary_detection(binary) # get corner of boundaries corners_rec = det.get_corner(boundary_rec) corners_nonrec = det.get_corner(boundary_nonrec) # *** Step 3 *** process data: identify blocks and imgs from rectangles -> identify compos -> identify irregular imgs # identify rectangular block and rectangular img from rectangular shapes corners_block, corners_img = det.img_or_block(org, binary, corners_rec) # identify potential buttons and input bars corners_block, corners_compo = det.uicomponent_or_block(org, corners_block) # shrink images with extra borders if is_shrink_img: corners_img = det.img_shrink(org, binary, corners_img)
import cv2 import time # initialization C = Config() start = time.clock() is_detect_line = False is_merge_img = False is_shrink_img = False is_ocr = True is_segment = False is_save = True # *** Step 1 *** pre-processing: gray, gradient, binary org, gray = pre.read_img('input/5.png', (0, 600)) # cut out partial img bin = pre.preprocess(gray, 1) # *** Step 2 *** line detection: for better boundary detection if is_detect_line: line_h, line_v = det.line_detection(bin, C.THRESHOLD_LINE_MIN_LENGTH_H, C.THRESHOLD_LINE_MIN_LENGTH_V, C.THRESHOLD_LINE_THICKNESS) bin_no_line = det.rm_line(bin, [line_h, line_v]) binary = bin_no_line else: binary = bin # *** Step 3 *** object detection: get connected areas -> get boundary -> get corners boundary_all, boundary_rec, boundary_nonrec = det.boundary_detection( binary, C.THRESHOLD_OBJ_MIN_AREA,
def split_word(img, hist, min_pix): front = -1 is_word = False for cur, h in enumerate(hist): if not is_word and h > min_pix: front = cur is_word = True continue if is_word and h < min_pix: word = img[:, front:cur] is_word = False cv2.imshow('word', word) cv2.waitKey(0) cv2.destroyWindow('word') clip_paths = glob.glob('output\\1\\*.png') for clip_path in clip_paths: clip = cv2.imread(clip_path, 0) p = pre.preprocess(clip) hist = [int(h / 255) for h in np.sum(p, 0)] cv2.imshow('org', clip) split_word(clip, hist, 1) break