Blame view

3rdparty/opencv-4.5.4/modules/gapi/test/gapi_graph_meta_tests.cpp 6.62 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  // 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.
  //
  // Copyright (C) 2020 Intel Corporation
  
  #include <tuple>
  #include <unordered_set>
  
  #include "test_precomp.hpp"
  #include "opencv2/gapi/streaming/meta.hpp"
  #include "opencv2/gapi/streaming/cap.hpp"
  
  namespace opencv_test {
  
  namespace {
  void initTestDataPath() {
  #ifndef WINRT
      static bool initialized = false;
      if (!initialized)
      {
          // Since G-API has no own test data (yet), it is taken from the common space
          const char* testDataPath = getenv("OPENCV_TEST_DATA_PATH");
          if (testDataPath != nullptr) {
              cvtest::addDataSearchPath(testDataPath);
              initialized = true;
          }
      }
  #endif // WINRT
  }
  } // anonymous namespace
  
  TEST(GraphMeta, Trad_AccessInput) {
      cv::GMat in;
      cv::GMat out1 = cv::gapi::blur(in, cv::Size(3,3));
      cv::GOpaque<int> out2 = cv::gapi::streaming::meta<int>(in, "foo");
      cv::GComputation graph(cv::GIn(in), cv::GOut(out1, out2));
  
      cv::Mat in_mat = cv::Mat::eye(cv::Size(64, 64), CV_8UC1);
      cv::Mat out_mat;
      int out_meta = 0;
  
      // manually set metadata in the input fields
      auto inputs = cv::gin(in_mat);
      inputs[0].meta["foo"] = 42;
  
      graph.apply(std::move(inputs), cv::gout(out_mat, out_meta));
      EXPECT_EQ(42, out_meta);
  }
  
  TEST(GraphMeta, Trad_AccessTmp) {
      cv::GMat in;
      cv::GMat tmp = cv::gapi::blur(in, cv::Size(3,3));
      cv::GMat out1 = tmp+1;
      cv::GOpaque<float> out2 = cv::gapi::streaming::meta<float>(tmp, "bar");
      cv::GComputation graph(cv::GIn(in), cv::GOut(out1, out2));
  
      cv::Mat in_mat = cv::Mat::eye(cv::Size(64, 64), CV_8UC1);
      cv::Mat out_mat;
      float out_meta = 0.f;
  
      // manually set metadata in the input fields
      auto inputs = cv::gin(in_mat);
      inputs[0].meta["bar"] = 1.f;
  
      graph.apply(std::move(inputs), cv::gout(out_mat, out_meta));
      EXPECT_EQ(1.f, out_meta);
  }
  
  TEST(GraphMeta, Trad_AccessOutput) {
      cv::GMat in;
      cv::GMat out1 = cv::gapi::blur(in, cv::Size(3,3));
      cv::GOpaque<std::string> out2 = cv::gapi::streaming::meta<std::string>(out1, "baz");
      cv::GComputation graph(cv::GIn(in), cv::GOut(out1, out2));
  
      cv::Mat in_mat = cv::Mat::eye(cv::Size(64, 64), CV_8UC1);
      cv::Mat out_mat;
      std::string out_meta;
  
      // manually set metadata in the input fields
      auto inputs = cv::gin(in_mat);
  
      // NOTE: Assigning explicitly an std::string is important,
      // otherwise a "const char*" will be stored and won't be
      // translated properly by util::any since std::string is
      // used within the graph.
      inputs[0].meta["baz"] = std::string("opencv");
  
      graph.apply(std::move(inputs), cv::gout(out_mat, out_meta));
      EXPECT_EQ("opencv", out_meta);
  }
  
  TEST(GraphMeta, Streaming_AccessInput) {
      initTestDataPath();
  
      cv::GMat in;
      cv::GMat out1 = cv::gapi::blur(in, cv::Size(3,3));
      cv::GOpaque<int64_t> out2 = cv::gapi::streaming::seq_id(in);
      cv::GComputation graph(cv::GIn(in), cv::GOut(out1, out2));
  
      auto ccomp = graph.compileStreaming();
      const auto path = findDataFile("cv/video/768x576.avi");
      try {
          ccomp.setSource<cv::gapi::wip::GCaptureSource>(path);
      } catch(...) {
          throw SkipTestException("Video file can not be opened");
      }
      ccomp.start();
  
      cv::Mat out_mat;
      int64_t out_meta = 0;
      int64_t expected_counter = 0;
  
      while (ccomp.pull(cv::gout(out_mat, out_meta))) {
          EXPECT_EQ(expected_counter, out_meta);
          ++expected_counter;
      }
  }
  
  TEST(GraphMeta, Streaming_AccessOutput) {
      initTestDataPath();
  
      cv::GMat in;
      cv::GMat out1 = cv::gapi::blur(in, cv::Size(3,3));
      cv::GOpaque<int64_t> out2 = cv::gapi::streaming::seq_id(out1);
      cv::GOpaque<int64_t> out3 = cv::gapi::streaming::timestamp(out1);
      cv::GComputation graph(cv::GIn(in), cv::GOut(out1, out2, out3));
  
      auto ccomp = graph.compileStreaming();
      const auto path = findDataFile("cv/video/768x576.avi");
      try {
          ccomp.setSource<cv::gapi::wip::GCaptureSource>(path);
      } catch(...) {
          throw SkipTestException("Video file can not be opened");
      }
      ccomp.start();
  
      cv::Mat out_mat;
      int64_t out_meta = 0;
      int64_t out_timestamp = 0;
      int64_t expected_counter = 0;
      int64_t prev_timestamp = -1;
  
      while (ccomp.pull(cv::gout(out_mat, out_meta, out_timestamp))) {
          EXPECT_EQ(expected_counter, out_meta);
          ++expected_counter;
  
          EXPECT_NE(prev_timestamp, out_timestamp);
          prev_timestamp = out_timestamp;
      }
  }
  
  TEST(GraphMeta, Streaming_AccessDesync) {
      initTestDataPath();
  
      cv::GMat in;
      cv::GOpaque<int64_t> out1 = cv::gapi::streaming::seq_id(in);
      cv::GOpaque<int64_t> out2 = cv::gapi::streaming::timestamp(in);
      cv::GMat             out3 = cv::gapi::blur(in, cv::Size(3,3));
  
      cv::GMat tmp = cv::gapi::streaming::desync(in);
      cv::GScalar mean = cv::gapi::mean(tmp);
      cv::GOpaque<int64_t> out4 = cv::gapi::streaming::seq_id(mean);
      cv::GOpaque<int64_t> out5 = cv::gapi::streaming::timestamp(mean);
      cv::GComputation graph(cv::GIn(in), cv::GOut(out1, out2, out3, out4, out5));
  
      auto ccomp = graph.compileStreaming();
      const auto path = findDataFile("cv/video/768x576.avi");
      try {
          ccomp.setSource<cv::gapi::wip::GCaptureSource>(path);
      } catch(...) {
          throw SkipTestException("Video file can not be opened");
      }
      ccomp.start();
  
      cv::optional<int64_t> out_sync_id;
      cv::optional<int64_t> out_sync_ts;
      cv::optional<cv::Mat> out_sync_mat;
  
      cv::optional<int64_t> out_desync_id;
      cv::optional<int64_t> out_desync_ts;
  
      std::unordered_set<int64_t> sync_ids;
      std::unordered_set<int64_t> desync_ids;
  
      while (ccomp.pull(cv::gout(out_sync_id, out_sync_ts, out_sync_mat,
                                 out_desync_id, out_desync_ts))) {
          if (out_sync_id.has_value()) {
              CV_Assert(out_sync_ts.has_value());
              CV_Assert(out_sync_mat.has_value());
              sync_ids.insert(out_sync_id.value());
          }
          if (out_desync_id.has_value()) {
              CV_Assert(out_desync_ts.has_value());
              desync_ids.insert(out_desync_id.value());
          }
      }
      // Visually report that everything is really ok
      std::cout << sync_ids.size() << " vs " << desync_ids.size() << std::endl;
  
      // Desync path should generate less objects than the synchronized one
      EXPECT_GE(sync_ids.size(), desync_ids.size());
  
      // ..but all desynchronized IDs must be present in the synchronized set
      for (auto &&d_id : desync_ids) {
          EXPECT_TRUE(sync_ids.count(d_id) > 0);
      }
  }
  
  } // namespace opencv_test