繁体   English   中英

使用 pytesseract 进行 OCR

[英]OCR with pytesseract

我正在尝试使用 OCR 来读取给定图片中的数字。

这是我的原图: https://prnt.sc/u42wn3

我试图改变这两个选项的图片:

我正在运行下一个代码:

img = Image.open(r"C:\Users\peleg\Desktop\screenshots\hp_black.png")  # read the black on white picture
new_img = img.resize((80 * 2, 15 * 3)) # resizing the picture
data = pytesseract.image_to_string(new_img,config='-c tessedit_char_whitelist=0123456789/') # exporting text from image
print(data)

我尝试了多种尺寸,但仍然没有收到任何东西。

您可能想检查您是否有合适的 OEM 和 PSM,您可以在此处了解更多信息(--psm 和 --oem 部分)

您可以指定pytesseract 文档中指定的 oem 或 psm 模式,如下所示:

添加任何附加选项的示例。

custom_oem_psm_config = r'--oem 3 --psm 6'
pytesseract.image_to_string(image, config=custom_oem_psm_config)

您的第二张图片应该可以工作。 但我@achraf 可能是对的:你应该提供一个 psm。 即使是最基本的 psm (6) 似乎也适用于这个图像。 您的缩放功能已关闭。 原件是 237x86。 你将它缩放到 160x45。 我建议将宽度和高度参数保留为 0 并提供 fx 和 fy 比例因子

import cv2
import pytesseract

img = cv2.imread('original.png', cv2.IMREAD_GRAYSCALE)  

thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1]
thresh = cv2.resize(thresh, (0,0), fx=2, fy=2)  # scale image 2X

detected_text = pytesseract.image_to_string(thresh)
print(detected_text)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM