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

相关推荐
星释10 分钟前
鸿蒙智能体开发实战:5.搭建A2A API服务
华为·ai·harmonyos·智能体
duluo1339 小时前
鸿蒙NEXT实战:从零构建高尔夫挥杆教学App(API 24 / ArkTS 深度解析)
华为·harmonyos·鸿蒙·鸿蒙系统
zjxcq52010 小时前
鸿蒙深入理解 HarmonyOS NEXT ArkTS 中 `height(‘100%‘)` 在嵌套容器中的行为机制
华为·harmonyos
国服第二切图仔14 小时前
HarmonyOS APP《画伴梦工厂》开发第38篇-自适应布局API实战——adaptiveLayout模块
华为·harmonyos
nashane18 小时前
HarmonyOS 6商城开发学习:剪贴板权限频繁弹窗的根治——从“自动嗅探“改为“用户主动触发“模型
华为·harmonyos
国服第二切图仔18 小时前
HarmonyOS APP《画伴梦工厂》开发第37篇-GridRow-GridCol——响应式网格布局
华为·harmonyos
zjxcq5201 天前
鸿蒙 ArkUI 实战:Swiper 组件构建高性能轮播图
android·华为·harmonyos
Swift社区3 天前
AI Runtime Kernel:鸿蒙 App 如何设计智能体内核?
人工智能·华为·harmonyos
国服第二切图仔5 天前
HarmonyOS APP《画伴梦工厂》开发第1篇:ArkTS 语言入门——从 TypeScript 到 ArkTS
华为·typescript·harmonyos
前端不太难5 天前
Tool Graph:HarmonyOS PC 如何统一所有系统能力?
华为·状态模式·harmonyos