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

相关推荐
SuperHeroWu77 小时前
【HarmonyOS 5】鸿蒙碰一碰分享功能开发指南
华为·harmonyos·应用·分享·碰一碰
lqj_本人10 小时前
鸿蒙OS&UniApp制作一个小巧的图片浏览器#三方框架 #Uniapp
华为·uni-app·harmonyos
lqj_本人13 小时前
鸿蒙OS&UniApp 开发的下拉刷新与上拉加载列表#三方框架 #Uniapp
华为·uni-app·harmonyos
Lucky me.13 小时前
关于mac配置hdc(鸿蒙)
macos·华为·harmonyos
xmweisi0215 小时前
【华为】现场配置OSPF
服务器·华为·华为认证·hcie·hcip·ospf·it培训
特立独行的猫a1 天前
HarmonyOS 【诗韵悠然】AI古诗词赏析APP开发实战从零到一系列(一、开篇,项目介绍)
人工智能·华为·harmonyos·古诗词
周胡杰1 天前
鸿蒙接入flutter环境变量配置windows-命令行或者手动配置-到项目的创建-运行demo项目
javascript·windows·flutter·华为·harmonyos·鸿蒙·鸿蒙系统
半青年1 天前
华为鸿蒙电脑能否作为开发机?开发非鸿蒙应用?
ide·华为·编辑器·电脑·idea·harmonyos·visual studio
沙振宇2 天前
【HarmonyOS】ArkTS开发应用的横竖屏切换
android·华为·harmonyos
仓颉编程语言2 天前
仓颉Magic亮相GOSIM AI Paris 2025:掀起开源AI框架新热潮
人工智能·华为·开源·鸿蒙·仓颉编程语言