Skip to content

📄 Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.

添加脚本, 使用pyinstaller打包为exe

AutumnSun1996a4f9cf12c885
diff --git a/.gitignore b/.gitignore
index 4c37a59..edaddfb 100755
--- a/.gitignore
+++ b/.gitignore
@@ -33,8 +33,9 @@ MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
-*.manifest
-*.spec
+# *.manifest
+# *.spec
+*.res
# Installer logs
pip-log.txt
@@ -154,4 +155,4 @@ long1.jpg
*.pdiparams.info
*.pdmodel
-.DS_Store
\ No newline at end of file
+.DS_Store
diff --git a/ocrweb_multi/build.py b/ocrweb_multi/build.py
new file mode 100644
index 0000000..a23ee27
--- /dev/null
+++ b/ocrweb_multi/build.py
@@ -0,0 +1,21 @@
+import os
+import shutil
+
+print('Compile ocrweb')
+os.system('pyinstaller -y main.spec')
+
+print('Compile wrapper')
+os.system('windres .\wrapper.rc -O coff -o wrapper.res')
+os.system('gcc .\wrapper.c wrapper.res -o dist/ocrweb.exe')
+
+print('Copy config.yaml')
+shutil.copy2('config.yaml', 'dist/config.yaml')
+
+print('Copy models')
+shutil.copytree('models', 'dist/models', dirs_exist_ok=True)
+os.remove('dist/models/.gitkeep')
+
+print('Pack to ocrweb.zip')
+shutil.make_archive('ocrweb', 'zip', 'dist')
+
+print('Done')
diff --git a/ocrweb_multi/main.spec b/ocrweb_multi/main.spec
new file mode 100644
index 0000000..8cd613c
--- /dev/null
+++ b/ocrweb_multi/main.spec
@@ -0,0 +1,52 @@
+# -*- mode: python ; coding: utf-8 -*-
+
+
+block_cipher = None
+
+
+a = Analysis(
+ ['main.py'],
+ pathex=[],
+ binaries=[],
+ datas=[
+ ('static', 'static'),
+ ],
+ hiddenimports=[],
+ hookspath=[],
+ hooksconfig={},
+ runtime_hooks=[],
+ excludes=[],
+ win_no_prefer_redirects=False,
+ win_private_assemblies=False,
+ cipher=block_cipher,
+ noarchive=False,
+)
+pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
+
+exe = EXE(
+ pyz,
+ a.scripts,
+ [],
+ exclude_binaries=True,
+ name='main',
+ debug=False,
+ bootloader_ignore_signals=False,
+ strip=False,
+ upx=True,
+ console=True,
+ disable_windowed_traceback=False,
+ argv_emulation=False,
+ target_arch=None,
+ codesign_identity=None,
+ entitlements_file=None,
+)
+coll = COLLECT(
+ exe,
+ a.binaries,
+ a.zipfiles,
+ a.datas,
+ strip=False,
+ upx=True,
+ upx_exclude=[],
+ name='ocrweb',
+)
diff --git a/ocrweb_multi/utils/config.py b/ocrweb_multi/utils/config.py
index 8a76e64..337e01f 100644
--- a/ocrweb_multi/utils/config.py
+++ b/ocrweb_multi/utils/config.py
@@ -4,15 +4,23 @@ import yaml
root_dir = Path(__file__).parent.parent
+
def get_resource_path(name: str):
"""依次检查资源文件的多个可能路径, 返回首个存在的路径"""
for path in [
- Path(name),
+ # wrapper.exe 所在目录
+ Path(root_dir.parent, name),
+ # main.exe 所在目录 / main.py 所在目录
Path(root_dir, name),
+ # main.exe 所在目录
Path(sys.argv[0]).parent / name,
+ # 工作目录
+ Path(name),
]:
if path.exists():
+ print('Loaded:', path)
return path
raise FileNotFoundError(name)
+
conf = yaml.safe_load(get_resource_path('config.yaml').read_text())
diff --git a/ocrweb_multi/wrapper.c b/ocrweb_multi/wrapper.c
new file mode 100644
index 0000000..246726f
--- /dev/null
+++ b/ocrweb_multi/wrapper.c
@@ -0,0 +1,55 @@
+/*
+针对Pyinstaller目录下文件过多的问题, 使用外部exe+system调用的方式实现资源文件/依赖库分离
+*/
+#include <windows.h>
+#include <stdio.h>
+
+void combine(char *destination, const char *path1, const char *path2)
+{
+ if (path1 == NULL && path2 == NULL)
+ {
+ strcpy(destination, "");
+ }
+ else if (path2 == NULL || strlen(path2) == 0)
+ {
+ strcpy(destination, path1);
+ }
+ else if (path1 == NULL || strlen(path1) == 0)
+ {
+ strcpy(destination, path2);
+ }
+ else
+ {
+ strcpy(destination, path1);
+
+ size_t idx = 0, sepIdx = 0;
+ size_t size1 = strlen(path1);
+ while (idx < size1)
+ {
+ idx++;
+ if (destination[idx] == '\\' || destination[idx] == '/')
+ {
+ sepIdx = idx;
+ }
+ }
+ // Trim destination: delete from last separator to end.
+ destination[sepIdx + 1] = '\0';
+ strcat(destination, path2);
+ }
+}
+
+void main()
+{
+ // Set title
+ system("title Rapid OCR Server");
+ // Get wrapper exe path
+ TCHAR path[MAX_PATH];
+ GetModuleFileName(NULL, path, MAX_PATH);
+
+ TCHAR exe_path[MAX_PATH];
+ // Get real exe path from wrapper exe path
+ combine(exe_path, path, "ocrweb\\main.exe");
+ printf("Run real exe: %s\n", exe_path);
+ // Run real exe
+ system(exe_path);
+}
diff --git a/ocrweb_multi/wrapper.rc b/ocrweb_multi/wrapper.rc
new file mode 100644
index 0000000..3dc1b63
--- /dev/null
+++ b/ocrweb_multi/wrapper.rc
@@ -0,0 +1 @@
+id ICON "static/favicon.ico"