dnn_superres_video.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include <iostream>
  5. #include <opencv2/dnn_superres.hpp>
  6. #include <opencv2/imgproc.hpp>
  7. #include <opencv2/highgui.hpp>
  8. using namespace std;
  9. using namespace cv;
  10. using namespace dnn_superres;
  11. int main(int argc, char *argv[])
  12. {
  13. // Check for valid command line arguments, print usage
  14. // if insufficient arguments were given.
  15. if (argc < 4) {
  16. cout << "usage: Arg 1: input video path" << endl;
  17. cout << "\t Arg 2: output video path" << endl;
  18. cout << "\t Arg 3: algorithm | edsr, espcn, fsrcnn or lapsrn" << endl;
  19. cout << "\t Arg 4: scale | 2, 3, 4 or 8 \n";
  20. cout << "\t Arg 5: path to model file \n";
  21. return -1;
  22. }
  23. string input_path = string(argv[1]);
  24. string output_path = string(argv[2]);
  25. string algorithm = string(argv[3]);
  26. int scale = atoi(argv[4]);
  27. string path = string(argv[5]);
  28. VideoCapture input_video(input_path);
  29. int ex = static_cast<int>(input_video.get(CAP_PROP_FOURCC));
  30. Size S = Size((int) input_video.get(CAP_PROP_FRAME_WIDTH) * scale,
  31. (int) input_video.get(CAP_PROP_FRAME_HEIGHT) * scale);
  32. VideoWriter output_video;
  33. output_video.open(output_path, ex, input_video.get(CAP_PROP_FPS), S, true);
  34. if (!input_video.isOpened())
  35. {
  36. std::cerr << "Could not open the video." << std::endl;
  37. return -1;
  38. }
  39. DnnSuperResImpl sr;
  40. sr.readModel(path);
  41. sr.setModel(algorithm, scale);
  42. for(;;)
  43. {
  44. Mat frame, output_frame;
  45. input_video >> frame;
  46. if ( frame.empty() )
  47. break;
  48. sr.upsample(frame, output_frame);
  49. output_video << output_frame;
  50. namedWindow("Upsampled video", WINDOW_AUTOSIZE);
  51. imshow("Upsampled video", output_frame);
  52. namedWindow("Original video", WINDOW_AUTOSIZE);
  53. imshow("Original video", frame);
  54. char c=(char)waitKey(25);
  55. if(c==27)
  56. break;
  57. }
  58. input_video.release();
  59. output_video.release();
  60. return 0;
  61. }