【问题标题】:Improve OCR results custom改进 OCR 结果自定义
【发布时间】:2021-03-26 16:58:38
【问题描述】:

我在使用 tesseract 引擎从图像中提取文本时遇到一些问题,谁能给我一些提高准确性的提示,因为这些信息应该至少 99% 准确,下面是使用的代码。

Sample of images here

image = cv2.imread(imgfile)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Remove horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50,1))
detect_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detect_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(thresh, [c], -1, (0,0,0), 2)

# Remove vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,15))
detect_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detect_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(thresh, [c], -1, (0,0,0), 3)

# Dilate to connect text and remove dots
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,1))
dilate = cv2.dilate(thresh, kernel, iterations=2)
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Bitwise-and to reconstruct image
result = cv2.bitwise_and(image, image, mask=dilate)
result[dilate==0] = (255,255,255)

# OCR
data = pytesseract.image_to_string(result, lang='eng',config='--psm 6 tessedit_char_whitelist="0123456789%."')
print(data)

cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.imshow('dilate', dilate)
cv2.waitKey()

提前致谢。

【问题讨论】:

  • 嗨@HamzehAbuAjamieh - 请更新您的答案以提供stackoverflow.com/help/minimal-reproducible-example - 目前尚不清楚您的问题是什么 - 什么不起作用,您尝试了什么/失败了?
  • 除上述评论外,请在问题中嵌入图片。还有,图中的数字代表什么?
  • Hi@ranka47,我是 ocr 的新手,我不知道问题到底出在哪里,并附上了图像样本。

标签: python ocr tesseract python-tesseract


【解决方案1】:

你读过Improving the quality of the output吗?

如果您对图像进行两次上采样

然后是 OCR:

24,026
56.5%
5,798
13.6%
4,236
10.0%
3,546
8.3%
4,905
11.5%
42,511
100.0%

20,968
66.0%
9,279
29.2%
1,328
4.2%
145
0.5%
47
0.1%
31,767
100.0%

5,854
77.8%

1,617
21.5%

45
0.6%

0.1%
7,523
100.0%

50,848
62.2%
16,694
20.4%

5,564

3,736
4.6%
4,959
6.1%

81,801

100.0%

24,826
78.1%
5,866
18.5%
11
0.0%

1.6%
562
1.8%
31,773
100.0%

26,499
81.1%
5,940
18.2%
55
0.2%
167
0.5%

32,661
100.0%

6,008
87.2%

839
12.2%

0.6%

0.0%

6,886
100.0%

57,333
80.4%
12,645
17.7%

0.1%
713
1.0%

0.8%

71,320
100.0%

代码:

import cv2
import pytesseract

# Load the image
img = cv2.imread("FD2sX.png")

# Up-sample
img = cv2.resize(img, (0, 0), fx=2, fy=2)

# Convert to the gray-scale
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# OCR
print(pytesseract.image_to_string(gry))

如果你的 pytesseract 版本不是0.3.7,你可能会得到不同的结果。

【讨论】:

  • 嗨@Ahx,仍然面临一些问题,它跳过了一些数字,将通过可能的方式提高准确性,谢谢。
  • 很高兴能帮上忙
猜你喜欢
  • 2015-03-07
  • 1970-01-01
  • 2019-01-28
  • 1970-01-01
  • 1970-01-01
  • 2015-02-24
  • 1970-01-01
  • 2017-09-22
  • 1970-01-01
相关资源
最近更新 更多