ot.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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) 2023 Intel Corporation
  6. #ifndef OPENCV_GAPI_OT_HPP
  7. #define OPENCV_GAPI_OT_HPP
  8. #include <opencv2/gapi.hpp>
  9. #include <opencv2/gapi/s11n.hpp>
  10. #include <opencv2/gapi/gkernel.hpp>
  11. namespace cv {
  12. namespace gapi {
  13. /**
  14. * @brief This namespace contains G-API Operation Types for
  15. * VAS Object Tracking module functionality.
  16. */
  17. namespace ot {
  18. /**
  19. * @enum TrackingStatus
  20. *
  21. * Tracking status twin for vas::ot::TrackingStatus
  22. */
  23. enum TrackingStatus
  24. {
  25. NEW = 0, /**< The object is newly added. */
  26. TRACKED, /**< The object is being tracked. */
  27. LOST /**< The object gets lost now. The object can be tracked again
  28. by specifying detected object manually. */
  29. };
  30. struct GAPI_EXPORTS_W_SIMPLE ObjectTrackerParams
  31. {
  32. /**
  33. * Maximum number of trackable objects in a frame.
  34. * Valid range: 1 <= max_num_objects. Or it can be -1 if there is no limitation
  35. * of maximum number in X86. KMB/TBH has limitation up to 1024.
  36. * Default value is -1 which means there is no limitation in X86. KMB/TBH is -1 means 200.
  37. */
  38. GAPI_PROP_RW int32_t max_num_objects = -1;
  39. /**
  40. * Input color format. Supports 0(BGR), 1(NV12), 2(BGRX) and 4(I420)
  41. */
  42. GAPI_PROP_RW int32_t input_image_format = 0;
  43. /**
  44. * Specifies whether tracker to use detection class for keeping id of an object.
  45. * If it is true, new detection will be associated from previous tracking only when
  46. * those two have same class.
  47. * class id of an object is fixed across video frames.
  48. * If it is false, new detection can be associated across different-class objects.
  49. * In this case, the class id of an object may change across video frames depending on the tracker input.
  50. * It is recommended to turn this option off when it is likely that detector confuses the class of object.
  51. * For example, when detector confuses bicycle and motorbike. Turning this option off will increase
  52. * the tracking reliability as tracker will ignore the class label of detector.
  53. * @n
  54. * Default value is true.
  55. */
  56. GAPI_PROP_RW bool tracking_per_class = true;
  57. bool operator==(const ObjectTrackerParams& other) const
  58. {
  59. return max_num_objects == other.max_num_objects
  60. && input_image_format == other.input_image_format
  61. && tracking_per_class == other.tracking_per_class;
  62. }
  63. };
  64. using GTrackedInfo = std::tuple<cv::GArray<cv::Rect>, cv::GArray<int32_t>, cv::GArray<uint64_t>, cv::GArray<int>>;
  65. G_API_OP(GTrackFromMat, <GTrackedInfo(cv::GMat, cv::GArray<cv::Rect>, cv::GArray<int32_t>, float)>, "com.intel.track_from_mat")
  66. {
  67. static std::tuple<cv::GArrayDesc, cv::GArrayDesc,
  68. cv::GArrayDesc, cv::GArrayDesc> outMeta(cv::GMatDesc, cv::GArrayDesc, cv::GArrayDesc, float)
  69. {
  70. return std::make_tuple(cv::empty_array_desc(), cv::empty_array_desc(),
  71. cv::empty_array_desc(), cv::empty_array_desc());
  72. }
  73. };
  74. G_API_OP(GTrackFromFrame, <GTrackedInfo(cv::GFrame, cv::GArray<cv::Rect>, cv::GArray<int32_t>, float)>, "com.intel.track_from_frame")
  75. {
  76. static std::tuple<cv::GArrayDesc, cv::GArrayDesc,
  77. cv::GArrayDesc, cv::GArrayDesc> outMeta(cv::GFrameDesc, cv::GArrayDesc, cv::GArrayDesc, float)
  78. {
  79. return std::make_tuple(cv::empty_array_desc(), cv::empty_array_desc(),
  80. cv::empty_array_desc(), cv::empty_array_desc());
  81. }
  82. };
  83. /**
  84. * @brief Tracks objects with video frames.
  85. * If a detected object is overlapped enough with one of tracked object, the tracked object's
  86. * informationis updated with the input detected object.
  87. * On the other hand, if a detected object is overlapped with none of tracked objects,
  88. * the detected object is newly added and ObjectTracker starts to track the object.
  89. * In zero term tracking type, ObjectTracker clears tracked objects in case that empty
  90. * list of detected objects is passed in.
  91. *
  92. * @param mat Input frame.
  93. * @param detected_rects Detected objects rectangles in the input frame.
  94. * @param detected_class_labels Detected objects class labels in the input frame.
  95. * @param delta Frame_delta_t Delta time between two consecutive tracking in seconds.
  96. * The valid range is [0.005 ~ 0.5].
  97. * @return Tracking results of target objects.
  98. * cv::GArray<cv::Rect> Array of rectangles for tracked objects.
  99. * cv::GArray<int32_t> Array of detected objects labels.
  100. * cv::GArray<uint64_t> Array of tracking IDs for objects.
  101. * Numbering sequence starts from 1.
  102. * The value 0 means the tracking ID of this object has
  103. * not been assigned.
  104. * cv::GArray<int> Array of tracking statuses for objects.
  105. */
  106. GAPI_EXPORTS_W std::tuple<cv::GArray<cv::Rect>,
  107. cv::GArray<int>,
  108. cv::GArray<uint64_t>,
  109. cv::GArray<int>>
  110. track(const cv::GMat& mat,
  111. const cv::GArray<cv::Rect>& detected_rects,
  112. const cv::GArray<int>& detected_class_labels,
  113. float delta);
  114. /**
  115. @overload
  116. * @brief Tracks objects with video frames. Overload of track(...) for frame as GFrame.
  117. *
  118. * @param frame Input frame.
  119. * @param detected_rects Detected objects rectangles in the input frame.
  120. * @param detected_class_labels Detected objects class labels in the input frame.
  121. * @param delta Frame_delta_t Delta time between two consecutive tracking in seconds.
  122. * The valid range is [0.005 ~ 0.5].
  123. * @return Tracking results of target objects.
  124. * @return Tracking results of target objects.
  125. * cv::GArray<cv::Rect> Array of rectangles for tracked objects.
  126. * cv::GArray<int32_t> Array of detected objects labels.
  127. * cv::GArray<uint64_t> Array of tracking IDs for objects.
  128. * Numbering sequence starts from 1.
  129. * The value 0 means the tracking ID of this object has
  130. * not been assigned.
  131. * cv::GArray<int> Array of tracking statuses for objects.
  132. */
  133. GAPI_EXPORTS_W std::tuple<cv::GArray<cv::Rect>,
  134. cv::GArray<int>,
  135. cv::GArray<uint64_t>,
  136. cv::GArray<int>>
  137. track(const cv::GFrame& frame,
  138. const cv::GArray<cv::Rect>& detected_rects,
  139. const cv::GArray<int>& detected_class_labels,
  140. float delta);
  141. } // namespace ot
  142. } // namespace gapi
  143. } // namespace cv
  144. // FIXME: move to a separate file?
  145. namespace cv
  146. {
  147. namespace detail
  148. {
  149. template<> struct CompileArgTag<cv::gapi::ot::ObjectTrackerParams>
  150. {
  151. static const char* tag()
  152. {
  153. return "cv.gapi.ot.object_tracker_params";
  154. }
  155. };
  156. } // namespace detail
  157. namespace gapi
  158. {
  159. namespace s11n
  160. {
  161. namespace detail
  162. {
  163. template<> struct S11N<cv::gapi::ot::ObjectTrackerParams> {
  164. static void serialize(IOStream &os, const cv::gapi::ot::ObjectTrackerParams &p) {
  165. os << p. max_num_objects << p.input_image_format << p.tracking_per_class;
  166. }
  167. static cv::gapi::ot::ObjectTrackerParams deserialize(IIStream &is) {
  168. cv::gapi::ot::ObjectTrackerParams p;
  169. is >> p. max_num_objects >> p.input_image_format >> p.tracking_per_class;
  170. return p;
  171. }
  172. };
  173. } // namespace detail
  174. } // namespace s11n
  175. } // namespace gapi
  176. } // namespace cv
  177. #endif // OPENCV_GAPI_OT_HPP