香橙派华为昇腾CANN架构编译opencv4.9

香橙派华为升腾AI盒子

为啥要编译opencv4.9.0, 因为在4.9.0 中增加了华为昇腾CANN的外接开发库,下图为盒子外观,此次一接到这个盒子,立刻开始开箱操作,首先就是要编译opencv4.9,以前在香橙派3588 的盒子中,也是同样的操作,不过当时编译的是4.6

华为昇腾CANN架构的优点我也不多说,昇腾AI视频转码解决方案搭载昇腾310处理器,硬件自带编解码及AI处理能力,编解码场景性价比提升最高可达75%,为编解码场景提供高性价比算力,AI 前面一定是视频解码后进行识别,同时转编码发送出去,利用gstreamer,ffmpeg,都可以编解码,但是如何最大化利用硬件资源,需要我们探索。

升级

以下这两部可能需要一些时间,升级时会找到华为云

c 复制代码
sudo apt update
sudo apt upgrade

过程中可能会安装一些开发包,比如下面的tbb,不过下面的命令还是执行一下,根据我观察,ffmpeg等库都会安装,需要注意的是一定要把opencv-gui的界面关闭,如果我们是一边安装升级操作,一边编译,达不到效果,因为升级改变了很多库和环境变量。

安装eigen

eigen是一个

c 复制代码
sudo apt-get install libeigen3-dev

安装tbb开发包

TBB全称Threading Building Blocks,是Intel针对基于多核处理器进行软件开发而创建的一套C++模板库,核心作用是用来在任务处理中做多线程加速,所以一定要安装tbb,以使用多核并发能力。

c 复制代码
sudo apt-get install libtbb-dev

写一个测试程序

c 复制代码
#include <tbb/tbb.h>
#include <iostream>
 
int main() {
    tbb::task_scheduler_init init; // 初始化TBB
 
    tbb::parallel_for(0, 10, [](int i)
        std::cout << "Hello from thread " << std::this_thread::get_id() << " with index " << i << std::endl;
    });
 
    return 0;
}

安装gstreamer 开发包

sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

安装cmake的界面版本

c 复制代码
sudo apt-get install cmake-qt-gui

配置好以下界面

WITH-CANN

重点来了,昇腾为后端的图像处理接口封装在 OpenCV 扩展包(opencv_contrib)的 cannops 模块中,包括图像矩阵的算术运算、通道拆分合并、图片裁剪、翻转、调整大小、转置等图像处理的 Python 和 C++ 接口,处理精度与 CPU 后端的计算结果相同。

CANN 的勾打上以后,ascend 中的toolkit包会找到

开始编译

CANN c++ 示例

c 复制代码
#include <iostream>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/cann.hpp>
#include <opencv2/cann_interface.hpp>

int main(int argc, char* argv[])
{
    cv::CommandLineParser parser(argc, argv,
                                 "{@input|puppy.png|path to input image}"
                                 "{@output|output.png|path to output image}"
                                 "{help||show help}");
    parser.about("This is a sample for image processing with Ascend NPU. \n");
    if (argc != 3 || parser.has("help"))
    {
        parser.printMessage();
        return 0;
    }

    std::string imagePath = parser.get<std::string>(0);
    std::string outputPath = parser.get<std::string>(1);

    // read input image and generate guass noise
    //! [input_noise]
    cv::Mat img = cv::imread(imagePath);
    // Generate gauss noise that will be added into the input image
    cv::Mat gaussNoise(img.rows, img.cols, img.type());
    cv::RNG rng;
    rng.fill(gaussNoise, cv::RNG::NORMAL, 0, 25);
    //! [input_noise]

    // setup cann
    //! [setup]
    cv::cann::initAcl();
    cv::cann::setDevice(0);
    //! [setup]

    //! [image-process]
    cv::Mat output;
    // add gauss noise to the image
    cv::cann::add(img, gaussNoise, output);
    // rotate the image with a certain mode (0, 1 and 2, correspond to rotation of 90, 180 and 270
    // degrees clockwise respectively)
    cv::cann::rotate(output, output, 0);
    // flip the image with a certain mode (0, positive and negative number, correspond to flipping
    // around the x-axis, y-axis and both axes respectively)
    cv::cann::flip(output, output, 0);
    //! [image-process]

    cv::imwrite(outputPath, output);

    //! [tear-down-cann]
    cv::cann::resetDevice();
    cv::cann::finalizeAcl();
    //! [tear-down-cann]
    return 0;
}

可以用下面的方式来编译

g++ pkg-config opencv --cflags test.cpp -o test pkg-config opencv --libs

其他总结

这块小盒子本身带了一些例子,不过我们最需要的是如何发挥他的关键,就是硬件资源调度,在教育、体育、安防、交通、医疗等领域中,AI检测应用发挥着至关重要的作用,比如在各种安全分析,各种体育训练时的实时人体关键点检测可以精确、实时地捕捉运动员的动作,在安防应用场景中,识别各种异常现象和异常行为或特定姿态,以达到场景安全防控的目的。

相关推荐
猫林老师2 天前
HarmonyOS数据持久化:Preferences轻量级存储实战
华为·harmonyos
Devil枫2 天前
鸿蒙深链落地实战:从安全解析到异常兜底的全链路设计
安全·华为·harmonyos
广州腾科助你拿下华为认证2 天前
华为考试:HCIE数通考试难度分析
大数据·华为
与天仙漫步星海2 天前
华为基本命令
华为
低调小一2 天前
Android传统开发 vs Android Compose vs HarmonyOS ArkUI 对照表
android·华为·harmonyos
猛码Memmat2 天前
华为HarmonyOS开发文档
华为·harmonyos
流影ng2 天前
【HarmonyOS】MVVM与三层架构
华为·架构·harmonyos
爱笑的眼睛112 天前
HarmonyOS Stage 模型与 ArkUI 声明式开发深度实践:构建高效稳定的应用
华为·harmonyos
安卓开发者2 天前
鸿蒙Next ArkWeb网页文件上传与下载完全指南
华为·harmonyos