AttributeError: ‘dict’ object has no attribute ‘mode’ when using Tsseract OCR

Issue

This Content is from Stack Overflow. Question asked by XYJ

Using Google Colab, I wrote the following code. I utilized Python. I intended to import an image file from my computer and then use Tesseract OCR to recignize the characters in the image.

After running the following code on colab, the error message: AttributeError: ‘dict’ object has no attribute ‘mode’
appeared.
Although there are same cases on the Internet with the error message of “‘dict’ object has no attribute ‘~'”, but I couldn’t find any case that was regarding the attribute “mode.”

Could you please advise me how to improve my code?

The code I wrote

!apt install tesseract-ocr libtesseract-dev tesseract-ocr-jpn
!pip install pyocr

from PIL import Image
import pyocr
import cv2
from google.colab.patches import cv2_imshow

from google.colab import files
uploaded=files.upload()

tools=pyocr.get_available_tools()
print(tools)
tool=tools[0]
print(tool.get_name())

txt1 = tool.image_to_string(
uploaded,
lang='jpn+eng',
builder=pyocr.builders.TextBuilder(tesseract_layout=6)
)

The error message showed by colab

AttributeError                            Traceback (most recent call last)
<ipython-input-38-2ad0a8585335> in <module>
      2     uploaded,
      3     lang='jpn+eng',
----> 4     builder=pyocr.builders.TextBuilder(tesseract_layout=6)
      5     )
      6 help(dict.items)

/usr/local/lib/python3.7/dist-packages/pyocr/tesseract.py in image_to_string(image, lang, builder)
    362         builder = builders.TextBuilder()
    363     with tempfile.TemporaryDirectory() as tmpdir:
--> 364         if image.mode != "RGB":
    365             image = image.convert("RGB")
    366         image.save(os.path.join(tmpdir, "input.bmp"))
AttributeError: 'dict' object has no attribute 'mode'



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?