Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an error when using the image_to_osd method with pytesseract

Here's my code:

import pytesseract
import cv2
from PIL import Image

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"


def main():
    original = cv2.imread('D_Testing.png', 0)
    # binary thresh it at value 100. It is now a black and white image
    ret, original = cv2.threshold(original, 100, 255, cv2.THRESH_BINARY)
    text = pytesseract.image_to_string(original, config='--psm 10')
    print(text)
    print(pytesseract.image_to_osd(Image.open('D_Testing.png')))


if __name__ == "__main__":
    main()

For the first out put I get what I need which is the letter D

D

Which is intended, but when it tries to do the second print statement it spits out this.

Traceback (most recent call last):
  File "C:/Users/Me/Documents/Python/OpenCV/OpenCV_WokringTest/PytesseractAttempt.py", line 18, in <module>
    main()
  File "C:/Users/Me/Documents/Python/OpenCV/OpenCV_WokringTest/PytesseractAttempt.py", line 14, in main
    print(pytesseract.image_to_osd(Image.open('D_Testing.png')))
  File "C:\Users\Me\Documents\Python\OpenCV\OpenCV_WokringTest\venv\lib\site-packages\pytesseract\pytesseract.py", line 402, in image_to_osd
    }[output_type]()
  File "C:\Users\Me\Documents\Python\OpenCV\OpenCV_WokringTest\venv\lib\site-packages\pytesseract\pytesseract.py", line 401, in <lambda>
    Output.STRING: lambda: run_and_get_output(*args),
  File "C:\Users\Me\Documents\Python\OpenCV\OpenCV_WokringTest\venv\lib\site-packages\pytesseract\pytesseract.py", line 218, in run_and_get_output
    run_tesseract(**kwargs)
  File "C:\Users\Me\Documents\Python\OpenCV\OpenCV_WokringTest\venv\lib\site-packages\pytesseract\pytesseract.py", line 194, in run_tesseract
    raise TesseractError(status_code, get_errors(error_string))
pytesseract.pytesseract.TesseractError: (1, 'Tesseract Open Source OCR Engine v4.0.0.20181030 with Leptonica Warning: Invalid resolution 0 dpi. Using 70 instead. Too few characters. Skipping this page Warning. Invalid resolution 0 dpi. Using 70 instead. Too few characters. Skipping this page Error during processing.'). 

I am not sure what to do. I can't really find too much about this error online. Also I am not sure what to do. The goal is simply to get it spit out the orientation of my letter. Thank you for all helpful comment in advance!

like image 609
Bob Stoops Avatar asked Jan 04 '19 22:01

Bob Stoops


1 Answers

Faced this problem and tried different approaches and finally solved it!!

Just pass the image location directly rather than through Pillow or OpenCV as mentioned by @Esraa Abdelmaksoud

text = pytesseract.image_to_osd(r'Report 2.jpeg')
like image 146
Aditya Mahimkar Avatar answered Sep 20 '22 06:09

Aditya Mahimkar