【问题标题】:Key error: "PNG" while using pytesseract python关键错误:使用 pytesseract python 时出现“PNG”
【发布时间】:2022-09-22 23:39:03
【问题描述】:

即使我关注了问题KeyError: \'PNG\' while using pytesseract.image_to_data,我也无法解决问题。

  1. 我在 google colab 中安装了 tesseract。

    !sudo apt-get install tesseract-ocr
    
  2. 然后安装 pytesseract=0.3.9

    !pip install pytesseract==0.3.9
    
  3. 这是我的代码:

     import cv2
     from pytesseract import Output
     from PIL import Image
     import pytesseract
    
     image = cv2.imread(\'0003.jpg\')
     rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
     results = pytesseract.image_to_data(rgb, output_type=Output.DICT)
    

    但我得到了错误:

    ---------------------------------------------------------------------------
    KeyError                                  Traceback (most recent call last)
    <ipython-input-25-21a4d6775e8a> in <module>
          9 image = cv2.imread(\'/content/sample_data/0003.jpg\')
         10 rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    ---> 11 results = pytesseract.image_to_data(rgb, output_type=Output.DICT)
         12 for i in range(0, len(results[\"text\"])):
         13     x = results[\"left\"][i]
    
    5 frames
    /usr/local/lib/python3.7/dist-packages/PIL/Image.py in save(self, fp, format, **params)
       2121         expand=0,
       2122         center=None,
    -> 2123         translate=None,
       2124         fillcolor=None,
       2125     ):
    
    KeyError: \'PNG\'
    
  • 你好! 0003.jpg 是 colab 工作区中的有效文件吗?
  • 是的,它有效。我还在 kaggle 中编译了相同的代码。它在那里工作!

标签: python python-tesseract


【解决方案1】:

我在我的系统(google colab)上复制了您的情况,并且效果很好。

除此之外,您的代码非常好。完全没有错误。

我认为是版本问题。你试试这个版本的cv2

cv2 version: 4.6.0

确保您获取的图像应该是 image.jpg

或者

您可以上传图像并使用图像路径而不是图像名称。

 import cv2
 from pytesseract import Output
 from PIL import Image
 import pytesseract

 image = cv2.imread('/content/0001.jpg') # here i use path instead of image name
 rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 results = pytesseract.image_to_data(rgb, output_type=Output.DICT)
 print(results.keys())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-14
    • 2021-12-14
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多