gapi_graph_meta_tests.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. //
  5. // Copyright (C) 2020 Intel Corporation
  6. #include <tuple>
  7. #include <unordered_set>
  8. #include "test_precomp.hpp"
  9. #include "opencv2/gapi/streaming/meta.hpp"
  10. #include "opencv2/gapi/streaming/cap.hpp"
  11. namespace opencv_test {
  12. TEST(GraphMeta, Trad_AccessInput) {
  13. cv::GMat in;
  14. cv::GMat out1 = cv::gapi::blur(in, cv::Size(3,3));
  15. cv::GOpaque<int> out2 = cv::gapi::streaming::meta<int>(in, "foo");
  16. cv::GComputation graph(cv::GIn(in), cv::GOut(out1, out2));
  17. cv::Mat in_mat = cv::Mat::eye(cv::Size(64, 64), CV_8UC1);
  18. cv::Mat out_mat;
  19. int out_meta = 0;
  20. // manually set metadata in the input fields
  21. auto inputs = cv::gin(in_mat);
  22. inputs[0].meta["foo"] = 42;
  23. graph.apply(std::move(inputs), cv::gout(out_mat, out_meta));
  24. EXPECT_EQ(42, out_meta);
  25. }
  26. TEST(GraphMeta, Trad_AccessTmp) {
  27. cv::GMat in;
  28. cv::GMat tmp = cv::gapi::blur(in, cv::Size(3,3));
  29. cv::GMat out1 = tmp+1;
  30. cv::GOpaque<float> out2 = cv::gapi::streaming::meta<float>(tmp, "bar");
  31. cv::GComputation graph(cv::GIn(in), cv::GOut(out1, out2));
  32. cv::Mat in_mat = cv::Mat::eye(cv::Size(64, 64), CV_8UC1);
  33. cv::Mat out_mat;
  34. float out_meta = 0.f;
  35. // manually set metadata in the input fields
  36. auto inputs = cv::gin(in_mat);
  37. inputs[0].meta["bar"] = 1.f;
  38. graph.apply(std::move(inputs), cv::gout(out_mat, out_meta));
  39. EXPECT_EQ(1.f, out_meta);
  40. }
  41. TEST(GraphMeta, Trad_AccessOutput) {
  42. cv::GMat in;
  43. cv::GMat out1 = cv::gapi::blur(in, cv::Size(3,3));
  44. cv::GOpaque<std::string> out2 = cv::gapi::streaming::meta<std::string>(out1, "baz");
  45. cv::GComputation graph(cv::GIn(in), cv::GOut(out1, out2));
  46. cv::Mat in_mat = cv::Mat::eye(cv::Size(64, 64), CV_8UC1);
  47. cv::Mat out_mat;
  48. std::string out_meta;
  49. // manually set metadata in the input fields
  50. auto inputs = cv::gin(in_mat);
  51. // NOTE: Assigning explicitly an std::string is important,
  52. // otherwise a "const char*" will be stored and won't be
  53. // translated properly by util::any since std::string is
  54. // used within the graph.
  55. inputs[0].meta["baz"] = std::string("opencv");
  56. graph.apply(std::move(inputs), cv::gout(out_mat, out_meta));
  57. EXPECT_EQ("opencv", out_meta);
  58. }
  59. TEST(GraphMeta, Streaming_AccessInput) {
  60. cv::GMat in;
  61. cv::GMat out1 = cv::gapi::blur(in, cv::Size(3,3));
  62. cv::GOpaque<int64_t> out2 = cv::gapi::streaming::seq_id(in);
  63. cv::GComputation graph(cv::GIn(in), cv::GOut(out1, out2));
  64. auto ccomp = graph.compileStreaming();
  65. const auto path = findDataFile("cv/video/768x576.avi");
  66. try {
  67. ccomp.setSource<cv::gapi::wip::GCaptureSource>(path);
  68. } catch(...) {
  69. throw SkipTestException("Video file can not be opened");
  70. }
  71. ccomp.start();
  72. cv::Mat out_mat;
  73. int64_t out_meta = 0;
  74. int64_t expected_counter = 0;
  75. while (ccomp.pull(cv::gout(out_mat, out_meta))) {
  76. EXPECT_EQ(expected_counter, out_meta);
  77. ++expected_counter;
  78. }
  79. }
  80. TEST(GraphMeta, Streaming_AccessOutput) {
  81. cv::GMat in;
  82. cv::GMat out1 = cv::gapi::blur(in, cv::Size(3,3));
  83. cv::GOpaque<int64_t> out2 = cv::gapi::streaming::seq_id(out1);
  84. cv::GOpaque<int64_t> out3 = cv::gapi::streaming::timestamp(out1);
  85. cv::GComputation graph(cv::GIn(in), cv::GOut(out1, out2, out3));
  86. auto ccomp = graph.compileStreaming();
  87. const auto path = findDataFile("cv/video/768x576.avi");
  88. try {
  89. ccomp.setSource<cv::gapi::wip::GCaptureSource>(path);
  90. } catch(...) {
  91. throw SkipTestException("Video file can not be opened");
  92. }
  93. ccomp.start();
  94. cv::Mat out_mat;
  95. int64_t out_meta = 0;
  96. int64_t out_timestamp = 0;
  97. int64_t expected_counter = 0;
  98. int64_t prev_timestamp = -1;
  99. while (ccomp.pull(cv::gout(out_mat, out_meta, out_timestamp))) {
  100. EXPECT_EQ(expected_counter, out_meta);
  101. ++expected_counter;
  102. EXPECT_NE(prev_timestamp, out_timestamp);
  103. prev_timestamp = out_timestamp;
  104. }
  105. }
  106. TEST(GraphMeta, Streaming_AccessDesync) {
  107. cv::GMat in;
  108. cv::GOpaque<int64_t> out1 = cv::gapi::streaming::seq_id(in);
  109. cv::GOpaque<int64_t> out2 = cv::gapi::streaming::timestamp(in);
  110. cv::GMat out3 = cv::gapi::blur(in, cv::Size(3,3));
  111. cv::GMat tmp = cv::gapi::streaming::desync(in);
  112. cv::GScalar mean = cv::gapi::mean(tmp);
  113. cv::GOpaque<int64_t> out4 = cv::gapi::streaming::seq_id(mean);
  114. cv::GOpaque<int64_t> out5 = cv::gapi::streaming::timestamp(mean);
  115. cv::GComputation graph(cv::GIn(in), cv::GOut(out1, out2, out3, out4, out5));
  116. auto ccomp = graph.compileStreaming();
  117. const auto path = findDataFile("cv/video/768x576.avi");
  118. try {
  119. ccomp.setSource<cv::gapi::wip::GCaptureSource>(path);
  120. } catch(...) {
  121. throw SkipTestException("Video file can not be opened");
  122. }
  123. ccomp.start();
  124. cv::optional<int64_t> out_sync_id;
  125. cv::optional<int64_t> out_sync_ts;
  126. cv::optional<cv::Mat> out_sync_mat;
  127. cv::optional<int64_t> out_desync_id;
  128. cv::optional<int64_t> out_desync_ts;
  129. std::unordered_set<int64_t> sync_ids;
  130. std::unordered_set<int64_t> desync_ids;
  131. while (ccomp.pull(cv::gout(out_sync_id, out_sync_ts, out_sync_mat,
  132. out_desync_id, out_desync_ts))) {
  133. if (out_sync_id.has_value()) {
  134. CV_Assert(out_sync_ts.has_value());
  135. CV_Assert(out_sync_mat.has_value());
  136. sync_ids.insert(out_sync_id.value());
  137. }
  138. if (out_desync_id.has_value()) {
  139. CV_Assert(out_desync_ts.has_value());
  140. desync_ids.insert(out_desync_id.value());
  141. }
  142. }
  143. // Visually report that everything is really ok
  144. std::cout << sync_ids.size() << " vs " << desync_ids.size() << std::endl;
  145. // Desync path should generate less objects than the synchronized one
  146. EXPECT_GE(sync_ids.size(), desync_ids.size());
  147. // ..but all desynchronized IDs must be present in the synchronized set
  148. for (auto &&d_id : desync_ids) {
  149. EXPECT_TRUE(sync_ids.count(d_id) > 0);
  150. }
  151. }
  152. } // namespace opencv_test