有任何问题欢迎在下面留言
本篇文章的代码运行界面均在Pycharm中进行
本篇文章配套的代码资源已经上传
上篇内容:
openCV实战-系列教程11:文档扫描OCR识别上(图像轮廓/模版匹配)项目实战、源码解读
中篇内容:
openCV实战-系列教程13:文档扫描OCR识别中(图像轮廓/模版匹配)项目实战、源码解读
上篇内容中,我们是安装了,我们需要在代码中实现这个效果,那么就需要在命令行中安装pytesseract,
pip install pytesseract
没有任何要求
preprocess = 'blur' #thresh
image = cv2.imread('scan.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
if preprocess == "thresh":gray = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
if preprocess == "blur":gray = dianBlur(gray, 3)
这段代码包含两个if语句,但是只会执行一个
是判断选择二值处理还是滤波处理,然后执行语句分别对应的是各自的操作
filename = "{}.png".pid())
cv2.imwrite(filename, gray)
text = pytesseract.image_to_string(Image.open(filename))
print(text)
os.remove(filename)
cv2.imshow("Image", image)
cv2.imshow("Output", gray)
cv2.waitKey(0)
打印结果:
x x ee eR OK OK Oe KR OK KK
WHOLE FOODS MARKET - WESTPORT, CT 06880 399 POST RD WEST - (203)
227-68586b $65 365
365
F
BACUN LS BACON LS BACON LS BACUN LS BROTH CHIL LOUR ALMUND
CHKN BRST BNLSS SK
HEAVY CREAM
BALSMC REDUCT
BEEF
GRND 85/15
JUICE COF CASHEW -. DOCS PINT ORGAWEL HNY ALMOND BUTTER
eunene TAX
.00 BAL
NP hiP NP NP NP NP NP NP
nm
C:Users18333anaconda3envs A:2_gupaoocrtest.py
Traceback (most recent call last):
File" C:Users18333anaconda3envspytorchlibsite-packagespytesseractpytesseract.py", line 255, in run_tesseract
proc = subprocess.Popen(cmd_args, **subprocess_args())
File “ C:Users18333anaconda3envspytorchlibsubprocess.py”, line 858, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File “C:Users18333anaconda3envspytorchlibsubprocess.py”, line 1327, 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 “A:2_gupaoocrtest.py”, line 27, in module
text = pytesseract.image_to_string(Image.open(filename))
File “C:Users18333anaconda3envspytorchlibsite-packagespytesseractpytesseract.py”, line 423, in image_to_string
return {
File “C:Users18333anaconda3envspytorchlibsite-packagespytesseractpytesseract.py”, line 426, in lambda
Output.STRING: lambda: run_and_get_output(*args),
File “C:Users18333anaconda3envspytorchlibsite-packagespytesseractpytesseract.py”, line 288, in run_and_get_output
run_tesseract(**kwargs)
File “C:Users18333anaconda3envspytorchlibsite-packagespytesseractpytesseract.py”, line 260, in run_tesseract
raise TesseractNotFoundError()
pytesseract. pytesseract.TesseractNotFoundError: tesseract is not installed or it’s not in your PATH. See README file for more information.
进程已结束,退出代码为 1
运行的时候出现这个bug,打开以下文件目录:
你的anaconda安装目录anaconda3envspytorchLibsite-packagespytesseract
注意这里,第一每个人的anaconda安装目录不一样,第二,每个人的python环境也不一样,这里
anaconda3envspytorchLibsite-packagespytesseract
如果实在base环境中就是直接是
anaconda3Libsite-packagespytesseract
我的是安装在了一个名为pytorch的python环境中
打开pytesseract.py文件,将这行代码:
tesseract_cmd = 'tesseract'
改为:
tesseract_cmd = 'C:/Program Files/'
你直接复制过来的地址是这个斜杠,应该改成这个/
这里每个人的tesseract的安装地址也不同,解决完这些应该就能运行了😇😇😇
如果还是不能运行,有可能是你的环境变量出错了,检查一下,一共有四处
如果还是不能运行!!!!!
建议更换<的版本,下载地址,推荐选择5.0.1版本,之前干的再干一遍。
还是不行的话😭😭😭
来加v找我给你debug:yzy-416
上篇内容:
openCV实战-系列教程11:文档扫描OCR识别上(图像轮廓/模版匹配)项目实战、源码解读
中篇内容:
openCV实战-系列教程13:文档扫描OCR识别中(图像轮廓/模版匹配)项目实战、源码解读
本文发布于:2024-01-28 09:00:02,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17064036076283.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |