在使用OCR从图像中提取文本的过程中,python中的子进程库出现问题



我一直在使用python OCR代码从图像中提取文本,但我遇到了一些错误。我认为错误在于子流程库,但它是内置在库中的。所以我无法准确地找出错误。有谁能帮我解决这个错误吗。我的代码和错误如下。

OCR代码

import os
import tempfile
import subprocess
def ocr(path):
temp = tempfile.NamedTemporaryFile(delete=False)
process = subprocess.Popen(['tesseract', path, temp.name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
process.communicate()
with open(temp.name + '.txt', 'r') as handle:
contents = handle.read()
os.remove(temp.name + '.txt')
os.remove(temp.name)
return contents
str = ocr("C:\Users\hp\Desktop\MS Thesis\opencv-text-detection\opencv-text-detection\images\sign.jpg")
print(str)

通过执行上面的代码,我得到了以下错误

[7] :运行文件('C:/Users/hp/Desktop/MS论文/python文本检测.py',wdir='C:/Users/hp/Destop/MS论文'(追踪(最近一次通话(:

文件",第1行,在runfile('C:/Users/hp/Desktop/MS论文/python文本检测.py',wdir='C:/Users/hp/Deskop/MS论文'(

运行文件中的文件"C:\Users\hp\Anaconda3\lib\site packages\spyder_kernels\customize\spyrcustimize.py",第704行execfile(文件名,命名空间(

execfile中的文件"C:\Users\hp\Anaconda3\lib\site packages\spyder_kernels\customize\spyercustimize.py",第108行exec(compile(f.read((,filename,'exec'(,namespace(

文件"C:\Users/hp/Desktop/MS论文/python文本检测.py",第26行,在str=ocr("C:\Users\hp\Desktop\MS Thesis \opencv text detection\opencv text-deection\images\sign.jpg"(

文件"C:\Users/hp/Desktop/MS论文/python文本检测.py",第15行,ocrprocess=子流程。Popen(['testract',path,temp.name],stdout=subprocess.PIPE,stderr=subprocess.stdout(

initsuper(SubprocessPopen,self(中的文件"C:\Users\hp\Anaconda3\lib\site-packages\spyder_kernels\customize\spyercustomize.py",第171行初始化(*args,**kwargs(

文件"C:\Users\hp\Anaconda3\lib\subprocess.py",第769行,在initrestore_signals,start_new_session(中

文件"C:\Users\hp\Anaconda3\lib\subprocess.py",第1172行,位于_execute_child中startupinfo(

FileNotFoundError:[WinError 2]系统找不到指定的文件

我一直在使用Python 3.7.1和窗口7 上的anaconda

由于找不到文件,看起来有一个错误如果文件在同一目录中,我建议您使用:

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.path.join(BASE_DIR, 'file_dir')

最新更新