Newer
Older

Nicolas Pope
committed
/*
* Copyright 2019 Nicolas Pope. All rights reserved.
*
* See LICENSE.
*/
#include <glog/logging.h>
#include <ftl/config.h>
#include <ftl/configuration.hpp>
// #include <lz4.h>

Nicolas Pope
committed
#include <string>
#include <vector>
#include <thread>
#include <chrono>
#include <mutex>
#include <opencv2/opencv.hpp>
#include <ftl/net/universe.hpp>
#include <ftl/display.hpp>

Nicolas Pope
committed
#include <nlohmann/json.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/core/utility.hpp"
#ifdef WIN32
#pragma comment(lib, "Rpcrt4.lib")
#endif
using ftl::net::Universe;
using ftl::Display;

Nicolas Pope
committed
using std::string;
using std::vector;
using cv::Mat;
using json = nlohmann::json;
using std::this_thread::sleep_for;
using std::chrono::milliseconds;
using std::mutex;
using std::unique_lock;

Nicolas Pope
committed
Universe net(config["net"]);

Nicolas Pope
committed
mutex m;
Display disp(config["display"]);

Nicolas Pope
committed
// Make sure connections are complete
sleep_for(milliseconds(500));
// TODO(nick) Allow for many sources
net.subscribe(config["source"], [&rgb,&m,&depth](const vector<unsigned char> &jpg, const vector<unsigned char> &d) {

Nicolas Pope
committed
unique_lock<mutex> lk(m);
cv::imdecode(jpg, cv::IMREAD_COLOR, &rgb);
//LOG(INFO) << "Received JPG : " << rgb.cols;

Nicolas Pope
committed
depth = Mat(rgb.size(), CV_16UC1);

Nicolas Pope
committed
/*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);

Nicolas Pope
committed
inflateEnd(&infstream);*/
//LZ4_decompress_safe((char*)d.data(), (char*)depth.data, d.size(), depth.step*depth.rows);

Nicolas Pope
committed
cv::imdecode(d, cv::IMREAD_UNCHANGED, &depth);
depth.convertTo(depth, CV_32FC1, 1.0f/16.0f);

Nicolas Pope
committed
});
while (disp.active()) {
Mat idepth;

Nicolas Pope
committed
unique_lock<mutex> lk(m);
if (depth.cols > 0) {
// If no calibration data then get it from the remote machine

Nicolas Pope
committed
// Must unlock before calling findOne to prevent net block!!
lk.unlock();
auto buf = net.findOne<vector<unsigned char>>((string)config["source"]+"/calibration");

Nicolas Pope
committed
lk.lock();
if (buf) {
Q = Mat(cv::Size(4,4), CV_32F);
memcpy(Q.data, (*buf).data(), (*buf).size());
if (Q.step*Q.rows != (*buf).size()) LOG(ERROR) << "Corrupted calibration";
disp.setCalibration(Q);
}
if (rgb.cols > 0) {
//cv::imshow("RGB", rgb);
disp.render(rgb,depth);
}
// TODO(nick) Use a swap buffer so this can be unlocked earlier

Nicolas Pope
committed
lk.unlock();
//if (cv::waitKey(40) == 27) break;
disp.wait(40);

Nicolas Pope
committed
}
}
int main(int argc, char **argv) {
ftl::configure(argc, argv, "representation"); // TODO(nick) change to "reconstruction"
run();