为了账号安全,请及时绑定邮箱和手机立即绑定

我可以让 pytesseract 命令在抛出错误的 pycharm 中正常工作吗

我可以让 pytesseract 命令在抛出错误的 pycharm 中正常工作吗

暮色呼如 2021-09-14 20:56:31
我正在定义一个功能,它将图像转换为灰度(位黑白色),然后将其传递给:text = pytesseract.image_to_string(Image.open(gray_scale_image))然后我打印我收到的文本,但它抛出错误:Traceback (most recent call last):  File "C:\Users\HP\PycharmProjects\nayaproject\venv\lib\site-packages\PIL\Image.py", line 2613, in openfp.seek(0)AttributeError: 'numpy.ndarray' object has no attribute 'seek'During handling of the above exception, another exception occurred:Traceback (most recent call last):  File "C:/Users/HP/PycharmProjects/nayaproject/new.py", line 17, in <module>text = pytesseract.image_to_string(Image.open(g))  File "C:\Users\HP\PycharmProjects\nayaproject\venv\lib\site-packages\PIL\Image.py", line 2615, in openfp = io.BytesIO(fp.read())AttributeError: 'numpy.ndarray' object has no attribute 'read'而不是 Image.open(grayscale),当我使用 Image.fromarray(grayscale) 我得到这些错误:Traceback (most recent call last):  File "C:\Users\HP\PycharmProjects\nayaproject\venv\lib\site-packages\pytesseract\pytesseract.py", line 170, in run_tesseractproc = subprocess.Popen(cmd_args, **subprocess_args())  File "C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 709, in __init__restore_signals, start_new_session)  File "C:\Users\HP\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 997, in _execute_childstartupinfo)FileNotFoundError: [WinError 2] The system cannot find the file specifiedDuring handling of the above exception, another exception occurred:我正在研究 PyCharm,并且我已经为这个项目安装了 Pillow、numpy、opencv-python、pip 和 pytesseract。
查看完整描述

1 回答

?
慕侠2389804

TA贡献1719条经验 获得超6个赞

因为我猜gray_scale_image是从 OpenCV 输出的,因此是 numpy 数组,如错误所示


AttributeError: 'numpy.ndarray' object has no attribute 'read'


您需要将数组转换为 PIL 对象。根据我自己的经验,我建议您将 numpy 数组自动转换为 np.uint8,因为 PIL 使用 8 位,并且您通常不了解 OpenCV 算法的内容。


text = pytesseract.image_to_string(Image.fromarray(gray_scale_image.astype(np.uint8)))

如果上述方法不起作用,则您绝对不会传递任何形式的 Image 数组。尝试输入这些以找到争论的特征:


print(type(gray_scale_image))

print(gray_scale_image.shape)

在这将解决您的第一个问题之后,将会出现您尚不知道的新问题。您需要将路径添加到您的 pytesseract


pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

解决方案是在开头添加您的路径


pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'

TESSDATA_PREFIX = 'C:/Program Files (x86)/Tesseract-OCR'


查看完整回答
反对 回复 2021-09-14
  • 1 回答
  • 0 关注
  • 293 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号