Tesseract OSD can't find files in /tmp
Asked Answered
P

1

0

So I'm trying to get the orientation of a table image with Tesseract's image_to_osd(). Full code here:

import cv2
from PIL import Image
import pytesseract
from skimage import io
from skimage.transform import rotate
import os

pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
# pytesseract.pytesseract.osd_filename = r'/usr/share/tesseract-ocr/tessdata/osd.traineddata'

image_path = "my_image.jpg"
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


text = pytesseract.image_to_osd(image,config="osd --psm 0",output_type=pytesseract.Output.DICT)
orientation = int(text.split('Rotate: ')[-1])

However, every time I run the code I get this error: [Errno 2] No such file or directory: '/tmp/tess_838ogh77.osd' The file name of the temporary file is different every time. I checked in the pytesseract script and the file is actually created but apparently it can't be found. I'm using tesseract 3.04.01 on Ubuntu 16.04 What could be the issue here?

Thanks a lot.

Pattern answered 13/11, 2023 at 21:23 Comment(2)
#54047616Escapee
I saw this post and tried out the suggested solutions. Nothing works unfortunately.Pattern
W
0

I "fix" the problem calling tesseract by command line, and capturing the result:

    # Construct the Tesseract command
    command = f'tesseract {image_path} stdout -psm 0'
    
    # Execute the command
    result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    
    # Check for errors
    if result.returncode != 0:
        print(f"Error processing {image_path}: {result.stderr.strip()}")
        return None
    
    # Parse the output to extract the orientation angle
    for line in result.stderr.splitlines():
        if 'Orientation in degrees' in line:
            angle = int(line.split(':')[-1].strip())
            return angle
    
    print(f"Failed to detect angle for {image_path}")
    return None

I hope helps someone

Wye answered 14/8 at 12:4 Comment(1)
You will need to import the subprocess routines: import subprocessWye

© 2022 - 2024 — McMap. All rights reserved.