所以我试图用 Tesseract 的
image_to_osd()
来获取桌面图像的方向。完整代码在这里:
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])
但是,每次运行代码时都会出现此错误: [Errno 2]没有这样的文件或目录:'/tmp/tess_838ogh77.osd' 临时文件的文件名每次都不一样。我检查了 pytesseract 脚本,文件实际上已创建,但显然找不到。 我在 Ubuntu 16.04 上使用 tesseract 3.04.01 这里可能有什么问题?
非常感谢。
我“修复”了通过命令行调用 tesseract 并捕获结果的问题:
# 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
我希望能帮助别人