• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <iostream>
2 
3 #include "opencv2/opencv_modules.hpp"
4 
5 #if defined(HAVE_OPENCV_CUDACODEC)
6 
7 #include <string>
8 #include <vector>
9 #include <algorithm>
10 #include <numeric>
11 
12 #include <opencv2/core.hpp>
13 #include <opencv2/core/opengl.hpp>
14 #include <opencv2/cudacodec.hpp>
15 #include <opencv2/highgui.hpp>
16 
17 #include "tick_meter.hpp"
18 
main(int argc,const char * argv[])19 int main(int argc, const char* argv[])
20 {
21     if (argc != 2)
22         return -1;
23 
24     const std::string fname(argv[1]);
25 
26     cv::namedWindow("CPU", cv::WINDOW_NORMAL);
27     cv::namedWindow("GPU", cv::WINDOW_OPENGL);
28     cv::cuda::setGlDevice();
29 
30     cv::Mat frame;
31     cv::VideoCapture reader(fname);
32 
33     cv::cuda::GpuMat d_frame;
34     cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname);
35 
36     TickMeter tm;
37     std::vector<double> cpu_times;
38     std::vector<double> gpu_times;
39 
40     for (;;)
41     {
42         tm.reset(); tm.start();
43         if (!reader.read(frame))
44             break;
45         tm.stop();
46         cpu_times.push_back(tm.getTimeMilli());
47 
48         tm.reset(); tm.start();
49         if (!d_reader->nextFrame(d_frame))
50             break;
51         tm.stop();
52         gpu_times.push_back(tm.getTimeMilli());
53 
54         cv::imshow("CPU", frame);
55         cv::imshow("GPU", d_frame);
56 
57         if (cv::waitKey(3) > 0)
58             break;
59     }
60 
61     if (!cpu_times.empty() && !gpu_times.empty())
62     {
63         std::cout << std::endl << "Results:" << std::endl;
64 
65         std::sort(cpu_times.begin(), cpu_times.end());
66         std::sort(gpu_times.begin(), gpu_times.end());
67 
68         double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
69         double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();
70 
71         std::cout << "CPU : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << std::endl;
72         std::cout << "GPU : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << std::endl;
73     }
74 
75     return 0;
76 }
77 
78 #else
79 
main()80 int main()
81 {
82     std::cout << "OpenCV was built without CUDA Video decoding support\n" << std::endl;
83     return 0;
84 }
85 
86 #endif
87