Merge pull request #20151 from smirnov-alexey:as/extend_media_frame
authorAlexey Smirnov <alexey.smirnov@intel.com>
Tue, 8 Jun 2021 08:58:51 +0000 (11:58 +0300)
committerGitHub <noreply@github.com>
Tue, 8 Jun 2021 08:58:51 +0000 (11:58 +0300)
G-API: Extend MediaFrame to be able to extract additional info besides access

* Extend MediaFrame to be able to extract additional info besides access

* Add default implementation for blobParams()

* Add comment on the default blobParams()

modules/gapi/include/opencv2/gapi/media.hpp
modules/gapi/src/api/media.cpp
modules/gapi/test/gapi_frame_tests.cpp
modules/gapi/test/infer/gapi_infer_ie_test.cpp

index 3d7f5a5..61a7098 100644 (file)
@@ -13,6 +13,7 @@
 #include <utility>    // forward<>()
 
 #include <opencv2/gapi/gframe.hpp>
 #include <utility>    // forward<>()
 
 #include <opencv2/gapi/gframe.hpp>
+#include <opencv2/gapi/util/any.hpp>
 
 namespace cv {
 
 
 namespace cv {
 
@@ -30,6 +31,10 @@ public:
     View access(Access) const;
     cv::GFrameDesc desc() const;
 
     View access(Access) const;
     cv::GFrameDesc desc() const;
 
+    // FIXME: design a better solution
+    // Should be used only if the actual adapter provides implementation
+    cv::util::any blobParams() const;
+
     // Cast underlying MediaFrame adapter to the particular adapter type,
     // return nullptr if underlying type is different
     template<typename T> T* get() const
     // Cast underlying MediaFrame adapter to the particular adapter type,
     // return nullptr if underlying type is different
     template<typename T> T* get() const
@@ -78,6 +83,9 @@ public:
     virtual ~IAdapter() = 0;
     virtual cv::GFrameDesc meta() const = 0;
     virtual MediaFrame::View access(MediaFrame::Access) = 0;
     virtual ~IAdapter() = 0;
     virtual cv::GFrameDesc meta() const = 0;
     virtual MediaFrame::View access(MediaFrame::Access) = 0;
+    // FIXME: design a better solution
+    // The default implementation does nothing
+    virtual cv::util::any blobParams() const;
 };
 
 } //namespace cv
 };
 
 } //namespace cv
index 3c171b2..884fc9e 100644 (file)
@@ -26,6 +26,11 @@ cv::MediaFrame::View cv::MediaFrame::access(Access code) const {
     return m->adapter->access(code);
 }
 
     return m->adapter->access(code);
 }
 
+cv::util::any cv::MediaFrame::blobParams() const
+{
+    return m->adapter->blobParams();
+}
+
 cv::MediaFrame::IAdapter* cv::MediaFrame::getAdapter() const {
     return m->adapter.get();
 }
 cv::MediaFrame::IAdapter* cv::MediaFrame::getAdapter() const {
     return m->adapter.get();
 }
@@ -42,5 +47,11 @@ cv::MediaFrame::View::~View() {
     }
 }
 
     }
 }
 
+cv::util::any cv::MediaFrame::IAdapter::blobParams() const
+{
+    // Does nothing by default
+    return {};
+}
+
 cv::MediaFrame::IAdapter::~IAdapter() {
 }
 cv::MediaFrame::IAdapter::~IAdapter() {
 }
index ab27108..5911ef9 100644 (file)
@@ -174,4 +174,11 @@ TEST(MediaFrame, Callback) {
     EXPECT_EQ(3, counter);
 }
 
     EXPECT_EQ(3, counter);
 }
 
+TEST(MediaFrame, blobParams) {
+    cv::Mat bgr = cv::Mat::eye(240, 320, CV_8UC3);
+    cv::MediaFrame frame = cv::MediaFrame::Create<TestMediaBGR>(bgr);
+
+    EXPECT_NO_THROW(frame.blobParams());
+}
+
 } // namespace opencv_test
 } // namespace opencv_test
index 4ea33f7..e33f165 100644 (file)
@@ -56,6 +56,15 @@ public:
         cv::MediaFrame::View::Strides ss = { m_mat.step, 0u, 0u, 0u };
         return cv::MediaFrame::View(std::move(pp), std::move(ss), Cb{m_cb});
     }
         cv::MediaFrame::View::Strides ss = { m_mat.step, 0u, 0u, 0u };
         return cv::MediaFrame::View(std::move(pp), std::move(ss), Cb{m_cb});
     }
+    cv::util::any blobParams() const override {
+        return std::make_pair<InferenceEngine::TensorDesc,
+                              InferenceEngine::ParamMap>({IE::Precision::U8,
+                                                          {1, 3, 300, 300},
+                                                          IE::Layout::NCHW},
+                                                         {{"HELLO", 42},
+                                                          {"COLOR_FORMAT",
+                                                           InferenceEngine::ColorFormat::NV12}});
+    }
 };
 
 class TestMediaNV12 final: public cv::MediaFrame::IAdapter {
 };
 
 class TestMediaNV12 final: public cv::MediaFrame::IAdapter {
@@ -2028,6 +2037,21 @@ TEST_F(ROIList, CallInferMultipleTimes)
     validate();
 }
 
     validate();
 }
 
+TEST(IEFrameAdapter, blobParams)
+{
+    cv::Mat bgr = cv::Mat::eye(240, 320, CV_8UC3);
+    cv::MediaFrame frame = cv::MediaFrame::Create<TestMediaBGR>(bgr);
+
+    auto expected = std::make_pair(IE::TensorDesc{IE::Precision::U8, {1, 3, 300, 300},
+                                                  IE::Layout::NCHW},
+                                   IE::ParamMap{{"HELLO", 42}, {"COLOR_FORMAT",
+                                                                IE::ColorFormat::NV12}});
+
+    auto actual = cv::util::any_cast<decltype(expected)>(frame.blobParams());
+
+    EXPECT_EQ(expected, actual);
+}
+
 } // namespace opencv_test
 
 #endif //  HAVE_INF_ENGINE
 } // namespace opencv_test
 
 #endif //  HAVE_INF_ENGINE