Skip to content

Commit

Permalink
Merge pull request DayBreak-u#370 from MistEO/onnx_c_dll
Browse files Browse the repository at this point in the history
新增编译C++动态库的选项,并添加C++导出接口
  • Loading branch information
DayBreak-u authored Aug 25, 2021
2 parents 7ad8354 + 57b6d96 commit e28632e
Show file tree
Hide file tree
Showing 20 changed files with 333 additions and 25 deletions.
8 changes: 6 additions & 2 deletions cpp_projects/OcrLiteNcnn/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,14 @@ if (OCR_STATIC)
endif ()
endif ()

if (OCR_LIB)
if (OCR_LIB) # 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
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 ()
add_executable(OcrLiteNcnn ${OCR_COMPILE_CODE})
target_link_libraries(OcrLiteNcnn ncnn ${OpenCV_LIBS} ${OpenMP_CXX_LIB_NAMES})
Expand All @@ -122,7 +126,7 @@ if (OCR_VULKAN)
endif ()

# benchmark
if (OCR_BENCHMARK AND NOT OCR_LIB)
if (OCR_BENCHMARK AND NOT OCR_LIB AND NOT OCR_CLIB)
add_executable(benchmark benchmark/benchmark.cpp
src/AngleNet.cpp
src/clipper.cpp
Expand Down
18 changes: 10 additions & 8 deletions cpp_projects/OcrLiteNcnn/generate-vs-project.bat
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ echo "========请先参考README.md准备好编译环境========"
echo.

echo "========编译选项========"
echo "请注意:项目默认使用Release库,除非您自行编译Debug版的Onnxruntime和Opencv,否则请不要选择Debug编译"
echo "请注意:项目默认使用Release库,除非您自行编译Debug版的Ncnn和Opencv,否则请不要选择Debug编译"
echo "请输入编译选项并回车: 1)Release, 2)Debug"
set BUILD_TYPE=Release
set /p flag=
Expand All @@ -25,7 +25,7 @@ echo.

echo "使用静态库时,编译出来的可执行文件较大,但部署起来比较方便。"
echo "使用动态库时,编译出来的可执行文件较小,但部署的时候记得把dll复制到可执行文件目录"
echo "请选择要使用的OnnxRuntime和Opencv库选项并回车: 1)Static静态库,2)Shared动态库"
echo "请选择要使用的Opencv库选项并回车: 1)Static静态库,2)Shared动态库"
set BUILD_STATIC=ON
set /p flag=
if %flag% == 1 (set BUILD_STATIC=ON)^
Expand All @@ -42,11 +42,13 @@ else (echo "输入错误!Input Error!")
echo.

echo "请注意:如果选择2)编译为JNI动态库时,必须安装配置Oracle JDK"
echo "请选择编译输出类型并回车: 1)编译成可执行文件,2)编译成JNI动态库"
set BUILD_LIB=OFF
echo "请选择编译输出类型并回车: 1)编译成可执行文件,2)编译成JNI动态库,3)编译成C层动态库"
set BUILD_JNI=OFF
set BUILD_CLIB=OFF
set /p flag=
if %flag% == 1 (set BUILD_LIB=OFF)^
else if %flag% == 2 (set BUILD_LIB=ON)^
if %flag% == 1 (set BUILD_JNI=OFF)^
else if %flag% == 2 (set BUILD_JNI=ON)^
else if %flag% == 3 (set BUILD_CLIB=ON)^
else (echo 输入错误!Input Error!)
echo.

Expand Down Expand Up @@ -96,8 +98,8 @@ popd
GOTO:EOF

:cmakeParams
echo cmake -G "%~1" -A "%~2" -DOCR_OPENMP=%BUILD_OPENMP% -DOCR_LIB=%BUILD_LIB% -DOCR_STATIC=%BUILD_STATIC% -DOCR_VULKAN=%BUILD_NCNN_VULKAN% ..
cmake -G "%~1" -A "%~2" -DOCR_OPENMP=%BUILD_OPENMP% -DOCR_LIB=%BUILD_LIB% -DOCR_STATIC=%BUILD_STATIC% -DOCR_VULKAN=%BUILD_NCNN_VULKAN% ..
echo cmake -G "%~1" -A "%~2" -DOCR_OPENMP=%BUILD_OPENMP% -DOCR_LIB=%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_LIB=%BUILD_JNI% -DOCR_CLIB=%BUILD_CLIB% -DOCR_STATIC=%BUILD_STATIC% -DOCR_VULKAN=%BUILD_NCNN_VULKAN% ..
GOTO:EOF

@ENDLOCAL
5 changes: 4 additions & 1 deletion cpp_projects/OcrLiteNcnn/include/OcrLite.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ class OcrLite {
OcrResult detect(const char *path, const char *imgName,
int padding, int maxSideLen,
float boxScoreThresh, float boxThresh, float unClipRatio, bool doAngle, bool mostAngle);


OcrResult detect(const cv::Mat& mat,
int padding, int maxSideLen,
float boxScoreThresh, float boxThresh, float unClipRatio, bool doAngle, bool mostAngle);
private:
bool isOutputConsole = false;
bool isOutputPartImg = false;
Expand Down
40 changes: 40 additions & 0 deletions cpp_projects/OcrLiteNcnn/include/OcrLiteCaller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <memory>
#include <string>

#include "OcrLitePort.h"
#include "OcrStruct.h"

namespace cv
{
class Mat;
}
class OcrLite;

class OCRLITE_PORT OcrLiteCaller
{
public:
OcrLiteCaller();
~OcrLiteCaller() = default;
OcrLiteCaller(const OcrLite&) = delete;
OcrLiteCaller(OcrLite&&) = delete;

void setNumThread(int numOfThread);
void setGpuIndex(int gpuIndex);
void initModels(const std::string& detPath, const std::string& clsPath,
const std::string& recPath, const std::string& keysPath);

OcrResult detect(const cv::Mat& mat,
int padding, int maxSideLen,
float boxScoreThresh, float boxThresh, float unClipRatio, bool doAngle, bool mostAngle);

OcrResult detect(const std::string& dir, const std::string& file,
int padding, int maxSideLen,
float boxScoreThresh, float boxThresh, float unClipRatio, bool doAngle, bool mostAngle);

OcrLiteCaller& operator=(const OcrLiteCaller&) = delete;
OcrLiteCaller& operator=(OcrLiteCaller&&) = delete;
private:
std::shared_ptr<OcrLite> m_ocrlite_ptr;
};
37 changes: 37 additions & 0 deletions cpp_projects/OcrLiteNcnn/include/OcrLitePort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#pragma once

// The way how the function is called
#if !defined(OCRLITE_CALL)
#if defined(_WIN32)
#define OCRLITE_CALL __stdcall
#else
#define OCRLITE_CALL
#endif /* _WIN32 */
#endif /* ISSCALL */

#if defined _WIN32 || defined __CYGWIN__
#define OCRLITE_EXPORT __declspec(dllexport)
#define OCRLITE_IMPORT __declspec(dllimport)
#define OCRLITE_LOCAL
#else // ! defined _WIN32 || defined __CYGWIN__
#if __GNUC__ >= 4
#define OCRLITE_EXPORT __attribute__ ((visibility ("default")))
#define OCRLITE_IMPORT __attribute__ ((visibility ("default")))
#define OCRLITE_LOCAL __attribute__ ((visibility ("hidden")))
#else // ! __GNUC__ >= 4
#define OCRLITE_EXPORT
#define OCRLITE_IMPORT
#endif // End __GNUC__ >= 4
#endif // End defined _WIN32 || defined __CYGWIN__

#ifdef __CLIB__
#define OCRLITE_PORT OCRLITE_EXPORT
#else
#define OCRLITE_PORT OCRLITE_IMPORT
#endif // OCRLITE_PORT

#define OCR_API OCRLITE_PORT OCRLITE_CALL

#define OCR_LOCAL OCRLITE_LOCAL OCRLITE_CALL
6 changes: 4 additions & 2 deletions cpp_projects/OcrLiteNcnn/include/OcrStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "opencv2/core.hpp"
#include <vector>

#include "OcrLitePort.h"

struct ScaleParam {
int srcWidth;
int srcHeight;
Expand All @@ -30,7 +32,7 @@ struct TextLine {
double time;
};

struct TextBlock {
struct OCRLITE_PORT TextBlock {
std::vector<cv::Point> boxPoint;
float boxScore;
int angleIndex;
Expand All @@ -42,7 +44,7 @@ struct TextBlock {
double blockTime;
};

struct OcrResult {
struct OCRLITE_PORT OcrResult {
double dbNetTime;
std::vector<TextBlock> textBlocks;
cv::Mat boxImg;
Expand Down
22 changes: 22 additions & 0 deletions cpp_projects/OcrLiteNcnn/src/OcrLite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ OcrResult OcrLite::detect(const char *path, const char *imgName,
return result;
}

OcrResult OcrLite::detect(const cv::Mat& mat, int padding, int maxSideLen, float boxScoreThresh, float boxThresh, float unClipRatio, bool doAngle, bool mostAngle)
{
cv::Mat originSrc;
cvtColor(mat, originSrc, cv::COLOR_BGR2RGB);// convert to RGB
int originMaxSide = (std::max)(originSrc.cols, originSrc.rows);
int resize;
if (maxSideLen <= 0 || maxSideLen > originMaxSide) {
resize = originMaxSide;
}
else {
resize = maxSideLen;
}
resize += 2 * padding;
cv::Rect paddingRect(padding, padding, originSrc.cols, originSrc.rows);
cv::Mat paddingSrc = makePadding(originSrc, padding);
ScaleParam scale = getScaleParam(paddingSrc, resize);
OcrResult result;
result = detect(NULL, NULL, paddingSrc, paddingRect, scale,
boxScoreThresh, boxThresh, unClipRatio, doAngle, mostAngle);
return result;
}

std::vector<cv::Mat> OcrLite::getPartImages(cv::Mat &src, std::vector<TextBox> &textBoxes,
const char *path, const char *imgName) {
std::vector<cv::Mat> partImages;
Expand Down
38 changes: 38 additions & 0 deletions cpp_projects/OcrLiteNcnn/src/OcrLiteCaller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifdef __CLIB__

#include "OcrLiteCaller.h"

#include "OcrLite.h"

OcrLiteCaller::OcrLiteCaller()
: m_ocrlite_ptr(std::make_shared<OcrLite>())
{
;
}

void OcrLiteCaller::setNumThread(int numOfThread)
{
m_ocrlite_ptr->setNumThread(numOfThread);
}

void OcrLiteCaller::setGpuIndex(int gpuIndex)
{
m_ocrlite_ptr->setGpuIndex(gpuIndex);
}

void OcrLiteCaller::initModels(const std::string& detPath, const std::string& clsPath, const std::string& recPath, const std::string& keysPath)
{
m_ocrlite_ptr->initModels(detPath, clsPath, recPath, keysPath);
}

OcrResult OcrLiteCaller::detect(const cv::Mat& mat, int padding, int maxSideLen, float boxScoreThresh, float boxThresh, float unClipRatio, bool doAngle, bool mostAngle)
{
return m_ocrlite_ptr->detect(mat, padding, maxSideLen, boxScoreThresh, boxThresh, unClipRatio, doAngle, mostAngle);
}

OcrResult OcrLiteCaller::detect(const std::string& dir, const std::string& file, int padding, int maxSideLen, float boxScoreThresh, float boxThresh, float unClipRatio, bool doAngle, bool mostAngle)
{
return m_ocrlite_ptr->detect(dir.c_str(), file.c_str(), padding, maxSideLen, boxScoreThresh, boxThresh, unClipRatio, doAngle, mostAngle);
}

#endif
4 changes: 4 additions & 0 deletions cpp_projects/OcrLiteNcnn/src/getopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Released under the MIT license
* https://github.com/takamin/win-c/blob/master/LICENSE
*/
#ifndef __CLIB__

#include <stdio.h>
#include <string.h>
#include "getopt.h"
Expand Down Expand Up @@ -220,3 +222,5 @@ int getopt_long_only(int argc, char* const argv[],
return -1;
}
********************************************************/

#endif // __CLIB__
3 changes: 2 additions & 1 deletion cpp_projects/OcrLiteNcnn/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef __JNI__

#ifndef __CLIB__
#include <cstdio>
#include "main.h"
#include "version.h"
Expand Down Expand Up @@ -202,4 +202,5 @@ int main(int argc, char **argv) {
return 0;
}

#endif
#endif
10 changes: 8 additions & 2 deletions cpp_projects/OcrLiteOnnx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ endif ()
# OnnxRuntime
if (OCR_STATIC)
set(OnnxRuntime_DIR "${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime-static")
elseif (ONNXRUNTIME_STATIC)
set(OnnxRuntime_DIR "${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime-static")
else ()
set(OnnxRuntime_DIR "${CMAKE_CURRENT_SOURCE_DIR}/onnxruntime-shared")
endif ()
Expand Down Expand Up @@ -103,10 +105,14 @@ if (OCR_STATIC)
endif ()
endif ()

if (OCR_LIB)
if (OCR_LIB) # JNI
add_library(OcrLiteOnnx SHARED ${OCR_COMPILE_CODE})
target_compile_definitions(OcrLiteOnnx PRIVATE __JNI__)
target_link_libraries(OcrLiteOnnx ${OnnxRuntime_LIBS} ${OpenCV_LIBS} ${JNI_LIBS} ${OpenMP_CXX_LIB_NAMES})
elseif(OCR_CLIB) # C LIB
add_library(OcrLiteOnnx SHARED ${OCR_COMPILE_CODE})
target_compile_definitions(OcrLiteOnnx PRIVATE __CLIB__)
target_link_libraries(OcrLiteOnnx ${OnnxRuntime_LIBS} ${OpenCV_LIBS} ${OpenMP_CXX_LIB_NAMES})
else ()
add_executable(OcrLiteOnnx ${OCR_COMPILE_CODE})
target_link_libraries(OcrLiteOnnx ${OnnxRuntime_LIBS} ${OpenCV_LIBS} ${OpenMP_CXX_LIB_NAMES})
Expand All @@ -117,7 +123,7 @@ if (OCR_OPENMP)
endif ()

# benchmark
if (OCR_BENCHMARK AND NOT OCR_LIB)
if (OCR_BENCHMARK AND NOT OCR_LIB AND NOT OCR_CLIB)
add_executable(benchmark benchmark/benchmark.cpp
src/AngleNet.cpp
src/clipper.cpp
Expand Down
19 changes: 12 additions & 7 deletions cpp_projects/OcrLiteOnnx/generate-vs-project.bat
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,25 @@ echo.

echo "使用静态库时,编译出来的可执行文件较大,但部署起来比较方便。"
echo "使用动态库时,编译出来的可执行文件较小,但部署的时候记得把dll复制到可执行文件目录"
echo "请选择要使用的OnnxRuntime和Opencv库选项并回车: 1)Static静态库,2)Shared动态库"
echo "请选择要使用的OnnxRuntime和Opencv库选项并回车: 1)Static静态库,2)Shared动态库,3)onnxruntime静态,opencv动态"
set BUILD_STATIC=ON
set BUILD_ONNXRUNTIME_STATIC=OFF
set /p flag=
if %flag% == 1 (set BUILD_STATIC=ON)^
else if %flag% == 2 (set BUILD_STATIC=OFF)^
else if %flag% == 3 (set BUILD_STATIC=OFF
set BUILD_ONNXRUNTIME_STATIC=ON)^
else (echo "输入错误!Input Error!")
echo.

echo "请注意:如果选择2)编译为JNI动态库时,必须安装配置Oracle JDK"
echo "请选择编译输出类型并回车: 1)编译成可执行文件,2)编译成JNI动态库"
set BUILD_LIB=OFF
echo "请选择编译输出类型并回车: 1)编译成可执行文件,2)编译成JNI动态库,3)编译成C层动态库"
set BUILD_JNI=OFF
set BUILD_CLIB=OFF
set /p flag=
if %flag% == 1 (set BUILD_LIB=OFF)^
else if %flag% == 2 (set BUILD_LIB=ON)^
if %flag% == 1 (set BUILD_JNI=OFF)^
else if %flag% == 2 (set BUILD_JNI=ON)^
else if %flag% == 3 (set BUILD_CLIB=ON)^
else (echo 输入错误!Input Error!)
echo.

Expand Down Expand Up @@ -88,8 +93,8 @@ popd
GOTO:EOF

:cmakeParams
echo cmake -G "%~1" -A "%~2" -DOCR_OPENMP=%BUILD_OPENMP% -DOCR_LIB=%BUILD_LIB% -DOCR_STATIC=%BUILD_STATIC% ..
cmake -G "%~1" -A "%~2" -DOCR_OPENMP=%BUILD_OPENMP% -DOCR_LIB=%BUILD_LIB% -DOCR_STATIC=%BUILD_STATIC% ..
echo cmake -G "%~1" -A "%~2" -DOCR_OPENMP=%BUILD_OPENMP% -DOCR_LIB=%BUILD_JNI% -DOCR_CLIB=%BUILD_CLIB% -DOCR_STATIC=%BUILD_STATIC% -DONNXRUNTIME_STATIC=%BUILD_ONNXRUNTIME_STATIC% ..
cmake -G "%~1" -A "%~2" -DOCR_OPENMP=%BUILD_OPENMP% -DOCR_LIB=%BUILD_JNI% -DOCR_CLIB=%BUILD_CLIB% -DOCR_STATIC=%BUILD_STATIC% -DONNXRUNTIME_STATIC=%BUILD_ONNXRUNTIME_STATIC% ..
GOTO:EOF

@ENDLOCAL
5 changes: 5 additions & 0 deletions cpp_projects/OcrLiteOnnx/include/OcrLite.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class OcrLite {
int padding, int maxSideLen,
float boxScoreThresh, float boxThresh, float unClipRatio, bool doAngle, bool mostAngle);


OcrResult detect(const cv::Mat& mat,
int padding, int maxSideLen,
float boxScoreThresh, float boxThresh, float unClipRatio, bool doAngle, bool mostAngle);

private:
bool isOutputConsole = false;
bool isOutputPartImg = false;
Expand Down
Loading

0 comments on commit e28632e

Please sign in to comment.