RK1126 NPU yolov5 6.2

RK1126 NPU yolov5 6.2_第1张图片

基于 rk npu , 实现 yolov5 6.2 模型推理

实现过程 ⚡️​

编译 opencv
  1. 需根据自己路径修改.
	cmake -D CMAKE_BUILD_TYPE=RELEASE \
	      -D CMAKE_C_COMPILER=./gcc-arm-8.3-2019.02-x86_64-arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc \
	      -D CMAKE_CXX_COMPILER=./gcc-arm-8.3-2019.02-x86_64-arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++ \
	      -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4.2_gnueabihf \
	      -D CMAKE_TOOLCHAIN_FILE=../platforms/linux/arm-gnueabi.toolchain.cmake \
	      -D OPENCV_FORCE_3RDPARTY_BUILD=ON \
	      -D BUILD_ZLIB=ON \
	      -D INSTALL_C_EXAMPLES=OFF \
	      -D INSTALL_PYTHON_EXAMPLES=OFF \
	      -D WITH_TBB=OFF \
	      -D WITH_V4L=ON \
	      -D WITH_CUBLAS=1 \
	      -D OPENCV_SKIP_PYTHON_LOADER=ON \
	      -D OPENCV_GENERATE_PKGCONFIG=ON \
	      -D WITH_QT=OFF \
	      -D WITH_OPENGL=ON \
	      -D WITH_FFMPEG=ON \
	      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.2.0/modules \
	      -D ENABLE_CXX11=ON \
	      -D BUILD_EXAMPLES=OFF .. \
	      -D BUILD_opencv_world=ON ..
	  make -j4
	  sudo make install

Q&A ❓​

  1. zlib.h:没有那个文件或目录 #include
    -D OPENCV_FORCE_3RDPARTY_BUILD=ON \
    -D BUILD_ZLIB=ON \
    
  2. opencv G_STATIC_ASSERT(sizeof (unsigned long long) == sizeof (guint64))
    -D CMAKE_TOOLCHAIN_FILE=../platforms/linux/arm-gnueabi.toolchain.cmake
    

Demo

  1. 参考: https://github.com/shaoshengsong/rockchip_rknn_yolov5
  2. 这里修改了下前处理,使用 letterbox
    cv::Mat letterbox(const cv::Mat &img, cv::Size new_shape, cv::Scalar color, bool _auto, bool scaleFill, bool scaleup, int stride)
    {
        float width = img.cols;
        float height = img.rows;
        float r = cv::min(new_shape.width / width, new_shape.height / height);
        if (!scaleup)
            r = cv::min(r, 1.0f);
        int new_unpadW = int(round(width * r));
        int new_unpadH = int(round(height * r));
        int dw = new_shape.width - new_unpadW;
        int dh = new_shape.height - new_unpadH;
        if (_auto)
        {
            dw %= stride;
            dh %= stride;
        }
        dw /= 2, dh /= 2;
        cv::Mat dst;
        resize(img, dst, cv::Size(new_unpadW, new_unpadH), 0, 0, cv::INTER_LINEAR);
        int top = int(round(dh - 0.1));
        int bottom = int(round(dh + 0.1));
        int left = int(round(dw - 0.1));
        int right = int(round(dw + 0.1));
        copyMakeBorder(dst, dst, top, bottom, left, right, cv::BORDER_CONSTANT, color);
    
        pad_left_ = left;
        pad_top_ = top;
        scale_ = r;
        return dst;
    }
    
  3. inference
    int inference(const cv::Mat &orig_img, rt_detect_result_group_t &detect_result_group){
    	...
    	cv::Mat img = letterbox(orig_img, cv::Size(width_, height_), cv::Scalar(114, 114, 114), false, false, false, 32);
    	cv::Mat resimg;
    	// cv::resize(orig_img, resimg, cv::Size(width_, height_), (0, 0), (0, 0), cv::INTER_LINEAR);
    	cv::cvtColor(img, resimg, cv::COLOR_BGR2RGB);
     ...
    }
    
  4. 执行 ./build.sh
  5. 我用的 adb ,将程序拷贝到 rk1126 板子上.
    adb.exe push xxx  xxxx
    

RK1126 NPU yolov5 6.2_第2张图片


参考文章 ​

  1. https://github.com/shaoshengsong/rockchip_rknn_yolov5
  2. https://github.com/rockchip-linux/rknpu

你可能感兴趣的:(Rockchip,rk1126,yolo5,6.2)