gcompiled.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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) 2018-2020 Intel Corporation
  6. #ifndef OPENCV_GAPI_GCOMPILED_HPP
  7. #define OPENCV_GAPI_GCOMPILED_HPP
  8. #include <vector>
  9. #include <opencv2/gapi/opencv_includes.hpp>
  10. #include <opencv2/gapi/own/assert.hpp>
  11. #include <opencv2/gapi/garg.hpp>
  12. namespace cv {
  13. // This class represents a compiled computation.
  14. // In theory (and ideally), it can be used w/o the rest of APIs.
  15. // In theory (and ideally), it can be serialized/deserialized.
  16. // It can enable scenarious like deployment to an autonomous devince, FuSa, etc.
  17. //
  18. // Currently GCompiled assumes all GMats you used to pass data to G-API
  19. // are valid and not destroyed while you use a GCompiled object.
  20. //
  21. // FIXME: In future, there should be a way to name I/O objects and specify it
  22. // to GCompiled externally (for example, when it is loaded on the target system).
  23. /**
  24. * \addtogroup gapi_main_classes
  25. * @{
  26. */
  27. /**
  28. * @brief Represents a compiled computation (graph). Can only be used
  29. * with image / data formats & resolutions it was compiled for, with
  30. * some exceptions.
  31. *
  32. * This class represents a product of graph compilation (calling
  33. * cv::GComputation::compile()). Objects of this class actually do
  34. * data processing, and graph execution is incapsulated into objects
  35. * of this class. Execution model itself depends on kernels and
  36. * backends which were using during the compilation, see @ref
  37. * gapi_compile_args for details.
  38. *
  39. * In a general case, GCompiled objects can be applied to data only in
  40. * that formats/resolutions they were compiled for (see @ref
  41. * gapi_meta_args). However, if the underlying backends allow, a
  42. * compiled object can be _reshaped_ to handle data (images) of
  43. * different resolution, though formats and types must remain the same.
  44. *
  45. * GCompiled is very similar to `std::function<>` in its semantics --
  46. * running it looks like a function call in the user code.
  47. *
  48. * At the moment, GCompiled objects are not reentrant -- generally,
  49. * the objects are stateful since graph execution itself is a stateful
  50. * process and this state is now maintained in GCompiled's own memory
  51. * (not on the process stack).
  52. *
  53. * At the same time, two different GCompiled objects produced from the
  54. * single cv::GComputation are completely independent and can be used
  55. * concurrently.
  56. *
  57. * @sa GStreamingCompiled
  58. */
  59. class GAPI_EXPORTS GCompiled
  60. {
  61. public:
  62. /// @private
  63. class GAPI_EXPORTS Priv;
  64. /**
  65. * @brief Constructs an empty object
  66. */
  67. GCompiled();
  68. /**
  69. * @brief Run the compiled computation, a generic version.
  70. *
  71. * @param ins vector of inputs to process.
  72. * @param outs vector of outputs to produce.
  73. *
  74. * Input/output vectors must have the same number of elements as
  75. * defined in the cv::GComputation protocol (at the moment of its
  76. * construction). Shapes of elements also must conform to protocol
  77. * (e.g. cv::Mat needs to be passed where cv::GMat has been
  78. * declared as input, and so on). Run-time exception is generated
  79. * otherwise.
  80. *
  81. * Objects in output vector may remain empty (like cv::Mat) --
  82. * G-API will automatically initialize output objects to proper formats.
  83. *
  84. * @note Don't construct GRunArgs/GRunArgsP objects manually, use
  85. * cv::gin()/cv::gout() wrappers instead.
  86. */
  87. void operator() (GRunArgs &&ins, GRunArgsP &&outs); // Generic arg-to-arg
  88. #if !defined(GAPI_STANDALONE)
  89. /**
  90. * @brief Execute an unary computation
  91. *
  92. * @overload
  93. * @param in input cv::Mat for unary computation
  94. * @param out output cv::Mat for unary computation
  95. * process.
  96. */
  97. void operator() (cv::Mat in, cv::Mat &out); // Unary overload
  98. /**
  99. * @brief Execute an unary computation
  100. *
  101. * @overload
  102. * @param in input cv::Mat for unary computation
  103. * @param out output cv::Scalar for unary computation
  104. * process.
  105. */
  106. void operator() (cv::Mat in, cv::Scalar &out); // Unary overload (scalar)
  107. /**
  108. * @brief Execute a binary computation
  109. *
  110. * @overload
  111. * @param in1 first input cv::Mat for binary computation
  112. * @param in2 second input cv::Mat for binary computation
  113. * @param out output cv::Mat for binary computation
  114. * process.
  115. */
  116. void operator() (cv::Mat in1, cv::Mat in2, cv::Mat &out); // Binary overload
  117. /**
  118. * @brief Execute an binary computation
  119. *
  120. * @overload
  121. * @param in1 first input cv::Mat for binary computation
  122. * @param in2 second input cv::Mat for binary computation
  123. * @param out output cv::Scalar for binary computation
  124. * process.
  125. */
  126. void operator() (cv::Mat in1, cv::Mat in2, cv::Scalar &out); // Binary overload (scalar)
  127. /**
  128. * @brief Execute a computation with arbitrary number of
  129. * inputs/outputs.
  130. *
  131. * @overload
  132. * @param ins vector of input cv::Mat objects to process by the
  133. * computation.
  134. * @param outs vector of output cv::Mat objects to produce by the
  135. * computation.
  136. *
  137. * Numbers of elements in ins/outs vectors must match numbers of
  138. * inputs/outputs which were used to define the source GComputation.
  139. */
  140. void operator() (const std::vector<cv::Mat> &ins, // Compatibility overload
  141. const std::vector<cv::Mat> &outs);
  142. #endif // !defined(GAPI_STANDALONE)
  143. /// @private
  144. Priv& priv();
  145. /**
  146. * @brief Check if compiled object is valid (non-empty)
  147. *
  148. * @return true if the object is runnable (valid), false otherwise
  149. */
  150. explicit operator bool () const;
  151. /**
  152. * @brief Vector of metadata this graph was compiled for.
  153. *
  154. * @return Unless _reshape_ is not supported, return value is the
  155. * same vector which was passed to cv::GComputation::compile() to
  156. * produce this compiled object. Otherwise, it is the latest
  157. * metadata vector passed to reshape() (if that call was
  158. * successful).
  159. */
  160. const GMetaArgs& metas() const; // Meta passed to compile()
  161. /**
  162. * @brief Vector of metadata descriptions of graph outputs
  163. *
  164. * @return vector with formats/resolutions of graph's output
  165. * objects, auto-inferred from input metadata vector by
  166. * operations which form this computation.
  167. *
  168. * @note GCompiled objects produced from the same
  169. * cv::GComputiation graph with different input metas may return
  170. * different values in this vector.
  171. */
  172. const GMetaArgs& outMetas() const;
  173. /**
  174. * @brief Check if the underlying backends support reshape or not.
  175. *
  176. * @return true if supported, false otherwise.
  177. */
  178. bool canReshape() const;
  179. /**
  180. * @brief Reshape a compiled graph to support new image
  181. * resolutions.
  182. *
  183. * Throws an exception if an error occurs.
  184. *
  185. * @param inMetas new metadata to reshape on. Vector size and
  186. * metadata shapes must match the computation's protocol.
  187. * @param args compilation arguments to use.
  188. */
  189. // FIXME: Why it requires compile args?
  190. void reshape(const GMetaArgs& inMetas, const GCompileArgs& args);
  191. /**
  192. * @brief Prepare inner kernels states for a new video-stream.
  193. *
  194. * GCompiled objects may be used to process video streams frame by frame.
  195. * In this case, a GCompiled is called on every image frame individually.
  196. * Starting OpenCV 4.4, some kernels in the graph may have their internal
  197. * states (see GAPI_OCV_KERNEL_ST for the OpenCV backend).
  198. * In this case, if user starts processing another video stream with
  199. * this GCompiled, this method needs to be called to let kernels re-initialize
  200. * their internal states to a new video stream.
  201. */
  202. void prepareForNewStream();
  203. protected:
  204. /// @private
  205. std::shared_ptr<Priv> m_priv;
  206. };
  207. /** @} */
  208. }
  209. #endif // OPENCV_GAPI_GCOMPILED_HPP