track.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*************************************************************************
  2. * Copyright (C) [2019] 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 MODULES_TRACK_INCLUDE_HPP_
  21. #define MODULES_TRACK_INCLUDE_HPP_
  22. /**
  23. * \file track.hpp
  24. *
  25. * This file contains a declaration of struct Tracker
  26. */
  27. #include <memory>
  28. #include <string>
  29. #include <unordered_map>
  30. #include "cnstream_frame.hpp"
  31. #include "cnstream_module.hpp"
  32. #include "easytrack/easy_track.h"
  33. namespace infer_server { class ModelInfo; }
  34. namespace cnstream {
  35. struct TrackerContext;
  36. /**
  37. * @class Tracker
  38. *
  39. * @brief Tracker is a module for realtime tracking.
  40. * It would be MLU feature extracting if the model_path is provided, otherwise it would be done on CPU.
  41. */
  42. class Tracker : public Module, public ModuleCreator<Tracker> {
  43. public:
  44. /**
  45. * @brief Generates a tracker.
  46. *
  47. * @param[in] Name Module name.
  48. *
  49. * @return None.
  50. */
  51. explicit Tracker(const std::string &name);
  52. /**
  53. * @brief Releases a tracker.
  54. *
  55. * @param None.
  56. *
  57. * @return None.
  58. */
  59. ~Tracker();
  60. /**
  61. * @brief Configures a module.
  62. *
  63. * @param[in] paramSet This module's parameters to configure Tracker. Please use `cnstream_inspect` tool to get each
  64. parameter's detail information.
  65. * @return Returns true if opened successfully, otherwise returns false.
  66. */
  67. bool Open(ModuleParamSet paramSet) override;
  68. /**
  69. * @brief Closes a module.
  70. *
  71. * @param None
  72. *
  73. * @return None
  74. */
  75. void Close() override;
  76. /**
  77. * @brief Processes each frame data.
  78. *
  79. * @param[in] data Pointer to the frame infomation.
  80. *
  81. * @retval 0 successful with no data intercepted.
  82. * @retval <0 failure
  83. */
  84. int Process(std::shared_ptr<CNFrameInfo> data) override;
  85. /**
  86. * @brief Checks the parameters for a module.
  87. *
  88. * @param[in] paramSet Parameters for this module.
  89. *
  90. * @return Returns true if this API run successfully. Otherwise, returns false.
  91. */
  92. bool CheckParamSet(const ModuleParamSet &paramSet) const override;
  93. private:
  94. bool InitFeatureExtractor(const CNFrameInfoPtr &data);
  95. TrackerContext *GetContext(const CNFrameInfoPtr &data);
  96. std::unordered_map<int, TrackerContext *> contexts_;
  97. std::shared_ptr<infer_server::ModelInfo> model_ = nullptr;
  98. std::mutex mutex_;
  99. std::function<void(const CNFrameInfoPtr, bool)> match_func_;
  100. int device_id_ = 0;
  101. std::string model_pattern1_ = "";
  102. std::string model_pattern2_ = "";
  103. std::string track_name_ = "";
  104. float max_cosine_distance_ = 0.2;
  105. int engine_num_ = 1;
  106. bool need_feature_ = true;
  107. }; // class Tracker
  108. } // namespace cnstream
  109. #endif // MODULES_TRACK_INCLUDE_HPP_