基于 rk npu , 实现 yolov5 6.2 模型推理
实现过程 ⚡️
编译 opencv
- 需根据自己路径修改.
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 ❓
zlib.h:没有那个文件或目录 #include <zlib.h>
-D OPENCV_FORCE_3RDPARTY_BUILD=ON \
-D BUILD_ZLIB=ON \
opencv G_STATIC_ASSERT(sizeof (unsigned long long) == sizeof (guint64))
-D CMAKE_TOOLCHAIN_FILE=../platforms/linux/arm-gnueabi.toolchain.cmake
Demo 👀
- 参考: https://github.com/shaoshengsong/rockchip_rknn_yolov5
- 这里修改了下前处理,使用
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;
}
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::cvtColor(img, resimg, cv::COLOR_BGR2RGB);
...
}
- 执行
./build.sh
- 我用的
adb
,将程序拷贝到 rk1126
板子上.adb.exe push xxx xxxx
参考文章 📖
- https://github.com/shaoshengsong/rockchip_rknn_yolov5
- https://github.com/rockchip-linux/rknpu