美文网首页
Opencv Hardware Decode/Encode

Opencv Hardware Decode/Encode

作者: 王王王王王景 | 来源:发表于2019-10-22 16:08 被阅读0次

1.Opencv Opencv Hardware Decode Environment Configuration

cmake \
     -D CMAKE_INSTALL_PREFIX=/DATACENTER1/workspace/libs/opencv2 \
    -D CMAKE_BUILD_TYPE=RELEASE \
    -D WITH_CUDA=ON \
    -D WITH_CUBLAS=ON \
    -D CUDA_FAST_MATH=ON \
    -D WITH_OPENGL=ON \
    -D WITH_CUFFT=ON \
    -D WITH_NVCUVID=ON \
    -D HAVE_FFMPEG=ON \
    -D WITH_FFMPEG=ON \
    -D WITH_V4L=ON \
    -D WITH_LIBV4L=ON \
    -D BUILD_OPENCV_CUDACODEC=ON \
    -D CUDA_GENERATION=Auto \
    ..

    -D FFMPEG_INCLUDE_DIRS="/DATACENTER1/zexin.wang/shanxi_docker/libs_/ffmpeg/lib/pkgconfig" \
   
在文件中查找关键词
grep -rl "FFMPEG_INCLUDE_DIRS" ./

 -D FFMPEG_INCLUDE_DIR="/DATACENTER1/zexin.wang/shanxi_docker/libs_/ffmpeg/include" \
    -D FFMPEG_LIB_DIR="/DATACENTER1/zexin.wang/shanxi_docker/libs_/ffmpeg/lib" \

1.2 Problem 1

CMake Warning at cmake/OpenCVPackaging.cmake:23 (message):
  CPACK_PACKAGE_VERSION does not match version provided by version.hpp
  header!
Call Stack (most recent call first):
  CMakeLists.txt:1103 (include)

Solution 1

1.2 Problem 2

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
opencv_dep_CUDA_nppi_LIBRARY

Solution 2:

https://blog.csdn.net/u014613745/article/details/78310916
解决方案如下:

1).找到FindCUDA.cmake文件

找到行

find_cuda_helper_libs(nppi)

改为

  find_cuda_helper_libs(nppial)
  find_cuda_helper_libs(nppicc)
  find_cuda_helper_libs(nppicom)
  find_cuda_helper_libs(nppidei)
  find_cuda_helper_libs(nppif)
  find_cuda_helper_libs(nppig)
  find_cuda_helper_libs(nppim)
  find_cuda_helper_libs(nppist)
  find_cuda_helper_libs(nppisu)
  find_cuda_helper_libs(nppitc)

2).找到行

set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}")

改为:

set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};${CUDA_nppig_LIBRARY};${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY};${CUDA_npps_LIBRARY}")

3).找到行

unset(CUDA_nppi_LIBRARY CACHE)

改为:

unset(CUDA_nppial_LIBRARY CACHE)
unset(CUDA_nppicc_LIBRARY CACHE)
unset(CUDA_nppicom_LIBRARY CACHE)
unset(CUDA_nppidei_LIBRARY CACHE)
unset(CUDA_nppif_LIBRARY CACHE)
unset(CUDA_nppig_LIBRARY CACHE)
unset(CUDA_nppim_LIBRARY CACHE)
unset(CUDA_nppist_LIBRARY CACHE)
unset(CUDA_nppisu_LIBRARY CACHE)
unset(CUDA_nppitc_LIBRARY CACHE)

4)由于cuda9.0不支持2.0,所以,在该目录下找到OpenCVDetectCUDA.cmake文件:
将内容

set(__cuda_arch_ptx "")
  if(CUDA_GENERATION STREQUAL "Fermi")
    set(__cuda_arch_bin "2.0")
  elseif(CUDA_GENERATION STREQUAL "Kepler")
    set(__cuda_arch_bin "3.0 3.5 3.7")

修改为:(去掉2.0)

set(__cuda_arch_bin "2.0 3.0 3.5 3.7 5.0 5.2 6.0 6.1") 

1.3 Problem 3

In file included from /DATACENTER1/workspace/libs/opencv-2.4.13.6/build/modules/gpu/opencv_gpu_pch_dephelp.cxx:1:0:
/DATACENTER1/workspace/libs/opencv-2.4.13.6/modules/gpu/src/precomp.hpp:98:29: fatal error: nvcuvid.h: 没有那个文件或目录
compilation terminated.

Solution 3

(1)拷贝Video_Codec_SDK下的 cuviddec.h nvcuvid.h两个文件到/opencv-2.4.13.6/modules/gpu/src

cp -f /DATACENTER1/zexin.wang/shanxi_docker/Video_Codec_SDK_9.0.20/include/nvcuvid.h .
cp -f /DATACENTER1/zexin.wang/shanxi_docker/Video_Codec_SDK_9.0.20/include/cuviddec.h .

1.4 Problem 4

Unsupported gpu architecture 'compute_20'

Solution4

在cmake的时候添加

-D CUDA_GENERATION=Auto

Problem 1.5

无法与ffmpeg交叉编译,ffmpeg的库不是全局的
将下面添加到~/.bashrc

export PKG_CONFIG_PATH=/DATACENTER1/zexin.wang/shanxi_docker/libs_/ffmpeg/lib/pkgconfig:$PKG_CONFIG_PATH

但还是遇到问题


最后暴力出奇迹

sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev

1.6 Final

make -j12
make install

2.Opencv Hardware Decode Example

参考文章:OpenCV gpu模块样例注释:video_reader.cpp

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
 
#include <opencv2/core/core.hpp>
#include <opencv2/core/opengl_interop.hpp>
#include <opencv2/gpu/gpu.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/contrib/contrib.hpp>
 
int main(int argc, const char* argv[])
{
    if (argc != 2)
        return -1;
 
  // 首先要检查是否CUDA模块是否可用
    if(cv::gpu::getCudaEnabledDeviceCount()==0){
        cerr<<"此OpenCV编译的时候没有启用CUDA模块"<<endl;
        return -1;
    }

    const std::string fname(argv[1]);//文件名
 
    cv::namedWindow("CPU", cv::WINDOW_NORMAL);//创建显示CPU读入图像的窗口
    cv::namedWindow("GPU", cv::WINDOW_OPENGL);//创建显示GPU读入图像的窗口
    cv::gpu::setGlDevice();//设置CUDA并且为当前使用OpenGL的进程初始化,参数默认为0
 
    cv::Mat frame;//用于存储CPU读入图像的数组
    cv::VideoCapture reader(fname);//实例化一个 用CPU从文件组读入视频的类
 
    cv::gpu::GpuMat d_frame;//用于存储GPU读入图像的数组
    cv::gpu::VideoReader_GPU d_reader(fname);//实例化一个 用GPU从文件组读入视频的类
    d_reader.dumpFormat(std::cout);//输出视频帧信息
 
    cv::TickMeter tm;
    std::vector<double> cpu_times;
    std::vector<double> gpu_times;
 
    for (;;)
    {
        tm.reset(); tm.start();//计算CPU读入一帧的时间
        if (!reader.read(frame))
            break;
        tm.stop();
        cpu_times.push_back(tm.getTimeMilli());
 
        tm.reset(); tm.start();//计算GPU读入一帧的时间
        if (!d_reader.read(d_frame))
            break;
        tm.stop();
        gpu_times.push_back(tm.getTimeMilli());
 
        cv::imshow("CPU", frame);//显示CPU读入的视频
        cv::imshow("GPU", d_frame);//显示GPU读入的视频
 
        if (cv::waitKey(3) > 0)//如果等待时间大于3,就终止读入视频
            break;
    }
 
    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();//求CPU读入一帧的平均时间
        double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();//求GPU读入一帧的平均时间
 
        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;
}

Makefile参考

INC_ROOT := /DATACENTER1/oil_project/oil_support/external/libs
BOOST := $(INC_ROOT)/boost
OPENCV := /DATACENTER1/workspace/libs/opencv2
GLOG := $(INC_ROOT)/glog
GFLAG := $(INC_ROOT)/gflag

STREAMER_OPENCV := ./streamer_opencv

COMMON :=/DATACENTER1/oil_project/oil_support/common

INC_DIR := $(BOOST)/include $(OPENCV)/include $(GLOG)/include $(STREAMER_OPENCV) 

LIB_DIR := $(GLOG)/lib $(BOOST)/lib $(GFLAG)/lib $(OPENCV)/lib
TAG_DIR := .

EXECUTABLE := $(TAG_DIR)/streamer
LIBS := pthread opencv_video opencv_core opencv_imgproc opencv_highgui opencv_contrib opencv_gpu \
glog gflags boost_program_options boost_thread boost_filesystem boost_system boost_date_time rt dl z#lzma # nvcuvid cuda
#ACEX_MACROS := -DACEX_NTRACE

CFLAGS := -g -Wall -fopenmp -m64 -w
CXXFLAGS := $(CFLAGS)
CPPFLAGS += $(addprefix -I, $(INC_DIR)) 
ARFLAGS :=

# CXX := /DATACENTER1/zexin.wang/gcc-9.2.0/build/bin/$(CXX)

RM-F := rm -f

SOURCE := $(wildcard *.c) $(wildcard *.cpp) $(wildcard ${STREAMER_OPENCV}/*.cpp)

OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))
DEPS := $(patsubst %.o,%.d,$(OBJS))
MISSING_DEPS := $(filter-out $(wildcard $(DEPS)),$(DEPS))
MISSING_DEPS_SOURCES := $(wildcard $(patsubst %.d,%.c,$(MISSING_DEPS)) $(patsubst %.d,%.cpp,$(MISSING_DEPS)))
CPPFLAGS += -MD -std=c++11 

.PHONY : everything deps objs clean veryclean rebuild

everything : $(EXECUTABLE)

deps : $(DEPS)

objs : $(OBJS)

clean :
        @$(RM-F) */*/*/*/*.o
        @$(RM-F) */*/*/*/*.d
        @$(RM-F) */*/*/*.o
        @$(RM-F) */*/*/*.d
        @$(RM-F) */*/*.o
        @$(RM-F) */*/*.d
        @$(RM-F) */*.d
        @$(RM-F) */*.o

veryclean: clean
        @$(RM-F) $(EXECUTABLE)

rebuild: veryclean everything


ifneq ($(MISSING_DEPS),)
$(MISSING_DEPS) :
        @$(RM-F) $(patsubst %.d,%.o,$@)
endif

-include $(DEPS)

$(EXECUTABLE) : $(OBJS) 
        rm -f $@
        $(CXX) -o $(EXECUTABLE) $(OBJS) $(addprefix -L, $(LIB_DIR)) $(addprefix -l, $(LIBS)) -fopenmp
        @$(RM-F) *.o
        @$(RM-F) *.d


相关文章

网友评论

      本文标题:Opencv Hardware Decode/Encode

      本文链接:https://www.haomeiwen.com/subject/hnxtvctx.html