我想从一张图片中提取文本,使用的是
图片: 代码:
Python
语言,为此我选择了pytesseract
。当我尝试从图像中提取文本时,结果并不理想。我还查阅了这篇文章并实现了其中列出的所有技术,但似乎效果并不好。图片: 代码:
import pytesseract
import cv2
import numpy as np
img = cv2.imread('D:\\wordsimg.png')
img = cv2.resize(img, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
kernel = np.ones((1,1), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)
img = cv2.threshold(cv2.medianBlur(img, 3), 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
txt = pytesseract.image_to_string(img ,lang = 'eng')
txt = txt[:-1]
txt = txt.replace('\n',' ')
print(txt)
输出:
t hose he large form might light another us should took mountai house n story important went own own thought girl over family look some much ask the under why miss point make mile grow do own school was
就算有一个不必要的空格,对我来说也可能代价高昂。我希望结果百分之百准确。非常感谢您的帮助。
config = '--oem 3 --psm %d' % psm
是什么意思? - Sushil--oem 3
运行默认设置。如果您使用--help
运行 Tesseract 可执行文件,则可以查看所有选项的完整列表。此页面也非常有帮助:https://github.com/tesseract-ocr/tesseract/blob/main/doc/tesseract.1.asc#config-files-and-augmenting-with-user-data。 - Edward Spencer