香橙派華為升騰AI盒子
為啥要編譯opencv4.9.0, 因為在4.9.0 中增加了華為昇騰CANN的外接開發庫,下圖為盒子外觀,此次一接到這個盒子,立刻開始開箱操作,首先就是要編譯opencv4.9,以前在香橙派3588 的盒子中,也是同樣的操作,不過當時編譯的是4.6
華為昇騰CANN架構的優點我也不多說,昇騰AI視頻轉碼解決方案搭載昇騰310處理器,硬件自帶編解碼及AI處理能力,編解碼場景性價比提升最高可達75%,為編解碼場景提供高性價比算力,AI 前面一定是視頻解碼后進行識別,同時轉編碼發送出去,利用gstreamer,ffmpeg,都可以編解碼,但是如何最大化利用硬件資源,需要我們探索。
升級
以下這兩部可能需要一些時間,升級時會找到華為云
sudo apt update
sudo apt upgrade
過程中可能會安裝一些開發包,比如下面的tbb,不過下面的命令還是執行一下,根據我觀察,ffmpeg等庫都會安裝,需要注意的是一定要把opencv-gui的界面關閉,如果我們是一邊安裝升級操作,一邊編譯,達不到效果,因為升級改變了很多庫和環境變量。
安裝eigen
eigen是一個
sudo apt-get install libeigen3-dev
安裝tbb開發包
TBB全稱Threading Building Blocks,是Intel針對基于多核處理器進行軟件開發而創建的一套C++模板庫,核心作用是用來在任務處理中做多線程加速,所以一定要安裝tbb,以使用多核并發能力。
sudo apt-get install libtbb-dev
寫一個測試程序
#include <tbb/tbb.h>
#include <iostream>int main() {tbb::task_scheduler_init init; // 初始化TBBtbb::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的界面版本
sudo apt-get install cmake-qt-gui
配置好以下界面
WITH-CANN
??重點來了,昇騰為后端的圖像處理接口封裝在 OpenCV 擴展包(opencv_contrib)的 cannops 模塊中,包括圖像矩陣的算術運算、通道拆分合并、圖片裁剪、翻轉、調整大小、轉置等圖像處理的 Python 和 C++ 接口,處理精度與 CPU 后端的計算結果相同。
CANN 的勾打上以后,ascend 中的toolkit包會找到
開始編譯
CANN 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 imagecv::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 imagecv::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檢測應用發揮著至關重要的作用,比如在各種安全分析,各種體育訓練時的實時人體關鍵點檢測可以精確、實時地捕捉運動員的動作,在安防應用場景中,識別各種異常現象和異常行為或特定姿態,以達到場景安全防控的目的。