Skip to content

Instantly share code, notes, and snippets.

@atinfinity
Created December 18, 2016 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atinfinity/a7c098c3ddcf1ea27a53bee7af679e4e to your computer and use it in GitHub Desktop.
Save atinfinity/a7c098c3ddcf1ea27a53bee7af679e4e to your computer and use it in GitHub Desktop.
デコード処理のテストプログラム
#include <opencv2/core.hpp>
#include <opencv2/core/opengl.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/cudacodec.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <numeric>
int decode_cpu(const std::string& fname)
{
cv::namedWindow("CPU", cv::WINDOW_OPENGL);
cv::Mat frame;
cv::VideoCapture reader(fname);
if (!reader.isOpened())
{
return -1;
}
std::vector<double> cpu_times;
double f = 1000.0f / cv::getTickFrequency();
int64 start = 0;
int64 end = 0;
for (;;)
{
start = cv::getTickCount();
reader >> frame;
if (frame.empty())
{
break;
}
end = cv::getTickCount();
cpu_times.push_back((end - start) * f);
cv::imshow("CPU", frame);
if (cv::waitKey(1) > 0)
{
break;
}
}
if (!cpu_times.empty())
{
std::cout << "Results:" << std::endl;
std::sort(cpu_times.begin(), cpu_times.end());
double gpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
std::cout << " GPU : Avg : " << gpu_avg << "[ms], FPS : " << 1000.0 / gpu_avg << std::endl;
}
cv::destroyWindow("CPU");
return 0;
}
int decode_gpu(const std::string& fname)
{
cv::namedWindow("GPU", cv::WINDOW_OPENGL);
cv::cuda::setGlDevice();
cv::cuda::GpuMat d_frame;
cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname);
if (d_reader == NULL)
{
return -1;
}
std::vector<double> gpu_times;
double f = 1000.0f / cv::getTickFrequency();
int64 start = 0;
int64 end = 0;
for (;;)
{
start = cv::getTickCount();
if (!d_reader->nextFrame(d_frame))
{
break;
}
end = cv::getTickCount();
gpu_times.push_back((end - start) * f);
cv::imshow("GPU", d_frame);
if (cv::waitKey(1) > 0)
{
break;
}
}
if (!gpu_times.empty())
{
std::cout << std::endl << "Results:" << std::endl;
std::sort(gpu_times.begin(), gpu_times.end());
double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();
std::cout << " GPU : Avg : " << gpu_avg << "[ms], FPS : " << 1000.0 / gpu_avg << std::endl;
}
cv::destroyWindow("GPU");
return 0;
}
int main(int argc, const char* argv[])
{
if (argc != 2)
{
return -1;
}
const std::string fname(argv[1]);
decode_cpu(fname);
decode_gpu(fname);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment