ToB企服应用市场:ToB评测及商务社交产业平台

标题: 在 C++ 项目中,通过源码利用 PaddlePaddle 实现 OCR 功能 [打印本页]

作者: 麻花痒    时间: 2024-5-12 14:54
标题: 在 C++ 项目中,通过源码利用 PaddlePaddle 实现 OCR 功能
My-PaddleOCR

介绍

怎样在 C++ 项目中,通过源码利用 PaddlePaddle 实现 OCR 功能。
本项目的所有源码:gitee: paddleocr
如今,官方提供利用 PaddleOcr 的方案有:
其它方法:
这里重要介绍一个更加直接的方法:
Paddle OCR C++ 源码

Paddle OCR 的堆栈,在github: PaddleOCR 大概 gitee: PaddleOCR
C++ 相关的代码在目次 deploy/cpp_infer 里。
怎样引入 Paddle OCR C++ 源码

必要安装的组件

  1. @REM 设置opencv目录环境变量
  2. setx OPENCV_ROOT "C:\3rd\opencv4.6\build"
复制代码
  1. @REM 设置paddle_inference目录环境变量
  2. setx PADDLE_ROOT "C:\3rd\paddle_inference"
复制代码
创建 C++项目

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  3.   <ImportGroup Label="PropertySheets" />
  4.   <PropertyGroup Label="UserMacros" />
  5.   <PropertyGroup />
  6.   <ItemDefinitionGroup>
  7.     <ClCompile>
  8.       <AdditionalIncludeDirectories>$(OPENCV_ROOT)\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
  9.     </ClCompile>
  10.     <Link>
  11.       <AdditionalLibraryDirectories>$(OPENCV_ROOT)\x64\vc15\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
  12.       <AdditionalDependencies>opencv_world460d.lib;%(AdditionalDependencies)</AdditionalDependencies>
  13.     </Link>
  14.   </ItemDefinitionGroup>
  15.   <ItemGroup />
  16. </Project>
复制代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  3.   <ImportGroup Label="PropertySheets" />
  4.   <PropertyGroup Label="UserMacros" />
  5.   <PropertyGroup />
  6.   <ItemDefinitionGroup>
  7.     <ClCompile>
  8.       <AdditionalIncludeDirectories>$(OPENCV_ROOT)\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
  9.     </ClCompile>
  10.     <Link>
  11.       <AdditionalLibraryDirectories>$(OPENCV_ROOT)\x64\vc15\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
  12.       <AdditionalDependencies>opencv_world460.lib;%(AdditionalDependencies)</AdditionalDependencies>
  13.     </Link>
  14.   </ItemDefinitionGroup>
  15.   <ItemGroup />
  16. </Project>
复制代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  3.   <ImportGroup Label="PropertySheets" />
  4.   <PropertyGroup Label="UserMacros" />
  5.   <PropertyGroup />
  6.   <ItemDefinitionGroup>
  7.     <ClCompile>
  8.       <AdditionalIncludeDirectories>./;$(PADDLE_ROOT)/paddle/include;$(PADDLE_ROOT)/third_party/install/zlib/include;$(PADDLE_ROOT)/third_party/boost;$(PADDLE_ROOT)/third_party/eigen3;$(PADDLE_ROOT)/third_party/install/mklml/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
  9.     </ClCompile>
  10.     <Link>
  11.       <AdditionalLibraryDirectories>$(PADDLE_ROOT)/third_party/install/zlib/lib;$(PADDLE_ROOT)/third_party/install/mklml/lib;$(PADDLE_ROOT)/paddle/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
  12.       <AdditionalDependencies>paddle_inference.lib;mklml.lib;libiomp5md.lib;%(AdditionalDependencies)</AdditionalDependencies>
  13.     </Link>
  14.   </ItemDefinitionGroup>
  15.   <ItemGroup />
  16. </Project>
复制代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  3.   <ImportGroup Label="PropertySheets" />
  4.   <PropertyGroup Label="UserMacros" />
  5.   <PropertyGroup />
  6.   <ItemDefinitionGroup>
  7.     <ClCompile>
  8.       <AdditionalIncludeDirectories>./;$(PADDLE_ROOT)/paddle/include;$(PADDLE_ROOT)/third_party/install/zlib/include;$(PADDLE_ROOT)/third_party/boost;$(PADDLE_ROOT)/third_party/eigen3;$(PADDLE_ROOT)/third_party/install/mklml/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
  9.     </ClCompile>
  10.     <Link>
  11.       <AdditionalLibraryDirectories>$(PADDLE_ROOT)/third_party/install/zlib/lib;$(PADDLE_ROOT)/third_party/install/mklml/lib;$(PADDLE_ROOT)/paddle/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
  12.       <AdditionalDependencies>paddle_inference.lib;mklml.lib;libiomp5md.lib;%(AdditionalDependencies)</AdditionalDependencies>
  13.     </Link>
  14.   </ItemDefinitionGroup>
  15.   <ItemGroup />
  16. </Project>
复制代码
  1.   <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
  2.     <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  3.     <Import Project=".\OpenCVDebug.props" />
  4.     <Import Project=".\PaddleDebug.props" />
  5.   </ImportGroup>
  6.   <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
  7.     <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  8.     <Import Project=".\OpenCVRelease.props" />
  9.     <Import Project=".\PaddleRelease.props" />
  10.   </ImportGroup>
  11.   <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
  12.     <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  13.     <Import Project=".\OpenCVDebug.props" />
  14.     <Import Project=".\PaddleDebug.props" />
  15.   </ImportGroup>
  16.   <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
  17.     <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  18.     <Import Project=".\OpenCVRelease.props" />
  19.     <Import Project=".\PaddleRelease.props" />
  20.   </ImportGroup>
复制代码
引入 Paddle OCR C++ 源码

  1. git clone https://gitee.com/paddlepaddle/PaddleOCR.git
  2. cd PaddleOCR
  3. git checkout release/2.6
复制代码
这个文件是为了替换 google 的 gflags 库的利用。
  1. #pragma once
  2. // common args
  3. #include <string>
  4. using std::string;
  5. #define DECLARE_bool(name) extern bool FLAGS_##name;
  6. #define DECLARE_int32(name) extern int FLAGS_##name;
  7. #define DECLARE_string(name) extern string FLAGS_##name;
  8. #define DECLARE_double(name) extern double FLAGS_##name;
  9. #define DEFINE_VARIABLE(type, name, value, help)                               \
  10.     static const type FLAGS_nono##name = value;                                \
  11.     type FLAGS_##name = FLAGS_nono##name;                                      \
  12.     static type FLAGS_no##name = FLAGS_nono##name;
  13. #define DEFINE_bool(name, val, txt) DEFINE_VARIABLE(bool, name, val, txt)
  14. #define DEFINE_int32(name, val, txt) DEFINE_VARIABLE(int, name, val, txt)
  15. #define DEFINE_string(name, val, txt) DEFINE_VARIABLE(string, name, val, txt)
  16. #define DEFINE_double(name, val, txt) DEFINE_VARIABLE(double, name, val, txt)
复制代码
不利用 gflags/gflags.h
表明行 // #include
增长行 #include "ocr_flags.h"
  1. ...
  2. // #include <gflags/gflags.h>
  3. #include "ocr_flags.h"
  4. ...
复制代码
不利用 gflags/gflags.h
表明行 // #include
增长行 #include "include/ocr_flags.h"
  1. ...
  2. // #include <gflags/gflags.h>
  3. #include "include/ocr_flags.h"
  4. ...
复制代码
不利用 auto_log/autolog.h
表明行 // #include "auto_log/autolog.h"
表明方法 PPOCR::benchmark_log 的内容。
  1. // #include "auto_log/autolog.h"
  2. ...
  3. void PPOCR::benchmark_log(int img_num) {
  4.   // if (this->time_info_det[0] + this->time_info_det[1] +
  5.   // this->time_info_det[2] >
  6.   //     0) {
  7.   //   AutoLogger autolog_det("ocr_det", FLAGS_use_gpu, FLAGS_use_tensorrt,
  8.   //                          FLAGS_enable_mkldnn, FLAGS_cpu_threads, 1,
  9.   //                          "dynamic", FLAGS_precision, this->time_info_det,
  10.   //                          img_num);
  11.   //   autolog_det.report();
  12.   // }
  13.   // if (this->time_info_rec[0] + this->time_info_rec[1] +
  14.   // this->time_info_rec[2] >
  15.   //     0) {
  16.   //   AutoLogger autolog_rec("ocr_rec", FLAGS_use_gpu, FLAGS_use_tensorrt,
  17.   //                          FLAGS_enable_mkldnn, FLAGS_cpu_threads,
  18.   //                          FLAGS_rec_batch_num, "dynamic", FLAGS_precision,
  19.   //                          this->time_info_rec, img_num);
  20.   //   autolog_rec.report();
  21.   // }
  22.   // if (this->time_info_cls[0] + this->time_info_cls[1] +
  23.   // this->time_info_cls[2] >
  24.   //     0) {
  25.   //   AutoLogger autolog_cls("ocr_cls", FLAGS_use_gpu, FLAGS_use_tensorrt,
  26.   //                          FLAGS_enable_mkldnn, FLAGS_cpu_threads,
  27.   //                          FLAGS_cls_batch_num, "dynamic", FLAGS_precision,
  28.   //                          this->time_info_cls, img_num);
  29.   //   autolog_cls.report();
  30.   // }
  31. }
  32. ...
复制代码
不利用 auto_log/autolog.h
表明行 // #include "auto_log/autolog.h"
表明方法 PPOCR::benchmark_log 的内容。
  1. // #include "auto_log/autolog.h"
  2. ...
  3. void PaddleStructure::benchmark_log(int img_num) {
  4.   // if (this->time_info_det[0] + this->time_info_det[1] +
  5.   // this->time_info_det[2] >
  6.   //     0) {
  7.   //   AutoLogger autolog_det("ocr_det", FLAGS_use_gpu, FLAGS_use_tensorrt,
  8.   //                          FLAGS_enable_mkldnn, FLAGS_cpu_threads, 1,
  9.   //                          "dynamic", FLAGS_precision, this->time_info_det,
  10.   //                          img_num);
  11.   //   autolog_det.report();
  12.   // }
  13.   // if (this->time_info_rec[0] + this->time_info_rec[1] +
  14.   // this->time_info_rec[2] >
  15.   //     0) {
  16.   //   AutoLogger autolog_rec("ocr_rec", FLAGS_use_gpu, FLAGS_use_tensorrt,
  17.   //                          FLAGS_enable_mkldnn, FLAGS_cpu_threads,
  18.   //                          FLAGS_rec_batch_num, "dynamic", FLAGS_precision,
  19.   //                          this->time_info_rec, img_num);
  20.   //   autolog_rec.report();
  21.   // }
  22.   // if (this->time_info_cls[0] + this->time_info_cls[1] +
  23.   // this->time_info_cls[2] >
  24.   //     0) {
  25.   //   AutoLogger autolog_cls("ocr_cls", FLAGS_use_gpu, FLAGS_use_tensorrt,
  26.   //                          FLAGS_enable_mkldnn, FLAGS_cpu_threads,
  27.   //                          FLAGS_cls_batch_num, "dynamic", FLAGS_precision,
  28.   //                          this->time_info_cls, img_num);
  29.   //   autolog_cls.report();
  30.   // }
  31.   // if (this->time_info_table[0] + this->time_info_table[1] +
  32.   //         this->time_info_table[2] >
  33.   //     0) {
  34.   //   AutoLogger autolog_table("table", FLAGS_use_gpu, FLAGS_use_tensorrt,
  35.   //                            FLAGS_enable_mkldnn, FLAGS_cpu_threads,
  36.   //                            FLAGS_cls_batch_num, "dynamic", FLAGS_precision,
  37.   //                            this->time_info_table, img_num);
  38.   //   autolog_table.report();
  39.   // }
  40.   // if (this->time_info_layout[0] + this->time_info_layout[1] +
  41.   //         this->time_info_layout[2] >
  42.   //     0) {
  43.   //   AutoLogger autolog_layout("layout", FLAGS_use_gpu, FLAGS_use_tensorrt,
  44.   //                             FLAGS_enable_mkldnn, FLAGS_cpu_threads,
  45.   //                             FLAGS_cls_batch_num, "dynamic",
  46.   //                             FLAGS_precision, this->time_info_layout,
  47.   //                             img_num);
  48.   //   autolog_layout.report();
  49.   // }
  50. }
  51. ...
复制代码
不利用 dirent.h
表明行 // #include
表明方法 Utility::GetAllFiles 的内容。
[code]// #include ...// list all files under a directoryvoid Utility::GetAllFiles(const char *dir_name,                          std::vector &all_inputs) {  // if (NULL == dir_name) {  //   std::cout




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4