Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/gpu/video_reader.cpp
16337 views
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
int main(int argc, const char* argv[])
18
{
19
if (argc != 2)
20
return -1;
21
22
const std::string fname(argv[1]);
23
24
cv::namedWindow("CPU", cv::WINDOW_NORMAL);
25
cv::namedWindow("GPU", cv::WINDOW_OPENGL);
26
cv::cuda::setGlDevice();
27
28
cv::Mat frame;
29
cv::VideoCapture reader(fname);
30
31
cv::cuda::GpuMat d_frame;
32
cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname);
33
34
cv::TickMeter tm;
35
std::vector<double> cpu_times;
36
std::vector<double> gpu_times;
37
38
int gpu_frame_count=0, cpu_frame_count=0;
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
cpu_frame_count++;
48
49
cv::imshow("CPU", frame);
50
51
if (cv::waitKey(3) > 0)
52
break;
53
}
54
55
for (;;)
56
{
57
tm.reset(); tm.start();
58
if (!d_reader->nextFrame(d_frame))
59
break;
60
tm.stop();
61
gpu_times.push_back(tm.getTimeMilli());
62
gpu_frame_count++;
63
64
cv::imshow("GPU", d_frame);
65
66
if (cv::waitKey(3) > 0)
67
break;
68
}
69
70
if (!cpu_times.empty() && !gpu_times.empty())
71
{
72
std::cout << std::endl << "Results:" << std::endl;
73
74
std::sort(cpu_times.begin(), cpu_times.end());
75
std::sort(gpu_times.begin(), gpu_times.end());
76
77
double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
78
double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();
79
80
std::cout << "CPU : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << " Frames " << cpu_frame_count << std::endl;
81
std::cout << "GPU : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << " Frames " << gpu_frame_count << std::endl;
82
}
83
84
return 0;
85
}
86
87
#else
88
89
int main()
90
{
91
std::cout << "OpenCV was built without CUDA Video decoding support\n" << std::endl;
92
return 0;
93
}
94
95
#endif
96
97