香橙派华为昇腾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检测应用发挥着至关重要的作用,比如在各种安全分析,各种体育训练时的实时人体关键点检测可以精确、实时地捕捉运动员的动作,在安防应用场景中,识别各种异常现象和异常行为或特定姿态,以达到场景安全防控的目的。

相关推荐
行者-全栈开发4 小时前
【鸿蒙心迹】深色模式适配全链路——从配置到自定义(HarmonyOS 7.x)
华为·harmonyos
行者-全栈开发14 小时前
【鸿蒙心迹】国际化 i18n 与多语言资源管理实战(HarmonyOS 7.x)
ui·华为·重构·国际化·harmonyos·多语言
钉先生贾1 天前
性能、体积与稳定性:怎样写出可信的适配结论
华为·harmonyos
redtank20002 天前
纯血鸿蒙小工具三件套实测:弹幕手持、爱记录与仓库管理软件
学习·华为·智能手机·harmonyos
xyzzklk2 天前
变相解决华为Mate 8锁屏后无法建立入站网络连接
linux·运维·服务器·网络·华为·go·安卓
贾伟康3 天前
【鸿蒙心迹】个人开发者上架复盘:备案、合规与审核指南没写透的细节
华为·harmonyos
不爱学英文的码字机器3 天前
2026鸿蒙远程控制软件完全选购指南:从入门到精通选型攻略
华为·harmonyos
JosieBook4 天前
【鸿蒙PC GUI适配】chiaki-ng 移植的工程实践:Qt6/QML 与 ArkUI 的混合窗口架构
华为·架构·harmonyos
贾伟康4 天前
HarmonyOS API 26 UKey PIN 授权门禁:状态查询、失败归因与资源闭环
华为·harmonyos
七月稻草人5 天前
鸿蒙平板能当电脑第二块屏幕吗?ToDesk、向日葵镜像/副屏实测
华为·电脑·harmonyos