tesseract安装+环境配置 windows实测

本文详细介绍了Tesseract OCR的安装步骤,包括下载安装包、设置环境变量、检查安装是否成功以及语言库的下载方法。关键在于环境变量的正确配置,确保绝对路径不包含中文字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下载tesseract安装包:
一路next,中间有一个安装语言库的可勾选下载
最关键的是环境变量的设置:

  1. 绝对路径不能带有中文,不然报错的,所以文件啥的命名不要带有中文
  2. 设置环境变量,不仅仅要在path中添加Tesseract-ORC文件夹的绝对路径
  3. 划重点!!!还要单独新建一个环境变量,最好两个都新建,用户的环境变量和系统环境变量
    名字:TESSDATA_PREFIX
    路径是:Tesseract-ORC下的tessData文件夹

检查是否安装成功:

  1. cmd界面下:tesseract -v # 查看版本命令
  2. 查看语言库,tesseract --list-langs # 如果报错,请重新启动cmd,或者检查上面第三点是否做好了
  3. 语言库下载,官方在github可以下载
    https://github.com/tesseract-ocr/tessdata_best/tree/master/script
<think>我们使用PIL打开图片,然后使用pytesseract进行识别。对于中文识别,需要指定语言参数为&#39;chi_sim&#39;(简体中文)。同时,确保已经安装了中文语言包。 步骤: 1. 安装必要的库:Pillow(PIL)和pytesseract。 2. 安装Tesseract-OCR引擎,并下载中文语言包。 3. 在代码中,使用PIL的Image.open打开图片文件。 4. 使用pytesseract.image_to_string函数,传入图片对象和语言参数。 注意:如果识别效果不好,可以尝试对图片进行预处理(如二值化、降噪等)以提高识别准确率。 示例代码: ```python from PIL import Image import pytesseract # 指定tesseract.exe的路径(如果环境变量中已添加,则不需要这行) # pytesseract.pytesseract.tesseract_cmd = r&#39;<full_path_to_tesseract_executable>&#39; # 打开图片 image = Image.open(&#39;your_image.jpg&#39;) # 替换为你的图片路径 # 识别中文文本 text = pytesseract.image_to_string(image, lang=&#39;chi_sim&#39;) print(text) ``` 如果识别效果不佳,可以尝试对图像进行预处理。例如,将图像转换为灰度图,然后二值化: ```python image = Image.open(&#39;your_image.jpg&#39;) # 转换为灰度图 image = image.convert(&#39;L&#39;) # 二值化,阈值可以根据情况调整 threshold = 140 image = image.point(lambda p: p > threshold and 255) # 然后进行识别 text = pytesseract.image_to_string(image, lang=&#39;chi_sim&#39;) print(text) ``` 另外,pytesseract还支持配置参数,例如设置识别模式(psm)。例如,设置psm为3(全自动识别,但无方向检测): ```python text = pytesseract.image_to_string(image, lang=&#39;chi_sim&#39;, config=&#39;--psm 3&#39;) ``` 关于psm参数: 0 = Orientation and script detection (OSD) only. 1 = Automatic page segmentation with OSD. 2 = Automatic page segmentation, but no OSD, or OCR. 3 = Fully automatic page segmentation, but no OSD. (Default) 4 = Assume a single column of text of variable sizes. 5 = Assume a single uniform block of vertically aligned text. 6 = Assume a single uniform block of text. 7 = Treat the image as a single text line. 8 = Treat the image as a single word. 9 = Treat the image as a single word in a circle. 10 = Treat the image as a single character. 根据图片情况选择合适的psm模式。 如果遇到问题,请检查: 1. Tesseract-OCR是否正确安装,并且路径已添加到环境变量(或者代码中指定了路径)。 2. 中文语言包是否已安装(在Tesseract-OCR的安装目录下的tessdata文件夹中应有chi_sim.traineddata文件)。 3. 图片路径是否正确。 参考文献: [^1]: 介绍了使用PIL和pytesseract识别验证码,但指出复杂验证码需要进一步处理。 [^2]: 提到了安装PIL和pytesseract,以及Tesseract-OCR引擎。 [^3]: 给出了一个使用pytesseract识别中文文本的示例。 [^4]: 展示了识别验证码图片的代码,并指定了psm参数。 [^5]: 提供了简单的代码示例,用于从图片中提取中文文字。 根据以上信息,我们编写了上述代码示例。 §§相关问题§§ 1. 如何提高pytesseract识别中文的准确率? 2. 除了pytesseract,还有哪些OCR库可以识别中文? 3. 如何对图片进行预处理以优化OCR识别效果? 4. Tesseract-OCR如何安装中文语言包? 5. 在哪些情况下需要设置pytesseract的psm参数?如何选择?</think>### 使用PIL和pytesseract识别中文文本的完整指南 #### 核心步骤: 1. **环境准备**: ```bash pip install Pillow pytesseract ``` 2. **安装OCR引擎**: - 下载[Tesseract-OCR](https://github.com/tesseract-ocr/tesseract) - 安装中文语言包:`chi_sim`(简体中文)和/或`chi_tra`(繁体中文) 3. **基础识别代码**: ```python from PIL import Image import pytesseract # 设置Tesseract路径(如果已添加环境变量可省略) # pytesseract.pytesseract.tesseract_cmd = r&#39;C:\Program Files\Tesseract-OCR\tesseract.exe&#39; # Windows示例 # 打开图片并识别中文 image = Image.open(&#39;chinese_text.png&#39;) text = pytesseract.image_to_string( image, lang=&#39;chi_sim&#39;, # 简体中文 config=&#39;--psm 3&#39; # 页面分割模式 ) print("识别结果:", text) ``` #### 提高准确率的技巧: 1. **图像预处理**: ```python # 转换为灰度图 + 二值化 image = Image.open(&#39;chinese_text.png&#39;).convert(&#39;L&#39;) threshold = 150 image = image.point(lambda p: p > threshold and 255) ``` 2. **参数优化组合**: ```python text = pytesseract.image_to_string( image, lang=&#39;chi_sim+eng&#39;, # 中英混合识别 config=&#39;--psm 6 -c preserve_interword_spaces=1&#39; ) ``` 3. **区域识别**(针对特定区域): ```python # 定义识别区域 (left, upper, right, lower) roi = (100, 200, 400, 300) cropped_img = image.crop(roi) text = pytesseract.image_to_string(cropped_img, lang=&#39;chi_sim&#39;) ``` #### 常见问题解决: - **乱码问题**:检查是否安装了正确的中文语言包 - **识别率低**: - 尝试不同的`psm`模式(3-6常用于多行文本) - 增加图像分辨率(建议300dpi以上) - 使用`config=&#39;--oem 3&#39;`启用LSTM引擎 - **中英混合识别**:`lang=&#39;chi_sim+eng&#39;` > 实测建议:对于复杂背景图片,推荐先用OpenCV进行降噪处理再识别[^5]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值