mirror of
https://github.com/opencv/opencv.git
synced 2025-06-10 19:24:07 +08:00
Merge pull request #20163 from smirnov-alexey:as/gapi_serialization_docs
G-API: add documentation on serialization functionality * Add documentation on serialization/deserialization * Add docs on bind() methods * Fix typo * Docs refactoring * Fix s11n docs * Fix deserialize() docs * Change deserialize docs * Fix warning * Address review comments * Fix sample * Fix warnings and errors * Fix docs warnings * Fix warnings * Address review comments * Add prefixes to snippets and fix indentation * Address review comments and move snippets to a single file
This commit is contained in:
parent
c0f63eb21f
commit
59ae0e0013
@ -2,7 +2,7 @@
|
|||||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
// 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.
|
// of this distribution and at http://opencv.org/license.html.
|
||||||
//
|
//
|
||||||
// Copyright (C) 2018 Intel Corporation
|
// Copyright (C) 2018-2021 Intel Corporation
|
||||||
|
|
||||||
|
|
||||||
#ifndef OPENCV_GAPI_HPP
|
#ifndef OPENCV_GAPI_HPP
|
||||||
@ -19,6 +19,7 @@
|
|||||||
@}
|
@}
|
||||||
@defgroup gapi_std_backends G-API Standard Backends
|
@defgroup gapi_std_backends G-API Standard Backends
|
||||||
@defgroup gapi_compile_args G-API Graph Compilation Arguments
|
@defgroup gapi_compile_args G-API Graph Compilation Arguments
|
||||||
|
@defgroup gapi_serialization G-API Serialization functionality
|
||||||
@}
|
@}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
// 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.
|
// of this distribution and at http://opencv.org/license.html.
|
||||||
//
|
//
|
||||||
// Copyright (C) 2018-2020 Intel Corporation
|
// Copyright (C) 2018-2021 Intel Corporation
|
||||||
|
|
||||||
|
|
||||||
#ifndef OPENCV_GAPI_GARG_HPP
|
#ifndef OPENCV_GAPI_GARG_HPP
|
||||||
@ -171,7 +171,7 @@ using GRunArgs = std::vector<GRunArg>;
|
|||||||
* It's an ordinary overload of addition assignment operator.
|
* It's an ordinary overload of addition assignment operator.
|
||||||
*
|
*
|
||||||
* Example of usage:
|
* Example of usage:
|
||||||
* @snippet dynamic_graph.cpp GRunArgs usage
|
* @snippet modules/gapi/samples/dynamic_graph.cpp GRunArgs usage
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
inline GRunArgs& operator += (GRunArgs &lhs, const GRunArgs &rhs)
|
inline GRunArgs& operator += (GRunArgs &lhs, const GRunArgs &rhs)
|
||||||
@ -223,7 +223,7 @@ using GRunArgsP = std::vector<GRunArgP>;
|
|||||||
* It's an ordinary overload of addition assignment operator.
|
* It's an ordinary overload of addition assignment operator.
|
||||||
*
|
*
|
||||||
* Example of usage:
|
* Example of usage:
|
||||||
* @snippet dynamic_graph.cpp GRunArgsP usage
|
* @snippet modules/gapi/samples/dynamic_graph.cpp GRunArgsP usage
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
inline GRunArgsP& operator += (GRunArgsP &lhs, const GRunArgsP &rhs)
|
inline GRunArgsP& operator += (GRunArgsP &lhs, const GRunArgsP &rhs)
|
||||||
@ -235,8 +235,39 @@ inline GRunArgsP& operator += (GRunArgsP &lhs, const GRunArgsP &rhs)
|
|||||||
|
|
||||||
namespace gapi
|
namespace gapi
|
||||||
{
|
{
|
||||||
GAPI_EXPORTS cv::GRunArgsP bind(cv::GRunArgs &results);
|
/**
|
||||||
|
* \addtogroup gapi_serialization
|
||||||
|
* @{
|
||||||
|
*
|
||||||
|
* @brief G-API functions and classes for serialization and deserialization.
|
||||||
|
*/
|
||||||
|
/** @brief Wraps deserialized output GRunArgs to GRunArgsP which can be used by GCompiled.
|
||||||
|
*
|
||||||
|
* Since it's impossible to get modifiable output arguments from deserialization
|
||||||
|
* it needs to be wrapped by this function.
|
||||||
|
*
|
||||||
|
* Example of usage:
|
||||||
|
* @snippet modules/gapi/samples/api_ref_snippets.cpp bind after deserialization
|
||||||
|
*
|
||||||
|
* @param out_args deserialized GRunArgs.
|
||||||
|
* @return the same GRunArgs wrapped in GRunArgsP.
|
||||||
|
* @see deserialize
|
||||||
|
*/
|
||||||
|
GAPI_EXPORTS cv::GRunArgsP bind(cv::GRunArgs &out_args);
|
||||||
|
/** @brief Wraps output GRunArgsP available during graph execution to GRunArgs which can be serialized.
|
||||||
|
*
|
||||||
|
* GRunArgsP is pointer-to-value, so to be serialized they need to be binded to real values
|
||||||
|
* which this function does.
|
||||||
|
*
|
||||||
|
* Example of usage:
|
||||||
|
* @snippet modules/gapi/samples/api_ref_snippets.cpp bind before serialization
|
||||||
|
*
|
||||||
|
* @param out output GRunArgsP available during graph execution.
|
||||||
|
* @return the same GRunArgsP wrapped in serializable GRunArgs.
|
||||||
|
* @see serialize
|
||||||
|
*/
|
||||||
GAPI_EXPORTS cv::GRunArg bind(cv::GRunArgP &out); // FIXME: think more about it
|
GAPI_EXPORTS cv::GRunArg bind(cv::GRunArgP &out); // FIXME: think more about it
|
||||||
|
/** @} */
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Ts> inline GRunArgs gin(const Ts&... args)
|
template<typename... Ts> inline GRunArgs gin(const Ts&... args)
|
||||||
|
@ -71,7 +71,7 @@ public:
|
|||||||
* It's an ordinary overload of addition assignment operator.
|
* It's an ordinary overload of addition assignment operator.
|
||||||
*
|
*
|
||||||
* Example of usage:
|
* Example of usage:
|
||||||
* @snippet dynamic_graph.cpp GIOProtoArgs usage
|
* @snippet modules/gapi/samples/dynamic_graph.cpp GIOProtoArgs usage
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
template<typename Tg>
|
template<typename Tg>
|
||||||
|
@ -17,65 +17,135 @@
|
|||||||
namespace cv {
|
namespace cv {
|
||||||
namespace gapi {
|
namespace gapi {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \addtogroup gapi_serialization
|
||||||
|
* @{
|
||||||
|
*/
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
GAPI_EXPORTS cv::GComputation getGraph(const std::vector<char> &p);
|
GAPI_EXPORTS cv::GComputation getGraph(const std::vector<char> &bytes);
|
||||||
|
|
||||||
GAPI_EXPORTS cv::GMetaArgs getMetaArgs(const std::vector<char> &p);
|
GAPI_EXPORTS cv::GMetaArgs getMetaArgs(const std::vector<char> &bytes);
|
||||||
|
|
||||||
GAPI_EXPORTS cv::GRunArgs getRunArgs(const std::vector<char> &p);
|
GAPI_EXPORTS cv::GRunArgs getRunArgs(const std::vector<char> &bytes);
|
||||||
|
|
||||||
GAPI_EXPORTS std::vector<std::string> getVectorOfStrings(const std::vector<char> &p);
|
GAPI_EXPORTS std::vector<std::string> getVectorOfStrings(const std::vector<char> &bytes);
|
||||||
|
|
||||||
template<typename... Types>
|
template<typename... Types>
|
||||||
cv::GCompileArgs getCompileArgs(const std::vector<char> &p);
|
cv::GCompileArgs getCompileArgs(const std::vector<char> &bytes);
|
||||||
|
|
||||||
template<typename RMatAdapterType>
|
template<typename RMatAdapterType>
|
||||||
cv::GRunArgs getRunArgsWithRMats(const std::vector<char> &p);
|
cv::GRunArgs getRunArgsWithRMats(const std::vector<char> &bytes);
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
|
/** @brief Serialize a graph represented by GComputation into an array of bytes.
|
||||||
|
*
|
||||||
|
* Check different overloads for more examples.
|
||||||
|
* @param c GComputation to serialize.
|
||||||
|
* @return serialized vector of bytes.
|
||||||
|
*/
|
||||||
GAPI_EXPORTS std::vector<char> serialize(const cv::GComputation &c);
|
GAPI_EXPORTS std::vector<char> serialize(const cv::GComputation &c);
|
||||||
//namespace{
|
|
||||||
|
|
||||||
|
/** @overload
|
||||||
|
* @param ca GCompileArgs to serialize.
|
||||||
|
*/
|
||||||
|
GAPI_EXPORTS std::vector<char> serialize(const cv::GCompileArgs& ca);
|
||||||
|
|
||||||
|
/** @overload
|
||||||
|
* @param ma GMetaArgs to serialize.
|
||||||
|
*/
|
||||||
|
GAPI_EXPORTS std::vector<char> serialize(const cv::GMetaArgs& ma);
|
||||||
|
|
||||||
|
/** @overload
|
||||||
|
* @param ra GRunArgs to serialize.
|
||||||
|
*/
|
||||||
|
GAPI_EXPORTS std::vector<char> serialize(const cv::GRunArgs& ra);
|
||||||
|
|
||||||
|
/** @overload
|
||||||
|
* @param vs std::vector<std::string> to serialize.
|
||||||
|
*/
|
||||||
|
GAPI_EXPORTS std::vector<char> serialize(const std::vector<std::string>& vs);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
template<typename T> static inline
|
template<typename T> static inline
|
||||||
T deserialize(const std::vector<char> &p);
|
T deserialize(const std::vector<char> &bytes);
|
||||||
|
|
||||||
//} //ananymous namespace
|
|
||||||
|
|
||||||
GAPI_EXPORTS std::vector<char> serialize(const cv::GCompileArgs&);
|
|
||||||
GAPI_EXPORTS std::vector<char> serialize(const cv::GMetaArgs&);
|
|
||||||
GAPI_EXPORTS std::vector<char> serialize(const cv::GRunArgs&);
|
|
||||||
GAPI_EXPORTS std::vector<char> serialize(const std::vector<std::string>&);
|
|
||||||
|
|
||||||
|
/** @brief Deserialize GComputation from a byte array.
|
||||||
|
*
|
||||||
|
* Check different overloads for more examples.
|
||||||
|
* @param bytes serialized vector of bytes.
|
||||||
|
* @return deserialized GComputation object.
|
||||||
|
*/
|
||||||
template<> inline
|
template<> inline
|
||||||
cv::GComputation deserialize(const std::vector<char> &p) {
|
cv::GComputation deserialize(const std::vector<char> &bytes) {
|
||||||
return detail::getGraph(p);
|
return detail::getGraph(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @brief Deserialize GMetaArgs from a byte array.
|
||||||
|
*
|
||||||
|
* Check different overloads for more examples.
|
||||||
|
* @param bytes serialized vector of bytes.
|
||||||
|
* @return deserialized GMetaArgs object.
|
||||||
|
*/
|
||||||
template<> inline
|
template<> inline
|
||||||
cv::GMetaArgs deserialize(const std::vector<char> &p) {
|
cv::GMetaArgs deserialize(const std::vector<char> &bytes) {
|
||||||
return detail::getMetaArgs(p);
|
return detail::getMetaArgs(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @brief Deserialize GRunArgs from a byte array.
|
||||||
|
*
|
||||||
|
* Check different overloads for more examples.
|
||||||
|
* @param bytes serialized vector of bytes.
|
||||||
|
* @return deserialized GRunArgs object.
|
||||||
|
*/
|
||||||
template<> inline
|
template<> inline
|
||||||
cv::GRunArgs deserialize(const std::vector<char> &p) {
|
cv::GRunArgs deserialize(const std::vector<char> &bytes) {
|
||||||
return detail::getRunArgs(p);
|
return detail::getRunArgs(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @brief Deserialize std::vector<std::string> from a byte array.
|
||||||
|
*
|
||||||
|
* Check different overloads for more examples.
|
||||||
|
* @param bytes serialized vector of bytes.
|
||||||
|
* @return deserialized std::vector<std::string> object.
|
||||||
|
*/
|
||||||
template<> inline
|
template<> inline
|
||||||
std::vector<std::string> deserialize(const std::vector<char> &p) {
|
std::vector<std::string> deserialize(const std::vector<char> &bytes) {
|
||||||
return detail::getVectorOfStrings(p);
|
return detail::getVectorOfStrings(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Deserialize GCompileArgs which types were specified in the template from a byte array.
|
||||||
|
*
|
||||||
|
* @note cv::gapi::s11n::detail::S11N template specialization must be provided to make a custom type
|
||||||
|
* in GCompileArgs deserializable.
|
||||||
|
*
|
||||||
|
* @param bytes vector of bytes to deserialize GCompileArgs object from.
|
||||||
|
* @return GCompileArgs object.
|
||||||
|
* @see GCompileArgs cv::gapi::s11n::detail::S11N
|
||||||
|
*/
|
||||||
template<typename T, typename... Types> inline
|
template<typename T, typename... Types> inline
|
||||||
typename std::enable_if<std::is_same<T, GCompileArgs>::value, GCompileArgs>::
|
typename std::enable_if<std::is_same<T, GCompileArgs>::value, GCompileArgs>::
|
||||||
type deserialize(const std::vector<char> &p) {
|
type deserialize(const std::vector<char> &bytes) {
|
||||||
return detail::getCompileArgs<Types...>(p);
|
return detail::getCompileArgs<Types...>(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Deserialize GRunArgs including RMat objects if any from a byte array.
|
||||||
|
*
|
||||||
|
* RMat adapter type is specified in the template.
|
||||||
|
* @note To be used properly specified adapter type must overload its serialize() and
|
||||||
|
* deserialize() methods.
|
||||||
|
* @param bytes vector of bytes to deserialize GRunArgs object from.
|
||||||
|
* @return GRunArgs including RMat objects if any.
|
||||||
|
* @see RMat
|
||||||
|
*/
|
||||||
template<typename T, typename RMatAdapterType> inline
|
template<typename T, typename RMatAdapterType> inline
|
||||||
typename std::enable_if<std::is_same<T, GRunArgs>::value, GRunArgs>::
|
typename std::enable_if<std::is_same<T, GRunArgs>::value, GRunArgs>::
|
||||||
type deserialize(const std::vector<char> &p) {
|
type deserialize(const std::vector<char> &bytes) {
|
||||||
return detail::getRunArgsWithRMats<RMatAdapterType>(p);
|
return detail::getRunArgsWithRMats<RMatAdapterType>(bytes);
|
||||||
}
|
}
|
||||||
} // namespace gapi
|
} // namespace gapi
|
||||||
} // namespace cv
|
} // namespace cv
|
||||||
@ -83,6 +153,17 @@ type deserialize(const std::vector<char> &p) {
|
|||||||
namespace cv {
|
namespace cv {
|
||||||
namespace gapi {
|
namespace gapi {
|
||||||
namespace s11n {
|
namespace s11n {
|
||||||
|
|
||||||
|
/** @brief This structure is an interface for serialization routines.
|
||||||
|
*
|
||||||
|
* It's main purpose is to provide multiple overloads for operator<<()
|
||||||
|
* with basic C++ in addition to OpenCV/G-API types.
|
||||||
|
*
|
||||||
|
* This sctructure can be inherited and further extended with additional types.
|
||||||
|
*
|
||||||
|
* For example, it is utilized in cv::gapi::s11n::detail::S11N as input parameter
|
||||||
|
* in serialize() method.
|
||||||
|
*/
|
||||||
struct GAPI_EXPORTS IOStream {
|
struct GAPI_EXPORTS IOStream {
|
||||||
virtual ~IOStream() = default;
|
virtual ~IOStream() = default;
|
||||||
// Define the native support for basic C++ types at the API level:
|
// Define the native support for basic C++ types at the API level:
|
||||||
@ -99,6 +180,16 @@ struct GAPI_EXPORTS IOStream {
|
|||||||
virtual IOStream& operator<< (const std::string&) = 0;
|
virtual IOStream& operator<< (const std::string&) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** @brief This structure is an interface for deserialization routines.
|
||||||
|
*
|
||||||
|
* It's main purpose is to provide multiple overloads for operator>>()
|
||||||
|
* with basic C++ in addition to OpenCV/G-API types.
|
||||||
|
*
|
||||||
|
* This structure can be inherited and further extended with additional types.
|
||||||
|
*
|
||||||
|
* For example, it is utilized in cv::gapi::s11n::detail::S11N as input parameter
|
||||||
|
* in deserialize() method.
|
||||||
|
*/
|
||||||
struct GAPI_EXPORTS IIStream {
|
struct GAPI_EXPORTS IIStream {
|
||||||
virtual ~IIStream() = default;
|
virtual ~IIStream() = default;
|
||||||
virtual IIStream& operator>> (bool &) = 0;
|
virtual IIStream& operator>> (bool &) = 0;
|
||||||
@ -116,7 +207,7 @@ struct GAPI_EXPORTS IIStream {
|
|||||||
};
|
};
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
GAPI_EXPORTS std::unique_ptr<IIStream> getInStream(const std::vector<char> &p);
|
GAPI_EXPORTS std::unique_ptr<IIStream> getInStream(const std::vector<char> &bytes);
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
@ -146,24 +237,26 @@ GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::Mat &m);
|
|||||||
|
|
||||||
// FIXME: for GRunArgs serailization
|
// FIXME: for GRunArgs serailization
|
||||||
#if !defined(GAPI_STANDALONE)
|
#if !defined(GAPI_STANDALONE)
|
||||||
GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::UMat &);
|
GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::UMat & um);
|
||||||
GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::UMat &);
|
GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::UMat & um);
|
||||||
#endif // !defined(GAPI_STANDALONE)
|
#endif // !defined(GAPI_STANDALONE)
|
||||||
|
|
||||||
GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::RMat &r);
|
GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::RMat &r);
|
||||||
GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::RMat &r);
|
GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::RMat &r);
|
||||||
|
|
||||||
GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::gapi::wip::IStreamSource::Ptr &);
|
GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::gapi::wip::IStreamSource::Ptr &issptr);
|
||||||
GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::gapi::wip::IStreamSource::Ptr &);
|
GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::gapi::wip::IStreamSource::Ptr &issptr);
|
||||||
|
|
||||||
GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::detail::VectorRef &);
|
GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::detail::VectorRef &vr);
|
||||||
GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::detail::VectorRef &);
|
GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::detail::VectorRef &vr);
|
||||||
|
|
||||||
GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::detail::OpaqueRef &);
|
GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::detail::OpaqueRef &opr);
|
||||||
GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::detail::OpaqueRef &);
|
GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::detail::OpaqueRef &opr);
|
||||||
|
|
||||||
GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::MediaFrame &);
|
/// @private -- Exclude this function from OpenCV documentation
|
||||||
GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::MediaFrame &);
|
GAPI_EXPORTS IOStream& operator<< (IOStream& os, const cv::MediaFrame &mf);
|
||||||
|
/// @private -- Exclude this function from OpenCV documentation
|
||||||
|
GAPI_EXPORTS IIStream& operator>> (IIStream& is, cv::MediaFrame &mf);
|
||||||
|
|
||||||
// Generic STL types ////////////////////////////////////////////////////////////////
|
// Generic STL types ////////////////////////////////////////////////////////////////
|
||||||
template<typename K, typename V>
|
template<typename K, typename V>
|
||||||
@ -186,6 +279,7 @@ IIStream& operator>> (IIStream& is, std::map<K, V> &m) {
|
|||||||
}
|
}
|
||||||
return is;
|
return is;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename K, typename V>
|
template<typename K, typename V>
|
||||||
IOStream& operator<< (IOStream& os, const std::unordered_map<K, V> &m) {
|
IOStream& operator<< (IOStream& os, const std::unordered_map<K, V> &m) {
|
||||||
const uint32_t sz = static_cast<uint32_t>(m.size());
|
const uint32_t sz = static_cast<uint32_t>(m.size());
|
||||||
@ -206,6 +300,7 @@ IIStream& operator>> (IIStream& is, std::unordered_map<K, V> &m) {
|
|||||||
}
|
}
|
||||||
return is;
|
return is;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
IOStream& operator<< (IOStream& os, const std::vector<T> &ts) {
|
IOStream& operator<< (IOStream& os, const std::vector<T> &ts) {
|
||||||
const uint32_t sz = static_cast<uint32_t>(ts.size());
|
const uint32_t sz = static_cast<uint32_t>(ts.size());
|
||||||
@ -233,16 +328,19 @@ template<typename V>
|
|||||||
IOStream& put_v(IOStream&, const V&, std::size_t) {
|
IOStream& put_v(IOStream&, const V&, std::size_t) {
|
||||||
GAPI_Assert(false && "variant>>: requested index is invalid");
|
GAPI_Assert(false && "variant>>: requested index is invalid");
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename V, typename X, typename... Xs>
|
template<typename V, typename X, typename... Xs>
|
||||||
IOStream& put_v(IOStream& os, const V& v, std::size_t x) {
|
IOStream& put_v(IOStream& os, const V& v, std::size_t x) {
|
||||||
return (x == 0u)
|
return (x == 0u)
|
||||||
? os << cv::util::get<X>(v)
|
? os << cv::util::get<X>(v)
|
||||||
: put_v<V, Xs...>(os, v, x-1);
|
: put_v<V, Xs...>(os, v, x-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename V>
|
template<typename V>
|
||||||
IIStream& get_v(IIStream&, V&, std::size_t, std::size_t) {
|
IIStream& get_v(IIStream&, V&, std::size_t, std::size_t) {
|
||||||
GAPI_Assert(false && "variant<<: requested index is invalid");
|
GAPI_Assert(false && "variant<<: requested index is invalid");
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename V, typename X, typename... Xs>
|
template<typename V, typename X, typename... Xs>
|
||||||
IIStream& get_v(IIStream& is, V& v, std::size_t i, std::size_t gi) {
|
IIStream& get_v(IIStream& is, V& v, std::size_t i, std::size_t gi) {
|
||||||
if (i == gi) {
|
if (i == gi) {
|
||||||
@ -254,11 +352,13 @@ IIStream& get_v(IIStream& is, V& v, std::size_t i, std::size_t gi) {
|
|||||||
}
|
}
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
|
//! @overload
|
||||||
template<typename... Ts>
|
template<typename... Ts>
|
||||||
IOStream& operator<< (IOStream& os, const cv::util::variant<Ts...> &v) {
|
IOStream& operator<< (IOStream& os, const cv::util::variant<Ts...> &v) {
|
||||||
os << static_cast<uint32_t>(v.index());
|
os << static_cast<uint32_t>(v.index());
|
||||||
return detail::put_v<cv::util::variant<Ts...>, Ts...>(os, v, v.index());
|
return detail::put_v<cv::util::variant<Ts...>, Ts...>(os, v, v.index());
|
||||||
}
|
}
|
||||||
|
//! @overload
|
||||||
template<typename... Ts>
|
template<typename... Ts>
|
||||||
IIStream& operator>> (IIStream& is, cv::util::variant<Ts...> &v) {
|
IIStream& operator>> (IIStream& is, cv::util::variant<Ts...> &v) {
|
||||||
int idx = -1;
|
int idx = -1;
|
||||||
@ -268,6 +368,7 @@ IIStream& operator>> (IIStream& is, cv::util::variant<Ts...> &v) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: consider a better solution
|
// FIXME: consider a better solution
|
||||||
|
/// @private -- Exclude this function from OpenCV documentation
|
||||||
template<typename... Ts>
|
template<typename... Ts>
|
||||||
void getRunArgByIdx (IIStream& is, cv::util::variant<Ts...> &v, uint32_t idx) {
|
void getRunArgByIdx (IIStream& is, cv::util::variant<Ts...> &v, uint32_t idx) {
|
||||||
is = detail::get_v<cv::util::variant<Ts...>, Ts...>(is, v, 0u, idx);
|
is = detail::get_v<cv::util::variant<Ts...>, Ts...>(is, v, 0u, idx);
|
||||||
@ -351,8 +452,8 @@ cv::GCompileArgs getCompileArgs(const std::vector<char> &sArgs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename RMatAdapterType>
|
template<typename RMatAdapterType>
|
||||||
cv::GRunArgs getRunArgsWithRMats(const std::vector<char> &p) {
|
cv::GRunArgs getRunArgsWithRMats(const std::vector<char> &bytes) {
|
||||||
std::unique_ptr<cv::gapi::s11n::IIStream> pIs = cv::gapi::s11n::detail::getInStream(p);
|
std::unique_ptr<cv::gapi::s11n::IIStream> pIs = cv::gapi::s11n::detail::getInStream(bytes);
|
||||||
cv::gapi::s11n::IIStream& is = *pIs;
|
cv::gapi::s11n::IIStream& is = *pIs;
|
||||||
cv::GRunArgs args;
|
cv::GRunArgs args;
|
||||||
|
|
||||||
@ -367,6 +468,8 @@ cv::GRunArgs getRunArgsWithRMats(const std::vector<char> &p) {
|
|||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
/** @} */
|
||||||
|
|
||||||
} // namespace gapi
|
} // namespace gapi
|
||||||
} // namespace cv
|
} // namespace cv
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
// 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.
|
// of this distribution and at http://opencv.org/license.html.
|
||||||
//
|
//
|
||||||
// Copyright (C) 2020 Intel Corporation
|
// Copyright (C) 2020-2021 Intel Corporation
|
||||||
|
|
||||||
#ifndef OPENCV_GAPI_S11N_BASE_HPP
|
#ifndef OPENCV_GAPI_S11N_BASE_HPP
|
||||||
#define OPENCV_GAPI_S11N_BASE_HPP
|
#define OPENCV_GAPI_S11N_BASE_HPP
|
||||||
@ -23,25 +23,54 @@ struct IIStream;
|
|||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
|
//! @addtogroup gapi_serialization
|
||||||
|
//! @{
|
||||||
|
|
||||||
struct NotImplemented {
|
struct NotImplemented {
|
||||||
};
|
};
|
||||||
|
|
||||||
// The default S11N for custom types is NotImplemented
|
/** @brief This structure allows to implement serialization routines for custom types.
|
||||||
// Don't! sublass from NotImplemented if you actually implement S11N.
|
*
|
||||||
|
* The default S11N for custom types is not implemented.
|
||||||
|
*
|
||||||
|
* @note When providing an overloaded implementation for S11N with your type
|
||||||
|
* don't inherit it from NotImplemented structure.
|
||||||
|
*
|
||||||
|
* @note There are lots of overloaded >> and << operators for basic and OpenCV/G-API types
|
||||||
|
* which can be utilized when serializing a custom type.
|
||||||
|
*
|
||||||
|
* Example of usage:
|
||||||
|
* @snippet modules/gapi/samples/api_ref_snippets.cpp S11N usage
|
||||||
|
*
|
||||||
|
*/
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct S11N: public NotImplemented {
|
struct S11N: public NotImplemented {
|
||||||
|
/**
|
||||||
|
* @brief This function allows user to serialize their custom type.
|
||||||
|
*
|
||||||
|
* @note The default overload throws an exception if called. User need to
|
||||||
|
* properly overload the function to use it.
|
||||||
|
*/
|
||||||
static void serialize(IOStream &, const T &) {
|
static void serialize(IOStream &, const T &) {
|
||||||
GAPI_Assert(false && "No serialization routine is provided!");
|
GAPI_Assert(false && "No serialization routine is provided!");
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* @brief This function allows user to deserialize their custom type.
|
||||||
|
*
|
||||||
|
* @note The default overload throws an exception if called. User need to
|
||||||
|
* properly overload the function to use it.
|
||||||
|
*/
|
||||||
static T deserialize(IIStream &) {
|
static T deserialize(IIStream &) {
|
||||||
GAPI_Assert(false && "No deserialization routine is provided!");
|
GAPI_Assert(false && "No deserialization routine is provided!");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// @private -- Exclude this struct from OpenCV documentation
|
||||||
template<typename T> struct has_S11N_spec {
|
template<typename T> struct has_S11N_spec {
|
||||||
static constexpr bool value = !std::is_base_of<NotImplemented,
|
static constexpr bool value = !std::is_base_of<NotImplemented,
|
||||||
S11N<typename std::decay<T>::type>>::value;
|
S11N<typename std::decay<T>::type>>::value;
|
||||||
};
|
};
|
||||||
|
//! @} gapi_serialization
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
} // namespace s11n
|
} // namespace s11n
|
||||||
|
@ -4,6 +4,10 @@
|
|||||||
#include <opencv2/gapi/core.hpp>
|
#include <opencv2/gapi/core.hpp>
|
||||||
#include <opencv2/gapi/imgproc.hpp>
|
#include <opencv2/gapi/imgproc.hpp>
|
||||||
|
|
||||||
|
#include <opencv2/gapi/s11n.hpp>
|
||||||
|
#include <opencv2/gapi/garg.hpp>
|
||||||
|
#include <opencv2/gapi/gcommon.hpp>
|
||||||
|
|
||||||
#include <opencv2/gapi/cpu/gcpukernel.hpp>
|
#include <opencv2/gapi/cpu/gcpukernel.hpp>
|
||||||
|
|
||||||
#include <opencv2/gapi/fluid/core.hpp>
|
#include <opencv2/gapi/fluid/core.hpp>
|
||||||
@ -55,6 +59,120 @@ static void typed_example()
|
|||||||
//! [Typed_Example]
|
//! [Typed_Example]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void bind_serialization_example()
|
||||||
|
{
|
||||||
|
// ! [bind after deserialization]
|
||||||
|
cv::GCompiled compd;
|
||||||
|
std::vector<char> bytes;
|
||||||
|
auto graph = cv::gapi::deserialize<cv::GComputation>(bytes);
|
||||||
|
auto meta = cv::gapi::deserialize<cv::GMetaArgs>(bytes);
|
||||||
|
|
||||||
|
compd = graph.compile(std::move(meta), cv::compile_args());
|
||||||
|
auto in_args = cv::gapi::deserialize<cv::GRunArgs>(bytes);
|
||||||
|
auto out_args = cv::gapi::deserialize<cv::GRunArgs>(bytes);
|
||||||
|
compd(std::move(in_args), cv::gapi::bind(out_args));
|
||||||
|
// ! [bind after deserialization]
|
||||||
|
}
|
||||||
|
|
||||||
|
static void bind_deserialization_example()
|
||||||
|
{
|
||||||
|
// ! [bind before serialization]
|
||||||
|
std::vector<cv::GRunArgP> graph_outs;
|
||||||
|
cv::GRunArgs out_args;
|
||||||
|
|
||||||
|
for (auto &&out : graph_outs) {
|
||||||
|
out_args.emplace_back(cv::gapi::bind(out));
|
||||||
|
}
|
||||||
|
const auto sargsout = cv::gapi::serialize(out_args);
|
||||||
|
// ! [bind before serialization]
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SimpleCustomType {
|
||||||
|
bool val;
|
||||||
|
bool operator==(const SimpleCustomType& other) const {
|
||||||
|
return val == other.val;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SimpleCustomType2 {
|
||||||
|
int val;
|
||||||
|
std::string name;
|
||||||
|
std::vector<float> vec;
|
||||||
|
std::map<int, uint64_t> mmap;
|
||||||
|
bool operator==(const SimpleCustomType2& other) const {
|
||||||
|
return val == other.val && name == other.name &&
|
||||||
|
vec == other.vec && mmap == other.mmap;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// ! [S11N usage]
|
||||||
|
namespace cv {
|
||||||
|
namespace gapi {
|
||||||
|
namespace s11n {
|
||||||
|
namespace detail {
|
||||||
|
template<> struct S11N<SimpleCustomType> {
|
||||||
|
static void serialize(IOStream &os, const SimpleCustomType &p) {
|
||||||
|
os << p.val;
|
||||||
|
}
|
||||||
|
static SimpleCustomType deserialize(IIStream &is) {
|
||||||
|
SimpleCustomType p;
|
||||||
|
is >> p.val;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> struct S11N<SimpleCustomType2> {
|
||||||
|
static void serialize(IOStream &os, const SimpleCustomType2 &p) {
|
||||||
|
os << p.val << p.name << p.vec << p.mmap;
|
||||||
|
}
|
||||||
|
static SimpleCustomType2 deserialize(IIStream &is) {
|
||||||
|
SimpleCustomType2 p;
|
||||||
|
is >> p.val >> p.name >> p.vec >> p.mmap;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace detail
|
||||||
|
} // namespace s11n
|
||||||
|
} // namespace gapi
|
||||||
|
} // namespace cv
|
||||||
|
// ! [S11N usage]
|
||||||
|
|
||||||
|
namespace cv {
|
||||||
|
namespace detail {
|
||||||
|
template<> struct CompileArgTag<SimpleCustomType> {
|
||||||
|
static const char* tag() {
|
||||||
|
return "org.opencv.test.simple_custom_type";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> struct CompileArgTag<SimpleCustomType2> {
|
||||||
|
static const char* tag() {
|
||||||
|
return "org.opencv.test.simple_custom_type_2";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace detail
|
||||||
|
} // namespace cv
|
||||||
|
|
||||||
|
static void s11n_example()
|
||||||
|
{
|
||||||
|
SimpleCustomType customVar1 { false };
|
||||||
|
SimpleCustomType2 customVar2 { 1248, "World", {1280, 720, 640, 480},
|
||||||
|
{ {5, 32434142342}, {7, 34242432} } };
|
||||||
|
|
||||||
|
std::vector<char> sArgs = cv::gapi::serialize(
|
||||||
|
cv::compile_args(customVar1, customVar2));
|
||||||
|
|
||||||
|
cv::GCompileArgs dArgs = cv::gapi::deserialize<cv::GCompileArgs,
|
||||||
|
SimpleCustomType,
|
||||||
|
SimpleCustomType2>(sArgs);
|
||||||
|
|
||||||
|
SimpleCustomType dCustomVar1 = cv::gapi::getCompileArg<SimpleCustomType>(dArgs).value();
|
||||||
|
SimpleCustomType2 dCustomVar2 = cv::gapi::getCompileArg<SimpleCustomType2>(dArgs).value();
|
||||||
|
|
||||||
|
(void) dCustomVar1;
|
||||||
|
(void) dCustomVar2;
|
||||||
|
}
|
||||||
|
|
||||||
G_TYPED_KERNEL(IAdd, <cv::GMat(cv::GMat)>, "test.custom.add") {
|
G_TYPED_KERNEL(IAdd, <cv::GMat(cv::GMat)>, "test.custom.add") {
|
||||||
static cv::GMatDesc outMeta(const cv::GMatDesc &in) { return in; }
|
static cv::GMatDesc outMeta(const cv::GMatDesc &in) { return in; }
|
||||||
};
|
};
|
||||||
@ -128,5 +246,8 @@ int main(int argc, char *argv[])
|
|||||||
// unused functions
|
// unused functions
|
||||||
typed_example();
|
typed_example();
|
||||||
gscalar_example();
|
gscalar_example();
|
||||||
|
bind_serialization_example();
|
||||||
|
bind_deserialization_example();
|
||||||
|
s11n_example();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -65,11 +65,11 @@ std::vector<char> cv::gapi::serialize(const std::vector<std::string>& vs)
|
|||||||
|
|
||||||
// FIXME: This function should move from S11N to GRunArg-related entities.
|
// FIXME: This function should move from S11N to GRunArg-related entities.
|
||||||
// it has nothing to do with the S11N as it is
|
// it has nothing to do with the S11N as it is
|
||||||
cv::GRunArgsP cv::gapi::bind(cv::GRunArgs &results)
|
cv::GRunArgsP cv::gapi::bind(cv::GRunArgs &out_args)
|
||||||
{
|
{
|
||||||
cv::GRunArgsP outputs;
|
cv::GRunArgsP outputs;
|
||||||
outputs.reserve(results.size());
|
outputs.reserve(out_args.size());
|
||||||
for (cv::GRunArg &res_obj : results)
|
for (cv::GRunArg &res_obj : out_args)
|
||||||
{
|
{
|
||||||
using T = cv::GRunArg;
|
using T = cv::GRunArg;
|
||||||
switch (res_obj.index())
|
switch (res_obj.index())
|
||||||
|
@ -754,8 +754,6 @@ TEST_F(S11N_Basic, Test_Deserialize_CompileArgs_RandomOrder) {
|
|||||||
std::vector<char> sArgs = cv::gapi::serialize(
|
std::vector<char> sArgs = cv::gapi::serialize(
|
||||||
cv::compile_args(simpleCustomVar, simpleCustomVar2));
|
cv::compile_args(simpleCustomVar, simpleCustomVar2));
|
||||||
GCompileArgs dArgs = cv::gapi::deserialize<GCompileArgs,
|
GCompileArgs dArgs = cv::gapi::deserialize<GCompileArgs,
|
||||||
// Here, types of passed to serialize() arguments
|
|
||||||
// are enumerated in reverse order
|
|
||||||
SimpleCustomType2,
|
SimpleCustomType2,
|
||||||
SimpleCustomType>(sArgs);
|
SimpleCustomType>(sArgs);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user