当前位置:   article > 正文

opencv-gpu版本编译(添加java支持,可选)实现硬解码_java opencv gpu

java opencv gpu

opencv gpu版本编译,实现硬解码,加速rtsp视频流读取

前置条件,显卡驱动,cuda 已安装

这里cuda安装路径为 /usr/local/cuda-11.2

1、准备文件

  • opencv-4.5.5

  • opencv_contrib-4.5.5
    在这里插入图片描述

  • nucuvid:官网下载
    在这里插入图片描述

2、复制 NVCUVID 头文件到 cuda 安装目录 include

sudo cp cuviddec.h nvcuid.h nvEncodeAPI. /usr/local/cuda-11.2/include
  • 1

3、安装相关依赖

  • 执行01_install_dependence.sh
#!/bin/bash

sudo apt update
sudo apt upgrade

#sudo apt install -y gcc-10 g++-10
sudo apt install -y build-essential cmake pkg-config yasm git checkinstall
sudo apt install -y pkg-config yasm checkinstall
sudo apt install -y libjpeg-dev libpng-dev libtiff-dev
sudo apt install -y libavcodec-dev libavformat-dev libswscale-dev 
sudo apt install -y libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev 
sudo apt install -y libfaac-dev libmp3lame-dev libvorbis-dev
sudo apt install -y libgtkglext1 libgtkglext1-dev
sudo apt-get install -y libavresample-dev libdc1394-22 libdc1394-22-dev libxine2-dev libv4l-dev v4l-utils
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4、 执行cmake

  • 执行02_install_opencv.sh
cd opencv_build
#unzip opencv.zip
#unzip opencv_contrib.zip

echo "Moving onto the build portion of things"
cd opencv-4.5.5
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_C_COMPILER=/usr/bin/gcc-9 \
    -D CMAKE_INSTALL_PREFIX=../install_dir \
    -D OPENCV_GENERATE_PKGCONFIG=ON \
    -D BUILD_opencv_python3=ON \
    -D CUDA_ARCH_BIN=8.6\
    -D WITH_CUDA=ON \
    -D WITH_CUDNN=ON \
    -D OPENCV_DNN_CUDA=ON \
    -D ENABLE_FAST_MATH=1 \
    -D CUDA_FAST_MATH=1 \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D WTIH_CUBLAS=1 \
    -D WITH_V4L=ON \
	-D WITH_NVCUVID=ON \
	-D WITH_OPENGL=ON \
	-D WITH_FFMPEG=ON \
	-D BUILD_opencv_java=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_project/opencv_build/opencv_contrib-4.5.5/modules ..

echo "Configuring build & making OpenCV"
echo 'finished all the shit'


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

需要注意的地方

  • -D CMAKE_INSTALL_PREFIX=../install_dir:安装路径

  • -D CUDA_ARCH_BIN=8.6 : 显卡算力

  • -D WITH_CUDA=ON:启用CUDA支持

  • -D WITH_CUDNN=ON:启用CUDNN支持

  • -D OPENCV_DNN_CUDA=ON:启用CUDA加速的深度学习模块

  • -D WITH_NVCUVID=ON:启用NVCUVID支持,允许OpenCV在NVIDIA GPU上解码视频。

  • -D BUILD_opencv_java=ON : 启用 java 支持,需要先配置好java环境变量(jdk,ant),可选项

cmake 执行完成

-- 
--   OpenCV modules:
--     To be built:                 alphamat aruco barcode bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform java line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 cvv hdf julia matlab ovis python2 python3 sfm viz
--     Applications:                tests perf_tests apps
--     Documentation:               NO
--     Non-free algorithms:         YES
-- 
--   GUI:                           GTK2
--     GTK+:                        YES (ver 2.24.32)
--       GThread :                  YES (ver 2.64.6)
--       GtkGlExt:                  YES (ver 1.2.0)
--     OpenGL support:              YES (/usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/x86_64-linux-gnu/libGLU.so)
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
--     WEBP:                        build (ver encoder: 0x020f)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.37)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.1.0)
--     JPEG 2000:                   build (ver 2.4.0)
--     OpenEXR:                     /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 2_3)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
--     DC1394:                      YES (2.2.5)
--     FFMPEG:                      YES
--       avcodec:                   YES (58.54.100)
--       avformat:                  YES (58.29.100)
--       avutil:                    YES (56.31.100)
--       swscale:                   YES (5.5.100)
--       avresample:                YES (4.0.0)
--     GStreamer:                   YES (1.16.3)
--     v4l/v4l2:                    YES (linux/videodev2.h)
-- 
--   Parallel framework:            pthreads
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Intel IPP:                   2020.0.0 Gold [2020.0.0]
--            at:                   /home/dell/ljn/opencv_project/opencv_build/opencv-4.5.5/build/3rdparty/ippicv/ippicv_lnx/icv
--     Intel IPP IW:                sources (2020.0.0)
--               at:                /home/dell/ljn/opencv_project/opencv_build/opencv-4.5.5/build/3rdparty/ippicv/ippicv_lnx/iw
--     VA:                          NO
--     Lapack:                      NO
--     Eigen:                       YES (ver 3.3.9)
--     Custom HAL:                  NO
--     Protobuf:                    build (3.19.1)
-- 
--   NVIDIA CUDA:                   YES (ver 11.2, CUFFT CUBLAS NVCUVID FAST_MATH)
--     NVIDIA GPU arch:             86
--     NVIDIA PTX archs:
-- 
--   cuDNN:                         YES (ver 8.6.0)
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/dell/ljn/opencv_project/opencv_build/opencv-4.5.5/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.8.10)
--     Libraries:                   NO
--     numpy:                       NO (Python3 wrappers can not be generated)
--     install path:                -
-- 
--   Python (for build):            /usr/bin/python3
-- 
--   Java:                          
--     ant:                         /usr/local/apache-ant-1.10.13/bin/ant (ver 1.10.13)
--     JNI:                         /usr/lib/jvm/jdk1.8.0_361/include /usr/lib/jvm/jdk1.8.0_361/include/linux /usr/lib/jvm/jdk1.8.0_361/include
--     Java wrappers:               YES
--     Java tests:                  YES
-- 
--   Install to:                    /home/dell/ljn/opencv_project/opencv_build/opencv-4.5.5/install_dir
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dell/ljn/opencv_project/opencv_build/opencv-4.5.5/build
Configuring build & making OpenCV
finished all the shit
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • gpu 加速开启成功
    在这里插入图片描述
  • java 支持开启成功
    在这里插入图片描述

5、编译安装

  • 进入cmake创建的build目录

    cd opencv_build/opencv-4.5.5/build
    
    #编译,根据电脑核心数选择合适线程
    make -j30  
    
    • 1
    • 2
    • 3
    • 4
  • 安装

    make install
    
    • 1

6、测试

  • 进入目录opencv_gpu_test,编译执行程序,在CMakeLIsts.txt中修改opencv的安装路径

    cd build
    make 
    ./opencv_test
    
    • 1
    • 2
    • 3
  • CMakeLIsts.txt

    cmake_minimum_required(VERSION 3.0.2)
    project(opencv_test)
    SET(CMAKE_BUILD_TYPE "Release")
    
    # 安装路径
    find_package(OpenCV 4.5.5 REQUIRED PATHS /home/lenovo/opencv_project/opencv_build/opencv-4.5.5/install_dir)
    
    include_directories(${OpenCV_INCLUDE_DIRS})
    
    add_executable(opencv_test test.cc )
    #add_executable( opencv_test gpu_mat.cpp )
    target_link_libraries( opencv_test
            ${OpenCV_LIBRARIES}
            /usr/lib/x86_64-linux-gnu
            )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • test.cc

    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    #include "opencv2/opencv_modules.hpp"
    #include <opencv2/core/utility.hpp>
    #include <opencv2/core.hpp>
    #include <opencv2/core/opengl.hpp>
    #include <opencv2/cudacodec.hpp>
    #include <opencv2/highgui.hpp>
    
    int main(int argc, const char* argv[])
    {
       //std::cout<<cv::getBuildInformation()<<std::endl;
       //将这个流改成你自己的
        const std::string fname = "rtsp://admin:abcd1234@192.168.1.110:554/smart264/ch1/main/av_stream";
        const std::string gfname = "rtsp://admin:abcd1234@192.168.1.110:554/Streaming/Channels/2";
        std::cout<<"Set device...."<<std::endl;
        int numDevice = cv::cuda::getCudaEnabledDeviceCount();
        std::cout<<"device count: "<<numDevice<<std::endl;
        int cudaDevice = 0;
        cv::cuda::setDevice(cudaDevice);
        //cv::cuda::setGlDevice(cudaDevice);
        //cv::cuda::setGlDevice(1);
    
        std::cout<<"read rtsp through cpu..."<<std::endl;
        cv::Mat frame;
        cv::VideoCapture reader(fname);
    
        cv::cuda::GpuMat d_frame;
        std::cout<<"read rtsp through cuda..."<<std::endl;
        cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname);
        cv::TickMeter tm;
        std::vector<double> cpu_times;
        std::vector<double> gpu_times;
        std::cout<<"test"<<std::endl;
        for (int i = 0;i<500;i++)
        {
            tm.reset(); tm.start();
            if (!reader.read(frame))
               break;
            tm.stop();
            cpu_times.push_back(tm.getTimeMilli());
    
             tm.reset(); tm.start();
            if (!d_reader->nextFrame(d_frame))
                break;
             tm.stop();
             gpu_times.push_back(tm.getTimeMilli());
        }
    
        if (!cpu_times.empty() || !gpu_times.empty())
        {
            std::cout << std::endl << "Results:" << std::endl;
    
            //std::sort(cpu_times.begin(), cpu_times.end());
            std::sort(gpu_times.begin(), gpu_times.end());
    
            //double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
            double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();
    
            //std::cout << "CPU : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << std::endl;
            std::cout << "GPU : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << std::endl;
        }
    
        return 0;
    }
    // #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
  • 结果如下安装成功

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/623275
推荐阅读
相关标签
  

闽ICP备14008679号