classification.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "classification_runner.h"
  28. #include "cxxutil/log.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_int32(wait_time, 0, "time of one test case");
  38. std::shared_ptr<StreamRunner> g_runner;
  39. bool g_exit = false;
  40. void HandleSignal(int sig) {
  41. g_runner->Stop();
  42. g_exit = true;
  43. LOGI(SAMPLES) << "Got INT signal, ready to exit!";
  44. }
  45. int main(int argc, char** argv) {
  46. gflags::ParseCommandLineFlags(&argc, &argv, true);
  47. edk::log::InitLogging(true, true);
  48. // check params
  49. CHECK(SAMPLES, FLAGS_data_path.size() != 0u); // NOLINT
  50. CHECK(SAMPLES, FLAGS_model_path.size() != 0u); // NOLINT
  51. CHECK(SAMPLES, FLAGS_func_name.size() != 0u); // NOLINT
  52. CHECK(SAMPLES, FLAGS_label_path.size() != 0u); // NOLINT
  53. CHECK(SAMPLES, FLAGS_wait_time >= 0); // NOLINT
  54. CHECK(SAMPLES, FLAGS_repeat_time >= 0); // NOLINT
  55. try {
  56. g_runner = std::make_shared<ClassificationRunner>(FLAGS_model_path, FLAGS_func_name, FLAGS_label_path,
  57. FLAGS_data_path, FLAGS_show, FLAGS_save_video);
  58. } catch (edk::Exception& e) {
  59. LOGE(SAMPLES) << "Create stream runner failed" << e.what();
  60. return -1;
  61. }
  62. std::future<bool> process_loop_return = std::async(std::launch::async, &StreamRunner::RunLoop, g_runner.get());
  63. if (0 < FLAGS_wait_time) {
  64. alarm(FLAGS_wait_time);
  65. }
  66. signal(SIGALRM, HandleSignal);
  67. // set mlu environment
  68. edk::MluContext context;
  69. context.SetDeviceId(0);
  70. context.BindDevice();
  71. g_runner->DemuxLoop(FLAGS_repeat_time);
  72. process_loop_return.wait();
  73. g_runner.reset();
  74. if (!process_loop_return.get()) {
  75. return 1;
  76. }
  77. LOGI(SAMPLES) << "run stream app SUCCEED!!!" << std::endl;
  78. edk::log::ShutdownLogging();
  79. return 0;
  80. }