diff --git a/cpp_projects/OcrLiteNcnn/.gitignore b/cpp_projects/OcrLiteNcnn/.gitignore index 738c1dc..46638ff 100644 --- a/cpp_projects/OcrLiteNcnn/.gitignore +++ b/cpp_projects/OcrLiteNcnn/.gitignore @@ -142,7 +142,10 @@ build-lib/ /ncnn/macos/ /ncnn/windows/ /ncnn/linux/ -ncnn-static/ -ncnn-vulkan-static/ -opencv-shared/ -opencv-static/ \ No newline at end of file +ncnn-static/* +!/ncnn-static/NcnnWrapperConfig.cmake +ncnn-vulkan-static/* +!/ncnn-vulkan-static/NcnnWrapperConfig.cmake +opencv-static/* +!/opencv-static/OpenCVWrapperConfig.cmake +/images/*-result.* \ No newline at end of file diff --git a/cpp_projects/OcrLiteNcnn/BUILD.md b/cpp_projects/OcrLiteNcnn/BUILD.md new file mode 100644 index 0000000..af851bb --- /dev/null +++ b/cpp_projects/OcrLiteNcnn/BUILD.md @@ -0,0 +1,141 @@ +# 编译说明 + +### 依赖的第三方库下载 + +1. 下载opencv,[下载地址](https://github.com/RapidAI/OpenCVBuilder/releases) + +* OpenCV静态库:opencv-(版本号)-平台.7z, +* 把压缩包解压到项目根目录,windows平台需要注意目录层次,解压后目录结构如下 +* windows平台请选择vs2017版本(也可以用于vs2019编译) +* 程序版本v1.6.0需要opencv 4.5.4 +``` +OcrLiteNcnn/opencv-static +├── OpenCVWrapperConfig.cmake +├── linux +├── macos +├── windows-x64 +└── windows-x86 +``` + +2. 下载ncnn,[下载地址](https://github.com/benjaminwan/NcnnBuilder/releases) + +* ncnn静态库含vulkan:ncnn-(版本号)-vulkan-平台.7z +* ncnn静态库:ncnn-(版本号)-平台.7z +* 注意:本demo从v1.5.1开始,去除crnn的lstm外循环改用框架的实现,需要配合ncnn>=20210124,请注意选择ncnn版本。 +* 程序版本v1.6.0需要ncnn 20210720 +* 把压缩包解压到项目根目录,windows平台需要注意目录层次,解压后目录结构如下 + +``` +OcrLiteNcnn/ncnn-static +├── NcnnWrapperConfig.cmake +├── linux +├── macos +├── windows-x64 +└── windows-x86 + +OcrLiteNcnn/ncnn-vulkan-static +├── NcnnWrapperConfig.cmake +├── linux +├── macos +├── windows-x64 +└── windows-x86 +``` + +3. Vulkan SDK,[下载地址](https://vulkan.lunarg.com/sdk/home) + +* 如果想编译ncnn带vulkan支持的版本,则必须先安装Vulkan SDK。 +* 一般下载最新版即可,当前最新版1.2.162.0 +* Windows:直接双击安装。 +* macOS:加载dmg后,终端执行```./install_vulkan.py``` +* Linux: 解压tar.gz文件后,把scripts文件夹里的install-vulkan-linux.sh复制到解压后的文件夹,并打开终端执行 + +``` +chmod a+x install-vulkan-linux.sh +./install-vulkan-linux.sh +``` + +### 编译环境 + +1. Windows 10 x64 +2. macOS 10.15 +3. Linux Ubuntu 1804 x64 + +**注意:以下说明仅适用于本机编译。如果需要交叉编译为arm等其它平台(参考android),则需要先交叉编译所有第三方依赖库(ncnn、opencv),然后再把依赖库整合替换到本项目里。** + +### Windows编译说明 + +#### Windows nmake编译 + +1. 安装VS2017或VS2019,安装时,至少选中'使用C++的桌面开发' +2. cmake>=3.12请自行下载&配置,[下载地址](https://cmake.org/download/) +3. 开始菜单打开"x64 Native Tools Command Prompt for VS 2019"或"适用于 VS2017 的 x64 本机工具",并转到本项目根目录 +4. 运行```build.bat```并按照提示输入选项,最后选择'编译成可执行文件' +5. 编译完成后运行```run-test.bat```进行测试(注意修改脚本内的目标图片路径) +6. 编译JNI动态运行库(可选,可用于java调用) + +* 下载jdk-8u221-windows-x64.exe,安装选项默认(确保“源代码”项选中),安装完成后,打开“系统”属性->高级->环境变量 +* 新建“系统变量”,变量名```JAVA_HOME``` ,变量值```C:\Program Files\Java\jdk1.8.0_221`` +* 新建“系统变量”,变量名```CLASSPATH``` ,变量值```.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;`` +* 编辑“系统变量”Path,Win7在变量值头部添加```%JAVA_HOME%\bin;``` ,win10直接添加一行```%JAVA_HOME%\bin``` +* 开始菜单打开"x64 Native Tools Command Prompt for VS 2019"或"适用于 VS2017 的 x64 本机工具",并转到本项目根目录 +* 运行```build.bat```并按照提示输入选项,最后选择'编译成JNI动态库' + +#### Windows Visual Studio编译说明 + +1. VS2017/VS2019,cmake……等安装配置参考上述步骤。 +2. 运行generate-vs-project.bat,输入数字选择要生成的visual studio项目解决方案版本。 +3. 根据你的编译环境,进入build-xxxx-x86或x64文件夹,打开OcrLiteNcnn.sln。 +4. 在顶部工具栏选择Release,在右边的"解决方案"窗口,右键选中"ALL_BUILD"->生成。要选择Debug,则您必须自行编译Debug版的opencv或ncnn。 + +#### Windows部署说明 + +1. 编译选项选择第三方库为动态库时,部署的时候记得把dll复制到可执行文件目录。 +2. 部署时如果提示缺少"VCRUNTIME140_1.dll",下载安装适用于 Visual Studio 2015、2017 和 2019 的 Microsoft Visual C++ 可再发行软件包, + [下载地址](https://support.microsoft.com/zh-cn/help/2977003/the-latest-supported-visual-c-downloads) + +### Mac编译说明 + +1. macOS Catalina 10.15.x,安装Xcode 12.1,并安装Xcode Command Line Tools, 终端运行```xcode-select –install``` +2. 自行下载安装HomeBrew,cmake >=3.17[下载地址](https://cmake.org/download/) +3. libomp: ```brew install libomp``` +4. 终端打开项目根目录,```./build.sh```并按照提示输入选项,最后选择'编译成可执行文件' +5. 测试:```./run-test.sh```(注意修改脚本内的目标图片路径) +6. 编译JNI动态运行库(可选,可用于java调用) + +* 下载jdk-8u221-macosx-x64.dmg,安装。 +* 编辑用户目录下的隐藏文件```.zshrc``` ,添加```export JAVA_HOME=$(/usr/libexec/java_home)``` +* 运行```build.sh```并按照提示输入选项,最后选择'编译成JNI动态库' + +#### macOS部署说明 + +opencv或onnxruntime使用动态库时,参考下列方法: + +* 把动态库所在路径加入DYLD_LIBRARY_PATH搜索路径 +* 把动态库复制或链接到到/usr/lib + +### Linux编译说明 + +1. Ubuntu18.04 LTS 其它发行版(请自行编译依赖库opencv和ncnn,或自行适配官方发布的动态库) +2. ```sudo apt-get install build-essential``` +3. g++>=5,cmake>=3.17[下载地址](https://cmake.org/download/) +4. 终端打开项目根目录,```./build.sh```并按照提示输入选项,最后选择'编译成可执行文件' +5. 测试:```./run-test.sh```(注意修改脚本内的目标图片路径) +6. 编译JNI动态运行库(可选,可用于java调用) + +* 下载jdk-8u221并安装配置 +* 运行```build.sh```并按照提示输入选项,最后选择'编译成JNI动态库' +* **注意:编译JNI时,g++版本要求>=6** + +#### Linux部署说明 + +opencv或onnxruntime使用动态库时,参考下列方法: + +* 把动态库所在路径加入LD_LIBRARY_PATH搜索路径 +* 把动态库复制或链接到到/usr/lib + +### 编译参数说明 + +build.sh编译参数 + +1. ```OCR_OUTPUT="BIN"或"JNI"或"CLIB"```: BIN时编译为可执行文件,JNI时默认编译为java jni 动态库,CLIB时便以为 C动态库; +2. ```OCR_VULKAN="CPU"或"GPU"```: CPU时则选择不带vulkan的版本编译,GPU时选择ncnn(带vulkan)静态库进行编译; \ No newline at end of file diff --git a/cpp_projects/OcrLiteNcnn/CMakeLists.txt b/cpp_projects/OcrLiteNcnn/CMakeLists.txt index e660484..458453b 100755 --- a/cpp_projects/OcrLiteNcnn/CMakeLists.txt +++ b/cpp_projects/OcrLiteNcnn/CMakeLists.txt @@ -1,17 +1,23 @@ -cmake_minimum_required(VERSION 3.1) +if (WIN32) + cmake_minimum_required(VERSION 3.12) +elseif (APPLE) + cmake_minimum_required(VERSION 3.17) +elseif (UNIX) + cmake_minimum_required(VERSION 3.17) +endif () project(OcrLiteNcnn) -option(OCR_JNI "OcrLite Jni Support" OFF) -option(OCR_CLIB "OcrLite CLib Support" OFF) -option(OCR_STATIC "Use Static Librarys For Build" ON) -option(OCR_OPENMP "OcrLite Enable OpenMP" ON) +if (NOT DEFINED OCR_OUTPUT) + set(OCR_OUTPUT "BIN") + message(STATUS "No OCR_OUTPUT, defaulting to BIN") +endif () +if (NOT DEFINED OCR_VULKAN) + set(OCR_VULKAN "CPU") + message(STATUS "No OCR_VULKAN, defaulting to CPU") +endif () option(OCR_BENCHMARK "build benchmark" ON) -option(OCR_VULKAN "OcrLite Enable Vulkan" ON) -#set(OCR_JNI ON) -#set(OCR_STATIC OFF) -#set(OCR_OPENMP OFF) set(OCR_BENCHMARK ON) -#set(OCR_VULKAN ON) +#set(OCR_OUTPUT "BIN") set(CMAKE_CXX_STANDARD 11) add_definitions(-DUNICODE -D_UNICODE) @@ -50,7 +56,7 @@ else () endif () # NCNN -if (OCR_VULKAN) +if (OCR_VULKAN STREQUAL "GPU") include(${CMAKE_CURRENT_SOURCE_DIR}/ncnn-vulkan-static/NcnnWrapperConfig.cmake) else () include(${CMAKE_CURRENT_SOURCE_DIR}/ncnn-static/NcnnWrapperConfig.cmake) @@ -63,11 +69,8 @@ else () endif (ncnn_FOUND) # OpenCV -if (OCR_STATIC) - include(${CMAKE_CURRENT_SOURCE_DIR}/opencv-static/OpenCVWrapperConfig.cmake) -else () - include(${CMAKE_CURRENT_SOURCE_DIR}/opencv-shared/OpenCVWrapperConfig.cmake) -endif () +set(BUILD_SHARED_LIBS false) +include(${CMAKE_CURRENT_SOURCE_DIR}/opencv-static/OpenCVWrapperConfig.cmake) find_package(OpenCV REQUIRED) if (OpenCV_FOUND) message(STATUS "OpenCV_LIBS: ${OpenCV_LIBS}") @@ -76,8 +79,15 @@ else () message(FATAL_ERROR "opencv Not Found!") endif (OpenCV_FOUND) +# project include +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) + +# source +file(GLOB OCR_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp) +set(OCR_COMPILE_CODE ${OCR_SRC}) + # JNI -if (OCR_JNI) +if (OCR_OUTPUT STREQUAL "JNI") find_package(JNI REQUIRED) if (JNI_FOUND) message("JNI FOUND") @@ -89,46 +99,31 @@ if (OCR_JNI) endif () endif () -# project include -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) - -# source -file(GLOB OCR_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp) -set(OCR_COMPILE_CODE ${OCR_SRC}) - -# static libgcc libstdc++ -if (OCR_STATIC) - if (APPLE) - set(CMAKE_EXE_LINKER_FLAGS "-static-libstdc++") - else () - set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++") - endif () -endif () - -if (OCR_JNI) # JNI +if (OCR_OUTPUT STREQUAL "JNI") # JNI add_library(OcrLiteNcnn SHARED ${OCR_COMPILE_CODE}) target_compile_definitions(OcrLiteNcnn PRIVATE __JNI__) target_link_libraries(OcrLiteNcnn ncnn ${OpenCV_LIBS} ${JNI_LIBS} ${OpenMP_CXX_LIB_NAMES}) -elseif(OCR_CLIB) # C LIB +elseif(OCR_OUTPUT STREQUAL "CLIB") # C LIB add_library(OcrLiteNcnn SHARED ${OCR_COMPILE_CODE}) target_compile_definitions(OcrLiteNcnn PRIVATE __CLIB__) target_link_libraries(OcrLiteNcnn ncnn ${OpenCV_LIBS} ${OpenMP_CXX_LIB_NAMES}) -else () +elseif(OCR_OUTPUT STREQUAL "BIN") # C LIB add_executable(OcrLiteNcnn ${OCR_COMPILE_CODE}) target_compile_definitions(OcrLiteNcnn PRIVATE __EXEC__) target_link_libraries(OcrLiteNcnn ncnn ${OpenCV_LIBS} ${OpenMP_CXX_LIB_NAMES}) endif () -if (OCR_OPENMP) - target_compile_definitions(OcrLiteNcnn PRIVATE __OPENMP__) -endif () +install(TARGETS OcrLiteNcnn EXPORT OcrLiteNcnn + ARCHIVE DESTINATION staticlib + LIBRARY DESTINATION sharedlib + RUNTIME DESTINATION bin) -if (OCR_VULKAN) +if (OCR_VULKAN STREQUAL "GPU") target_compile_definitions(OcrLiteNcnn PRIVATE __VULKAN__) endif () # benchmark -if (OCR_BENCHMARK AND NOT OCR_JNI AND NOT OCR_CLIB) +if (OCR_BENCHMARK AND (OCR_OUTPUT STREQUAL "BIN")) add_executable(benchmark benchmark/benchmark.cpp src/AngleNet.cpp src/clipper.cpp @@ -139,11 +134,12 @@ if (OCR_BENCHMARK AND NOT OCR_JNI AND NOT OCR_CLIB) src/OcrUtils.cpp) target_link_libraries(benchmark ncnn ${OpenCV_LIBS} ${OpenMP_CXX_LIB_NAMES}) target_compile_definitions(benchmark PRIVATE __EXEC__) - if (OCR_OPENMP) - target_compile_definitions(benchmark PRIVATE __OPENMP__) - endif () - if (OCR_VULKAN) + if (OCR_VULKAN STREQUAL "GPU") target_compile_definitions(benchmark PRIVATE __VULKAN__) endif () -endif () \ No newline at end of file + install(TARGETS benchmark EXPORT benchmark + ARCHIVE DESTINATION staticlib + LIBRARY DESTINATION sharedlib + RUNTIME DESTINATION bin) +endif () diff --git a/cpp_projects/OcrLiteNcnn/README.md b/cpp_projects/OcrLiteNcnn/README.md index c68c585..721a1a2 100644 --- a/cpp_projects/OcrLiteNcnn/README.md +++ b/cpp_projects/OcrLiteNcnn/README.md @@ -1,13 +1,23 @@ # OcrLiteNcnn +### 联系方式 + +QQ①群:994929053 QQ②群:820132154 + ### Project下载 -* 有整合好源码和依赖库的完整工程项目,文件比较大,可到Q群共享内下载,找以Project开头的压缩包文件 +* 整合好源码和依赖库的完整工程项目,文件比较大,可到Q群共享内下载,以Project开头的压缩包文件为源码工程,例:Project_OcrLiteNcnn-版本号.7z * 如果想自己折腾,则请继续阅读本说明 ### Demo下载(win、mac、linux) -编译好的demo文件可以到Q群共享内下载 或者[Gitee下载](https://gitee.com/benjaminwan/ocr-lite-ncnn/releases) +* 编译好的demo,可以在release中下载,或者Q群共享内下载 +* 或者[Gitee下载](https://gitee.com/benjaminwan/ocr-lite-ncnn/releases) +* 或者[Github下载](https://github.com/benjaminwan/OcrLiteNcnn/releases) +* 各平台可执行文件:linux-bin.7z、macos-bin.7z、windows-bin.7z +* 用于java的jni库:linux-jni.7z、macos-jni.7z、windows-jni.7z +* 用于C的动态库:linux-clib.7z、macos-clib.7z、windows-clib.7z +* 注意:linux编译平台为ubuntu18.04,如果你的linux版本无法运行demo,请自行从源码编译依赖库和完整项目。 ### 介绍 @@ -21,6 +31,14 @@ ChineseOcr Lite Ncnn,超轻量级中文OCR PC Demo,支持ncnn推理 采用ncnn神经网络前向计算框架[https://github.com/Tencent/ncnn](https://github.com/Tencent/ncnn) +### 更新说明 + +#### 2021-10-13 update + +* opencv 4.5.4 +* ncnn 20210720 +* 优化编译脚本和测试脚本 + ### 模型下载 [模型下载地址](https://github.com/ouyanghuiyu/chineseocr_lite/tree/onnx/models_ncnn) @@ -37,125 +55,35 @@ OcrLiteNcnn/models └── keys.txt ``` -### 依赖的第三方库下载 - -1. 下载opencv和ncnn,[下载地址](https://gitee.com/benjaminwan/ocr-lite-ncnn/releases/v1.0) - -* OpenCv动态库:opencv-(版本号)-sharedLib.7z -* OpenCv静态库:opencv-(版本号)-staticLib.7z -* ncnn静态库含vulkan:ncnn-(版本号)-vulkan-staticLib.7z -* ncnn静态库:ncnn-(版本号)-staticLib.7z -* 把压缩包解压到项目根目录,解压后目录结构 -* 注意:本demo从v1.5.1开始,去除crnn的lstm外循环改用框架的实现,需要配合ncnn>=20210124,请注意选择ncnn版本。 - -``` -OcrLiteNcnn - ├── ncnn-static - ├── ncnn-vulkan-static - ├── opencv-shared - ├── opencv-static -``` - -2. Vulkan SDK,[下载地址](https://vulkan.lunarg.com/sdk/home) - -* 如果想编译ncnn带vulkan支持的版本,则必须先安装Vulkan SDK。 -* 一般下载最新版即可,当前最新版1.2.162.0 -* Windows:直接双击安装。 -* macOS:加载dmg后,终端执行```./install_vulkan.py``` -* Linux: 解压tar.gz文件后,把scripts文件夹里的install-vulkan-linux.sh复制到解压后的文件夹,并打开终端执行 - -``` -chmod a+x install-vulkan-linux.sh -./install-vulkan-linux.sh -``` - -### 编译环境 +### [编译说明](./BUILD.md) -1. Windows 10 x64 -2. macOS 10.15 -3. Linux Ubuntu 1604 x64 +### 测试说明 -**注意:以下说明仅适用于本机编译。如果需要交叉编译为arm等其它平台(参考android),则需要先交叉编译所有第三方依赖库(ncnn、opencv),然后再把依赖库整合替换到本项目里。** +1. 根据系统下载对应的程序包linux-bin.7z、macos-bin.7z、windows-bin.7z,并解压. +2. 把上面的模型下载,解压到第一步解压的文件夹里. +3. 终端运行run-test.sh或命令行运行run-test.bat,查看识别结果. +4. 终端运行run-benchmark.sh或命令行运行run-benchmark.bat,查看识别过程平均耗时. -### Windows编译说明 +### FAQ -#### Windows nmake编译 +#### macOS缺少openmp -1. 安装VS2017或VS2019,安装时,至少选中'使用C++的桌面开发' -2. cmake请自行下载&配置,[下载地址](https://cmake.org/download/) -3. 开始菜单打开"x64 Native Tools Command Prompt for VS 2019"或"适用于 VS2017 的 x64 本机工具",并转到本项目根目录 -4. 运行```build.bat```并按照提示输入选项,最后选择'编译成可执行文件' -5. 编译完成后运行```run-test.bat```进行测试(注意修改脚本内的目标图片路径) -6. 编译JNI动态运行库(可选,可用于java调用) +```brew install libomp``` -* 下载jdk-8u221-windows-x64.exe,安装选项默认(确保“源代码”项选中),安装完成后,打开“系统”属性->高级->环境变量 -* 新建“系统变量”,变量名```JAVA_HOME``` ,变量值```C:\Program Files\Java\jdk1.8.0_221`` -* 新建“系统变量”,变量名```CLASSPATH``` ,变量值```.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;`` -* 编辑“系统变量”Path,Win7在变量值头部添加```%JAVA_HOME%\bin;``` ,win10直接添加一行```%JAVA_HOME%\bin``` -* 开始菜单打开"x64 Native Tools Command Prompt for VS 2019"或"适用于 VS2017 的 x64 本机工具",并转到本项目根目录 -* 运行```build.bat```并按照提示输入选项,最后选择'编译成JNI动态库' +#### gpu版程序运行出错,缺少vulkan sdk -#### Windows Visual Studio编译说明 +参考[编译说明](./BUILD.md) 安装vulkan sdk -1. VS2017/VS2019,cmake……等安装配置参考上述步骤。 -2. 运行generate-vs-project.bat,输入数字选择要生成的visual studio项目解决方案版本。 -3. 根据你的编译环境,进入build-xxxx-x86或x64文件夹,打开OcrLiteNcnn.sln。 -4. 在顶部工具栏选择Release,在右边的"解决方案"窗口,右键选中"ALL_BUILD"->生成。要选择Debug,则您必须自行编译Debug版的opencv或ncnn。 +#### windows提示缺少"VCRUNTIME140_1.dll" -#### Windows部署说明 +下载安装适用于 Visual Studio 2015、2017 和 2019 的 Microsoft Visual C++ 可再发行软件包 +[下载地址](https://support.microsoft.com/zh-cn/help/2977003/the-latest-supported-visual-c-downloads) -1. 编译选项选择第三方库为动态库时,部署的时候记得把dll复制到可执行文件目录。 -2. 部署时如果提示缺少"VCRUNTIME140_1.dll",下载安装适用于 Visual Studio 2015、2017 和 2019 的 Microsoft Visual C++ 可再发行软件包, - [下载地址](https://support.microsoft.com/zh-cn/help/2977003/the-latest-supported-visual-c-downloads) +#### Windows7执行错误|中文乱码 -### Mac编译说明 - -1. macOS Catalina 10.15.x,安装Xcode 12.1,并安装Xcode Command Line Tools, 终端运行```xcode-select –install``` -2. 自行下载安装HomeBrew,cmake >=3.1[下载地址](https://cmake.org/download/) -3. libomp: ```brew install libomp``` -4. 终端打开项目根目录,```./build.sh```并按照提示输入选项,最后选择'编译成可执行文件' -5. 测试:```./run-test.sh```(注意修改脚本内的目标图片路径) -6. 编译JNI动态运行库(可选,可用于java调用) - -* 下载jdk-8u221-macosx-x64.dmg,安装。 -* 编辑用户目录下的隐藏文件```.zshrc``` ,添加```export JAVA_HOME=$(/usr/libexec/java_home)``` -* 运行```build.sh```并按照提示输入选项,最后选择'编译成JNI动态库' - -#### macOS部署说明 - -opencv或onnxruntime使用动态库时,参考下列方法: - -* 把动态库所在路径加入DYLD_LIBRARY_PATH搜索路径 -* 把动态库复制或链接到到/usr/lib - -### Linux编译说明 - -1. Ubuntu16.04 LTS 或其它发行版 -2. ```sudo apt-get install build-essential``` -3. g++>=5,cmake>=3.1[下载地址](https://cmake.org/download/) -4. 终端打开项目根目录,```./build.sh```并按照提示输入选项,最后选择'编译成可执行文件' -5. 测试:```./run-test.sh```(注意修改脚本内的目标图片路径) -6. 编译JNI动态运行库(可选,可用于java调用) - -* 下载jdk-8u221并安装配置 -* 运行```build.sh```并按照提示输入选项,最后选择'编译成JNI动态库' -* **注意:编译JNI时,g++版本要求>=6** - -#### Linux部署说明 - -opencv或onnxruntime使用动态库时,参考下列方法: - -* 把动态库所在路径加入LD_LIBRARY_PATH搜索路径 -* 把动态库复制或链接到到/usr/lib - -### 编译参数说明 - -build.sh编译参数 - -1. ```OCR_OPENMP=ON```:启用(ON)或禁用(OFF) AngleNet和CrnnNet阶段使用OpenMP并行运算。 -2. ```OCR_LIB=ON```: 启用(ON)或禁用(OFF) ON时编译为jni lib,OFF时编译为可执行文件 -3. ```OCR_STATIC=ON```: 启用(ON)或禁用(OFF) ON时选择opencv静态库进行编译,OFF时则选择动态库编译 -4. ```OCR_VULKAN=ON```: 启用(ON)或禁用(OFF) ON时选择ncnn(带vulkan)静态库进行编译,OFF时则选择不带vulkan的版本编译 +1. cmd窗口左上角-属性 +2. 字体选项卡-选择除了“点阵字体”以外的TrueType字体,例如:Lucida Console、宋体 +3. 重新执行bat ### 输入参数说明 @@ -180,10 +108,6 @@ build.sh编译参数 15. ```-h或--help```:打印命令行帮助。 16. ```-G或--GPU```:尝试使用gpu进行计算,-1(使用CPU)/0(使用GPU0)/1(使用GPU1)/...,GPU选择失败时,则使用CPU进行计算。 -### 编译脚本说明 - -* scripts文件夹内有一些脚本,用于自行编译ncnn,另有编译opencv的脚本,请到OcrLiteOnnx项目内寻找。 - ### 关于内存泄漏与valgrind * 项目根目录的valgrind-memcheck.sh用来检查内存泄漏(需要debug编译)。 diff --git a/cpp_projects/OcrLiteNcnn/build-default.bat b/cpp_projects/OcrLiteNcnn/build-default.bat new file mode 100644 index 0000000..6a2c0ba --- /dev/null +++ b/cpp_projects/OcrLiteNcnn/build-default.bat @@ -0,0 +1,114 @@ +@ECHO OFF +chcp 65001 +cls +@SETLOCAL + +mkdir win-BIN-CPU-x64 +pushd win-BIN-CPU-x64 +cmake -T "v141,host=x64" -A "x64" ^ + -DCMAKE_INSTALL_PREFIX=install ^ + -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="BIN" -DOCR_VULKAN="CPU" .. +cmake --build . --config Release -j %NUMBER_OF_PROCESSORS% +cmake --build . --config Release --target install +popd + +mkdir win-BIN-CPU-Win32 +pushd win-BIN-CPU-Win32 +cmake -T "v141,host=x64" -A "Win32" ^ + -DCMAKE_INSTALL_PREFIX=install ^ + -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="BIN" -DOCR_VULKAN="CPU" .. +cmake --build . --config Release -j %NUMBER_OF_PROCESSORS% +cmake --build . --config Release --target install +popd + +mkdir win-JNI-CPU-x64 +pushd win-JNI-CPU-x64 +cmake -T "v141,host=x64" -A "x64" ^ + -DCMAKE_INSTALL_PREFIX=install ^ + -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="JNI" -DOCR_VULKAN="CPU" .. +cmake --build . --config Release -j %NUMBER_OF_PROCESSORS% +cmake --build . --config Release --target install +popd + +mkdir win-JNI-CPU-Win32 +pushd win-JNI-CPU-Win32 +cmake -T "v141,host=x64" -A "Win32" ^ + -DCMAKE_INSTALL_PREFIX=install ^ + -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="JNI" -DOCR_VULKAN="CPU" .. +cmake --build . --config Release -j %NUMBER_OF_PROCESSORS% +cmake --build . --config Release --target install +popd + +mkdir win-CLIB-CPU-x64 +pushd win-CLIB-CPU-x64 +cmake -T "v141,host=x64" -A "x64" ^ + -DCMAKE_INSTALL_PREFIX=install ^ + -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="CLIB" -DOCR_VULKAN="CPU" .. +cmake --build . --config Release -j %NUMBER_OF_PROCESSORS% +cmake --build . --config Release --target install +popd + +mkdir win-CLIB-CPU-Win32 +pushd win-CLIB-CPU-Win32 +cmake -T "v141,host=x64" -A "Win32" ^ + -DCMAKE_INSTALL_PREFIX=install ^ + -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="CLIB" -DOCR_VULKAN="CPU" .. +cmake --build . --config Release -j %NUMBER_OF_PROCESSORS% +cmake --build . --config Release --target install +popd + +mkdir win-BIN-GPU-x64 +pushd win-BIN-GPU-x64 +cmake -T "v141,host=x64" -A "x64" ^ + -DCMAKE_INSTALL_PREFIX=install ^ + -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="BIN" -DOCR_VULKAN="GPU" .. +cmake --build . --config Release -j %NUMBER_OF_PROCESSORS% +cmake --build . --config Release --target install +popd + +mkdir win-BIN-GPU-Win32 +pushd win-BIN-GPU-Win32 +cmake -T "v141,host=x64" -A "Win32" ^ + -DCMAKE_INSTALL_PREFIX=install ^ + -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="BIN" -DOCR_VULKAN="GPU" .. +cmake --build . --config Release -j %NUMBER_OF_PROCESSORS% +cmake --build . --config Release --target install +popd + +mkdir win-JNI-GPU-x64 +pushd win-JNI-GPU-x64 +cmake -T "v141,host=x64" -A "x64" ^ + -DCMAKE_INSTALL_PREFIX=install ^ + -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="JNI" -DOCR_VULKAN="GPU" .. +cmake --build . --config Release -j %NUMBER_OF_PROCESSORS% +cmake --build . --config Release --target install +popd + +mkdir win-JNI-GPU-Win32 +pushd win-JNI-GPU-Win32 +cmake -T "v141,host=x64" -A "Win32" ^ + -DCMAKE_INSTALL_PREFIX=install ^ + -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="JNI" -DOCR_VULKAN="GPU" .. +cmake --build . --config Release -j %NUMBER_OF_PROCESSORS% +cmake --build . --config Release --target install +popd + +mkdir win-CLIB-GPU-x64 +pushd win-CLIB-GPU-x64 +cmake -T "v141,host=x64" -A "x64" ^ + -DCMAKE_INSTALL_PREFIX=install ^ + -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="CLIB" -DOCR_VULKAN="GPU" .. +cmake --build . --config Release -j %NUMBER_OF_PROCESSORS% +cmake --build . --config Release --target install +popd + +mkdir win-CLIB-GPU-Win32 +pushd win-CLIB-GPU-Win32 +cmake -T "v141,host=x64" -A "Win32" ^ + -DCMAKE_INSTALL_PREFIX=install ^ + -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="CLIB" -DOCR_VULKAN="GPU" .. +cmake --build . --config Release -j %NUMBER_OF_PROCESSORS% +cmake --build . --config Release --target install +popd + +@ENDLOCAL diff --git a/cpp_projects/OcrLiteNcnn/build-default.sh b/cpp_projects/OcrLiteNcnn/build-default.sh new file mode 100755 index 0000000..5a09122 --- /dev/null +++ b/cpp_projects/OcrLiteNcnn/build-default.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash + +sysOS=$(uname -s) +NUM_THREADS=1 +if [ $sysOS == "Darwin" ]; then + #echo "I'm MacOS" + NUM_THREADS=$(sysctl -n hw.nCPU) +elif [ $sysOS == "Linux" ]; then + #echo "I'm Linux" + NUM_THREADS=$(grep ^processor /proc/cpuinfo | wc -l) +else + echo "Other OS: $sysOS" +fi + +mkdir -p ${sysOS}-BIN-CPU +pushd ${sysOS}-BIN-CPU +cmake -DCMAKE_INSTALL_PREFIX=install -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="BIN" -DOCR_VULKAN="CPU" .. +cmake --build . --config Release -j $NUM_THREADS +cmake --build . --config Release --target install +popd + +mkdir -p ${sysOS}-BIN-GPU +pushd ${sysOS}-BIN-GPU +cmake -DCMAKE_INSTALL_PREFIX=install -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="BIN" -DOCR_VULKAN="GPU" .. +cmake --build . --config Release -j $NUM_THREADS +cmake --build . --config Release --target install +popd + +mkdir -p ${sysOS}-JNI-CPU +pushd ${sysOS}-JNI-CPU +cmake -DCMAKE_INSTALL_PREFIX=install -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="JNI" -DOCR_VULKAN="CPU" .. +cmake --build . --config Release -j $NUM_THREADS +cmake --build . --config Release --target install +popd + +mkdir -p ${sysOS}-JNI-GPU +pushd ${sysOS}-JNI-GPU +cmake -DCMAKE_INSTALL_PREFIX=install -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="JNI" -DOCR_VULKAN="GPU" .. +cmake --build . --config Release -j $NUM_THREADS +cmake --build . --config Release --target install +popd + +mkdir -p ${sysOS}-CLIB-CPU +pushd ${sysOS}-CLIB-CPU +cmake -DCMAKE_INSTALL_PREFIX=install -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="CLIB" -DOCR_VULKAN="CPU" .. +cmake --build . --config Release -j $NUM_THREADS +cmake --build . --config Release --target install +popd + +mkdir -p ${sysOS}-CLIB-GPU +pushd ${sysOS}-CLIB-GPU +cmake -DCMAKE_INSTALL_PREFIX=install -DCMAKE_BUILD_TYPE=Release -DOCR_OUTPUT="CLIB" -DOCR_VULKAN="GPU" .. +cmake --build . --config Release -j $NUM_THREADS +cmake --build . --config Release --target install +popd diff --git a/cpp_projects/OcrLiteNcnn/build.bat b/cpp_projects/OcrLiteNcnn/build.bat old mode 100755 new mode 100644 index 1a4cb40..8f1bf8b --- a/cpp_projects/OcrLiteNcnn/build.bat +++ b/cpp_projects/OcrLiteNcnn/build.bat @@ -7,111 +7,65 @@ echo. echo "========编译选项========" echo "请注意:项目默认使用Release库,除非您自行编译Debug版的ncnn和Opencv,否则请不要选择Debug编译" -echo "请输入编译选项并回车: 1)Release, 2)Debug, 3)预设" +echo "请输入编译选项并回车: 1)Release, 2)Debug" set BUILD_TYPE=Release set /p flag= if %flag% == 1 (set BUILD_TYPE=Release)^ else if %flag% == 2 (set BUILD_TYPE=Debug)^ -else if %flag% == 3 (goto :makeAllExe)^ else (echo 输入错误!Input Error!) echo. -echo "请输入OpenMP选项并回车: 1)启用OpenMP(Angle阶段和Crnn阶段多线程并行执行), 2)禁用OpenMP(Angle阶段和Crnn阶段单线程执行)" -set BUILD_OPENMP=ON -set /p flag= -if %flag% == 1 (set BUILD_OPENMP=ON)^ -else if %flag% == 2 (set BUILD_OPENMP=OFF)^ -else (echo 输入错误!Input Error!) -echo. - -echo "使用静态库时,编译出来的可执行文件较大,但部署起来比较方便。" -echo "使用动态库时,编译出来的可执行文件较小,但部署的时候记得把dll复制到可执行文件目录" -echo "请选择要使用的Opencv库选项并回车: 1)Static静态库,2)Shared动态库" -set BUILD_STATIC=ON +echo "请选择要使用的ncnn库选项并回车: 1)ncnn(CPU),2)ncnn(vulkan)" +set BUILD_NCNN_VULKAN="CPU" set /p flag= -if %flag% == 1 (set BUILD_STATIC=ON)^ -else if %flag% == 2 (set BUILD_STATIC=OFF)^ +if %flag% == 1 (set BUILD_NCNN_VULKAN="CPU")^ +else if %flag% == 2 (set BUILD_NCNN_VULKAN="GPU")^ else (echo "输入错误!Input Error!") echo. -echo "请选择要使用的ncnn库选项并回车: 1)ncnn(CPU),2)ncnn(vulkan)" -set BUILD_NCNN_VULKAN=OFF +echo "请注意:如果选择2)JNI动态库时,必须安装配置Oracle JDK" +echo "请选择编译输出类型并回车: 1)BIN可执行文件,2)JNI动态库,3)C动态库" +set BUILD_OUTPUT="EXE" set /p flag= -if %flag% == 1 (set BUILD_NCNN_VULKAN=OFF)^ -else if %flag% == 2 (set BUILD_NCNN_VULKAN=ON)^ -else (echo "输入错误!Input Error!") +if %flag% == 1 (set BUILD_OUTPUT="BIN")^ +else if %flag% == 2 (set BUILD_OUTPUT="JNI")^ +else if %flag% == 3 (set BUILD_OUTPUT="CLIB")^ +else (echo 输入错误!Input Error!) echo. -echo "请注意:如果选择2)编译为JNI动态库时,必须安装配置Oracle JDK" -echo "请选择编译输出类型并回车: 1)编译成可执行文件,2)编译成JNI动态库,3)编译成C层动态库" -set BUILD_JNI=OFF -set BUILD_CLIB=OFF +echo "VS版本: 1)vs2017-x64,2)vs2017-x86, 3)vs2019-x64, 4)vs2019-x86" +set BUILD_CMAKE_T="v141" +set BUILD_CMAKE_A="x64" set /p flag= -if %flag% == 1 (set BUILD_JNI=OFF)^ -else if %flag% == 2 (set BUILD_JNI=ON)^ -else if %flag% == 3 (set BUILD_CLIB=ON)^ +if %flag% == 1 ( + set BUILD_CMAKE_T="v141" + set BUILD_CMAKE_A="x64" +)^ +else if %flag% == 2 ( + set BUILD_CMAKE_T="v141" + set BUILD_CMAKE_A="Win32" +)^ +else if %flag% == 3 ( + set BUILD_CMAKE_T="v142" + set BUILD_CMAKE_A="x64" +)^ +else if %flag% == 4 ( + set BUILD_CMAKE_T="v142" + set BUILD_CMAKE_A="Win32" +)^ else (echo 输入错误!Input Error!) echo. -if %BUILD_JNI% == ON (call :makeJni)^ -else if %BUILD_CLIB% == ON (call :makeCLib)^ -else (call :makeExe) -echo cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DOCR_OPENMP=%BUILD_OPENMP% -DOCR_JNI=%BUILD_JNI% -DOCR_CLIB=%BUILD_CLIB% -DOCR_STATIC=%BUILD_STATIC% -DOCR_VULKAN=%BUILD_NCNN_VULKAN% .. -cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DOCR_OPENMP=%BUILD_OPENMP% -DOCR_JNI=%BUILD_JNI% -DOCR_CLIB=%BUILD_CLIB% -DOCR_STATIC=%BUILD_STATIC% -DOCR_VULKAN=%BUILD_NCNN_VULKAN% .. -nmake -popd -GOTO:EOF - -:makeExe -mkdir build -pushd build -GOTO:EOF - -:makeJni -mkdir build-jni -pushd build-jni -GOTO:EOF - -:makeCLib -mkdir build-clib -pushd build-clib -GOTO:EOF +mkdir win-%BUILD_OUTPUT%-%BUILD_NCNN_VULKAN%-%BUILD_CMAKE_A% +pushd win-%BUILD_OUTPUT%-%BUILD_NCNN_VULKAN%-%BUILD_CMAKE_A% -:makeAllExe -mkdir win-cpu-%VSCMD_ARG_TGT_ARCH% -pushd win-cpu-%VSCMD_ARG_TGT_ARCH% -cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DOCR_OPENMP=OFF -DOCR_JNI=OFF -DOCR_CLIB=OFF -DOCR_STATIC=ON -DOCR_VULKAN=OFF .. -nmake -popd +cmake -T "%BUILD_CMAKE_T%,host=x64" -A %BUILD_CMAKE_A% ^ + -DCMAKE_INSTALL_PREFIX=install ^ + -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DOCR_OUTPUT=%BUILD_OUTPUT% -DOCR_VULKAN=%BUILD_NCNN_VULKAN% .. +cmake --build . --config %BUILD_TYPE% -j %NUMBER_OF_PROCESSORS% +cmake --build . --config %BUILD_TYPE% --target install -mkdir win-gpu-%VSCMD_ARG_TGT_ARCH% -pushd win-gpu-%VSCMD_ARG_TGT_ARCH% -cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DOCR_OPENMP=OFF -DOCR_JNI=OFF -DOCR_CLIB=OFF -DOCR_STATIC=ON -DOCR_VULKAN=ON .. -nmake -popd - -mkdir win-jni-cpu-%VSCMD_ARG_TGT_ARCH% -pushd win-jni-cpu-%VSCMD_ARG_TGT_ARCH% -cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DOCR_OPENMP=OFF -DOCR_JNI=ON -DOCR_CLIB=OFF -DOCR_STATIC=ON -DOCR_VULKAN=OFF .. -nmake -popd - -mkdir win-jni-gpu-%VSCMD_ARG_TGT_ARCH% -pushd win-jni-gpu-%VSCMD_ARG_TGT_ARCH% -cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DOCR_OPENMP=OFF -DOCR_JNI=ON -DOCR_CLIB=OFF -DOCR_STATIC=ON -DOCR_VULKAN=ON .. -nmake -popd - -mkdir win-clib-cpu-%VSCMD_ARG_TGT_ARCH% -pushd win-clib-cpu-%VSCMD_ARG_TGT_ARCH% -cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DOCR_OPENMP=OFF -DOCR_JNI=OFF -DOCR_CLIB=ON -DOCR_STATIC=ON -DOCR_VULKAN=OFF .. -nmake -popd - -mkdir win-clib-gpu-%VSCMD_ARG_TGT_ARCH% -pushd win-clib-gpu-%VSCMD_ARG_TGT_ARCH% -cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DOCR_OPENMP=OFF -DOCR_JNI=OFF -DOCR_CLIB=ON -DOCR_STATIC=ON -DOCR_VULKAN=ON .. -nmake popd +GOTO:EOF @ENDLOCAL diff --git a/cpp_projects/OcrLiteNcnn/build.sh b/cpp_projects/OcrLiteNcnn/build.sh index 14c71b3..5fe8f27 100755 --- a/cpp_projects/OcrLiteNcnn/build.sh +++ b/cpp_projects/OcrLiteNcnn/build.sh @@ -1,90 +1,15 @@ #!/usr/bin/env bash -function buildAll() { - mkdir -p ${sysOS}-CPU - pushd ${sysOS}-CPU - cmake -DCMAKE_BUILD_TYPE=Release -DOCR_OPENMP=OFF -DOCR_JNI=OFF -DOCR_CLIB=OFF -DOCR_STATIC=ON -DOCR_VULKAN=OFF .. - make -j $NUM_THREADS - popd - - mkdir -p ${sysOS}-GPU - pushd ${sysOS}-GPU - cmake -DCMAKE_BUILD_TYPE=Release -DOCR_OPENMP=OFF -DOCR_JNI=OFF -DOCR_CLIB=OFF -DOCR_STATIC=ON -DOCR_VULKAN=ON .. - make -j $NUM_THREADS - popd - - mkdir -p ${sysOS}-Jni-CPU - pushd ${sysOS}-Jni-CPU - cmake -DCMAKE_BUILD_TYPE=Release -DOCR_OPENMP=OFF -DOCR_JNI=ON -DOCR_CLIB=OFF -DOCR_STATIC=ON -DOCR_VULKAN=OFF .. - make -j $NUM_THREADS - popd - - mkdir -p ${sysOS}-Jni-GPU - pushd ${sysOS}-Jni-GPU - cmake -DCMAKE_BUILD_TYPE=Release -DOCR_OPENMP=OFF -DOCR_JNI=ON -DOCR_CLIB=OFF -DOCR_STATIC=ON -DOCR_VULKAN=ON .. - make -j $NUM_THREADS - popd - - mkdir -p ${sysOS}-CLib-CPU - pushd ${sysOS}-CLib-CPU - cmake -DCMAKE_BUILD_TYPE=Release -DOCR_OPENMP=OFF -DOCR_JNI=OFF -DOCR_CLIB=ON -DOCR_STATIC=ON -DOCR_VULKAN=OFF .. - make -j $NUM_THREADS - popd - - mkdir -p ${sysOS}-CLib-GPU - pushd ${sysOS}-CLib-GPU - cmake -DCMAKE_BUILD_TYPE=Release -DOCR_OPENMP=OFF -DOCR_JNI=OFF -DOCR_CLIB=ON -DOCR_STATIC=ON -DOCR_VULKAN=ON .. - make -j $NUM_THREADS - popd -} - -sysOS=$(uname -s) -NUM_THREADS=1 -if [ $sysOS == "Darwin" ]; then - #echo "I'm MacOS" - NUM_THREADS=$(sysctl -n hw.ncpu) -elif [ $sysOS == "Linux" ]; then - #echo "I'm Linux" - NUM_THREADS=$(grep ^processor /proc/cpuinfo | wc -l) -else - echo "Other OS: $sysOS" -fi - echo "========请先参考README.md准备好编译环境========" echo echo "========编译选项========" -echo "请输入编译选项并回车: 1)Release, 2)Debug, 3)预设" +echo "请输入编译选项并回车: 1)Release, 2)Debug" read -p "" BUILD_TYPE if [ $BUILD_TYPE == 1 ]; then BUILD_TYPE=Release elif [ $BUILD_TYPE == 2 ]; then BUILD_TYPE=Debug -elif [ $BUILD_TYPE == 3 ]; then - buildAll - exit -else - echo -e "输入错误!Input Error!" -fi - -echo "请输入OpenMP选项并回车: 1)启用OpenMP(Angle阶段和Crnn阶段多线程并行执行), 2)禁用OpenMP(Angle阶段和Crnn阶段单线程执行)" -read -p "" BUILD_OPENMP -if [ $BUILD_OPENMP == 1 ]; then - BUILD_OPENMP=ON -elif [ $BUILD_OPENMP == 2 ]; then - BUILD_OPENMP=OFF -else - echo -e "输入错误!Input Error!" -fi - -echo "使用静态库时,编译出来的可执行文件较大,但部署起来比较方便。" -echo "使用动态库时,编译出来的可执行文件较小,但Linux部署时要配置LD_LIBRARY_PATH或链接|复制到/usr/lib。" -echo "请选择要使用的Opencv库选项并回车: 1)Static静态库,2)Shared动态库" -read -p "" BUILD_STATIC -if [ $BUILD_STATIC == 1 ]; then - BUILD_STATIC=ON -elif [ $BUILD_STATIC == 2 ]; then - BUILD_STATIC=OFF else echo -e "输入错误!Input Error!" fi @@ -92,40 +17,43 @@ fi echo "请选择要使用的ncnn库选项并回车: 1)ncnn(CPU),2)ncnn(vulkan)" read -p "" BUILD_VULKAN if [ $BUILD_VULKAN == 1 ]; then - BUILD_VULKAN=OFF + BUILD_VULKAN="CPU" elif [ $BUILD_VULKAN == 2 ]; then - BUILD_VULKAN=ON + BUILD_VULKAN="GPU" else echo -e "输入错误!Input Error!" fi -echo "请注意:如果选择2)编译为JNI动态库时,必须安装配置Oracle JDK" -echo "请选择编译输出类型并回车: 1)编译成可执行文件,2)编译成JNI动态库,3)编译成C层动态库" -read -p "" BUILD_JNI -BUILD_CLIB=OFF -if [ $BUILD_JNI == 1 ]; then - BUILD_JNI=OFF -elif [ $BUILD_JNI == 2 ]; then - BUILD_JNI=ON -elif [ $BUILD_JNI == 3 ]; then - BUILD_JNI=OFF - BUILD_CLIB=ON +echo "请注意:如果选择2)JNI动态库时,必须安装配置Oracle JDK" +echo "请选择编译输出类型并回车: 1)BIN可执行文件,2)JNI动态库,3)C动态库" +read -p "" BUILD_OUTPUT +if [ $BUILD_OUTPUT == 1 ]; then + BUILD_OUTPUT="BIN" +elif [ $BUILD_OUTPUT == 2 ]; then + BUILD_OUTPUT="JNI" +elif [ $BUILD_OUTPUT == 3 ]; then + BUILD_OUTPUT="CLIB" else echo -e "输入错误!Input Error!" fi -if [ $BUILD_JNI == ON ]; then - mkdir -p build-jni - pushd build-jni -elif [ $BUILD_CLIB == ON ]; then - mkdir -p build-clib - pushd build-clib +sysOS=$(uname -s) +NUM_THREADS=1 +if [ $sysOS == "Darwin" ]; then + #echo "I'm MacOS" + NUM_THREADS=$(sysctl -n hw.ncpu) +elif [ $sysOS == "Linux" ]; then + #echo "I'm Linux" + NUM_THREADS=$(grep ^processor /proc/cpuinfo | wc -l) else - mkdir -p build - pushd build + echo "Other OS: $sysOS" fi -echo "cmake -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DOCR_OPENMP=${BUILD_OPENMP} -DOCR_JNI=${BUILD_JNI} -DOCR_CLIB=${BUILD_CLIB} -DOCR_STATIC=${BUILD_STATIC} -DOCR_VULKAN=${BUILD_VULKAN} .." -cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DOCR_OPENMP=$BUILD_OPENMP -DOCR_JNI=$BUILD_JNI -DOCR_CLIB=$BUILD_CLIB -DOCR_STATIC=$BUILD_STATIC -DOCR_VULKAN=$BUILD_VULKAN .. -make -j $NUM_THREADS +mkdir -p $sysOS-$BUILD_OUTPUT-$BUILD_VULKAN +pushd $sysOS-$BUILD_OUTPUT-$BUILD_VULKAN + +echo "cmake -DCMAKE_INSTALL_PREFIX=install -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DOCR_OUTPUT=$BUILD_OUTPUT -DOCR_VULKAN=$BUILD_VULKAN .." +cmake -DCMAKE_INSTALL_PREFIX=install -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DOCR_OUTPUT=$BUILD_OUTPUT -DOCR_VULKAN=$BUILD_VULKAN .. +cmake --build . --config $BUILD_TYPE -j $NUM_THREADS +cmake --build . --config $BUILD_TYPE --target install popd diff --git a/cpp_projects/OcrLiteNcnn/generate-vs-project.bat b/cpp_projects/OcrLiteNcnn/generate-vs-project.bat index 3d653c9..18747bd 100644 --- a/cpp_projects/OcrLiteNcnn/generate-vs-project.bat +++ b/cpp_projects/OcrLiteNcnn/generate-vs-project.bat @@ -15,40 +15,21 @@ else if %flag% == 2 (set BUILD_TYPE=Debug)^ else (echo 输入错误!Input Error!) echo. -echo "请输入OpenMP选项并回车: 1)启用OpenMP(Angle阶段和Crnn阶段多线程并行执行), 2)禁用OpenMP(Angle阶段和Crnn阶段单线程执行)" -set BUILD_OPENMP=ON -set /p flag= -if %flag% == 1 (set BUILD_OPENMP=ON)^ -else if %flag% == 2 (set BUILD_OPENMP=OFF)^ -else (echo 输入错误!Input Error!) -echo. - -echo "使用静态库时,编译出来的可执行文件较大,但部署起来比较方便。" -echo "使用动态库时,编译出来的可执行文件较小,但部署的时候记得把dll复制到可执行文件目录" -echo "请选择要使用的Opencv库选项并回车: 1)Static静态库,2)Shared动态库" -set BUILD_STATIC=ON -set /p flag= -if %flag% == 1 (set BUILD_STATIC=ON)^ -else if %flag% == 2 (set BUILD_STATIC=OFF)^ -else (echo "输入错误!Input Error!") -echo. - echo "请选择要使用的ncnn库选项并回车: 1)ncnn(CPU),2)ncnn(vulkan)" -set BUILD_NCNN_VULKAN=OFF +set BUILD_NCNN_VULKAN="CPU" set /p flag= -if %flag% == 1 (set BUILD_NCNN_VULKAN=OFF)^ -else if %flag% == 2 (set BUILD_NCNN_VULKAN=ON)^ +if %flag% == 1 (set BUILD_NCNN_VULKAN="CPU")^ +else if %flag% == 2 (set BUILD_NCNN_VULKAN="GPU")^ else (echo "输入错误!Input Error!") echo. echo "请注意:如果选择2)编译为JNI动态库时,必须安装配置Oracle JDK" -echo "请选择编译输出类型并回车: 1)编译成可执行文件,2)编译成JNI动态库,3)编译成C层动态库" -set BUILD_JNI=OFF -set BUILD_CLIB=OFF +echo "请选择编译输出类型并回车: 1)编译成可执行文件,2)编译成JNI动态库,3)编译成C动态库" +set BUILD_OUTPUT="EXE" set /p flag= -if %flag% == 1 (set BUILD_JNI=OFF)^ -else if %flag% == 2 (set BUILD_JNI=ON)^ -else if %flag% == 3 (set BUILD_CLIB=ON)^ +if %flag% == 1 (set BUILD_OUTPUT="BIN")^ +else if %flag% == 2 (set BUILD_OUTPUT="JNI")^ +else if %flag% == 3 (set BUILD_OUTPUT="CLIB")^ else (echo 输入错误!Input Error!) echo. @@ -98,8 +79,8 @@ popd GOTO:EOF :cmakeParams -echo cmake -G "%~1" -A "%~2" -DOCR_OPENMP=%BUILD_OPENMP% -DOCR_JNI=%BUILD_JNI% -DOCR_CLIB=%BUILD_CLIB% -DOCR_STATIC=%BUILD_STATIC% -DOCR_VULKAN=%BUILD_NCNN_VULKAN% .. -cmake -G "%~1" -A "%~2" -DOCR_OPENMP=%BUILD_OPENMP% -DOCR_JNI=%BUILD_JNI% -DOCR_CLIB=%BUILD_CLIB% -DOCR_STATIC=%BUILD_STATIC% -DOCR_VULKAN=%BUILD_NCNN_VULKAN% .. +echo cmake -G "%~1" -A "%~2" -DOCR_OUTPUT=%BUILD_OUTPUT% -DOCR_VULKAN=%BUILD_NCNN_VULKAN% .. +cmake -G "%~1" -A "%~2" -DOCR_OUTPUT=%BUILD_OUTPUT% -DOCR_VULKAN=%BUILD_NCNN_VULKAN% .. GOTO:EOF @ENDLOCAL diff --git a/cpp_projects/OcrLiteNcnn/images/1.jpg b/cpp_projects/OcrLiteNcnn/images/1.jpg new file mode 100644 index 0000000..614ef20 Binary files /dev/null and b/cpp_projects/OcrLiteNcnn/images/1.jpg differ diff --git a/cpp_projects/OcrLiteNcnn/images/clear.cmd b/cpp_projects/OcrLiteNcnn/images/clear.cmd new file mode 100644 index 0000000..6b1005e --- /dev/null +++ b/cpp_projects/OcrLiteNcnn/images/clear.cmd @@ -0,0 +1,15 @@ +@ECHO OFF +echo Delete part img +DEL /Q *-part-*.jpg + +echo Delete angle img +DEL /Q *-angle-*.jpg + +echo Delete debug img +DEL /Q *-debug-*.jpg + +echo Delete result img +DEL /Q *-result.jpg + +echo Delete result txt +DEL /Q *-result.txt diff --git a/cpp_projects/OcrLiteNcnn/images/clear.sh b/cpp_projects/OcrLiteNcnn/images/clear.sh new file mode 100755 index 0000000..b56b6e2 --- /dev/null +++ b/cpp_projects/OcrLiteNcnn/images/clear.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +echo Delete part img +rm -f *-part-*.jpg + +echo Delete angle img +rm -f *-angle-*.jpg + +echo Delete debug img +rm -f *-debug-*.jpg + +echo Delete result img +rm -f *-result.jpg + +echo Delete result txt +rm -f *-result.txt diff --git a/cpp_projects/OcrLiteNcnn/include/AngleNet.h b/cpp_projects/OcrLiteNcnn/include/AngleNet.h index 883450d..7a34515 100644 --- a/cpp_projects/OcrLiteNcnn/include/AngleNet.h +++ b/cpp_projects/OcrLiteNcnn/include/AngleNet.h @@ -3,7 +3,7 @@ #include "OcrStruct.h" #include "ncnn/net.h" -#include +#include class AngleNet { public: diff --git a/cpp_projects/OcrLiteNcnn/include/CrnnNet.h b/cpp_projects/OcrLiteNcnn/include/CrnnNet.h index 7bb7d14..6a26913 100644 --- a/cpp_projects/OcrLiteNcnn/include/CrnnNet.h +++ b/cpp_projects/OcrLiteNcnn/include/CrnnNet.h @@ -3,7 +3,7 @@ #include "OcrStruct.h" #include "ncnn/net.h" -#include +#include class CrnnNet { public: diff --git a/cpp_projects/OcrLiteNcnn/include/DbNet.h b/cpp_projects/OcrLiteNcnn/include/DbNet.h index 3d584c1..c17ce2f 100644 --- a/cpp_projects/OcrLiteNcnn/include/DbNet.h +++ b/cpp_projects/OcrLiteNcnn/include/DbNet.h @@ -3,7 +3,7 @@ #include "OcrStruct.h" #include "ncnn/net.h" -#include +#include class DbNet { public: diff --git a/cpp_projects/OcrLiteNcnn/include/version.h b/cpp_projects/OcrLiteNcnn/include/version.h index e09f1a4..c97105e 100755 --- a/cpp_projects/OcrLiteNcnn/include/version.h +++ b/cpp_projects/OcrLiteNcnn/include/version.h @@ -1,6 +1,6 @@ #ifndef __OCR_VERSION_H__ #define __OCR_VERSION_H__ -#define VERSION "1.5.1.20210128" +#define VERSION "1.6.0.20211013" #endif //__OCR_VERSION_H__ diff --git a/cpp_projects/OcrLiteNcnn/run-benchmark.bat b/cpp_projects/OcrLiteNcnn/run-benchmark.bat index 3c545f1..507d274 100644 --- a/cpp_projects/OcrLiteNcnn/run-benchmark.bat +++ b/cpp_projects/OcrLiteNcnn/run-benchmark.bat @@ -5,19 +5,33 @@ chcp 65001 echo "Setting the Number of Threads=%NUMBER_OF_PROCESSORS% Using an OpenMP Environment Variable" set OMP_NUM_THREADS=%NUMBER_OF_PROCESSORS% +:MainExec +echo "Gpu版本测试前请先安装Vulkan SDK v1.2.162.0,https://vulkan.lunarg.com/sdk/home" +echo "请输入测试选项并回车: 1)CPU-x64, 2)CPU-x86, 3)GPU-x64, 4)GPU-x86" +set GPU_INDEX=-1 +set /p flag= +if %flag% == 1 (call :PrepareCpuX64)^ +else if %flag% == 2 (call :PrepareCpuX86)^ +else if %flag% == 3 (call :PrepareGpuX64)^ +else if %flag% == 4 (call :PrepareGpuX86)^ +else (echo 输入错误!Input Error!) + echo "请输入循环次数:" set /p LOOP_COUNT= -SET TARGET_IMG=../../images/1.jpg +SET TARGET_IMG=images/1.jpg if not exist %TARGET_IMG% ( echo "找不到待识别的目标图片:%TARGET_IMG%,请打开本文件并编辑TARGET_IMG" PAUSE exit ) -:: run Windows -build\benchmark.exe --version -build\benchmark.exe --models models ^ +if exist %EXE_PATH%\install\bin ( +SET EXE_PATH=%EXE_PATH%\install\bin +) + +%EXE_PATH%\benchmark.exe --version +%EXE_PATH%\benchmark.exe --models models ^ --det dbnet_op ^ --cls angle_op ^ --rec crnn_lite_op ^ @@ -25,12 +39,37 @@ build\benchmark.exe --models models ^ --image %TARGET_IMG% ^ --numThread %NUMBER_OF_PROCESSORS% ^ --padding 50 ^ ---maxSideLen 1024 ^ +--maxSideLen 0 ^ --boxScoreThresh 0.6 ^ --boxThresh 0.3 ^ --unClipRatio 2.0 ^ --doAngle 1 ^ --mostAngle 1 ^ ---loopCount %LOOP_COUNT% -G 0 -PAUSE +--GPU %GPU_INDEX% ^ +--loopCount %LOOP_COUNT% + +popd +echo. +GOTO:MainExec + +:PrepareCpuX64 +set EXE_PATH=win-BIN-CPU-x64 +set GPU_INDEX=-1 +GOTO:EOF + +:PrepareCpuX86 +set EXE_PATH=win-BIN-CPU-Win32 +set GPU_INDEX=-1 +GOTO:EOF + +:PrepareGpuX64 +set EXE_PATH=win-BIN-GPU-x64 +set GPU_INDEX=0 +GOTO:EOF + +:PrepareGpuX86 +set EXE_PATH=win-BIN-GPU-Win32 +set GPU_INDEX=0 +GOTO:EOF + @ENDLOCAL diff --git a/cpp_projects/OcrLiteNcnn/run-benchmark.sh b/cpp_projects/OcrLiteNcnn/run-benchmark.sh index c2b1a3f..7e4bed0 100755 --- a/cpp_projects/OcrLiteNcnn/run-benchmark.sh +++ b/cpp_projects/OcrLiteNcnn/run-benchmark.sh @@ -1,5 +1,20 @@ #!/usr/bin/env bash +function PrepareVar(){ +echo "Gpu版本测试前请先安装Vulkan SDK v1.2.162.0,https://vulkan.lunarg.com/sdk/home" +echo "请输入测试选项并回车: 1)CPU, 2)GPU" +read -p "" RUN_ARCH +if [ $RUN_ARCH == 1 ]; then + EXE_PATH=${sysOS}-BIN-CPU + GPU_INDEX=-1 +elif [ $RUN_ARCH == 2 ]; then + EXE_PATH=${sysOS}-BIN-GPU + GPU_INDEX=0 +else + echo -e "输入错误!Input Error!" +fi +} + sysOS=`uname -s` NUM_THREADS=1 if [ $sysOS == "Darwin" ];then @@ -15,18 +30,19 @@ fi echo "Setting the Number of Threads=$NUM_THREADS Using an OpenMP Environment Variable" set OMP_NUM_THREADS=$NUM_THREADS -echo "请输入循环次数:" +PrepareVar + +echo "请输入循环次数" read -p "" LOOP_COUNT -TARGET_IMG=../images/1.jpg +TARGET_IMG=images/1.jpg if [ ! -f "$TARGET_IMG" ]; then echo "找不到待识别的目标图片:${TARGET_IMG},请打开本文件并编辑TARGET_IMG" exit fi -##### run test on MacOS or Linux -./build/benchmark --version -./build/benchmark --models models \ +./${EXE_PATH}/benchmark --version +./${EXE_PATH}/benchmark --models models \ --det dbnet_op \ --cls angle_op \ --rec crnn_lite_op \ @@ -34,10 +50,11 @@ fi --image $TARGET_IMG \ --numThread $NUM_THREADS \ --padding 50 \ ---maxSideLen 1024 \ +--maxSideLen 0 \ --boxScoreThresh 0.6 \ --boxThresh 0.3 \ --unClipRatio 2.0 \ --doAngle 1 \ --mostAngle 1 \ ---loopCount $LOOP_COUNT -G 0 \ No newline at end of file +--GPU $GPU_INDEX \ +--loopCount $LOOP_COUNT \ No newline at end of file diff --git a/cpp_projects/OcrLiteNcnn/run-test.bat b/cpp_projects/OcrLiteNcnn/run-test.bat old mode 100755 new mode 100644 index ac7d5d4..ba9f1f6 --- a/cpp_projects/OcrLiteNcnn/run-test.bat +++ b/cpp_projects/OcrLiteNcnn/run-test.bat @@ -5,16 +5,30 @@ chcp 65001 echo "Setting the Number of Threads=%NUMBER_OF_PROCESSORS% Using an OpenMP Environment Variable" set OMP_NUM_THREADS=%NUMBER_OF_PROCESSORS% -SET TARGET_IMG=../../images/1.jpg +:MainExec +echo "Gpu版本测试前请先安装Vulkan SDK v1.2.162.0,https://vulkan.lunarg.com/sdk/home" +echo "请输入测试选项并回车: 1)CPU-x64, 2)CPU-x86, 3)GPU-x64, 4)GPU-x86" +set GPU_INDEX=-1 +set /p flag= +if %flag% == 1 (call :PrepareCpuX64)^ +else if %flag% == 2 (call :PrepareCpuX86)^ +else if %flag% == 3 (call :PrepareGpuX64)^ +else if %flag% == 4 (call :PrepareGpuX86)^ +else (echo 输入错误!Input Error!) + +SET TARGET_IMG=images/1.jpg if not exist %TARGET_IMG% ( echo "找不到待识别的目标图片:%TARGET_IMG%,请打开本文件并编辑TARGET_IMG" PAUSE exit ) -:: run Windows -build\OcrLiteNcnn.exe --version -build\OcrLiteNcnn.exe --models models ^ +if exist %EXE_PATH%\install\bin ( +SET EXE_PATH=%EXE_PATH%\install\bin +) + +%EXE_PATH%\OcrLiteNcnn.exe --version +%EXE_PATH%\OcrLiteNcnn.exe --models models ^ --det dbnet_op ^ --cls angle_op ^ --rec crnn_lite_op ^ @@ -22,11 +36,35 @@ build\OcrLiteNcnn.exe --models models ^ --image %TARGET_IMG% ^ --numThread %NUMBER_OF_PROCESSORS% ^ --padding 50 ^ ---maxSideLen 1024 ^ +--maxSideLen 0 ^ --boxScoreThresh 0.6 ^ --boxThresh 0.3 ^ --unClipRatio 2.0 ^ --doAngle 1 ^ ---mostAngle 1 -G 0 -PAUSE +--mostAngle 1 ^ +--GPU %GPU_INDEX% + +echo. +GOTO:MainExec + +:PrepareCpuX64 +set EXE_PATH=win-BIN-CPU-x64 +set GPU_INDEX=-1 +GOTO:EOF + +:PrepareCpuX86 +set EXE_PATH=win-BIN-CPU-Win32 +set GPU_INDEX=-1 +GOTO:EOF + +:PrepareGpuX64 +set EXE_PATH=win-BIN-GPU-x64 +set GPU_INDEX=0 +GOTO:EOF + +:PrepareGpuX86 +set EXE_PATH=win-BIN-GPU-Win32 +set GPU_INDEX=0 +GOTO:EOF + @ENDLOCAL diff --git a/cpp_projects/OcrLiteNcnn/run-test.sh b/cpp_projects/OcrLiteNcnn/run-test.sh index 6db9b84..883e7e4 100755 --- a/cpp_projects/OcrLiteNcnn/run-test.sh +++ b/cpp_projects/OcrLiteNcnn/run-test.sh @@ -1,5 +1,20 @@ #!/usr/bin/env bash +function PrepareVar(){ +echo "Gpu版本测试前请先安装Vulkan SDK v1.2.162.0,https://vulkan.lunarg.com/sdk/home" +echo "请输入测试选项并回车: 1)CPU, 2)GPU" +read -p "" RUN_ARCH +if [ $RUN_ARCH == 1 ]; then + EXE_PATH=${sysOS}-BIN-CPU + GPU_INDEX=-1 +elif [ $RUN_ARCH == 2 ]; then + EXE_PATH=${sysOS}-BIN-GPU + GPU_INDEX=0 +else + echo -e "输入错误!Input Error!" +fi +} + sysOS=`uname -s` NUM_THREADS=1 if [ $sysOS == "Darwin" ];then @@ -15,15 +30,17 @@ fi echo "Setting the Number of Threads=$NUM_THREADS Using an OpenMP Environment Variable" set OMP_NUM_THREADS=$NUM_THREADS -TARGET_IMG=../images/1.jpg +PrepareVar + +TARGET_IMG=images/1.jpg if [ ! -f "$TARGET_IMG" ]; then echo "找不到待识别的目标图片:${TARGET_IMG},请打开本文件并编辑TARGET_IMG" exit fi ##### run test on MacOS or Linux -./build/OcrLiteNcnn --version -./build/OcrLiteNcnn --models models \ +./${EXE_PATH}/OcrLiteNcnn --version +./${EXE_PATH}/OcrLiteNcnn --models models \ --det dbnet_op \ --cls angle_op \ --rec crnn_lite_op \ @@ -31,9 +48,10 @@ fi --image $TARGET_IMG \ --numThread $NUM_THREADS \ --padding 50 \ ---maxSideLen 1024 \ +--maxSideLen 0 \ --boxScoreThresh 0.6 \ --boxThresh 0.3 \ --unClipRatio 2.0 \ --doAngle 1 \ ---mostAngle 1 -G 0 \ No newline at end of file +--mostAngle 1 \ +--GPU $GPU_INDEX diff --git a/cpp_projects/OcrLiteNcnn/src/AngleNet.cpp b/cpp_projects/OcrLiteNcnn/src/AngleNet.cpp index 0a18bd7..31f2c3e 100644 --- a/cpp_projects/OcrLiteNcnn/src/AngleNet.cpp +++ b/cpp_projects/OcrLiteNcnn/src/AngleNet.cpp @@ -67,9 +67,6 @@ std::vector AngleNet::getAngles(std::vector &partImgs, const cha int size = partImgs.size(); std::vector angles(size); if (doAngle) { -#ifdef __OPENMP__ -#pragma omp parallel for num_threads(numThread) -#endif for (int i = 0; i < size; ++i) { double startAngle = getCurrentTime(); auto angleImg = adjustTargetImg(partImgs[i], dstWidth, dstHeight); diff --git a/cpp_projects/OcrLiteNcnn/src/CrnnNet.cpp b/cpp_projects/OcrLiteNcnn/src/CrnnNet.cpp index 0c800f8..e52a79c 100644 --- a/cpp_projects/OcrLiteNcnn/src/CrnnNet.cpp +++ b/cpp_projects/OcrLiteNcnn/src/CrnnNet.cpp @@ -112,9 +112,6 @@ TextLine CrnnNet::getTextLine(const cv::Mat &src) { std::vector CrnnNet::getTextLines(std::vector &partImg, const char *path, const char *imgName) { int size = partImg.size(); std::vector textLines(size); -#ifdef __OPENMP__ -#pragma omp parallel for num_threads(numThread) -#endif for (int i = 0; i < size; ++i) { //OutPut DebugImg if (isOutputDebugImg) {