stream_app.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include <gflags/gflags.h>
  21. #include <unistd.h>
  22. #include <csignal>
  23. #include <future>
  24. #include <iostream>
  25. #include <memory>
  26. #include <utility>
  27. #include "cxxutil/log.h"
  28. #include "detection_runner.h"
  29. #include "device/mlu_context.h"
  30. DEFINE_bool(show, false, "show image");
  31. DEFINE_bool(save_video, true, "save output to local video file");
  32. DEFINE_int32(repeat_time, 0, "process repeat time");
  33. DEFINE_string(data_path, "", "video path");
  34. DEFINE_string(model_path, "", "infer offline model path");
  35. DEFINE_string(label_path, "", "label path");
  36. DEFINE_string(func_name, "subnet0", "model function name");
  37. DEFINE_string(track_model_path, "", "track model path");
  38. DEFINE_string(track_func_name, "subnet0", "track model function name");
  39. DEFINE_int32(wait_time, 0, "time of one test case");
  40. DEFINE_string(net_type, "", "neural network type, SSD or YOLOv3");
  41. std::shared_ptr<StreamRunner> g_runner;
  42. bool g_exit = false;
  43. void HandleSignal(int sig) {
  44. g_runner->Stop();
  45. g_exit = true;
  46. LOGI(SAMPLES) << "Got INT signal, ready to exit!";
  47. }
  48. int main(int argc, char** argv) {
  49. gflags::ParseCommandLineFlags(&argc, &argv, true);
  50. edk::log::InitLogging(true, true);
  51. // check params
  52. CHECK(SAMPLES, FLAGS_data_path. size() != 0u); // NOLINT
  53. CHECK(SAMPLES, FLAGS_model_path.size() != 0u); // NOLINT
  54. CHECK(SAMPLES, FLAGS_func_name. size() != 0u); // NOLINT
  55. CHECK(SAMPLES, FLAGS_label_path.size() != 0u); // NOLINT
  56. CHECK(SAMPLES, FLAGS_net_type. size() != 0u); // NOLINT
  57. CHECK(SAMPLES, FLAGS_wait_time >= 0); // NOLINT
  58. CHECK(SAMPLES, FLAGS_repeat_time >= 0); // NOLINT
  59. try {
  60. g_runner = std::make_shared<DetectionRunner>(FLAGS_model_path, FLAGS_func_name, FLAGS_label_path,
  61. FLAGS_track_model_path, FLAGS_track_func_name,
  62. FLAGS_data_path, FLAGS_net_type, FLAGS_show, FLAGS_save_video);
  63. } catch (edk::Exception& e) {
  64. LOGE(SAMPLES) << "Create stream runner failed" << e.what();
  65. return -1;
  66. }
  67. std::future<bool> process_loop_return = std::async(std::launch::async, &StreamRunner::RunLoop, g_runner.get());
  68. if (0 < FLAGS_wait_time) {
  69. alarm(FLAGS_wait_time);
  70. }
  71. signal(SIGALRM, HandleSignal);
  72. // set mlu environment
  73. edk::MluContext context;
  74. context.SetDeviceId(0);
  75. context.BindDevice();
  76. g_runner->DemuxLoop(FLAGS_repeat_time);
  77. process_loop_return.wait();
  78. g_runner.reset();
  79. if (!process_loop_return.get()) {
  80. return 1;
  81. }
  82. LOGI(SAMPLES) << "run stream app SUCCEED!!!" << std::endl;
  83. edk::log::ShutdownLogging();
  84. return 0;
  85. }