main.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. LOGD("=== Event Callback: Device Offline!");
  6. // Note:
  7. // Please set TY_BOOL_KEEP_ALIVE_ONOFF feature to false if you need to debug with breakpoint!
  8. }
  9. else if (event_info->eventId == TY_EVENT_LICENSE_ERROR) {
  10. LOGD("=== Event Callback: License Error!");
  11. }
  12. }
  13. int main(int argc, char* argv[])
  14. {
  15. std::string ID, IP;
  16. TY_INTERFACE_HANDLE hIface = NULL;
  17. TY_ISP_HANDLE hColorIspHandle = NULL;
  18. TY_DEV_HANDLE hDevice = NULL;
  19. int32_t color, ir, depth;
  20. color = ir = depth = 1;
  21. for(int i = 1; i < argc; i++) {
  22. if(strcmp(argv[i], "-id") == 0){
  23. ID = argv[++i];
  24. } else if(strcmp(argv[i], "-ip") == 0) {
  25. IP = argv[++i];
  26. } else if(strcmp(argv[i], "-color=off") == 0) {
  27. color = 0;
  28. } else if(strcmp(argv[i], "-depth=off") == 0) {
  29. depth = 0;
  30. } else if(strcmp(argv[i], "-ir=off") == 0) {
  31. ir = 0;
  32. } else if(strcmp(argv[i], "-h") == 0) {
  33. LOGI("Usage: SimpleView_FetchFrame [-h] [-id <ID>] [-ip <IP>]");
  34. return 0;
  35. }
  36. }
  37. if (!color && !depth && !ir) {
  38. LOGD("At least one component need to be on");
  39. return -1;
  40. }
  41. LOGD("Init lib");
  42. ASSERT_OK( TYInitLib() );
  43. TY_VERSION_INFO ver;
  44. ASSERT_OK( TYLibVersion(&ver) );
  45. LOGD(" - lib version: %d.%d.%d", ver.major, ver.minor, ver.patch);
  46. std::vector<TY_DEVICE_BASE_INFO> selected;
  47. ASSERT_OK( selectDevice(TY_INTERFACE_ALL, ID, IP, 1, selected) );
  48. ASSERT(selected.size() > 0);
  49. TY_DEVICE_BASE_INFO& selectedDev = selected[0];
  50. ASSERT_OK( TYOpenInterface(selectedDev.iface.id, &hIface) );
  51. ASSERT_OK( TYOpenDevice(hIface, selectedDev.id, &hDevice) );
  52. TY_COMPONENT_ID allComps;
  53. ASSERT_OK( TYGetComponentIDs(hDevice, &allComps) );
  54. ///try to enable color camera
  55. if(allComps & TY_COMPONENT_RGB_CAM && color) {
  56. LOGD("Has RGB camera, open RGB cam");
  57. ASSERT_OK( TYEnableComponents(hDevice, TY_COMPONENT_RGB_CAM) );
  58. //create a isp handle to convert raw image(color bayer format) to rgb image
  59. ASSERT_OK(TYISPCreate(&hColorIspHandle));
  60. //Init code can be modified in common.hpp
  61. //NOTE: Should set RGB image format & size before init ISP
  62. ASSERT_OK(ColorIspInitSetting(hColorIspHandle, hDevice));
  63. //You can call follow function to show color isp supported features
  64. #if 0
  65. ColorIspShowSupportedFeatures(hColorIspHandle);
  66. #endif
  67. //You can turn on auto exposure function as follow ,but frame rate may reduce .
  68. //Device may be casually stucked 1~2 seconds while software is trying to adjust device exposure time value
  69. #if 0
  70. ASSERT_OK(ColorIspInitAutoExposure(hColorIspHandle, hDevice));
  71. #endif
  72. }
  73. if (allComps & TY_COMPONENT_IR_CAM_LEFT && ir) {
  74. LOGD("Has IR left camera, open IR left cam");
  75. ASSERT_OK(TYEnableComponents(hDevice, TY_COMPONENT_IR_CAM_LEFT));
  76. }
  77. if (allComps & TY_COMPONENT_IR_CAM_RIGHT && ir) {
  78. LOGD("Has IR right camera, open IR right cam");
  79. ASSERT_OK(TYEnableComponents(hDevice, TY_COMPONENT_IR_CAM_RIGHT));
  80. }
  81. //try to enable depth map
  82. LOGD("Configure components, open depth cam");
  83. DepthViewer depthViewer("Depth");
  84. if (allComps & TY_COMPONENT_DEPTH_CAM && depth) {
  85. TY_IMAGE_MODE image_mode;
  86. ASSERT_OK(get_default_image_mode(hDevice, TY_COMPONENT_DEPTH_CAM, image_mode));
  87. LOGD("Select Depth Image Mode: %dx%d", TYImageWidth(image_mode), TYImageHeight(image_mode));
  88. ASSERT_OK(TYSetEnum(hDevice, TY_COMPONENT_DEPTH_CAM, TY_ENUM_IMAGE_MODE, image_mode));
  89. ASSERT_OK(TYEnableComponents(hDevice, TY_COMPONENT_DEPTH_CAM));
  90. //depth map pixel format is uint16_t ,which default unit is 1 mm
  91. //the acutal depth (mm)= PixelValue * ScaleUnit
  92. float scale_unit = 1.;
  93. TYGetFloat(hDevice, TY_COMPONENT_DEPTH_CAM, TY_FLOAT_SCALE_UNIT, &scale_unit);
  94. depthViewer.depth_scale_unit = scale_unit;
  95. }
  96. LOGD("Prepare image buffer");
  97. uint32_t frameSize;
  98. ASSERT_OK( TYGetFrameBufferSize(hDevice, &frameSize) );
  99. LOGD(" - Get size of framebuffer, %d", frameSize);
  100. LOGD(" - Allocate & enqueue buffers");
  101. char* frameBuffer[2];
  102. frameBuffer[0] = new char[frameSize];
  103. frameBuffer[1] = new char[frameSize];
  104. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[0], frameSize);
  105. ASSERT_OK( TYEnqueueBuffer(hDevice, frameBuffer[0], frameSize) );
  106. LOGD(" - Enqueue buffer (%p, %d)", frameBuffer[1], frameSize);
  107. ASSERT_OK( TYEnqueueBuffer(hDevice, frameBuffer[1], frameSize) );
  108. LOGD("Register event callback");
  109. ASSERT_OK(TYRegisterEventCallback(hDevice, eventCallback, NULL));
  110. bool hasTrigger;
  111. ASSERT_OK(TYHasFeature(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_PARAM, &hasTrigger));
  112. if (hasTrigger) {
  113. LOGD("Disable trigger mode");
  114. TY_TRIGGER_PARAM trigger;
  115. trigger.mode = TY_TRIGGER_MODE_OFF;
  116. ASSERT_OK(TYSetStruct(hDevice, TY_COMPONENT_DEVICE, TY_STRUCT_TRIGGER_PARAM, &trigger, sizeof(trigger)));
  117. }
  118. LOGD("Start capture");
  119. ASSERT_OK( TYStartCapture(hDevice) );
  120. LOGD("While loop to fetch frame");
  121. bool exit_main = false;
  122. TY_FRAME_DATA frame;
  123. int index = 0;
  124. while(!exit_main) {
  125. int err = TYFetchFrame(hDevice, &frame, -1);
  126. if( err == TY_STATUS_OK ) {
  127. LOGD("Get frame %d", ++index);
  128. int fps = get_fps();
  129. if (fps > 0){
  130. LOGI("fps: %d", fps);
  131. }
  132. cv::Mat depth, irl, irr, color;
  133. parseFrame(frame, &depth, &irl, &irr, &color, hColorIspHandle);
  134. if(!depth.empty()){
  135. depthViewer.show(depth);
  136. }
  137. if(!irl.empty()){ cv::imshow("LeftIR", irl); }
  138. if(!irr.empty()){ cv::imshow("RightIR", irr); }
  139. if(!color.empty()){ cv::imshow("Color", color); }
  140. int key = cv::waitKey(1);
  141. switch(key & 0xff) {
  142. case 0xff:
  143. break;
  144. case 'q':
  145. exit_main = true;
  146. break;
  147. default:
  148. LOGD("Unmapped key %d", key);
  149. }
  150. TYISPUpdateDevice(hColorIspHandle);
  151. LOGD("Re-enqueue buffer(%p, %d)"
  152. , frame.userBuffer, frame.bufferSize);
  153. ASSERT_OK( TYEnqueueBuffer(hDevice, frame.userBuffer, frame.bufferSize) );
  154. }
  155. }
  156. ASSERT_OK( TYStopCapture(hDevice) );
  157. ASSERT_OK( TYCloseDevice(hDevice));
  158. ASSERT_OK( TYCloseInterface(hIface) );
  159. ASSERT_OK(TYISPRelease(&hColorIspHandle));
  160. ASSERT_OK( TYDeinitLib() );
  161. delete frameBuffer[0];
  162. delete frameBuffer[1];
  163. LOGD("Main done!");
  164. return 0;
  165. }