Pyinstaller 和 Tesseract OCR [英] Pyinstaller and Tesseract OCR

查看:252
本文介绍了Pyinstaller 和 Tesseract OCR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的程序使用 Tesseract OCR,我将使用 pyinstaller 将其转换为单个 .exe 文件.问题是,为了使 Tesseract 能够工作,我需要引用我计算机上安装的程序的路径,如下所示:pytesseract.pytesseract.tesseract_cmd = 'E:\\Tesseract-OCR\\tesseract'

I am using Tesseract OCR for my program and I am going to convert it into a single .exe file using pyinstaller. The problem is that in order for Tesseract to work, I need to reference the path to the program installed on my computer, like this: pytesseract.pytesseract.tesseract_cmd = 'E:\\Tesseract-OCR\\tesseract'

由于这不仅仅是一个可以导入的单独库,而是一个独立程序,因此我无法将其作为--add_data"参数传递给 pyinstaller.那么我如何制作一个单一文件的可执行文件?

Since this is not just a separate library that can be imported, but a standalone program, I can't pass it to pyinstaller as an '--add_data' argument. How do I make a one-file executable then?

推荐答案

假设你在 Windows 上,我遇到了这个问题,并认为我通过编译一个静态版本的 tesseract(不需要安装)来解决它并将其路径作为二进制文件包含在 pyinstaller 规范文件中.

Assuming you're on Windows, I ran into this problem and think I solved it by compiling a static version of tesseract (which does not need to be installed) and including its path as a binary in the pyinstaller spec file.

官方编译说明在这里:

https://tesseract-ocr.github.io/tessdoc/Compiling.html#windows

安装 MS Visual Studio 15(使用 C++)和 vcpkg 并通过命令提示符执行以下操作之一:

Install MS Visual Studio 15 (with c++) and vcpkg and execute one of the following through command prompt:

对于 64 位:vcpkg 安装 tesseract:x64-windows-static

for 64-bit: vcpkg install tesseract:x64-windows-static

对于 32 位:vcpkg 安装 tesseract:x86-windows-static

for 32-bit: vcpkg install tesseract:x86-windows-static

tesseract 可执行文件将位于您 PC 上 vcpkg 文件夹中的几个子文件夹中.使用该文件,您还需要下载一个 .trainneddata 文件,并将其放置在与 tesseract exe 相同的目录中名为tessdata"的文件夹中.

The tesseract executable will be located a few subfolders within the vcpkg folder on your PC. With that file, you also need to download a .trainneddata file and place it within a folder called 'tessdata' in the same directory with the tesseract exe.

创建一个 pyinstaller 规范文件并编辑 Analysis(binaries=[]) 部分以包含 tesseract 所在的文件夹路径(如果您没有使用 tesseract 的子文件夹,我认为您需要同时添加 tesseract.exe 和 tessdata 子文件夹).我也改变了 inclide_binaries=True

Create a pyinstaller spec file and edit the Analysis(binaries=[]) section to include the folder path where tesseract is located (if you're not using a subfolder for tesseract I think you'd need to add both tesseract.exe and the tessdata subfolder). I also changed inclide_binaries=True

运行 pyinstaller 并包含选项 --specpath 'yourspecfile.spec'

Run pyinstaller and include the option --specpath 'yourspecfile.spec'

我还没有尝试在不同的 PC 上尝试它,所以还没有完全测试它是否按预期工作(我对编译 C++ 一无所知,tesseract 可能需要其他文件/链接仍然完好无损,因为我只在构建 PC 上进行了测试)

I haven't yet attempted to try it on a different PC, so haven't fully tested that it works as intended (I don't know anything about compiling c++, there may be additional files/links needed for tesseract that are still intact since I've only been testing on the build PC)

这篇关于Pyinstaller 和 Tesseract OCR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆