depth_guide.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "depth_guide.h"
  2. #include "core/image_utility.hpp"
  3. #include "core/imgui_utility.hpp"
  4. #include "core/pc_utility.h"
  5. #include "image_process/impl/versatile_convertor_impl.h"
  6. #include "image_process/process_funcs.h"
  7. app_depth_guide::app_depth_guide(const create_config &_conf) {
  8. conf = _conf;
  9. // initialize object manager
  10. OBJ_SAVE(img_color, image_u8c3());
  11. OBJ_SAVE(img_depth, image_f32c1());
  12. OBJ_SAVE(img_remap, image_ptr());
  13. OBJ_SAVE(img_depth_fake, image_u8c3());
  14. auto fake_info = fake_color_config{.mode = FAKE_800P, .lower = 200, .upper = 1000};
  15. OBJ_SAVE(img_depth_fake_info, versatile_convertor_impl::encode_config(fake_info));
  16. OBJ_SAVE(img_out, image_u8c4());
  17. OBJ_SAVE(img_obj_mask, image_ptr());
  18. OBJ_SAVE(pc_raw, pc_ptr());
  19. OBJ_SAVE(pc_obj_raw, pc_ptr());
  20. OBJ_SAVE(pc_obj, pc_ptr());
  21. OBJ_SIG(img_obj_mask)->connect([](auto _) {
  22. gen_pc_rgb_with_mask::call(
  23. {img_color, img_depth, img_obj_mask,
  24. img_remap, pc_obj_raw, default_cuda_stream});
  25. });
  26. OBJ_SIG(pc_obj_raw)->connect([](auto _) {
  27. shrink_pc_zero_depth::call(
  28. {pc_obj_raw, pc_obj, default_cuda_stream});
  29. });
  30. // initialize modules
  31. auto orb_cam_conf = orb_camera_ui::create_config{
  32. .cf_name = img_color, .df_name = img_depth,
  33. .pc_name = pc_raw, .remap_name = img_remap,
  34. .ctx = conf.asio_ctx, .stream = default_cuda_stream,
  35. };
  36. orb_cam = std::make_unique<orb_camera_ui>(orb_cam_conf);
  37. auto fake_conf = versatile_convertor::create_config{
  38. .in_name = img_depth, .ext_in = img_depth_fake_info, .out_name = img_depth_fake,
  39. .cvt_opt = CVT_FAKE_ENCODE_800P, .stream = default_cuda_stream,
  40. };
  41. depth_encode = std::make_unique<versatile_convertor>(fake_conf);
  42. auto ai_conf = fast_sam::create_config{
  43. .img_in = img_color, .mask_out = img_obj_mask,
  44. .cuda_ctx = conf.cuda_ctx, .ctx = conf.asio_ctx,
  45. };
  46. ai_seg = std::make_unique<fast_sam>(ai_conf);
  47. auto out_conf = stereo_augment_helper::create_config{
  48. .left_name = img_color, .right_name = img_depth_fake, .out_name = img_out,
  49. .stream = default_cuda_stream
  50. };
  51. out_combiner = std::make_unique<stereo_augment_helper>(out_conf);
  52. out_combiner->fix_ui_config({.follow_image_size=true, .enable_halve_width=false});
  53. auto bg_viewer_conf = image_viewer::create_config{
  54. .mode = VIEW_STEREO, .flip_y = true,
  55. .stream = default_cuda_stream,
  56. };
  57. auto &bg_extra_conf = bg_viewer_conf.extra.stereo;
  58. // bg_extra_conf.c_name = img_color;
  59. bg_extra_conf.left_name = img_color;
  60. bg_extra_conf.c_fmt = COLOR_RGB;
  61. bg_extra_conf.right_name = img_obj_mask;
  62. bg_viewer = std::make_unique<image_viewer>(bg_viewer_conf);
  63. auto out_streamer_conf = image_streamer::create_config{
  64. .img_name = img_out,
  65. .ext_name = img_depth_fake_info, // comment this for tiny player to work
  66. .asio_ctx = conf.asio_ctx,
  67. .cuda_ctx = conf.cuda_ctx, .stream = default_cuda_stream
  68. };
  69. out_streamer = std::make_unique<image_streamer>(out_streamer_conf);
  70. auto saver_conf = image_saver::create_config{.ctx = conf.asio_ctx};
  71. saver_conf.img_list.emplace_back("Image", img_color);
  72. saver_conf.img_list.emplace_back("Image Out", img_out);
  73. saver_conf.img_list.emplace_back("PC Raw", pc_raw);
  74. saver_conf.img_list.emplace_back("PC Object", pc_obj);
  75. out_saver = std::make_unique<image_saver>(saver_conf);
  76. auto pc_conf = pc_viewer::create_config{
  77. .name = pc_raw, .stream = default_cuda_stream,
  78. };
  79. scene_viewer = std::make_unique<pc_viewer>(pc_conf);
  80. }
  81. void app_depth_guide::show_ui() {
  82. auto ctx = conf.asio_ctx;
  83. if (ImGui::Begin("Depth Guide Control")) {
  84. ImGui::PushItemWidth(200);
  85. if (ImGui::CollapsingHeader("Camera")) {
  86. auto id_guard = imgui_id_guard("camera");
  87. orb_cam->show();
  88. }
  89. if (ImGui::CollapsingHeader("AI Segmentation")) {
  90. auto id_guard = imgui_id_guard("ai_segment");
  91. ai_seg->show();
  92. }
  93. if (ImGui::CollapsingHeader("Streamer")) {
  94. auto id_guard = imgui_id_guard("streamer");
  95. out_streamer->show();
  96. }
  97. if (ImGui::CollapsingHeader("Debug")) {
  98. if (ImGui::TreeNode("Background")) {
  99. ImGui::RadioButton("Image", &enable_scene_viewer, false);
  100. ImGui::SameLine();
  101. ImGui::RadioButton("3D", &enable_scene_viewer, true);
  102. ImGui::TreePop();
  103. }
  104. if (enable_scene_viewer) {
  105. if (ImGui::TreeNode("3D Viewer")) {
  106. scene_viewer->show();
  107. ImGui::TreePop();
  108. }
  109. } else {
  110. if (ImGui::TreeNode("Image Viewer")) {
  111. bg_viewer->show();
  112. ImGui::TreePop();
  113. }
  114. }
  115. if (ImGui::TreeNode("Image Saver")) {
  116. out_saver->show();
  117. ImGui::TreePop();
  118. }
  119. if (ImGui::TreeNode("Memory Pool")) {
  120. if (ImGui::Button("Purge")) {
  121. post(*ctx, [] { global_mp.purge(); });
  122. }
  123. ImGui::TreePop();
  124. }
  125. if (ImGui::TreeNode("Performance")) {
  126. ImGui::Text("UI Refresh Rate: %.2fms", perf_timer.query().interval);
  127. ImGui::TreePop();
  128. }
  129. }
  130. ImGui::PopItemWidth();
  131. }
  132. ImGui::End();
  133. perf_timer.record();
  134. }
  135. void app_depth_guide::render_background() {
  136. if (enable_scene_viewer) {
  137. scene_viewer->render();
  138. } else {
  139. bg_viewer->render();
  140. }
  141. }