desync.hpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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-2021 Intel Corporation
  6. #ifndef OPENCV_GAPI_GSTREAMING_DESYNC_HPP
  7. #define OPENCV_GAPI_GSTREAMING_DESYNC_HPP
  8. #include <tuple>
  9. #include <opencv2/gapi/util/util.hpp>
  10. #include <opencv2/gapi/gtype_traits.hpp>
  11. #include <opencv2/gapi/garg.hpp>
  12. #include <opencv2/gapi/gcall.hpp>
  13. #include <opencv2/gapi/gkernel.hpp>
  14. namespace cv {
  15. namespace gapi {
  16. namespace streaming {
  17. namespace detail {
  18. struct GDesync {
  19. static const char *id() {
  20. return "org.opencv.streaming.desync";
  21. }
  22. // An universal yield for desync.
  23. // Yields output objects according to the input Types...
  24. // Reuses gkernel machinery.
  25. // FIXME: This function can be generic and declared in gkernel.hpp
  26. // (it is there already, but a part of GKernelType[M]
  27. template<typename... R, int... IIs>
  28. static std::tuple<R...> yield(cv::GCall &call, cv::detail::Seq<IIs...>) {
  29. return std::make_tuple(cv::detail::Yield<R>::yield(call, IIs)...);
  30. }
  31. };
  32. template<typename G>
  33. G desync(const G &g) {
  34. cv::GKernel k{
  35. GDesync::id() // kernel id
  36. , "" // kernel tag
  37. , [](const GMetaArgs &a, const GArgs &) {return a;} // outMeta callback
  38. , {cv::detail::GTypeTraits<G>::shape} // output Shape
  39. , {cv::detail::GTypeTraits<G>::op_kind} // input data kinds
  40. , {cv::detail::GObtainCtor<G>::get()} // output template ctors
  41. , {cv::detail::GTypeTraits<G>::op_kind} // output data kinds
  42. };
  43. cv::GCall call(std::move(k));
  44. call.pass(g);
  45. return std::get<0>(GDesync::yield<G>(call, cv::detail::MkSeq<1>::type()));
  46. }
  47. } // namespace detail
  48. /**
  49. * @brief Starts a desynchronized branch in the graph.
  50. *
  51. * This operation takes a single G-API data object and returns a
  52. * graph-level "duplicate" of this object.
  53. *
  54. * Operations which use this data object can be desynchronized
  55. * from the rest of the graph.
  56. *
  57. * This operation has no effect when a GComputation is compiled with
  58. * regular cv::GComputation::compile(), since cv::GCompiled objects
  59. * always produce their full output vectors.
  60. *
  61. * This operation only makes sense when a GComputation is compiled in
  62. * streaming mode with cv::GComputation::compileStreaming(). If this
  63. * operation is used and there are desynchronized outputs, the user
  64. * should use a special version of cv::GStreamingCompiled::pull()
  65. * which produces an array of cv::util::optional<> objects.
  66. *
  67. * @note This feature is highly experimental now and is currently
  68. * limited to a single GMat/GFrame argument only.
  69. */
  70. GAPI_EXPORTS GMat desync(const GMat &g);
  71. GAPI_EXPORTS GFrame desync(const GFrame &f);
  72. } // namespace streaming
  73. } // namespace gapi
  74. } // namespace cv
  75. #endif // OPENCV_GAPI_GSTREAMING_DESYNC_HPP