Skip to content
Snippets Groups Projects
Commit 0379ba57 authored by Max Breitenfeldt's avatar Max Breitenfeldt
Browse files

Change line endings

parent c3407622
Branches
No related tags found
No related merge requests found
Pipeline #38682 canceled
#include "Config.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <QStringList>
#include <iostream>
#include <QFile>
#include <QDir>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/lexical_cast.hpp>
#include <QStandardPaths>
template<typename Stream>
Stream &operator>>(Stream& s, QString& q)
{
std::string tmp;
s >> tmp;
q = tmp.data();
return s;
}
template<typename Stream>
Stream &operator<<(Stream& s, QString const& q)
{
return s << q.toStdString();
}
void Config::load(QString dir, QString file)
{
using namespace boost::property_tree;
auto tree = ptree{};
QDir d(dir);
d.mkpath(dir);
QFile fin(dir + "/" + file);
if(!fin.exists())
{
fin.open(QIODevice::ReadWrite);
fin.close();
}
read_ini((dir + "/" + file).toStdString(), tree);
Config* config = this;
std::string globalPrefix = "General.";
config->EnableView = tree.get<int>(globalPrefix+"EnableView",config->EnableView);
config->EnableMove = tree.get<int>(globalPrefix+"EnableMove",config->EnableMove);
config->EnableRemove = tree.get<int>(globalPrefix+"EnableRemove",config->EnableRemove);
config->EnableSwap = tree.get<int>(globalPrefix+"EnableSwap",config->EnableSwap);
config->EnableAdd = tree.get<int>(globalPrefix+"EnableAdd",config->EnableAdd);
config->EnableRotate = tree.get<int>(globalPrefix+"EnableRotate",config->EnableRotate);
}
void Config::save(QString dir, QString file)
{
using namespace boost::property_tree;
auto tree = ptree{};
Config *config = this;
std::string globalPrefix = "General.";
tree.put(globalPrefix+"EnableView", config->EnableView);
tree.put(globalPrefix+"EnableMove", config->EnableMove);
tree.put(globalPrefix+"EnableRemove", config->EnableRemove);
tree.put(globalPrefix+"EnableSwap", config->EnableSwap);
tree.put(globalPrefix+"EnableAdd", config->EnableAdd);
tree.put(globalPrefix+"EnableRotate", config->EnableRotate);
write_ini((dir + "/" + file).toStdString(), tree);
}
#include "Config.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <QStringList>
#include <iostream>
#include <QFile>
#include <QDir>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/lexical_cast.hpp>
#include <QStandardPaths>
template<typename Stream>
Stream &operator>>(Stream& s, QString& q)
{
std::string tmp;
s >> tmp;
q = tmp.data();
return s;
}
template<typename Stream>
Stream &operator<<(Stream& s, QString const& q)
{
return s << q.toStdString();
}
void Config::load(QString dir, QString file)
{
using namespace boost::property_tree;
auto tree = ptree{};
QDir d(dir);
d.mkpath(dir);
QFile fin(dir + "/" + file);
if(!fin.exists())
{
fin.open(QIODevice::ReadWrite);
fin.close();
}
read_ini((dir + "/" + file).toStdString(), tree);
Config* config = this;
std::string globalPrefix = "General.";
config->EnableView = tree.get<int>(globalPrefix+"EnableView",config->EnableView);
config->EnableMove = tree.get<int>(globalPrefix+"EnableMove",config->EnableMove);
config->EnableRemove = tree.get<int>(globalPrefix+"EnableRemove",config->EnableRemove);
config->EnableSwap = tree.get<int>(globalPrefix+"EnableSwap",config->EnableSwap);
config->EnableAdd = tree.get<int>(globalPrefix+"EnableAdd",config->EnableAdd);
config->EnableRotate = tree.get<int>(globalPrefix+"EnableRotate",config->EnableRotate);
}
void Config::save(QString dir, QString file)
{
using namespace boost::property_tree;
auto tree = ptree{};
Config *config = this;
std::string globalPrefix = "General.";
tree.put(globalPrefix+"EnableView", config->EnableView);
tree.put(globalPrefix+"EnableMove", config->EnableMove);
tree.put(globalPrefix+"EnableRemove", config->EnableRemove);
tree.put(globalPrefix+"EnableSwap", config->EnableSwap);
tree.put(globalPrefix+"EnableAdd", config->EnableAdd);
tree.put(globalPrefix+"EnableRotate", config->EnableRotate);
write_ini((dir + "/" + file).toStdString(), tree);
}
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include "BioTrackerTrackingAlgorithm.h"
#include <future>
#include "TrackedComponents/pose/FishPose.h"
#include <chrono>
#include <zmq.hpp>
#define JSON_USE_IMPLICIT_CONVERSIONS 0
#include "json.hpp"
using json = nlohmann::json;
void* init_shm_mmap(const char *path, int len) {
int fd = shm_open(path, O_RDWR, 0);
if (fd == -1) {
throw "shm_open";
}
void *shm_buf = mmap(NULL, len,
PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
close(fd);
if (shm_buf == MAP_FAILED) {
throw "mmap";
}
return shm_buf;
}
BioTrackerTrackingAlgorithm::BioTrackerTrackingAlgorithm(IController *parent, IModel* parameter, IModel* trajectory)
: IModelTrackingAlgorithm(parent)
{
_cfg = static_cast<ControllerTrackingAlgorithm*>(parent)->getConfig();
_TrackingParameter = (TrackerParameter*)parameter;
_TrackedTrajectoryMajor = (BST::TrackedTrajectory*)trajectory;
_noFish = -1;
_lastImage = nullptr;
_lastFramenumber = -1;
start_python();
}
BioTrackerTrackingAlgorithm::~BioTrackerTrackingAlgorithm()
{
stop_python();
}
void BioTrackerTrackingAlgorithm::request_shared_memory() {
json j = {
{ "type", "request_shared_memory" },
{ "width", _imageX },
{ "height", _imageY },
};
_sock.send(zmq::buffer(j.dump()));
auto res = _sock.recv(_zmq_msg, zmq::recv_flags::none);
auto msg = json::parse(_zmq_msg.to_string_view());
std::string shm_path = msg["path"].get<std::string>();
_shm_img = (float*)init_shm_mmap(shm_path.c_str(), _imageX * _imageY * sizeof(float));
}
void BioTrackerTrackingAlgorithm::stop_python() {
if (_python_process.has_value()) {
_python_process_group.terminate();
_python_process.value().wait();
}
}
void BioTrackerTrackingAlgorithm::start_python() {
stop_python();
_python_process = boost::process::child(
boost::process::search_path("python3"),
boost::process::args({
"-c", "from biotracker import BiotrackerAdapter;"
"BiotrackerAdapter('/home/max/tmp/example.multi_instance'"
",verbose=True).run();"
}), _python_process_group);
_sock = zmq::socket_t(_ctx, zmq::socket_type::req);
_sock.connect("ipc:///tmp/biotracker.python.zmq");
}
void BioTrackerTrackingAlgorithm::receiveAreaDescriptorUpdate(IModelAreaDescriptor *areaDescr) {
_areaInfo = areaDescr;
}
void BioTrackerTrackingAlgorithm::receiveParametersChanged() {
if (_lastFramenumber >= 0 && _lastImage && !_lastImage->empty()) {
doTracking(_lastImage, _lastFramenumber);
}
}
void BioTrackerTrackingAlgorithm::doTracking(std::shared_ptr<cv::Mat> p_image, uint framenumber)
{
_lastImage = p_image;
_lastFramenumber = framenumber;
//dont do nothing if we ain't got an image
if (p_image->empty()) {
return;
}
if (_imageX != p_image->size().width || _imageY != p_image->size().height) {
_imageX = p_image->size().width;
_imageY = p_image->size().height;
request_shared_memory();
Q_EMIT emitDimensionUpdate(_imageX, _imageY);
}
//Refuse to run tracking if we have no area info...
if (_AreaInfo == nullptr) {
Q_EMIT emitTrackingDone(framenumber);
return;
}
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
cv::Mat grayscale, float_img;
cv::cvtColor(*p_image, grayscale, cv::COLOR_BGR2GRAY);
grayscale.convertTo(float_img, CV_32F);
float *img_data = float_img.ptr<float>(0);
int img_data_len = float_img.cols * float_img.rows * sizeof(float);
memcpy(_shm_img, img_data, img_data_len);
json j = {
{ "type", "predict_frame" },
{ "frame_id", framenumber },
};
_sock.send(zmq::buffer(j.dump()));
auto res = _sock.recv(_zmq_msg, zmq::recv_flags::none);
auto msg = json::parse(_zmq_msg.to_string_view());
std::vector<std::tuple<int, FishPose>> poses;
for (auto pos : msg["data"]) {
auto pose = std::make_tuple(
pos["id"].get<int>(),
FishPose(
_areaInfo,
cv::Point2f(pos["x"].get<int>(), pos["y"].get<int>()),
pos["orientation"].get<float>(),
1.0)); // FIXME: score
poses.push_back(pose);
}
std::sort(poses.begin(), poses.end(),
[](std::tuple<int, FishPose> a, std::tuple<int, FishPose> b) {
return std::get<0>(a) < std::get<0>(b);
});
//Insert new poses into data structure
int trajNumber = 0;
for (int i = 0; i < _TrackedTrajectoryMajor->size(); i++) {
BST::TrackedTrajectory *t = dynamic_cast<BST::TrackedTrajectory *>(_TrackedTrajectoryMajor->getChild(i));
if (t && t->getValid() && !t->getFixed()) {
BST::TrackedElement *e = new BST::TrackedElement(t, "n.a.", t->getId());
e->setFishPose(std::get<1>(poses[trajNumber]));
e->setTime(start);
t->add(e, framenumber);
trajNumber++;
}
}
std::string newSel = _TrackingParameter->getNewSelection();
Q_EMIT emitChangeDisplayImage("Original");
Q_EMIT emitTrackingDone(framenumber);
}
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include "BioTrackerTrackingAlgorithm.h"
#include <future>
#include "TrackedComponents/pose/FishPose.h"
#include <chrono>
#include <zmq.hpp>
#define JSON_USE_IMPLICIT_CONVERSIONS 0
#include "json.hpp"
using json = nlohmann::json;
void* init_shm_mmap(const char *path, int len) {
int fd = shm_open(path, O_RDWR, 0);
if (fd == -1) {
throw "shm_open";
}
void *shm_buf = mmap(NULL, len,
PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
close(fd);
if (shm_buf == MAP_FAILED) {
throw "mmap";
}
return shm_buf;
}
BioTrackerTrackingAlgorithm::BioTrackerTrackingAlgorithm(IController *parent, IModel* parameter, IModel* trajectory)
: IModelTrackingAlgorithm(parent)
{
_cfg = static_cast<ControllerTrackingAlgorithm*>(parent)->getConfig();
_TrackingParameter = (TrackerParameter*)parameter;
_TrackedTrajectoryMajor = (BST::TrackedTrajectory*)trajectory;
_noFish = -1;
_lastImage = nullptr;
_lastFramenumber = -1;
start_python();
}
BioTrackerTrackingAlgorithm::~BioTrackerTrackingAlgorithm()
{
stop_python();
}
void BioTrackerTrackingAlgorithm::request_shared_memory() {
json j = {
{ "type", "request_shared_memory" },
{ "width", _imageX },
{ "height", _imageY },
};
_sock.send(zmq::buffer(j.dump()));
auto res = _sock.recv(_zmq_msg, zmq::recv_flags::none);
auto msg = json::parse(_zmq_msg.to_string_view());
std::string shm_path = msg["path"].get<std::string>();
_shm_img = (float*)init_shm_mmap(shm_path.c_str(), _imageX * _imageY * sizeof(float));
}
void BioTrackerTrackingAlgorithm::stop_python() {
if (_python_process.has_value()) {
_python_process_group.terminate();
_python_process.value().wait();
}
}
void BioTrackerTrackingAlgorithm::start_python() {
stop_python();
_python_process = boost::process::child(
boost::process::search_path("python3"),
boost::process::args({
"-c", "from biotracker import BiotrackerAdapter;"
"BiotrackerAdapter('/home/max/tmp/example.multi_instance'"
",verbose=True).run();"
}), _python_process_group);
_sock = zmq::socket_t(_ctx, zmq::socket_type::req);
_sock.connect("ipc:///tmp/biotracker.python.zmq");
}
void BioTrackerTrackingAlgorithm::receiveAreaDescriptorUpdate(IModelAreaDescriptor *areaDescr) {
_areaInfo = areaDescr;
}
void BioTrackerTrackingAlgorithm::receiveParametersChanged() {
if (_lastFramenumber >= 0 && _lastImage && !_lastImage->empty()) {
doTracking(_lastImage, _lastFramenumber);
}
}
void BioTrackerTrackingAlgorithm::doTracking(std::shared_ptr<cv::Mat> p_image, uint framenumber)
{
_lastImage = p_image;
_lastFramenumber = framenumber;
//dont do nothing if we ain't got an image
if (p_image->empty()) {
return;
}
if (_imageX != p_image->size().width || _imageY != p_image->size().height) {
_imageX = p_image->size().width;
_imageY = p_image->size().height;
request_shared_memory();
Q_EMIT emitDimensionUpdate(_imageX, _imageY);
}
//Refuse to run tracking if we have no area info...
if (_AreaInfo == nullptr) {
Q_EMIT emitTrackingDone(framenumber);
return;
}
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
cv::Mat grayscale, float_img;
cv::cvtColor(*p_image, grayscale, cv::COLOR_BGR2GRAY);
grayscale.convertTo(float_img, CV_32F);
float *img_data = float_img.ptr<float>(0);
int img_data_len = float_img.cols * float_img.rows * sizeof(float);
memcpy(_shm_img, img_data, img_data_len);
json j = {
{ "type", "predict_frame" },
{ "frame_id", framenumber },
};
_sock.send(zmq::buffer(j.dump()));
auto res = _sock.recv(_zmq_msg, zmq::recv_flags::none);
auto msg = json::parse(_zmq_msg.to_string_view());
std::vector<std::tuple<int, FishPose>> poses;
for (auto pos : msg["data"]) {
auto pose = std::make_tuple(
pos["id"].get<int>(),
FishPose(
_areaInfo,
cv::Point2f(pos["x"].get<int>(), pos["y"].get<int>()),
pos["orientation"].get<float>(),
1.0)); // FIXME: score
poses.push_back(pose);
}
std::sort(poses.begin(), poses.end(),
[](std::tuple<int, FishPose> a, std::tuple<int, FishPose> b) {
return std::get<0>(a) < std::get<0>(b);
});
//Insert new poses into data structure
int trajNumber = 0;
for (int i = 0; i < _TrackedTrajectoryMajor->size(); i++) {
BST::TrackedTrajectory *t = dynamic_cast<BST::TrackedTrajectory *>(_TrackedTrajectoryMajor->getChild(i));
if (t && t->getValid() && !t->getFixed()) {
BST::TrackedElement *e = new BST::TrackedElement(t, "n.a.", t->getId());
e->setFishPose(std::get<1>(poses[trajNumber]));
e->setTime(start);
t->add(e, framenumber);
trajNumber++;
}
}
std::string newSel = _TrackingParameter->getNewSelection();
Q_EMIT emitChangeDisplayImage("Original");
Q_EMIT emitTrackingDone(framenumber);
}
#ifndef BIOTRACKERTRACKINGALGORITHM_H
#define BIOTRACKERTRACKINGALGORITHM_H
#include "Interfaces/IModel/IModel.h"
#include "TrackerParameter.h"
#include <boost/process.hpp>
#include <opencv2/opencv.hpp>
#include "Interfaces/IModel/IModelTrackingAlgorithm.h"
#include "Interfaces/IModel/IModelDataExporter.h"
#include "TrackedComponents/TrackedElement.h"
#include "TrackedComponents/TrackedTrajectory.h"
#include "../Controller/ControllerTrackingAlgorithm.h"
#include "Interfaces/IModel/IModelAreaDescriptor.h"
#include <iostream>
#include <zmq.hpp>
#include "../Config.h"
class BioTrackerTrackingAlgorithm : public IModelTrackingAlgorithm
{
Q_OBJECT
public:
BioTrackerTrackingAlgorithm(IController *parent, IModel* parameter, IModel* trajectory);
~BioTrackerTrackingAlgorithm();
Q_SIGNALS:
void emitCvMatA(std::shared_ptr<cv::Mat> image, QString name);
void emitDimensionUpdate(int x, int y);
void emitTrackingDone(uint framenumber);
// ITrackingAlgorithm interface
public Q_SLOTS:
void doTracking(std::shared_ptr<cv::Mat> image, uint framenumber) override;
void receiveAreaDescriptorUpdate(IModelAreaDescriptor *areaDescr);
void receiveParametersChanged();
private:
void request_shared_memory();
void start_python();
void stop_python();
BST::TrackedTrajectory* _TrackedTrajectoryMajor;
TrackerParameter* _TrackingParameter;
IModelAreaDescriptor* _AreaInfo;
int _noFish;
std::optional<boost::process::child> _python_process;
boost::process::group _python_process_group;
int _imageX;
int _imageY;
std::shared_ptr<cv::Mat> _lastImage;
uint _lastFramenumber;
Config *_cfg;
zmq::context_t _ctx;
zmq::socket_t _sock;
zmq::message_t _zmq_msg;
float *_shm_img;
IModelAreaDescriptor* _areaInfo;
};
#endif // BIOTRACKERTRACKINGALGORITHM_H
#ifndef BIOTRACKERTRACKINGALGORITHM_H
#define BIOTRACKERTRACKINGALGORITHM_H
#include "Interfaces/IModel/IModel.h"
#include "TrackerParameter.h"
#include <boost/process.hpp>
#include <opencv2/opencv.hpp>
#include "Interfaces/IModel/IModelTrackingAlgorithm.h"
#include "Interfaces/IModel/IModelDataExporter.h"
#include "TrackedComponents/TrackedElement.h"
#include "TrackedComponents/TrackedTrajectory.h"
#include "../Controller/ControllerTrackingAlgorithm.h"
#include "Interfaces/IModel/IModelAreaDescriptor.h"
#include <iostream>
#include <zmq.hpp>
#include "../Config.h"
class BioTrackerTrackingAlgorithm : public IModelTrackingAlgorithm
{
Q_OBJECT
public:
BioTrackerTrackingAlgorithm(IController *parent, IModel* parameter, IModel* trajectory);
~BioTrackerTrackingAlgorithm();
Q_SIGNALS:
void emitCvMatA(std::shared_ptr<cv::Mat> image, QString name);
void emitDimensionUpdate(int x, int y);
void emitTrackingDone(uint framenumber);
// ITrackingAlgorithm interface
public Q_SLOTS:
void doTracking(std::shared_ptr<cv::Mat> image, uint framenumber) override;
void receiveAreaDescriptorUpdate(IModelAreaDescriptor *areaDescr);
void receiveParametersChanged();
private:
void request_shared_memory();
void start_python();
void stop_python();
BST::TrackedTrajectory* _TrackedTrajectoryMajor;
TrackerParameter* _TrackingParameter;
IModelAreaDescriptor* _AreaInfo;
int _noFish;
std::optional<boost::process::child> _python_process;
boost::process::group _python_process_group;
int _imageX;
int _imageY;
std::shared_ptr<cv::Mat> _lastImage;
uint _lastFramenumber;
Config *_cfg;
zmq::context_t _ctx;
zmq::socket_t _sock;
zmq::message_t _zmq_msg;
float *_shm_img;
IModelAreaDescriptor* _areaInfo;
};
#endif // BIOTRACKERTRACKINGALGORITHM_H
#include "TrackerParameter.h"
#include "../Controller/ControllerTrackingAlgorithm.h"
TrackerParameter::TrackerParameter(QObject *parent) :
IModel(parent)
{
_cfg = static_cast<ControllerTrackingAlgorithm*>(parent)->getConfig();
Q_EMIT notifyView();
}
#include "TrackerParameter.h"
#include "../Controller/ControllerTrackingAlgorithm.h"
TrackerParameter::TrackerParameter(QObject *parent) :
IModel(parent)
{
_cfg = static_cast<ControllerTrackingAlgorithm*>(parent)->getConfig();
Q_EMIT notifyView();
}
#ifndef TRACKERPARAMETER_H
#define TRACKERPARAMETER_H
#include "Interfaces/IModel/IModel.h"
#include "../Config.h"
class TrackerParameter : public IModel
{
Q_OBJECT
public:
TrackerParameter(QObject *parent = 0);
std::string getNewSelection() { return _newSelection; };
void setNewSelection(std::string x) {
_newSelection = x;
}
private:
std::string _newSelection;
Config *_cfg;
};
#endif // TRACKERPARAMETER_H
#ifndef TRACKERPARAMETER_H
#define TRACKERPARAMETER_H
#include "Interfaces/IModel/IModel.h"
#include "../Config.h"
class TrackerParameter : public IModel
{
Q_OBJECT
public:
TrackerParameter(QObject *parent = 0);
std::string getNewSelection() { return _newSelection; };
void setNewSelection(std::string x) {
_newSelection = x;
}
private:
std::string _newSelection;
Config *_cfg;
};
#endif // TRACKERPARAMETER_H
#include "TrackerParameterView.h"
#include "ui_TrackerParameterView.h"
#include <iostream>
TrackerParameterView::TrackerParameterView(QWidget *parent, IController *controller, IModel *model) :
IViewWidget(parent, controller, model),
_ui(new Ui::TrackerParameterView)
{
_ui->setupUi(this);
}
TrackerParameterView::~TrackerParameterView()
{
delete _ui;
}
void TrackerParameterView::getNotified()
{
}
#include "TrackerParameterView.h"
#include "ui_TrackerParameterView.h"
#include <iostream>
TrackerParameterView::TrackerParameterView(QWidget *parent, IController *controller, IModel *model) :
IViewWidget(parent, controller, model),
_ui(new Ui::TrackerParameterView)
{
_ui->setupUi(this);
}
TrackerParameterView::~TrackerParameterView()
{
delete _ui;
}
void TrackerParameterView::getNotified()
{
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment