Hos*_*mal 5 python ocr tesseract image python-tesseract
我想从这样的图像中提取文本(主要是数字)
我试过这个代码
import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
img = Image.open('1.jpg')
text = pytesseract.image_to_string(img, lang='eng')
print(text)
Run Code Online (Sandbox Code Playgroud)
但我得到的只是这个(hE PPAR)
执行 OCR 时,重要的是对图像进行预处理,以便要检测的文本为黑色,背景为白色。要做到这一点,这里有一个简单的方法,使用 OpenCV 对 Otsu 的阈值图像,这将产生一个二值图像。这是预处理后的图像:
我们使用--psm 6配置设置将图像视为统一的文本块。以下是您可以尝试的其他配置选项。Pytesseract 的结果
01153521976
代码
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image = cv2.imread('1.png', 0)
thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
data = pytesseract.image_to_string(thresh, lang='eng',config='--psm 6')
print(data)
cv2.imshow('thresh', thresh)
cv2.waitKey()
Run Code Online (Sandbox Code Playgroud)