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

相关推荐
lilian23321 小时前
Harmony os 技术实战|拼豆制图09:把 50 张图库素材做成可复跑的生产管线
华为·harmonyos
chjif1 天前
Android 设备管控开发实战:企业受控终端的 DNS 域名策略实现
android·华为·harmonyos
OH_TPC1 天前
【鸿蒙优选三方库】@ohos/grpc:在 HarmonyOS 上像调本地方法一样调远程服务
华为·harmonyos·鸿蒙
达子6661 天前
第30章_综合案例:HarmonyOs开发图解之 俄罗斯方块游戏
游戏·华为·harmonyos
云端漫步19871 天前
HarmonyOS NEXT AI 应用开发总结:30 篇之旅
人工智能·华为·harmonyos
math_hongfan1 天前
鸿蒙Agent高级技能自定义开发:自定义意图服务/能力注册/Agent技能编排/服务插件开发实战
android·学习·华为·harmonyos·鸿蒙
math_hongfan1 天前
鸿蒙AI权限与隐私保护高级:智能数据脱敏/端侧处理优先/数据最小化采集/隐私计算技术栈
人工智能·学习·华为·harmonyos·鸿蒙
云端漫步19871 天前
HarmonyOS NEXT AI 智能生活助手:项目打包与发布
人工智能·华为·生活·harmonyos
less_121381 天前
HarmonyOS WPS Open SDK:关闭回传 URI-FD 与沙箱 filePath
华为·harmonyos·wps
math_hongfan1 天前
鸿蒙多模态AI交互高级:图文+语音+手势融合交互/多模态大模型端侧适配/跨模态检索高阶实战
人工智能·学习·华为·交互·语音识别·harmonyos·鸿蒙