Skip to content
Snippets Groups Projects
Commit b87c727c authored by Sebastian Gomez-Gonzalez's avatar Sebastian Gomez-Gonzalez
Browse files

Server code for the ball tracking partially tested

parent bec1efd3
Branches
Tags
No related merge requests found
cmake_minimum_required(VERSION 3.5)
project(ball_tracking)
add_definitions(-DBOOST_LOG_DYN_LINK)
find_package( Boost 1.58 COMPONENTS log REQUIRED )
find_package(OpenCV REQUIRED)
find_package(CUDA 8.0)
find_library(ZMQPP NAMES zmqpp)
find_path(ZMQPP_INCLUDES NAMES zmqpp/zmqpp.hpp)
find_library(CAMLIB NAMES camera)
find_path(CAMLIB_INC NAMES camera.hpp)
if (CUDA_FOUND)
option (WITH_CUDA "Compile the library with GPU implementations" ON)
......@@ -30,6 +36,8 @@ endif(WITH_CUDA)
include_directories(include
${OpenCV_INCLUDES}
${ZMQPP_INCLUDES}
${CAMLIB_INC}
)
add_library(ball_tracking SHARED
......@@ -42,6 +50,9 @@ add_library(ball_tracking SHARED
target_link_libraries(ball_tracking
${OpenCV_LIBS}
${GPU_BT_LIB}
${ZMQPP}
${CAMLIB}
${Boost_LIBRARIES}
)
......
......@@ -27,6 +27,14 @@ target_link_libraries(track
${Boost_LIBRARIES}
)
add_executable(track_server
tracking/server.cpp
)
target_link_libraries(track_server
ball_tracking
${Boost_LIBRARIES}
)
if (WITH_CUDA)
add_executable(log_reg_approach
img_proc/log_reg_approach.cpp
......
#include <ball_tracking/server.hpp>
#include <ball_tracking/utils.hpp>
#include <iostream>
#include <boost/program_options.hpp>
#include <json.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
using namespace ball_tracking;
using json = nlohmann::json;
int main(int argc, char** argv) {
try {
namespace po = boost::program_options;
po::options_description desc("Options");
desc.add_options()
("help", "Produce help message")
("conf,c", po::value<string>(), "path to the JSON configuration");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << endl;
return 0;
}
if (!vm.count("conf")) {
cerr << "Error: You should provide the video to process" << endl;
return 1;
}
json server_conf = load_json(vm.at("conf").as<string>());
cout << "Loading server configuration" << endl;
TrackServer srv;
srv.start(server_conf);
cout << "Tracking started. Type q to quit" << endl;
char c;
do {
cin >> c;
} while (c != 'q');
cout << "Stopping" << endl;
srv.stop();
} catch (std::exception& ex) {
cerr << "Exception: " << ex.what() << endl;
return 1;
}
}
{
"server": {
"servers": {
"position_publisher": "tcp://*:7650"
},
"log": {
......
......@@ -11,8 +11,10 @@ namespace ball_tracking {
class Impl;
std::shared_ptr<Impl> _impl;
public:
TrackServer(const nlohmann::json& conf);
TrackServer();
~TrackServer();
void start(const nlohmann::json& conf);
void stop();
};
};
......
......@@ -28,10 +28,15 @@ namespace ball_tracking {
public:
unordered_map<unsigned int,Tracker> trackers;
ThreadedListener tlistener;
zmqpp::socket position_pub;
unique_ptr<zmqpp::context> context;
unique_ptr<zmqpp::socket> position_pub;
CameraSet cams;
high_resolution_clock::time_point start_time;
Impl() {
position_pub = nullptr;
}
void start_trackers(const json& conf) {
for (auto tracker : conf) {
unsigned int ID = tracker.at("ID");
......@@ -41,6 +46,7 @@ namespace ball_tracking {
BOOST_LOG_TRIVIAL(info) << "Reading configuration for ID " << ID << " in " << fname;
json tconf = load_json(fname);
trackers[ID] = Tracker(tconf);
BOOST_LOG_TRIVIAL(info) << "Tracker with ID " << ID << " started successfully";
} else {
throw std::logic_error("Configuration for the tracking object expected but not given");
}
......@@ -53,12 +59,12 @@ namespace ball_tracking {
//1) Start the networking sockets
const json& srv_conf = conf.at("servers");
zmqpp::context context;
context = unique_ptr<zmqpp::context>(new zmqpp::context);
auto socket_type = zmqpp::socket_type::pub;
position_pub = zmqpp::socket(context, socket_type);
position_pub = unique_ptr<zmqpp::socket>(new zmqpp::socket(*context, socket_type));
const string& pp_url = srv_conf.at("position_publisher");
BOOST_LOG_TRIVIAL(info) << "Starting 2D position publisher server in " << pp_url;
position_pub.bind(pp_url);
position_pub->bind(pp_url);
//2) Start the tracking pipeline
start_trackers(conf.at("trackers"));
......@@ -92,7 +98,7 @@ namespace ball_tracking {
BOOST_LOG_TRIVIAL(debug) << "Sending message: " << jframe.dump();
zmqpp::message msg;
msg << topic << jframe.dump();
this->position_pub.send(msg);
this->position_pub->send(msg);
};
//3.2) Add the call-back as a new thread
......@@ -102,5 +108,30 @@ namespace ball_tracking {
BOOST_LOG_TRIVIAL(debug) << "Starting all the camera listeners";
cams.start(tlistener);
}
void stop() {
BOOST_LOG_TRIVIAL(info) << "Stopping the position publisher server";
cams.stop();
tlistener.stop();
}
~Impl() {
stop();
}
};
TrackServer::TrackServer() {
_impl = make_shared<Impl>();
}
TrackServer::~TrackServer() = default;
void TrackServer::start(const nlohmann::json& conf) {
_impl->start_server(conf);
}
void TrackServer::stop() {
_impl->stop();
}
};
......@@ -12,6 +12,14 @@
#include <opencv2/core.hpp>
#endif
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
namespace logging = boost::log;
using namespace cv;
using namespace std;
using json = nlohmann::json;
......@@ -316,7 +324,9 @@ namespace ball_tracking {
cv::cuda::Stream stream;
public:
GPUTracker(const json& conf) {
BOOST_LOG_TRIVIAL(debug) << "Creating a log-likelihood object in the GPU";
llh = ball_tracking::cuda::BallLogLikelihood(conf.at("ball_log_lh"));
BOOST_LOG_TRIVIAL(debug) << "GPU log-likelihood creation was successful. Now creating CPU blob";
blob_detect = FindBallBlob(conf.at("blob_detection"));
}
......@@ -337,6 +347,7 @@ namespace ball_tracking {
class GPUTracker {
public:
GPUTracker(const json& conf) {
BOOST_LOG_TRIVIAL(error) << "Attempting to create a GPU object on a CPU only compiled library";
throw new std::logic_error("Calling a GPU function on a CPU only compiled tracker");
}
......@@ -354,9 +365,12 @@ namespace ball_tracking {
Impl(const json& conf) {
string type = conf.at("type");
if (type == "cpu") {
BOOST_LOG_TRIVIAL(debug) << "Creating a CPU tracker...";
tracker = CPUTracker(conf.at("conf"));
} else if (type == "gpu") {
tracker = GPUTracker(conf.at("gpu"));
BOOST_LOG_TRIVIAL(debug) << "Creating a GPU tracker...";
tracker = GPUTracker(conf.at("conf"));
BOOST_LOG_TRIVIAL(debug) << "GPU tracker successfully created";
} else {
throw std::logic_error("Type of tracker selected in the configuration is not recognized");
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment