conda环境下TesseractNotFoundError: tesseract is not installed or it‘s not in your PATH问题解决

1 问题描述

使用tesseract实现图片的文字识别,

import pytesseract
from PIL import Image

image_path = '../data/ocr_englist.jpg'
result = pytesseract.image_to_string(Image.open(image_path), lang='eng')
print(result)

运行程序报如下错误:

Traceback (most recent call last):
  File "C:\Users\lishu\anaconda3\envs\pt2\lib\site-packages\pytesseract\pytesseract.py", line 362, in get_languages
    result = subprocess.run(
  File "C:\Users\lishu\anaconda3\envs\pt2\lib\subprocess.py", line 505, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Users\lishu\anaconda3\envs\pt2\lib\subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\lishu\anaconda3\envs\pt2\lib\subprocess.py", line 1420, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\code\ptcontainer\ocr\tesseract_test.py", line 6, in 
    print(pytesseract.get_languages(config=''))
  File "C:\Users\lishu\anaconda3\envs\pt2\lib\site-packages\pytesseract\pytesseract.py", line 148, in wrapper
    wrapper._result = func(*args, **kwargs)
  File "C:\Users\lishu\anaconda3\envs\pt2\lib\site-packages\pytesseract\pytesseract.py", line 368, in get_languages
    raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.

2 问题分析

从错误描述中分析可知,在PATH路径上找不到tesseract

tesseract is not installed or it's not in your PATH

3 问题解决

3.1 确认tesseract是否正确安装

正确安装的显示如下:

conda环境下TesseractNotFoundError: tesseract is not installed or it‘s not in your PATH问题解决_第1张图片

如果显示找不到命令,需要执行安装操作,

安装方法参见文章:https://blog.csdn.net/lsb2002/article/details/134429406

3.2 设置python包源码中的变量

根据本地conda环境路径,找到pytesseract.py的源码

C:\Users\lishu\anaconda3\envs\pt2\Lib\site-packages\pytesseract\pytesseract.py
tesseract_cmd = 'tesseract'
修改为:
tesseract_cmd = 'D:\Tesseract-OCR\\tesseract'

 注:本地tesseract程序包的安装路径在D:\Tesseract-OCR

再次运行程序,问题解决。

你可能感兴趣的:(AI运行环境,tesseract,ocr,光学字符识别,conda)