Skip to content
Snippets Groups Projects
main.cpp 3.24 KiB
Newer Older
Nicolas Pope's avatar
Nicolas Pope committed
 * Copyright 2019 Nicolas Pope. All rights reserved.
 *
 * See LICENSE.
Nicolas Pope's avatar
Nicolas Pope committed
#define LOGURU_WITH_STREAMS 1
#include <loguru.hpp>
#include <ftl/configuration.hpp>
#include <ctpl_stl.h>
// #include <zlib.h>

#include <string>
#include <map>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <opencv2/opencv.hpp>
Nicolas Pope's avatar
Nicolas Pope committed
#include <ftl/rgbd.hpp>
#include <ftl/display.hpp>
#include <ftl/rgbd/streamer.hpp>
#include <ftl/slave.hpp>
#include <nlohmann/json.hpp>
Nicolas Pope's avatar
Nicolas Pope committed
#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::rgbd::Source;
using ftl::rgbd::Camera;
using ftl::Display;
using ftl::rgbd::Streamer;
using std::string;
using std::vector;
using std::condition_variable;
using std::this_thread::sleep_for;
using std::chrono::milliseconds;
using std::mutex;
using std::unique_lock;
using json = nlohmann::json;
Nicolas Pope's avatar
Nicolas Pope committed
namespace ftl {
void disparityToDepth(const cv::Mat &disparity, cv::Mat &depth, const cv::Mat &q) {
	cv::Matx44d _Q;
    q.convertTo(_Q, CV_64F);

	if (depth.empty()) depth = cv::Mat(disparity.size(), CV_32F);

	for( int y = 0; y < disparity.rows; y++ ) {
		const float *sptr = disparity.ptr<float>(y);
		float *dptr = depth.ptr<float>(y);

		for( int x = 0; x < disparity.cols; x++ ) {
			double d = sptr[x];
			cv::Vec4d homg_pt = _Q*cv::Vec4d(x, y, d, 1.0);
			//dptr[x] = Vec3d(homg_pt.val);
			//dptr[x] /= homg_pt[3];
			dptr[x] = (homg_pt[2] / homg_pt[3]) / 1000.0f; // Depth in meters

			if( fabs(d) <= FLT_EPSILON )
				dptr[x] = 1000.0f;
		}
	}
}
};

Nicolas Pope's avatar
Nicolas Pope committed
static void run(ftl::Configurable *root) {
	Universe *net = ftl::create<Universe>(root, "net");
	ftl::ctrl::Slave slave(net, root);
Nicolas Pope's avatar
Nicolas Pope committed
	auto paths = root->get<vector<string>>("paths");
	string file = "";
	if (paths && (*paths).size() > 0) file = (*paths)[0];

	Source *source = nullptr;
	source = ftl::create<Source>(root, "source");

	if (file != "") source->set("uri", file);
Nicolas Pope's avatar
Nicolas Pope committed
	Display *display = ftl::create<Display>(root, "display", "local");
Nicolas Pope's avatar
Nicolas Pope committed
	Streamer *stream = ftl::create<Streamer>(root, "stream", net);
	stream->add(source);
	stream->run();
	net->start();
	LOG(INFO) << "Running...";

	while (ftl::running && display->active()) {
		cv::Mat rgb, depth;
		source->getFrames(rgb, depth);
		if (!rgb.empty()) display->render(rgb, depth, source->parameters());
Nicolas Pope's avatar
Nicolas Pope committed
		display->wait(10);
	LOG(INFO) << "Stopping...";
	slave.stop();
Nicolas Pope's avatar
Nicolas Pope committed
	stream->stop();
	net->shutdown();
Nicolas Pope's avatar
Nicolas Pope committed
	delete stream;
	delete display;
	//delete source;  // TODO(Nick) Add ftl::destroy
Nicolas Pope's avatar
Nicolas Pope committed
	delete net;
Nicolas Pope's avatar
Nicolas Pope committed
	std::cout << "FTL Vision Node " << FTL_VERSION_LONG << std::endl;
Nicolas Pope's avatar
Nicolas Pope committed
	auto root = ftl::configure(argc, argv, "vision_default");
Nicolas Pope's avatar
Nicolas Pope committed
	//config["ftl://vision/default"]["paths"] = paths;
	// Choose normal or middlebury modes
Nicolas Pope's avatar
Nicolas Pope committed
	//if (config["middlebury"]["dataset"] == "") {
Nicolas Pope's avatar
Nicolas Pope committed
		std::cout << "Loading..." << std::endl;
Nicolas Pope's avatar
Nicolas Pope committed
		run(root);
	//} else {
	//	ftl::middlebury::test(config);
	//}
	delete root;
	LOG(INFO) << "Terminating with code " << ftl::exit_code;
	LOG(INFO) << "Branch: " << ftl::branch_name;
	return ftl::exit_code;