From c9794db96c89d6ec252455c047bd55b0d48bffb6 Mon Sep 17 00:00:00 2001 From: Tae-Young Chung Date: Mon, 7 Nov 2022 16:56:22 +0900 Subject: [PATCH] Add remote display to depthstream_test_suite [Issue type] update Change-Id: I06add78361bcb62f1e4930a5414bc2acd4784d32 Signed-off-by: Tae-Young Chung --- test/testsuites/mv3d/depthstream_test_suite.cpp | 154 ++++++++++++++++++------ 1 file changed, 119 insertions(+), 35 deletions(-) diff --git a/test/testsuites/mv3d/depthstream_test_suite.cpp b/test/testsuites/mv3d/depthstream_test_suite.cpp index 0a29ef6..0c47769 100644 --- a/test/testsuites/mv3d/depthstream_test_suite.cpp +++ b/test/testsuites/mv3d/depthstream_test_suite.cpp @@ -41,6 +41,7 @@ #include #ifdef BUILD_VISUALIZER +#include "mv_util_visualizer_2d.h" #include "mv_util_visualizer_3d.h" #endif @@ -52,6 +53,15 @@ using namespace open3d; #endif #define VISION_SOURCE_DEVICE_ID 3 +#define __max(a, b) (((a) > (b)) ? (a) : (b)) +#define __min(a, b) (((a) < (b)) ? (a) : (b)) + +#define TRUNCATE_MIN(a,b,c) ((a < b) ? c : a) +#define TRUNCATE_MAX(a,b,c) ((a > b) ? c : a) +#define TRANSLATE_VAL(val) (TRUNCATE_MAX(TRUNCATE_MIN(val, 0, -0.05), 1, 1.05)/1.15 + 0.1) +#define COLORMAP_JET_R(val) __max(0, __min(255, (int) (round(255 * (1.5 - 4 * fabs(val - .75)))))) +#define COLORMAP_JET_G(val) __max(0, __min(255, (int) (round(255 * (1.5 - 4 * fabs(val - .5)))))) +#define COLORMAP_JET_B(val) __max(0, __min(255, (int) (round(255 * (1.5 - 4 * fabs(val - .25)))))) class StopWatch { @@ -80,6 +90,7 @@ typedef struct _appdata std::string rgbName; std::string datasetName; std::string dispResultFile; + float minDisp; float maxDisp; int fmt; @@ -89,12 +100,21 @@ typedef struct _appdata unsigned char *buffer; mv_source_h mv_source; mv_3d_h mv3d_handle; + + struct { + std::string url; + mv_source_h source; + unsigned char *buffer; + } remoteData; + } appdata; static appdata app; // MediaVision mv_engine_config_h engine_config; +// Vision source handle +vision_source_h vision_source_handle; static GMainLoop *loop; @@ -106,8 +126,9 @@ static int minDisp; static int maxDisp; static int display_xpos; static int display_ypos; +static bool isRemote; -void int_handler(int sig) +static void int_handler(int sig) { char c; @@ -127,7 +148,30 @@ void int_handler(int sig) getchar(); // Get new line character } -void _depth_stereo_cb(mv_source_h source, unsigned short *depth, unsigned int width, +static void _depth_stereo_remote_cb(mv_source_h source, unsigned short *depth, unsigned int width, + unsigned int height, void *user_data) +{ + auto mv3d = static_cast(user_data); + + for (int y = 0; y < height; y++) { + unsigned short* src = depth + y * width; + unsigned char* dst = mv3d->remoteData.buffer + y * (width * 4); + for (int x = 0; x < width; x++) { + float value = TRANSLATE_VAL(((440.92750f * 21.87095f / static_cast(src[x])) - 8.0f) / (88.f/*96-8*/)); + dst[x*4] = COLORMAP_JET_R(value); + dst[x*4 + 1] = COLORMAP_JET_G(value); + dst[x*4 + 2] = COLORMAP_JET_B(value); + dst[x*4 + 3] = 255; + } + } + mv_source_fill_by_buffer(mv3d->remoteData.source, mv3d->remoteData.buffer, width * height * 4, width, height, MEDIA_VISION_COLORSPACE_RGBA); +#if BUILD_VISUALIZER + mv_util_visualizer_2d(mv3d->remoteData.source, mv3d->remoteData.url.c_str()); +#endif + mv_source_clear(mv3d->remoteData.source); +} + +static void _depth_stereo_cb(mv_source_h source, unsigned short *depth, unsigned int width, unsigned int height, void *user_data) { auto mv3d = static_cast(user_data); @@ -162,7 +206,11 @@ static int _vision_source_cb(vision_source_buffer_s *buffer, void *user_data) mv_source_fill_by_buffer(app->mv_source, app->buffer, app->imgWidth * app->imgHeight, app->imgWidth, app->imgHeight, MEDIA_VISION_COLORSPACE_Y800); - mv_3d_run_async(app->mv3d_handle, app->mv_source, nullptr, nullptr); + if (isAsync) + mv_3d_run_async(app->mv3d_handle, app->mv_source, nullptr, nullptr); + else + mv_3d_run(app->mv3d_handle, app->mv_source, nullptr, nullptr); + mv_source_clear(app->mv_source); return 0; @@ -176,9 +224,11 @@ int main(int argc, char *argv[]) camHeight = 400; isLive = true; isAsync = true; + isRemote = false; std::string stereoConfigFile; std::string dispResultFile; - // mv_depthstream_test_suite 1 8 96 0 0 640 400 /usr/share/dfs-qcmv/stereoCalibDA.yaml /tmp/test.png + std::string remoteUrl; + // mv_depthstream_test_suite 1 8 96 0 0 640 400 /usr/share/dfs-qcmv/stereoCalibDA.yaml /tmp/test.png 1 192.168.0.4:50051 if (argc > 1) { isAsync = (atoi(argv[1])!=0); minDisp = atoi(argv[2]); @@ -189,6 +239,8 @@ int main(int argc, char *argv[]) camHeight = atoi(argv[7]); stereoConfigFile = argv[8]; dispResultFile = argv[9]; + isRemote = (atoi(argv[10])!=0); + if (isRemote) remoteUrl = argv[11]; } #ifdef MV_3D_POINTCLOUD_IS_AVAILABLE utility::SetVerbosityLevel(utility::VerbosityLevel::Debug); @@ -197,101 +249,114 @@ int main(int argc, char *argv[]) int ret = MEDIA_VISION_ERROR_NONE; printf("enter main\n"); + vision_source_format_s format { + .pixel_format = VISION_SOURCE_PIXEL_FORMAT_NV12, + .resolution = { camWidth << 1, camHeight }, + .fps = 10, + .quality = 0, + .bitrate = 0 + }; + // mediavision // engine_config ret = mv_create_engine_config(&engine_config); if (ret != MEDIA_VISION_ERROR_NONE) { - return -1; + goto _err; } ret = mv_engine_config_set_int_attribute(engine_config, MV_3D_DEPTH_WIDTH, camWidth); if (ret != MEDIA_VISION_ERROR_NONE) { - return -1; + goto _err; } ret = mv_engine_config_set_int_attribute(engine_config, MV_3D_DEPTH_HEIGHT, camHeight); if (ret != MEDIA_VISION_ERROR_NONE) { - return -1; + goto _err; } ret = mv_engine_config_set_int_attribute(engine_config, MV_3D_DEPTH_MIN_DISPARITY, minDisp); if (ret != MEDIA_VISION_ERROR_NONE) { - return -1; + goto _err; } ret = mv_engine_config_set_int_attribute(engine_config, MV_3D_DEPTH_MAX_DISPARITY, maxDisp); if (ret != MEDIA_VISION_ERROR_NONE) { - return -1; + goto _err; } ret = mv_engine_config_set_string_attribute( engine_config, MV_3D_DEPTH_STEREO_CONFIG_FILE_PATH, stereoConfigFile.c_str()); if (ret != MEDIA_VISION_ERROR_NONE) { - return -1; + goto _err; } app.dispResultFile = dispResultFile; + app.remoteData.url = remoteUrl; // mv3d handle ret = mv_3d_create(&app.mv3d_handle); if (ret != MEDIA_VISION_ERROR_NONE) { - return -1; + goto _err; } ret = mv_3d_configure(app.mv3d_handle, engine_config); if (ret != MEDIA_VISION_ERROR_NONE) { - return -1; + goto _err; } - ret = mv_3d_set_depth_cb(app.mv3d_handle, _depth_stereo_cb, &app); + if (isRemote) + ret = mv_3d_set_depth_cb(app.mv3d_handle, _depth_stereo_remote_cb, &app); + else + ret = mv_3d_set_depth_cb(app.mv3d_handle, _depth_stereo_cb, &app); if (ret != MEDIA_VISION_ERROR_NONE) { - return -1; + goto _err; } ret = mv_3d_prepare(app.mv3d_handle); if (ret != MEDIA_VISION_ERROR_NONE) { - return -1; + goto _err; } // source ret = mv_create_source(&app.mv_source); if (ret != MEDIA_VISION_ERROR_NONE) { - return -1; + goto _err; } - vision_source_format_s format { - .pixel_format = VISION_SOURCE_PIXEL_FORMAT_NV12, - .resolution = { camWidth << 1, camHeight }, - .fps = 10, - .quality = 0, - .bitrate = 0 - }; + ret = mv_create_source(&app.remoteData.source); + if (ret != MEDIA_VISION_ERROR_NONE) { + goto _err; + } app.imgWidth = camWidth << 1; app.imgHeight = camHeight; - app.buffer = new unsigned char [app.imgWidth * app.imgHeight]; app.stopWatch.resetTime(); + try { + app.buffer = new unsigned char [app.imgWidth * app.imgHeight]; + app.remoteData.buffer = new unsigned char [app.imgWidth * app.imgHeight * 4]; + } catch (const std::exception &e) { + goto _err; + } // vision-source - vision_source_h vision_source_handle = nullptr; ret = vision_source_init(&vision_source_handle); if (ret != VISION_SOURCE_ERROR_NONE) { - return -1; + goto _err; } ret = vision_source_open_device(vision_source_handle, VISION_SOURCE_DEVICE_ID); if (ret != VISION_SOURCE_ERROR_NONE) { - return -1; + goto _err; } loop = g_main_loop_new(NULL,false); ret = vision_source_set_stream_format(vision_source_handle, &format); if (ret != VISION_SOURCE_ERROR_NONE) { - return -1; + goto _err; } vision_source_start_stream(vision_source_handle, _vision_source_cb, &app); @@ -299,15 +364,34 @@ int main(int argc, char *argv[]) g_main_loop_run(loop); printf("stop\n"); - delete [] app.buffer; - vision_source_stop_stream(vision_source_handle); - vision_source_close_device(vision_source_handle); - vision_source_exit(vision_source_handle); - mv_destroy_source(app.mv_source); - mv_3d_destroy(app.mv3d_handle); - mv_destroy_engine_config(engine_config); +_err: + if (loop) + g_main_loop_unref(loop); + + if (vision_source_handle) { + vision_source_close_device(vision_source_handle); + vision_source_exit(vision_source_handle); + } + + if (app.buffer) + delete [] app.buffer; + + if (app.mv_source) + mv_destroy_source(app.mv_source); + + if (app.mv3d_handle) + mv_3d_destroy(app.mv3d_handle); + + if (engine_config) + mv_destroy_engine_config(engine_config); + + if (app.remoteData.source) + mv_destroy_source(app.remoteData.source); + + if (app.remoteData.buffer) + delete [] app.remoteData.buffer; return 0; } -- 2.7.4