video_helper.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*************************************************************************
  2. * Copyright (C) [2020] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #ifndef INFER_SERVER_VIDEO_HELPER_H_
  21. #define INFER_SERVER_VIDEO_HELPER_H_
  22. #include <memory>
  23. #include <string>
  24. #include <utility>
  25. #include <vector>
  26. #include "cnis/buffer.h"
  27. #include "cnis/infer_server.h"
  28. #include "cnis/processor.h"
  29. namespace infer_server {
  30. namespace video {
  31. enum class PixelFmt {
  32. I420 = 0,
  33. NV12 = 1,
  34. NV21 = 2,
  35. RGB24 = 3,
  36. BGR24 = 4,
  37. RGBA = 5,
  38. BGRA = 6,
  39. ARGB = 7,
  40. ABGR = 8,
  41. };
  42. struct BoundingBox {
  43. float x{0.f}; ///< The x-axis coordinate in the upper left corner of the bounding box.
  44. float y{0.f}; ///< The y-axis coordinate in the upper left corner of the bounding box.
  45. float w{0.f}; ///< The width of the bounding box.
  46. float h{0.f}; ///< The height of the bounding box.
  47. };
  48. size_t GetPlaneNum(PixelFmt format) noexcept;
  49. struct VideoFrame {
  50. #define MAX_PLANE_NUM 3
  51. Buffer plane[MAX_PLANE_NUM];
  52. size_t stride[MAX_PLANE_NUM] = {0, 0, 0};
  53. size_t width;
  54. size_t height;
  55. size_t plane_num;
  56. PixelFmt format;
  57. BoundingBox roi;
  58. size_t GetPlaneSize(size_t plane_idx) const noexcept;
  59. size_t GetTotalSize() const noexcept;
  60. };
  61. enum class PreprocessType {
  62. UNKNOWN = 0,
  63. RESIZE_CONVERT = 1,
  64. SCALER = 2,
  65. CNCV_PREPROC = 3,
  66. };
  67. struct PreprocessorMLUPrivate;
  68. // assume that inference model only has one input
  69. class PreprocessorMLU : public ProcessorForkable<PreprocessorMLU> {
  70. public:
  71. PreprocessorMLU() noexcept;
  72. ~PreprocessorMLU();
  73. Status Process(PackagePtr data) noexcept override;
  74. Status Init() noexcept override;
  75. private:
  76. PreprocessorMLUPrivate* priv_;
  77. }; // class PreprocessorMLU
  78. class VideoInferServer : public InferServer {
  79. public:
  80. explicit VideoInferServer(int device_id) noexcept : InferServer(device_id) {}
  81. VideoInferServer() = delete;
  82. /**
  83. * @brief send a inference request
  84. *
  85. * @warning async api, can be invoked with async Session only.
  86. * @note redefine to unhide method with same name of base class
  87. */
  88. bool Request(Session_t session, PackagePtr input, any user_data, int timeout = -1) noexcept {
  89. return InferServer::Request(session, std::move(input), std::move(user_data), timeout);
  90. }
  91. /**
  92. * @brief send a inference request and wait for response
  93. *
  94. * @warning synchronous api, can be invoked with synchronous Session only.
  95. * @note redefine to unhide method with same name of base class
  96. */
  97. bool RequestSync(Session_t session, PackagePtr input, Status* status, PackagePtr response,
  98. int timeout = -1) noexcept {
  99. return InferServer::RequestSync(session, std::move(input), status, std::move(response), timeout);
  100. }
  101. /**
  102. * @brief image process helper, send a inference request
  103. *
  104. * @warning async api, can be invoked with async Session only.
  105. * @see CreateSession(SessionDesc, std::shared_ptr<Observer>)
  106. *
  107. * @param session infer session
  108. * @param vframe image data
  109. * @param tag tag of request
  110. * @param user_data user data
  111. * @param timeout timeout threshold (milliseconds), -1 for endless
  112. */
  113. bool Request(Session_t session, const VideoFrame& vframe,
  114. const std::string& tag, any user_data, int timeout = -1) noexcept {
  115. PackagePtr in = Package::Create(1, tag);
  116. in->data[0]->Set(vframe);
  117. in->tag = tag;
  118. return Request(session, std::move(in), std::move(user_data), timeout);
  119. }
  120. /**
  121. * @brief image with object process helper, send a inference request
  122. *
  123. * @warning async api, can be invoked with async Session only.
  124. * @see CreateSession(SessionDesc, std::shared_ptr<Observer>)
  125. *
  126. * @param session infer session
  127. * @param vframe image data
  128. * @param objs objects detected in video frame (model process on objs)
  129. * @param tag tag of request
  130. * @param user_data user data
  131. * @param timeout timeout threshold (milliseconds), -1 for endless
  132. */
  133. bool Request(Session_t session, const VideoFrame& vframe, const std::vector<BoundingBox>& objs,
  134. const std::string& tag, any user_data, int timeout = -1) noexcept {
  135. PackagePtr in = Package::Create(objs.size(), tag);
  136. for (unsigned int i = 0; i < objs.size(); i++) {
  137. VideoFrame vf = vframe;
  138. vf.roi = objs[i];
  139. in->data[i]->Set(std::move(vf));
  140. }
  141. return Request(session, std::move(in), std::move(user_data), timeout);
  142. }
  143. /**
  144. * @brief image process helper, send a inference request and wait for response
  145. *
  146. * @warning sync api, can be invoked with sync Session only. @see CreateSession(SessionDesc)
  147. *
  148. * @param session infer session
  149. * @param vframe image data
  150. * @param tag tag of request
  151. * @param status[out] execute status
  152. * @param output[out] output result
  153. * @param timeout timeout threshold (milliseconds), -1 for endless
  154. */
  155. bool RequestSync(Session_t session, const VideoFrame& vframe, const std::string& tag, Status* status,
  156. PackagePtr response, int timeout = -1) noexcept {
  157. PackagePtr in = Package::Create(1, tag);
  158. in->data[0]->Set(vframe);
  159. return RequestSync(session, std::move(in), status, std::move(response), timeout);
  160. }
  161. /**
  162. * @brief image with object process helper, send a inference request and wait for response
  163. *
  164. * @warning sync api, can be invoked with sync Session only. @see CreateSession(SessionDesc)
  165. *
  166. * @param session infer session
  167. * @param vframe image data
  168. * @param objs objects detected in video frame (model process on objs)
  169. * @param tag tag of request
  170. * @param status[out] execute status
  171. * @param output[out] output result
  172. * @param timeout timeout threshold (milliseconds), -1 for endless
  173. */
  174. bool RequestSync(Session_t session, const VideoFrame& vframe, const std::vector<BoundingBox>& objs,
  175. const std::string& tag, Status* status, PackagePtr response, int timeout = -1) noexcept {
  176. PackagePtr in = Package::Create(objs.size(), tag);
  177. for (unsigned int i = 0; i < objs.size(); ++i) {
  178. VideoFrame vf = vframe;
  179. vf.roi = objs[i];
  180. in->data[i]->Set(std::move(vf));
  181. }
  182. return RequestSync(session, std::move(in), status, std::move(response), timeout);
  183. }
  184. };
  185. } // namespace video
  186. } // namespace infer_server
  187. #endif // INFER_SERVER_VIDEO_HELPER_H_