You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Tesseract image_to_osd()报错:找不到/tmp临时OSD文件

问题解决:Tesseract image_to_osd() 报错找不到临时OSD文件

可能原因

  • Config参数错误:Tesseract 3.x版本中,image_to_osd无需在config里额外指定osd,多余参数会导致Tesseract生成临时文件时出现路径异常。
  • OSD训练数据缺失:系统中未安装osd.traineddata时,Tesseract无法完成方向检测,可能触发临时文件相关报错。
  • 版本兼容性问题:新版pytesseract对Tesseract 3.x的支持有限,存在临时文件处理逻辑的不兼容。
  • 代码逻辑错误:指定output_type=DICT却尝试对返回的字典执行字符串split操作,这本身也是错误(当前报错优先解决文件问题)。

解决方案

1. 确认OSD训练数据存在

执行以下命令安装OSD训练数据(Ubuntu 16.04):

sudo apt-get install tesseract-ocr-osd

安装完成后,确认路径/usr/share/tesseract-ocr/tessdata/osd.traineddata存在。

2. 修正代码参数与逻辑

去掉config中的多余osd参数,并正确处理字典类型的返回结果:

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
# 若OSD训练数据路径异常,可放开下面这行指定路径
# pytesseract.pytesseract.osd_filename = '/usr/share/tesseract-ocr/tessdata/osd.traineddata'

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

# 修正config参数,仅保留--psm 0
osd_result = pytesseract.image_to_osd(image, config="--psm 0", output_type=pytesseract.Output.DICT)
# 直接从字典中提取旋转角度
orientation = osd_result['rotate']
print(f"图片旋转角度: {orientation}")

3. 适配pytesseract版本

若上述步骤仍报错,安装适配Tesseract 3.x的旧版pytesseract:

pip uninstall pytesseract -y
pip install pytesseract==0.3.9

4. 检查临时目录权限

确保/tmp目录权限正常,执行:

ls -ld /tmp

若输出不是drwxrwxrwt,执行以下命令修复:

sudo chmod 1777 /tmp

额外建议

Tesseract 3.04版本较老旧,OSD功能稳定性不足,若条件允许,建议升级到Tesseract 4.x或5.x版本,可大幅减少兼容性问题。

内容的提问来源于stack exchange,提问作者Sonya Voneva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 10:10:23