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

安装chi_sim语言包后,pytesseract无输出结果的问题求助

问题描述

已为chi_sim安装语言支持,/usr/share/tesseract-ocr/5/tessdata目录下的文件列表:

ls /usr/share/tesseract-ocr/5/tessdata
chi_sim.traineddata  eng.traineddata  pdf.ttf
configs          osd.traineddata  tessconfigs

运行以下测试代码后无任何输出:

import cv2
from PIL import Image
import pytesseract
from pyocr import tesseract
image_path = 'photo.jpeg'
image = cv2.imread(image_path)
image = Image.fromarray(image)
text = pytesseract.image_to_string(image, lang='chi_sim')
print(text)

执行print(pytesseract.get_languages(config=''))返回['chi_sim', 'eng', 'osd'],询问无输出的原因。


可能的原因及解决方法

1. 图像颜色空间不匹配

OpenCV默认以BGR格式读取图像,而PIL使用RGB格式,直接转换会导致颜色信息错乱,干扰Tesseract识别。
修复代码:

import cv2
from PIL import Image
import pytesseract

image_path = 'photo.jpeg'
image = cv2.imread(image_path)
# 将BGR格式转为RGB
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = Image.fromarray(image_rgb)
text = pytesseract.image_to_string(image, lang='chi_sim')
print(text)

2. 图像质量未达识别要求

Tesseract对图像清晰度、对比度要求较高,若图像存在模糊、噪声、倾斜等问题,会导致无法识别文字。可添加预处理步骤:

import cv2
from PIL import Image
import pytesseract

image_path = 'photo.jpeg'
image = cv2.imread(image_path)
# 转为灰度图
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 二值化增强对比度
_, image_binary = cv2.threshold(image_gray, 127, 255, cv2.THRESH_BINARY)
# 去除噪声
image_clean = cv2.medianBlur(image_binary, 3)
# 转为PIL图像
image = Image.fromarray(image_clean)
text = pytesseract.image_to_string(image, lang='chi_sim')
print(text)

3. Tesseract配置参数不合理

默认配置可能不适配中文文本的排版,可指定页面分割模式(PSM)优化识别:

text = pytesseract.image_to_string(image, lang='chi_sim', config='--psm 6')

PSM 6表示假设图像为单一均匀文本块,适合多数常规场景。

4. 训练数据版本不兼容

确认chi_sim.traineddata与Tesseract 5.x版本兼容,可重新下载官方最新训练数据替换:

wget https://github.com/tesseract-ocr/tessdata/raw/main/chi_sim.traineddata -O /usr/share/tesseract-ocr/5/tessdata/chi_sim.traineddata

5. 测试图像无有效中文文本

需确认photo.jpeg中确实包含清晰、无遮挡的中文文本,排除图像本身无识别内容的情况。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 15:04:51