Blame view

3rdparty/opencv-4.5.4/modules/videoio/test/test_dynamic.cpp 3.95 KB
f4334277   Hu Chunming   提交3rdparty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  // This file is part of OpenCV project.
  // It is subject to the license terms in the LICENSE file found in the top-level directory
  // of this distribution and at http://opencv.org/license.html.
  
  #include "test_precomp.hpp"
  
  using namespace std;
  
  namespace opencv_test { namespace {
  
  const int FRAME_COUNT = 120;
  
  inline void generateFrame(int i, Mat & frame)
  {
      ::generateFrame(i, FRAME_COUNT, frame);
  }
  
  TEST(videoio_dynamic, basic_write)
  {
      const Size FRAME_SIZE(640, 480);
      const double FPS = 100;
      const String filename = cv::tempfile(".avi");
      const int fourcc = VideoWriter::fourcc('M', 'J', 'P', 'G');
  
      bool fileExists = false;
      {
          vector<VideoCaptureAPIs> backends = videoio_registry::getWriterBackends();
          for (VideoCaptureAPIs be : backends)
          {
              VideoWriter writer;
              writer.open(filename, be, fourcc, FPS, FRAME_SIZE, true);
              if (writer.isOpened())
              {
                  Mat frame(FRAME_SIZE, CV_8UC3);
                  for (int j = 0; j < FRAME_COUNT; ++j)
                  {
                      generateFrame(j, frame);
                      writer << frame;
                  }
                  writer.release();
                  fileExists = true;
              }
              EXPECT_FALSE(writer.isOpened());
          }
      }
      if (!fileExists)
      {
          cout << "None of backends has been able to write video file - SKIP reading part" << endl;
          return;
      }
      {
          vector<VideoCaptureAPIs> backends = videoio_registry::getStreamBackends();
          for (VideoCaptureAPIs be : backends)
          {
              VideoCapture cap;
              cap.open(filename, be);
              if(cap.isOpened())
              {
                  int count = 0;
                  while (true)
                  {
                      Mat frame;
                      if (cap.grab())
                      {
                          if (cap.retrieve(frame))
                          {
                              ++count;
                              continue;
                          }
                      }
                      break;
                  }
                  EXPECT_EQ(count, FRAME_COUNT);
                  cap.release();
              }
              EXPECT_FALSE(cap.isOpened());
          }
      }
      remove(filename.c_str());
  }
  
  TEST(videoio_dynamic, write_invalid)
  {
      vector<VideoCaptureAPIs> backends = videoio_registry::getWriterBackends();
      for (VideoCaptureAPIs be : backends)
      {
          SCOPED_TRACE(be);
          const string filename = cv::tempfile(".mkv");
          VideoWriter writer;
          bool res = true;
  
          // Bad FourCC
          EXPECT_NO_THROW(res = writer.open(filename, be, VideoWriter::fourcc('A', 'B', 'C', 'D'), 1, Size(640, 480), true));
          EXPECT_FALSE(res);
          EXPECT_FALSE(writer.isOpened());
  
          // Empty filename
          EXPECT_NO_THROW(res = writer.open(String(), be, VideoWriter::fourcc('H', '2', '6', '4'), 1, Size(640, 480), true));
          EXPECT_FALSE(res);
          EXPECT_FALSE(writer.isOpened());
          EXPECT_NO_THROW(res = writer.open(String(), be, VideoWriter::fourcc('M', 'J', 'P', 'G'), 1, Size(640, 480), true));
          EXPECT_FALSE(res);
          EXPECT_FALSE(writer.isOpened());
  
          // zero FPS
          EXPECT_NO_THROW(res = writer.open(filename, be, VideoWriter::fourcc('H', '2', '6', '4'), 0, Size(640, 480), true));
          EXPECT_FALSE(res);
          EXPECT_FALSE(writer.isOpened());
  
          // cleanup
          EXPECT_NO_THROW(writer.release());
          remove(filename.c_str());
      }
  
      // Generic
      {
          VideoWriter writer;
          bool res = true;
          EXPECT_NO_THROW(res = writer.open(std::string(), VideoWriter::fourcc('H', '2', '6', '4'), 1, Size(640, 480)));
          EXPECT_FALSE(res);
          EXPECT_FALSE(writer.isOpened());
          EXPECT_NO_THROW(res = writer.open(std::string(), VideoWriter::fourcc('M', 'J', 'P', 'G'), 1, Size(640, 480)));
          EXPECT_FALSE(res);
          EXPECT_FALSE(writer.isOpened());
      }
  }
  
  
  }} // opencv_test::<anonymous>::