main.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include "../common/common.hpp"
  2. void eventCallback(TY_EVENT_INFO *event_info, void *userdata)
  3. {
  4. if (event_info->eventId == TY_EVENT_DEVICE_OFFLINE)
  5. {
  6. LOGD("=== Event Callback: Device Offline!");
  7. // Note:
  8. // Please set TY_BOOL_KEEP_ALIVE_ONOFF feature to false if you need to debug with breakpoint!
  9. }
  10. else if (event_info->eventId == TY_EVENT_LICENSE_ERROR)
  11. {
  12. LOGD("=== Event Callback: License Error!");
  13. }
  14. }
  15. static void init_cmd_argv(CommandLineFeatureHelper &ctx)
  16. {
  17. ctx.add_feature("img_num", TY_COMPONENT_DEPTH_CAM, TY_INT_SGBM_IMAGE_NUM, -1, "frames per depth");
  18. ctx.add_feature("disp_num", TY_COMPONENT_DEPTH_CAM, TY_INT_SGBM_DISPARITY_NUM, -1, "disparity num");
  19. ctx.add_feature("disp_off", TY_COMPONENT_DEPTH_CAM, TY_INT_SGBM_DISPARITY_OFFSET, -1, "disparity offset");
  20. ctx.add_feature("win_h", TY_COMPONENT_DEPTH_CAM, TY_INT_SGBM_MATCH_WIN_HEIGHT, -1, "match win height");
  21. ctx.add_feature("p1", TY_COMPONENT_DEPTH_CAM, TY_INT_SGBM_SEMI_PARAM_P1, -1, "semi p1");
  22. ctx.add_feature("p2", TY_COMPONENT_DEPTH_CAM, TY_INT_SGBM_SEMI_PARAM_P2, -1, "semi p2");
  23. ctx.add_feature("uniq_factor", TY_COMPONENT_DEPTH_CAM, TY_INT_SGBM_UNIQUE_FACTOR, -1, "uniqueness factor");
  24. ctx.add_feature("uniq_absdiff", TY_COMPONENT_DEPTH_CAM, TY_INT_SGBM_UNIQUE_ABSDIFF, -1, "uniqueness min absoulut diff");
  25. ctx.add_feature("half_win", TY_COMPONENT_DEPTH_CAM, TY_BOOL_SGBM_HFILTER_HALF_WIN, -1, "enalble half window size");
  26. ctx.add_feature("win_w", TY_COMPONENT_DEPTH_CAM, TY_INT_SGBM_MATCH_WIN_WIDTH, -1, "match win width");
  27. ctx.add_feature("med_filter", TY_COMPONENT_DEPTH_CAM, TY_BOOL_SGBM_MEDFILTER, -1, "enalble median filter");
  28. ctx.add_feature("lrc", TY_COMPONENT_DEPTH_CAM, TY_BOOL_SGBM_LRC, -1, "enalble left right consist check");
  29. ctx.add_feature("lrc_diff", TY_COMPONENT_DEPTH_CAM, TY_INT_SGBM_LRC_DIFF, -1, "max left right consist diff");
  30. ctx.add_feature("med_thresh", TY_COMPONENT_DEPTH_CAM, TY_INT_SGBM_MEDFILTER_THRESH, -1, "median filter thresh");
  31. ctx.add_feature("p1_scale", TY_COMPONENT_DEPTH_CAM, TY_INT_SGBM_SEMI_PARAM_P1_SCALE, -1, "semi param p1 scale");
  32. ctx.add_feature("id", 0, 0, "", "device id");
  33. ctx.add_feature("ip", 0, 0, "", "device ip");
  34. ctx.add_feature("color=off", 0, 0, "", "disable color image", true);
  35. ctx.add_feature("ir=off", 0, 0, "", "disable ir image", true);
  36. ctx.add_feature("depth=off", 0, 0, "", "disable depth image", true);
  37. ctx.add_feature("h", 0, 0, "", "print help message", true);
  38. }
  39. static CommandLineFeatureHelper param_ctx;
  40. ////////////////////////////////////////
  41. int main(int argc, char *argv[])
  42. {
  43. std::string ID, IP;
  44. TY_INTERFACE_HANDLE hIface = NULL;
  45. TY_ISP_HANDLE hColorIspHandle = NULL;
  46. TY_DEV_HANDLE hDevice = NULL;
  47. int32_t color, ir, depth; ///< flag for data stream 0: off, 1: on
  48. color = ir = depth = 1;
  49. init_cmd_argv(param_ctx);
  50. param_ctx.parse_argv(argc, argv);
  51. ID = param_ctx.get_feature("id")->get_str_val();
  52. IP = param_ctx.get_feature("ip")->get_str_val();
  53. color = !param_ctx.get_feature("color=off")->has_set;
  54. ir = !param_ctx.get_feature("ir=off")->has_set;
  55. depth = !param_ctx.get_feature("depth=off")->has_set;
  56. if (param_ctx.get_feature("h")->has_set)
  57. {
  58. //show command line parameter usage
  59. LOGD("%s", param_ctx.usage_describe().c_str());
  60. return 0;
  61. }
  62. if (!color && !depth && !ir)
  63. {
  64. LOGD("At least one component need to be on");
  65. return -1;
  66. }
  67. LOGD("Init lib");
  68. ASSERT_OK(TYInitLib());
  69. TY_VERSION_INFO ver;
  70. ASSERT_OK(TYLibVersion(&ver));
  71. LOGD(" - lib version: %d.%d.%d", ver.major, ver.minor, ver.patch);
  72. std::vector<TY_DEVICE_BASE_INFO> selected;
  73. ASSERT_OK(selectDevice(TY_INTERFACE_ALL, ID, IP, 1, selected));
  74. ASSERT(selected.size() > 0);
  75. TY_DEVICE_BASE_INFO &selectedDev = selected[0];
  76. ASSERT_OK(TYOpenInterface(selectedDev.iface.id, &hIface));
  77. ASSERT_OK(TYOpenDevice(hIface, selectedDev.id, &hDevice));
  78. TY_COMPONENT_ID allComps;
  79. ASSERT_OK(TYGetComponentIDs(hDevice, &allComps));
  80. /// try to enable color camera
  81. if (allComps & TY_COMPONENT_RGB_CAM && color)
  82. {
  83. LOGD("Has RGB camera, open RGB cam");
  84. ASSERT_OK(TYEnableComponents(hDevice, TY_COMPONENT_RGB_CAM));
  85. // create a isp handle to convert raw image(color bayer format) to rgb image
  86. ASSERT_OK(TYISPCreate(&hColorIspHandle));
  87. // Init code can be modified in common.hpp
  88. // NOTE: Should set RGB image format & size before init ISP
  89. ASSERT_OK(ColorIspInitSetting(hColorIspHandle, hDevice));
  90. // You can call follow function to show color isp supported features
  91. #if 0
  92. ColorIspShowSupportedFeatures(hColorIspHandle);
  93. #endif
  94. // You can turn on auto exposure function as follow ,but frame rate may reduce .
  95. // Device may be casually stucked 1~2 seconds while software is trying to adjust device exposure time value
  96. #if 0
  97. ASSERT_OK(ColorIspInitAutoExposure(hColorIspHandle, hDevice));
  98. #endif
  99. }
  100. if (allComps & TY_COMPONENT_IR_CAM_LEFT && ir)
  101. {
  102. LOGD("Has IR left camera, open IR left cam");
  103. ASSERT_OK(TYEnableComponents(hDevice, TY_COMPONENT_IR_CAM_LEFT));
  104. }
  105. if (allComps & TY_COMPONENT_IR_CAM_RIGHT && ir)
  106. {
  107. LOGD("Has IR right camera, open IR right cam");
  108. ASSERT_OK(TYEnableComponents(hDevice, TY_COMPONENT_IR_CAM_RIGHT));
  109. }
  110. // try to enable depth map
  111. LOGD("Configure components, open depth cam");
  112. DepthViewer depthViewer("Depth");
  113. if (allComps & TY_COMPONENT_DEPTH_CAM && depth)
  114. {
  115. TY_IMAGE_MODE image_mode;
  116. ASSERT_OK(get_default_image_mode(hDevice, TY_COMPONENT_DEPTH_CAM, image_mode));
  117. LOGD("Select Depth Image Mode: %dx%d", TYImageWidth(image_mode), TYImageHeight(image_mode));
  118. ASSERT_OK(TYSetEnum(hDevice, TY_COMPONENT_DEPTH_CAM, TY_ENUM_IMAGE_MODE, image_mode));
  119. ASSERT_OK(TYEnableComponents(hDevice, TY_COMPONENT_DEPTH_CAM));
  120. // depth map pixel format is uint16_t ,which default unit is 1 mm
  121. // the acutal depth (mm)= PixelValue * ScaleUnit
  122. float scale_unit = 1.;
  123. TYGetFloat(hDevice, TY_COMPONENT_DEPTH_CAM, TY_FLOAT_SCALE_UNIT, &scale_unit);
  124. depthViewer.depth_scale_unit = scale_unit;
  125. }
  126. LOGD("Prepare image buffer");
  127. uint32_t frameSize;
  128. ASSERT_OK(TYGetFrameBufferSize(hDevice, &frameSize));
  129. LOGD(" - Get size of framebuffer, %d", frameSize);
  130. LOGD(" - Allocate & enqueue buffers");
  131. char *frameBuffer[2];
  132. frameBuffer[0] = new char[frameSize];
  133. frameBuffer[1] = new char[frameSize];
  134. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[0], frameSize);
  135. ASSERT_OK(TYEnqueueBuffer(hDevice, frameBuffer[0], frameSize));
  136. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[1], frameSize);
  137. ASSERT_OK(TYEnqueueBuffer(hDevice, frameBuffer[1], frameSize));
  138. LOGD("Register event callback");
  139. ASSERT_OK(TYRegisterEventCallback(hDevice, eventCallback, NULL));
  140. bool hasTrigger;
  141. ASSERT_OK(TYHasFeature(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_PARAM, &hasTrigger));
  142. if (hasTrigger)
  143. {
  144. LOGD("Disable trigger mode");
  145. TY_TRIGGER_PARAM trigger;
  146. trigger.mode = TY_TRIGGER_MODE_OFF;
  147. ASSERT_OK(TYSetStruct(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_PARAM, &trigger, sizeof(trigger)));
  148. }
  149. // set other device feature param from command line argv
  150. // ref header file : CommandLineFeatureHelper.hpp for detail
  151. param_ctx.set_device_feature(hDevice);
  152. LOGD("Start capture");
  153. ASSERT_OK(TYStartCapture(hDevice));
  154. LOGD("While loop to fetch frame");
  155. bool exit_main = false;
  156. TY_FRAME_DATA frame;
  157. int index = 0;
  158. while (!exit_main)
  159. {
  160. int err = TYFetchFrame(hDevice, &frame, -1);
  161. if (err == TY_STATUS_OK)
  162. {
  163. LOGD("Get frame %d", ++index);
  164. int fps = get_fps();
  165. if (fps > 0)
  166. {
  167. LOGI("fps: %d", fps);
  168. }
  169. cv::Mat depth, irl, irr, color;
  170. parseFrame(frame, &depth, &irl, &irr, &color, hColorIspHandle);
  171. if (!depth.empty())
  172. {
  173. depthViewer.show(depth);
  174. }
  175. if (!irl.empty())
  176. {
  177. cv::imshow("LeftIR", irl);
  178. }
  179. if (!irr.empty())
  180. {
  181. cv::imshow("RightIR", irr);
  182. }
  183. if (!color.empty())
  184. {
  185. cv::imshow("Color", color);
  186. }
  187. int key = cv::waitKey(1);
  188. switch (key & 0xff)
  189. {
  190. case 0xff:
  191. break;
  192. case 'q':
  193. exit_main = true;
  194. break;
  195. default:
  196. LOGD("Unmapped key %d", key);
  197. }
  198. TYISPUpdateDevice(hColorIspHandle);
  199. LOGD("Re-enqueue buffer(%p, %d)", frame.userBuffer, frame.bufferSize);
  200. ASSERT_OK(TYEnqueueBuffer(hDevice, frame.userBuffer, frame.bufferSize));
  201. }
  202. }
  203. ASSERT_OK(TYStopCapture(hDevice));
  204. ASSERT_OK(TYCloseDevice(hDevice));
  205. ASSERT_OK(TYCloseInterface(hIface));
  206. ASSERT_OK(TYISPRelease(&hColorIspHandle));
  207. ASSERT_OK(TYDeinitLib());
  208. delete frameBuffer[0];
  209. delete frameBuffer[1];
  210. LOGD("Main done!");
  211. return 0;
  212. }