[使用tesseract-4.0进行文本提取时如何保留图像中的所有空白?

问题描述 投票:0回答:1

我正在使用tesseract-ocr 4.0从图像中提取表格文本,并在excel中导出结果,同时保持数据对齐。

我想在提取表的图像中保留所有空格。但是OCR会跳过很多前导和尾随空格,并将其删除。

我有图像,某些地方桌子上有空白。我在tesseract中使用了[[preserve whitespaces选项,但OCR仍然跳过很多空白。

使用OCR提取时,有没有一种方法可以检测或保留表中的所有空白空间?还是有使用表中距离测量来检测空白的技术?

附加相同的图像:

enter image description here

python deep-learning ocr whitespace tesseract
1个回答
0
投票
我认为您应该将tesseract升级到版本5,并使用“ -c prepare_interword_spaces = 1”来保留空格。但是也许您必须进行后处理,因为输出结果可能不符合您的期望。

编辑

您的问题类似于this。但是由于我不能直接使用它,因此我对其进行了很少的修改。归功于igrinis。

import cv2 import pytesseract from pytesseract import Output import pandas as pd img = cv2.imread("bsShN.jpg", cv2.COLOR_BGR2GRAY) gauss = cv2.GaussianBlur(img, (3, 3), 0) custom_config = r' -l eng --oem 1 --psm 6 -c preserve_interword_spaces=1 -c tessedit_char_whitelist="0123456789- " ' d = pytesseract.image_to_data(gauss, config=custom_config, output_type=Output.DICT) df = pd.DataFrame(d) # clean up blanks df1 = df[(df.conf != '-1') & (df.text != ' ') & (df.text != '')] # sort blocks vertically sorted_blocks = df1.groupby('block_num').first().sort_values('top').index.tolist() for block in sorted_blocks: curr = df1[df1['block_num'] == block] sel = curr[curr.text.str.len() > 3] char_w = (sel.width / sel.text.str.len()).mean() prev_par, prev_line, prev_left = 0, 0, 0 text = '' for ix, ln in curr.iterrows(): # add new line when necessary if prev_par != ln['par_num']: text += '\n' prev_par = ln['par_num'] prev_line = ln['line_num'] prev_left = 0 elif prev_line != ln['line_num']: text += '\n' prev_line = ln['line_num'] prev_left = 0 added = 0 # num of spaces that should be added if ln['left'] / char_w > prev_left + 1: added = int((ln['left']) / char_w) - prev_left text += ' ' * added text += ln['text'] + ' ' prev_left += len(ln['text']) + added + 1 text += '\n' print(text)

这里是输出。并非所有数字都能正确识别。在某些地方也必须固定空间。

56 0 232 35 197 19363 0 3 22 10 12 1586 60 200 165 0 165 11626 44 345 69 50 610 75 54 7593 52 789 191 480 96 618 149 6324 84 71 34 50 8610 20 74 4837 77 680- 131 61 1 71 3000 11 6 103 0 103 9932 2 52 29 3 26 4451 12 65 23 4 19 1626 24 62 100 10 -1 90 6621 497 897 63 360 292 100 0 31 3056 863 1285 331 50 197 50 0 134 17037 0 5 24 2 22 3159 15 131 144 47 97 15070 44 61 86 44 4 112 22 1320 10 90 85 50 135 0 0 3 8 54 11 -9 43 2334

© www.soinside.com 2019 - 2024. All rights reserved.