From e775bff72d3346c141bee6e4576a8f75875e93ea Mon Sep 17 00:00:00 2001 From: Nicolas Pope <nwpope@utu.fi> Date: Fri, 12 Apr 2019 07:45:30 +0300 Subject: [PATCH] Zlib depth stream and publish to network. Deflate in reconstruct and display --- CMakeLists.txt | 1 + reconstruct/CMakeLists.txt | 2 +- reconstruct/src/main.cpp | 29 +++++++++++++++++++++++++++-- vision/CMakeLists.txt | 2 +- vision/src/streamer.cpp | 22 ++++++++++++++++++++-- 5 files changed, 50 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 15ef5027a..d12b4d1a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ find_package( Threads REQUIRED ) find_package( URIParser REQUIRED ) find_package( MsgPack REQUIRED ) find_package( LibSGM ) +find_package( ZLIB REQUIRED ) # Readline library is not required on Windows # May also entirely remove dependence on this... it should be optional at least. diff --git a/reconstruct/CMakeLists.txt b/reconstruct/CMakeLists.txt index f12c03057..8dfbccded 100644 --- a/reconstruct/CMakeLists.txt +++ b/reconstruct/CMakeLists.txt @@ -12,6 +12,6 @@ add_executable(ftl-reconstruct ${REPSRC}) add_dependencies(ftl-reconstruct ftlnet) #target_include_directories(cv-node PUBLIC ${PROJECT_SOURCE_DIR}/include) -target_link_libraries(ftl-reconstruct Threads::Threads ${OpenCV_LIBS} glog::glog ftlnet) +target_link_libraries(ftl-reconstruct Threads::Threads ZLIB::ZLIB ${OpenCV_LIBS} glog::glog ftlnet) diff --git a/reconstruct/src/main.cpp b/reconstruct/src/main.cpp index f35adb20d..806e17bb9 100644 --- a/reconstruct/src/main.cpp +++ b/reconstruct/src/main.cpp @@ -6,6 +6,7 @@ #include <glog/logging.h> #include <ftl/config.h> +#include <zlib.h> #include <string> #include <map> @@ -114,23 +115,47 @@ static void process_options(const map<string, string> &opts) { static void run(const string &file) { Universe net(config["net"]); - Mat rgb; + Mat rgb, depth; mutex m; // Make sure connections are complete sleep_for(milliseconds(500)); - net.subscribe(config["source"], [&rgb,&m](const vector<unsigned char> &jpg) { + net.subscribe(config["source"], [&rgb,&m,&depth](const vector<unsigned char> &jpg, const vector<unsigned char> &d) { unique_lock<mutex> lk(m); cv::imdecode(jpg, cv::IMREAD_COLOR, &rgb); //LOG(INFO) << "Received JPG : " << rgb.cols; + + depth = Mat(rgb.size(), CV_32FC1); + + z_stream infstream; + infstream.zalloc = Z_NULL; + infstream.zfree = Z_NULL; + infstream.opaque = Z_NULL; + // setup "b" as the input and "c" as the compressed output + infstream.avail_in = (uInt)d.size(); // size of input + infstream.next_in = (Bytef *)d.data(); // input char array + infstream.avail_out = (uInt)depth.step*depth.rows; // size of output + infstream.next_out = (Bytef *)depth.data; // output char array + + // the actual DE-compression work. + inflateInit(&infstream); + inflate(&infstream, Z_NO_FLUSH); + inflateEnd(&infstream); }); while (true) { + Mat idepth; + unique_lock<mutex> lk(m); if (rgb.cols > 0) { cv::imshow("RGB", rgb); } + if (depth.cols > 0) { + depth.convertTo(idepth, CV_8U, 255.0f / 256.0f); // TODO(nick) + applyColorMap(idepth, idepth, cv::COLORMAP_JET); + cv::imshow("Depth", idepth); + } lk.unlock(); if (cv::waitKey(40) == 27) break; } diff --git a/vision/CMakeLists.txt b/vision/CMakeLists.txt index 28c79e0d8..b07dcc8b2 100644 --- a/vision/CMakeLists.txt +++ b/vision/CMakeLists.txt @@ -47,6 +47,6 @@ set_property(TARGET ftl-vision PROPERTY CUDA_SEPARABLE_COMPILATION ON) endif() #target_include_directories(cv-node PUBLIC ${PROJECT_SOURCE_DIR}/include) -target_link_libraries(ftl-vision Threads::Threads libelas ${OpenCV_LIBS} ${LIBSGM_LIBRARIES} ${CUDA_LIBRARIES} glog::glog ftlnet) +target_link_libraries(ftl-vision Threads::Threads ZLIB::ZLIB libelas ${OpenCV_LIBS} ${LIBSGM_LIBRARIES} ${CUDA_LIBRARIES} glog::glog ftlnet) diff --git a/vision/src/streamer.cpp b/vision/src/streamer.cpp index 79ef05b6b..848c8596f 100644 --- a/vision/src/streamer.cpp +++ b/vision/src/streamer.cpp @@ -1,6 +1,7 @@ #include <glog/logging.h> #include <ftl/streamer.hpp> #include <vector> +#include <zlib.h> using ftl::Streamer; using ftl::net::Universe; @@ -22,8 +23,25 @@ void Streamer::send(const Mat &rgb, const Mat &depth) { // Compress the rgb as jpeg. vector<unsigned char> rgb_buf; cv::imencode(".jpg", rgb, rgb_buf); - net_.publish(uri_, rgb_buf); - LOG(INFO) << "JPG Size = " << ((float)rgb_buf.size() / (1024.0f*1024.0f)) << "Mb"; + vector<unsigned char> d_buf; + d_buf.resize(depth.step*depth.rows); + z_stream defstream; + defstream.zalloc = Z_NULL; + defstream.zfree = Z_NULL; + defstream.opaque = Z_NULL; + defstream.avail_in = depth.step*depth.rows; + defstream.next_in = (Bytef *)depth.data; // input char array + defstream.avail_out = (uInt)depth.step*depth.rows; // size of output + defstream.next_out = (Bytef *)d_buf.data(); // output char array + + deflateInit(&defstream, Z_BEST_COMPRESSION); + deflate(&defstream, Z_FINISH); + deflateEnd(&defstream); + + d_buf.resize(defstream.total_out); + LOG(INFO) << "Depth Size = " << ((float)d_buf.size() / (1024.0f*1024.0f)); + + net_.publish(uri_, rgb_buf, d_buf); } -- GitLab