如何解决pytesseract的OCR
我正在尝试使用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,可以从中进一步了解here(-psm和--oem部分)
您可以按照pytesseract documentation中的指定,指定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。我建议将width和height参数保留为0,并提供fx和fy比例因子
import cv2
import pytesseract
img = cv2.imread('original.png',cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img,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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。