maskmoo 发表于 2024-11-3 20:02

《Cmake构建实战》5 onnxruntime手写识别实践(libpng引用失败)

本帖最后由 maskmoo 于 2024-11-3 20:02 编辑

<p><strong><span style="font-size:20px;">1 工程设计</span></strong></p>

<p><strong>设计目标</strong></p>

<p>实现包括手写数字识别库以及一个识别命令行工具,识别库能够被C语言等其他编程语言所调用。</p>

<p><strong>目录结构</strong></p>

<p>├─cli<br />
├─cmake<br />
├─include<br />
├─models<br />
└─src</p>

<p>└─CMakeList.txt</p>

<p><strong>接口设计</strong></p>

<ul>
        <li>初始化手写数字识别库&nbsp;&nbsp;NUM_RECOGNIZER_EXPORT void num_recognizer_init();</li>
        <li>
        <p>识别器创建&nbsp; &nbsp;NUM_RECOGNIZER_EXPORT void num_recognizer_create(const char *model_path,&nbsp; Recognizer **out_recognizer);</p>
        </li>
        <li>
        <p>析构识别器&nbsp; &nbsp;NUM_RECOGNIZER_EXPORT void num_recognizer_delete(Recognizer *recognizer);</p>
        </li>
        <li>
        <p>识别图片&nbsp;&nbsp;NUM_RECOGNIZER_EXPORT int num_recognizer_recognize_png(Recognizer *recognizer,&nbsp;&nbsp;&nbsp;const char *png_path,&nbsp; int *result);</p>
        </li>
</ul>

<p>&nbsp;</p>

<p><span style="font-size:20px;"><strong>2 安装依赖库</strong></span></p>

<p>第3方库的依赖主要包含有libpng,zlib以及onnxruntime。</p>

<p><strong>onnxruntime安装:</strong></p>

<p><a href="https://bbs.eeworld.com.cn/thread-1298134-1-1.html" target="_blank">《Cmake构建实战》4 onnxruntime的查找模块实践</a></p>

<p><strong>zlib安装</strong></p>

<p><a href="https://github.com/madler/zlib/releases/tag/v1.3.1">Release zlib 1.3.1 &middot; madler/zlib &middot; GitHub</a></p>

<pre>
<code class="language-bash">cd zlib
mkdir build
cd build

cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --config Release

#需要管理员权限 默认安装路径 C:\Program Files(x86)\zlib
cmake --install .</code></pre>

<p> &nbsp;</p>

<p>&nbsp;</p>

<p><strong>libpng安装</strong></p>

<p>需要先安装zib</p>

<p><a href="https://github.com/pnggroup/libpng/releases/tag/v1.6.40">Release v1.6.40 &middot; pnggroup/libpng &middot; GitHub</a></p>

<pre>
<code>cd libpng
mkdir build
cd build

cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --config Release

#需要管理员权限 默认安装路径 C:\Program Files(x86)\libpng
cmake --install . </code></pre>

<p> &nbsp;</p>

<p>&nbsp;</p>

<p>Cmake导入脚本</p>

<pre>
<code>#[=======================================================================[.rst:
Findlibpng
-------

Finds the libpng library.(查找libpng库)

Imported Targets(导入目标)
^^^^^^^^^^^^^^^^^^^^^^^^^^

This module provides the following imported targets, if found(若查找成功,该模块会创建如下导入目标):

``png_shared``
The libpng shared library(libpng动态库)
``png_static``
The libpng static library(libpng静态库)

Result Variables(结果变量)
^^^^^^^^^^^^^^^^^^^^^^^^^^

This will define the following variables(该模块会定义如下变量):

``libpng_FOUND``
True if the system has the libpng library.(若成功查找libpng库,则为真值)
``libpng_INCLUDE_DIRS``
Include directories needed to use libpng.(作为使用要求的libpng的头文件目录)
``libpng_LIBRARIES``
Libraries needed to link to libpng shared library.(作为使用要求的libpng的动态链接库文件路径)

Cache Variables(缓存变量)
^^^^^^^^^^^^^^^^^^^^^^^^^

The following cache variables may also be set(该模块会定义如下缓存变量):

``libpng_INCLUDE_DIR``
The directory containing ``png.h``.(``png.h``所在目录)
#]=======================================================================]

# 调用libpng库自带的配置文件来查找软件包,其自带配置文件会创建两个导入库目标:
# 1. 动态库导入目标``png_shared``
# 2. 静态库导入目标``png_static``
find_package(libpng CONFIG CONFIGS libpng16.cmake)

# 若成功查找,为两个库目标补上缺失的头文件目录属性
if(libpng_FOUND)
# 获取png动态库导入目标对应动态库文件的路径,首先尝试其IMPORTED_LOCATION属性
get_target_property(libpng_LIBRARY png_shared IMPORTED_LOCATION)
# 若未能获得动态库文件路径,再尝试其IMPORTED_LOCATION_RELEASE属性
if(NOT libpng_LIBRARY)
    get_target_property(libpng_LIBRARY png_shared IMPORTED_LOCATION_RELEASE)
endif()
# 根据png动态库的路径,设置libpng的根目录
set(_png_root "${libpng_LIBRARY}/../..")

# 查找png.h头文件所在目录的路径
find_path(libpng_INCLUDE_DIR png.h
    HINTS ${_png_root}
    PATH_SUFFIXES include)
# 为png和png_static导入库目标设置头文件目录属性
target_include_directories(png_shared INTERFACE ${libpng_INCLUDE_DIR})
target_include_directories(png_static INTERFACE ${libpng_INCLUDE_DIR})
endif()

include(FindPackageHandleStandardArgs)

# 检查变量是否有效以及配置文件是否成功执行
find_package_handle_standard_args(libpng
REQUIRED_VARS libpng_LIBRARY libpng_INCLUDE_DIR
CONFIG_MODE)

# 若一切成功,设置结果变量
if(libpng_FOUND)
set(libpng_INCLUDE_DIRS ${libpng_INCLUDE_DIR})
set(libpng_LIBRARIES ${libpng_LIBRARY})
endif()
</code></pre>

<p>&nbsp;</p>

<p><span style="font-size:20px;"><strong>构建和运行</strong></span></p>

<pre>
<code>cd ch011
mkdir build
cd build

cmake -DCMAKE_BUILD_TYPE=Debug ..
cmake --build . --config Debug
</code></pre>

<p>&nbsp;</p>

<p>最终编译失败,提示找不到png_shared。但是查看安装目录也可以看到目标png_shared的IMPORTED_LOCATION_RELEASE属性。有了解错误原因的大佬帮忙指导一下。</p>

<p> &nbsp;</p>

<p>&nbsp;</p>

<p> &nbsp;</p>
页: [1]
查看完整版本: 《Cmake构建实战》5 onnxruntime手写识别实践(libpng引用失败)