infer_handler.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*************************************************************************
  2. * Copyright (C) [2021] 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_INFER_HANDLER_HPP_
  21. #define MODULES_INFER_HANDLER_HPP_
  22. #include <memory>
  23. #include <string>
  24. #include "inferencer2.hpp"
  25. namespace cnstream {
  26. class InferDataObserver;
  27. /**
  28. * @brief for inference handler used to do inference based on infer_server.
  29. */
  30. class InferHandlerImpl : public InferHandler {
  31. public:
  32. explicit InferHandlerImpl(Inferencer2* module, Infer2Param infer_params,
  33. std::shared_ptr<VideoPostproc> post_processor, std::shared_ptr<VideoPreproc> pre_processor,
  34. std::shared_ptr<ObjFilter> obj_filter)
  35. : InferHandler(module, infer_params, post_processor, pre_processor, obj_filter) {}
  36. virtual ~InferHandlerImpl();
  37. bool Open() override;
  38. void Close() override;
  39. int Process(CNFrameInfoPtr data, bool with_objs = false) override;
  40. void WaitTaskDone(const std::string& stream_id) override;
  41. void PostEvent(EventType e, const std::string& msg) { module_->PostEvent(e, msg); }
  42. private:
  43. bool LinkInferServer();
  44. private:
  45. std::unique_ptr<InferEngine> infer_server_ = nullptr;
  46. std::shared_ptr<InferDataObserver> data_observer_ = nullptr;
  47. InferEngineSession session_ = nullptr;
  48. InferPreprocessType scale_platform_ = InferPreprocessType::UNKNOWN;
  49. };
  50. inline void InferHandler::TransmitData(const CNFrameInfoPtr& data) {
  51. if (module_) module_->TransmitData(data);
  52. }
  53. } // namespace cnstream
  54. #endif // MODULES_INFER_HANDLER_HPP_