Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Classe CycloConfig
/*
* CycloConfig.cpp
*
* Created on: 5 de out de 2016
* Author: consultoria@carlosdelfino.eti.br
*/
#include "CycloConfig.hpp"
#include <opencv2/opencv.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <string>
#include <exception>
#include <iostream>
#include<getopt.h>
#include "Utils.hpp"
namespace pt = boost::property_tree;
CycloConfig* CycloConfig::get() {
#if __cplusplus < 201103L
#warning "Este código é seguro com o uso do C+11, pode haver problemas relativo a concorrencia em verões inferiores."
#endif
static CycloConfig *instance = new CycloConfig();
return instance;
}
void CycloConfig::setFullFrameSize(cv::Size size) {
this->data.width = size.width;
this->data.height = size.height;
if (reconfigure()) {
setPerspectiveArea(size);
setCropArea(size);
setInterestArea(size);
}
}
void CycloConfig::setFullFrameWidth(unsigned int w) {
this->data.width = w;
}
void CycloConfig::setFullFrameHeight(unsigned int h) {
this->data.height = h;
}
cv::Size CycloConfig::getFullFrameSize() {
cv::Size sz(getFullFrameWidth(), getFullFrameHeight());
return sz;
}
/**
* @deprecated
*/
void CycloConfig::setPerspectiveArea(cv::Size size) {
this->data.x[0] = 0;
this->data.y[0] = 0;
this->data.x[1] = size.width;
this->data.y[1] = 0;
this->data.x[2] = 0;
this->data.y[2] = size.height;
this->data.x[3] = size.width;
this->data.y[3] = size.height;
}
/**
* @deprecated
*/
void CycloConfig::setInterestArea(cv::Size size) {
this->data.x_interest[0] = 0;
this->data.y_interest[0] = 0;
this->data.x_interest[1] = size.width;
this->data.y_interest[1] = size.height;
}
/**
* @deprecated
*/
void CycloConfig::setCropArea(cv::Size size) {
this->data.x_crop[0] = 0;
this->data.y_crop[0] = 0;
this->data.x_crop[1] = size.width;
this->data.y_crop[1] = size.height;
}
CycloConfig::CycloConfig() {
this->LoadData();
std::cout << "** CycloConfig> Criado." << std::endl;
}
CycloConfig::~CycloConfig() {
if (getSaveConfig()) {
this->PersistData();
std::cout << "** CycloConfig> Descartado." << std::endl;
}
std::cout << "** CycloConfig> Descartado." << std::endl;
}
unsigned int CycloConfig::GetLeftCounter() {
return this->data.left_counter;
}
void CycloConfig::SetLeftCounter(unsigned int counter) {
this->data.left_counter = counter;
}
unsigned int CycloConfig::GetRightCounter() {
return this->data.right_counter;
}
void CycloConfig::SetRightCounter(unsigned int counter) {
this->data.right_counter = counter;
}
cv::Point CycloConfig::GetCounterPos(unsigned int index) {
cv::Point pt(this->data.x_counter[index], this->data.y_counter[index]);
return pt;
}
cv::Point CycloConfig::GetPerspectivePos(unsigned int index) {
cv::Point pt;
pt.x = this->data.x[index];
pt.y = this->data.y[index];
return pt;
}
cv::Point CycloConfig::GetCropPos(unsigned int index) {
cv::Point pt;
pt.x = this->data.x_crop[index];
pt.y = this->data.y_crop[index];
return pt;
}
cv::Point CycloConfig::GetInterestPos(unsigned int index) {
cv::Point pt;
pt.x = this->data.x_interest[index];
pt.y = this->data.y_interest[index];
return pt;
}
unsigned int CycloConfig::getFullFrameHeight() {
return this->data.height;
}
unsigned int CycloConfig::getFullFrameWidth() {
return this->data.width;
}
void CycloConfig::clearPerspectiveArea() {
setPerspectiveArea(getFullFrameSize());
}
void CycloConfig::clearCropArea() {
setCropArea(getFullFrameSize());
}
void CycloConfig::setPathImageLogo(std::string p) {
this->data.pathImageLogo = p;
}
void CycloConfig::setPathImageCyclist(std::string p) {
this->data.pathImageCyclist = p;
}
std::string CycloConfig::getPathPrincipalLogo() {
return this->data.pathImageLogo;
}
std::string CycloConfig::getPathSecondLogo() {
return this->data.pathImageCyclist;
}
void CycloConfig::clearInteresstArea() {
setInterestArea(getFullFrameSize());
}
void CycloConfig::PersistData() {
PersistData(this->configFileName);
}
void CycloConfig::PersistData(std::string fileName) {
std::cout << "** CycloConfig> Persistindo configurações" << std::endl;
pt::ptree tree;
std::cout << "** CycloConfig> gravando tamanho padrão da janela principal"
<< std::endl;
tree.put(PROP_FULL_FRAME_WIDTH, this->getFullFrameWidth());
tree.put(PROP_FULL_FRAME_HEIGHT, this->getFullFrameHeight());
std::cout << "** CycloConfig> gravando valor dos contadores" << std::endl;
tree.put(PROP_DETECTEDOBJ_COUNTER_1, this->GetLeftCounter());
tree.put(PROP_DETECTEDOBJ_COUNTER_2, this->GetRightCounter());
std::cout << "** CycloConfig> gravando posição dos contadores" << std::endl;
tree.put(PROP_DETECTEDOBJ_COUNTER_1_X, this->getCounterX(0));
tree.put(PROP_DETECTEDOBJ_COUNTER_1_Y, this->getCounterY(0));
tree.put(PROP_DETECTEDOBJ_COUNTER_2_X, this->getCounterX(1));
tree.put(PROP_DETECTEDOBJ_COUNTER_2_Y, this->getCounterY(1));
tree.put(PROP_COUNTER_DETECTEDOBJ_TOTAL, getCounterDetectedObject());
std::cout << "** CycloConfig> gravando area de corte" << std::endl;
tree.put(PROP_CROP_WINDOW_X, this->getCropWindowPosX());
tree.put(PROP_CROP_WINDOW_Y, this->getCropWindowPosY());
tree.put(PROP_CROP_LEFT_X, this->getCropX(0));
tree.put(PROP_CROP_LEFT_Y, this->getCropY(0));
tree.put(PROP_CROP_RIGHT_X, this->getCropX(1));
tree.put(PROP_CROP_RIGHT_Y, this->getCropY(1));
std::cout << "** CycloConfig> gravando area de interesse" << std::endl;
tree.put("interest.start_x", this->getInterestX(0));
tree.put("interest.start_y", this->getInterestY(0));
tree.put("interest.end_x", this->getInterestX(1));
tree.put("interest.end_y", this->getInterestY(1));
std::cout << "** CycloConfig> gravando perspectiva" << std::endl;
tree.put(PROP_PERSPECTIVE_SUPERIOR_LEFT_X, this->getX(0));
tree.put(PROP_PERSPECTIVE_SUPERIOR_LEFT_Y, this->getY(0));
tree.put(PROP_PERSPECTIVE_SUPERIOR_RIGHT_X, this->getX(1));
tree.put(PROP_PERSPECTIVE_SUPERIOR_RIGHT_Y, this->getY(1));
tree.put(PROP_PERSPECTIVE_INFERIOR_LEFT_X, this->getX(2));
tree.put(PROP_PERSPECTIVE_INFERIOR_LEFT_Y, this->getY(2));
tree.put(PROP_PERSPECTIVE_INFERIOR_RIGHT_X, this->getX(3));
tree.put(PROP_PERSPECTIVE_INFERIOR_RIGHT_Y, this->getY(3));
tree.put(PROP_COUNTER_DETECTEDOBJ_FILE_PATH, getFilePahtDetectedObject());
tree.put(PROP_COUNTER_DETECTEDOBJ_FILE_PREFIX,
getFilePrefixDetectedObject());
tree.put(PROP_COUNTER_DETECTEDOBJ_FILE_EXTENSION,
getFileExtensionDetectedObject());
tree.put(PROP_SHOW_SECOND_WINDOW, showZoom());
tree.put(PROP_SHOW_PERSPECTIVE, doPerspective());
std::cout << "** CycloConfig> gravando Logo Principal atual" << std::endl;
tree.put(PROP_LOGO_PRINCIPAL, this->getPathPrincipalLogo());
std::cout << "** CycloConfig> gravando Logo Secundário atual" << std::endl;
tree.put(PROP_LOGO_SECOND, this->getPathSecondLogo());
std::cout << "** CycloConfig> gravando endereço" << std::endl;
tree.put(PROP_ADDRESS, this->getAddress());
std::cout << "** CycloConfig> gravando tempo de espera por tecla"
<< std::endl;
tree.put(PROP_WAIT_KEY_DELAY, this->getWaitKeyDelay());
std::cout << "** CycloConfig> Finalizando Persistencia da configurações"
<< std::endl;
pt::write_json(fileName, tree);
std::cout << "** CycloConfig> Configurações Armazenada" << std::endl;
}
void CycloConfig::LoadData() {
std::cout << "** CycloConfig> Lendo configurações" << std::endl;
pt::ptree tree;
try {
try {
pt::read_json(this->configFileName, tree);
} catch (boost::property_tree::json_parser::json_parser_error& e) {
std::cerr << e.what() << std::endl;
std::cout << "**#############################" << std::endl;
std::cout
<< "** CycloConfig> Nenhum arquivo encontrado, criando um novo."
<< std::endl;
this->PersistData();
std::cout << "**#############################" << std::endl;
pt::read_json(this->configFileName, tree);
std::cout << "**#############################" << std::endl;
}
if (tree.empty()) {
std::cout << "**#############################" << std::endl;
std::cout << "** CycloConfig> Criando um novo arquivo, atual vazio"
<< std::endl;
this->PersistData();
std::cout << "**#############################" << std::endl;
pt::read_json(this->configFileName, tree);
std::cout << "**#############################" << std::endl;
}
std::cout
<< "** CycloConfig> gravando tamanho padrão da janela principal"
<< std::endl;
this->setFullFrameWidth(tree.get<unsigned int>(PROP_FULL_FRAME_WIDTH));
this->setFullFrameHeight(
tree.get<unsigned int>(PROP_FULL_FRAME_HEIGHT));
std::cout << "** CycloConfig> Lendo valor dos contadores" << std::endl;
this->SetLeftCounter(tree.get<int>(PROP_DETECTEDOBJ_COUNTER_1));
this->SetRightCounter(tree.get<int>(PROP_DETECTEDOBJ_COUNTER_2));
std::cout << "** CycloConfig> Lendo posição dos contadores"
<< std::endl;
this->setCounterX(0, tree.get<int>(PROP_DETECTEDOBJ_COUNTER_1_X));
this->setCounterY(0, tree.get<int>(PROP_DETECTEDOBJ_COUNTER_1_Y));
this->setCounterX(1, tree.get<int>(PROP_DETECTEDOBJ_COUNTER_2_X));
this->setCounterY(1, tree.get<int>(PROP_DETECTEDOBJ_COUNTER_2_Y));
setCounterDetectedObject(
tree.get<unsigned int>(PROP_COUNTER_DETECTEDOBJ_TOTAL));
setFilePahtDetectedObject(
tree.get<std::string>(PROP_COUNTER_DETECTEDOBJ_FILE_PATH));
setFilePrefixDetectedObject(
tree.get<std::string>(PROP_COUNTER_DETECTEDOBJ_FILE_PREFIX));
setFileExtensionDetectedObject(
tree.get<std::string>(PROP_COUNTER_DETECTEDOBJ_FILE_EXTENSION));
std::cout << "** CycloConfig> Lendo area de corte" << std::endl;
this->setCropWindowX(tree.get<unsigned int>(PROP_CROP_WINDOW_X));
this->setCropWindowY(tree.get<unsigned int>(PROP_CROP_WINDOW_Y));
this->setCropX(0, tree.get<int>(PROP_CROP_LEFT_X));
this->setCropY(0, tree.get<int>(PROP_CROP_LEFT_Y));
this->setCropX(1, tree.get<int>(PROP_CROP_RIGHT_X));
this->setCropY(1, tree.get<int>(PROP_CROP_RIGHT_Y));
std::cout << "** CycloConfig> Lendo area de interesse" << std::endl;
this->setInterestX(0, tree.get<int>("interest.start_x"));
this->setInterestY(0, tree.get<int>("interest.start_y"));
this->setInterestX(1, tree.get<int>("interest.end_x"));
this->setInterestY(1, tree.get<int>("interest.end_y"));
std::cout << "** CycloConfig> Lendo perspectiva" << std::endl;
this->setX(0, tree.get<int>(PROP_PERSPECTIVE_SUPERIOR_LEFT_X));
this->setY(0, tree.get<int>(PROP_PERSPECTIVE_SUPERIOR_LEFT_Y));
this->setX(1, tree.get<int>(PROP_PERSPECTIVE_SUPERIOR_RIGHT_X));
this->setY(1, tree.get<int>(PROP_PERSPECTIVE_SUPERIOR_RIGHT_Y));
this->setX(2, tree.get<int>(PROP_PERSPECTIVE_INFERIOR_LEFT_X));
this->setY(2, tree.get<int>(PROP_PERSPECTIVE_INFERIOR_LEFT_Y));
this->setX(3, tree.get<int>(PROP_PERSPECTIVE_INFERIOR_RIGHT_X));
this->setY(3, tree.get<int>(PROP_PERSPECTIVE_INFERIOR_RIGHT_Y));
std::cout << "** CycloConfig> Lendo Logo Principal atual" << std::endl;
this->setPathImageLogo(tree.get<std::string>(PROP_LOGO_PRINCIPAL));
std::cout << "** CycloConfig> Lendo Logo Secundário atual" << std::endl;
this->setPathImageCyclist(tree.get<std::string>(PROP_LOGO_SECOND));
std::cout << "** CycloConfig> Lendo endereço" << std::endl;
this->setAddress(tree.get<std::string>(PROP_ADDRESS));
std::cout << "** CycloConfig> lendo tempo de espera por tecla"
<< std::endl;
this->setWaitKeyDelay(tree.get<unsigned int>(PROP_WAIT_KEY_DELAY));
this->setShowZoom(tree.get<bool>(PROP_SHOW_SECOND_WINDOW));
this->setDoPerspective(tree.get<bool>(PROP_SHOW_PERSPECTIVE));
} catch (boost::property_tree::ptree_bad_path& e) {
std::cerr << e.what() << std::endl;
this->PersistData();
std::cout << "**#############################" << std::endl;
std::cout
<< "** CycloConfig> Verifique o arquivo de configuração e reinicie a aplicação."
<< std::endl;
exit(255);
} catch (boost::property_tree::json_parser::json_parser_error& e) {
std::cerr << e.what() << std::endl;
this->PersistData();
std::cout << "**#############################" << std::endl;
std::cout
<< "** CycloConfig> Verifique o arquivo de configuração e reinicie a aplicação."
<< std::endl;
exit(255);
}
}
void CycloConfig::SetCounterPos(unsigned int i, int x, int y) {
data.x_counter[i] = x;
data.y_counter[i] = y;
}
void CycloConfig::SetPerspectivePos(unsigned int i, int x, int y) {
data.x[i] = x;
data.y[i] = y;
}
void CycloConfig::SetCropPos(unsigned int i, int x, int y) {
data.x_crop[i] = x;
data.y_crop[i] = y;
}
void CycloConfig::SetInterestPos(unsigned int i, int x, int y) {
data.x_interest[i] = x;
data.y_interest[i] = y;
}
void CycloConfig::SetCounterPos(unsigned int i, cv::Point pt) {
SetCounterPos(i, pt.x, pt.y);
}
void CycloConfig::SetPerspectivePos(unsigned int i, cv::Point p) {
SetPerspectivePos(i, p.x, p.y);
}
void CycloConfig::SetCropPos(unsigned int i, cv::Point pt) {
SetCropPos(i, pt.x, pt.y);
}
void CycloConfig::SetInterestPos(unsigned int i, cv::Point pt) {
SetInterestPos(i, pt.x, pt.y);
}
void CycloConfig::setX(unsigned int i, int x) {
data.x[i] = x;
}
void CycloConfig::setY(unsigned int i, int y) {
data.y[i] = y;
}
void CycloConfig::setCropWindowX(unsigned int x) {
data.x_crop_windowPos = x;
}
void CycloConfig::setCropWindowY(unsigned int y) {
data.y_crop_windowPos = y;
}
void CycloConfig::setCropX(unsigned int i, int x) {
data.x_crop[i] = x;
}
void CycloConfig::setCropY(unsigned int i, int y) {
data.y_crop[i] = y;
}
unsigned int CycloConfig::getX(int i) {
return data.x[i];
}
unsigned int CycloConfig::getCounterX(int i) {
return data.x_counter[i];
}
void CycloConfig::setCounterX(int i, int x) {
data.x_counter[i] = x;
}
void CycloConfig::setCounterY(int i, int y) {
data.y_counter[i] = y;
}
unsigned int CycloConfig::getCropX(int i) {
return data.x_crop[i];
}
unsigned int CycloConfig::getInterestX(int i) {
return data.x_interest[i];
}
void CycloConfig::setInterestX(int i, int x) {
data.x_interest[i] = x;
}
unsigned int CycloConfig::getY(int i) {
return data.y[i];
}
unsigned int CycloConfig::getCounterY(int i) {
return data.y_counter[i];
}
cv::Rect CycloConfig::getCropArea() {
cv::Rect cropArea(getCropX(0), getCropY(0), getCropWidth(),
getCropHeight());
return cropArea;
}
unsigned int CycloConfig::getCropY(int i) {
return data.y_crop[i];
}
unsigned int CycloConfig::getInterestY(int i) {
return data.y_interest[i];
}
void CycloConfig::setInterestY(int i, int y) {
data.y_interest[i] = y;
}
unsigned int CycloConfig::getInterestWidth() {
return getInterestX(1) - getInterestX(0);
}
unsigned int CycloConfig::getInterestHeight() {
return getInterestY(1) - getInterestY(0);
}
unsigned int CycloConfig::getCropWidth() {
return getCropX(1) - getCropX(0);
}
unsigned int CycloConfig::getCropHeight() {
return getCropY(1) - getCropY(0);
}
std::string CycloConfig::getAddress() {
return data.address;
}
void CycloConfig::setAddress(std::string a) {
if (a.empty()) {
std::cout << "You must specify your address. " << "faria_lima-1280."
<< std::endl;
exit(EXIT_FAILURE);
}
this->data.address = a;
}
unsigned int CycloConfig::getCropWindowPosX() {
return data.x_crop_windowPos;
}
unsigned int CycloConfig::getCropWindowPosY() {
return data.y_crop_windowPos;
}
unsigned int CycloConfig::getCropWindowMaxWidth() {
return data.cropMaxWindowWidth;
}
unsigned int CycloConfig::getCropWindowMaxHeight() {
return data.cropMaxWindowWeight;
}
cv::Rect CycloConfig::getInterestArea() {
cv::Rect interestArea(getInterestX(0), getInterestY(0), getInterestWidth(),
getInterestHeight());
return interestArea;
}
bool CycloConfig::reconfigure() {
return reconfigureFlag;
}
void CycloConfig::setReconfigure(bool b) {
reconfigureFlag = b;
}
void CycloConfig::setSaveConfig(bool b) {
saveConfigFlag = b;
}
bool CycloConfig::getSaveConfig() {
return saveConfigFlag;
}
unsigned long CycloConfig::countInteraction() {
// contabilizar tempo entre interações também.
return ++interaction;
}
void CycloConfig::parseCommandOptions(int argc, char * const *argv) {
struct option long_options[] = //..
{ //..
{ "reg_source", required_argument, nullptr, 's' }, //..
{ "override", required_argument, &reconfigureFlag, 1 }, //..
{ "dev_source", required_argument, nullptr, 'D' }, //..
{ "record", required_argument, nullptr, 'r' }, //..
{ "stream", required_argument, nullptr, 'S' }, //..
{ "address", required_argument, nullptr, 'a' }, //..
{ "sensor", required_argument, nullptr, 't' }, //..
{ "waitKeydelay", required_argument, nullptr, 'w' }, //..
{ "saveconfig", required_argument, &saveConfigFlag, 1 },//..
{ "showzoom", no_argument, &showZoomFlag, 1 }, //.
{ "perspective", no_argument, &doPerspectiveFlag, 1 }, //..
{ "help", no_argument, nullptr, 'h' }, //..
{ nullptr, 0, nullptr, 0 } //..
};
parseCommandOptions(argc, argv, long_options);
}
void CycloConfig::parseCommandOptions( //.
int argc, //.
char * const *argv, //.
option *options //.
) {
int opt = 0;
int long_index = 0;
while ((opt = getopt_long(argc, argv, "s:r:S:t:a:D:c:w:h", options,
&long_index)) != -1) {
switch (opt) {
case 0:
continue;
case 's':
if (source == DEV_FILE) {
std::cout
<< "You can only have one source. It has to be either a "
<< "regular file or a device file, not both."
<< std::endl;
exit(EXIT_FAILURE);
}
source_file = optarg;
source = REG_FILE;
if (!test_file(source_file)) {
std::cout << "Verifique o arquivo de saida se é acessível."
<< std::endl;
exit(EXIT_FAILURE);
}
break;
case 'D':
if (source == REG_FILE) {
std::cout
<< "You can only have one source. It has to be either a "
<< "regular file or a device file, not both."
<< std::endl;
exit(EXIT_FAILURE);
}
source_device = atoi(optarg);
source = DEV_FILE;
break;
case 'r':
record_file = optarg;
if (!test_file(source_file)) {
std::cout << "Verifique o arquivo está acessivel." << std::endl;
exit(EXIT_FAILURE);
}
break;
case 'S':
stream_device = optarg;
if (!test_file(sensor_device)) {
std::cout << "Verifique o dispositivo de stream." << std::endl;
exit(EXIT_FAILURE);
}
break;
case 'a':
data.address = optarg;
break;
case 't':
sensor_device = optarg;
if (!test_file(sensor_device)) {
std::cout << "Verifique o dispositivo sensor." << std::endl;
exit(EXIT_FAILURE);
}
break;
case 'w':
this->setWaitKeyDelay(atoi(optarg));
break;
case 'h':
print_usage((std::string) argv[0]);
exit(EXIT_SUCCESS);
break;
default:
exit(EXIT_FAILURE);
break;
}
}
if (source == UNK_FILE) {
std::cout << "You must specify where data comes from. Use either:"
<< std::endl << "\t--reg_source <source_file>." << std::endl
<< "\t--dev_source <device number>." << std::endl
<< "\tOr --help to print usage." << std::endl;
exit(EXIT_FAILURE);
}
if (((source & REG_FILE) && (source_file == record_file))
|| ((source & REG_FILE) && (source_file == stream_device))
|| ((record_file == stream_device) && !record_file.empty())) {
std::cout << "Files must not be the same." << std::endl;
exit(EXIT_FAILURE);
} //Check all files forced.
}
std::string CycloConfig::getOutputDeviceName() {
return this->stream_device;
}
bool CycloConfig::hasOutputDeviceName() {
return !this->stream_device.empty();
}
std::string CycloConfig::getSensorDeviceName() {
return this->sensor_device;
}
std::string CycloConfig::getSourceFileName() {
return this->source_file;
}
int CycloConfig::getSourceDeviceId() {
return this->source_device;
}
std::string CycloConfig::getRecordFileName() {
return this->record_file;
}
bool CycloConfig::hasRecordFileName() {
return !this->record_file.empty();
}
bool CycloConfig::hasSensorDeviceName() {
return !this->sensor_device.empty();
}
FileType CycloConfig::getSourceType() {
return this->source;
}
unsigned int CycloConfig::getCounterDetectedObject() {
return this->data.counterDetectedObject;
}
void CycloConfig::setCounterDetectedObject(unsigned int i) {
this->data.counterDetectedObject = i;
}
void CycloConfig::setFilePahtDetectedObject(std::string d) {
this->data.filePahtDetectedObject = d;
}
void CycloConfig::setFilePrefixDetectedObject(std::string d) {
this->data.filePrefixDetectedObject = d;
}
void CycloConfig::setFileExtensionDetectedObject(std::string d) {
this->data.fileExtensionDetectedObject = d;
}
std::string CycloConfig::getFilePahtDetectedObject() {
return this->data.filePahtDetectedObject;
}
std::string CycloConfig::getFilePrefixDetectedObject() {
return this->data.filePrefixDetectedObject;
}
std::string CycloConfig::getFileExtensionDetectedObject() {
return this->data.fileExtensionDetectedObject;
}
unsigned int CycloConfig::getDistanceThreshold() {
return this->data.distanceThreshold;
}
double CycloConfig::getContourThreshold() {
return this->data.contourThreshold;
}
void CycloConfig::setDistanceThreshold(unsigned int d) {
this->data.distanceThreshold = d;
}
void CycloConfig::setContourThreshold(double c) {
this->data.contourThreshold = c;
}
unsigned int CycloConfig::getCaptureFrameDelay() {
return this->data.captureFrameDelay;
}
void CycloConfig::setCaptureFrameDelay(unsigned int c) {
this->data.captureFrameDelay = c;
}
unsigned int CycloConfig::getWaitKeyDelay() {
return this->data.waitKeyDelay;
}
void CycloConfig::setWaitKeyDelay(unsigned int d) {
this->data.waitKeyDelay = d;
}
std::string CycloConfig::getFullWindowName() {
return this->data.fullWindowName;
}
void CycloConfig::setFullWindowName(std::string n) {
this->data.fullWindowName = n;
}
void CycloConfig::setShowZoom(bool b = true) {
this->showZoomFlag = b;
}
bool CycloConfig::showZoom() {
return this->showZoomFlag;
}
bool CycloConfig::doPerspective() {
return this->doPerspectiveFlag;
}
void CycloConfig::setDoPerspective(bool b = true) {
this->doPerspectiveFlag = b;
}
/*
* CycloConfig.h
*
* Created on: 5 de out de 2016
* Author: consultoria@carlosdelfino.eti.br
*/
#ifndef CYCLOCONFIG_HPP_
#define CYCLOCONFIG_HPP_
#include<opencv2/opencv.hpp>
#include <string>
#include <exception>
#include <iostream>
#include <getopt.h>
#define CROP_MAX_WINDOW_PERCENT (410/640)
#define DEFAULT_CAPTURE_FRAME_DELAY 30
#define MAX_CAPTURE_FRAME_DELAY (INT_MAX-1)
#define PROP_FULL_FRAME_WIDTH "frame.width"
#define PROP_FULL_FRAME_HEIGHT "frame.height"
#define PROP_DETECTEDOBJ_COUNTER_1 "counter.track.1.counter"
#define PROP_DETECTEDOBJ_COUNTER_2 "counter.track.2.counter"
#define PROP_DETECTEDOBJ_COUNTER_1_X "counter.track.1.x"
#define PROP_DETECTEDOBJ_COUNTER_2_X "counter.track.2.x"
#define PROP_DETECTEDOBJ_COUNTER_1_Y "counter.track.1.y"
#define PROP_DETECTEDOBJ_COUNTER_2_Y "counter.track.2.y"
#define PROP_COUNTER_DETECTEDOBJ_TOTAL "counter.track.total"
#define PROP_COUNTER_DETECTEDOBJ_FILE_PATH "counter.file.basepath"
#define PROP_COUNTER_DETECTEDOBJ_FILE_PREFIX "counter.file.prefix"
#define PROP_COUNTER_DETECTEDOBJ_FILE_EXTENSION "counter.file.extension"
#define PROP_CROP_WINDOW_X "crop.window.dist_x"
#define PROP_CROP_WINDOW_Y "crop.window.dist_y"
#define PROP_CROP_LEFT_X "crop.left_x"
#define PROP_CROP_LEFT_Y "crop.left_y"
#define PROP_CROP_RIGHT_X "crop.right_x"
#define PROP_CROP_RIGHT_Y "crop.right_y"
#define PROP_PERSPECTIVE_SUPERIOR_LEFT_X "perspective.superior.left_x"
#define PROP_PERSPECTIVE_SUPERIOR_LEFT_Y "perspective.superior.left_y"
#define PROP_PERSPECTIVE_SUPERIOR_RIGHT_X "perspective.superior.right_x"
#define PROP_PERSPECTIVE_SUPERIOR_RIGHT_Y "perspective.superior.right_y"
#define PROP_PERSPECTIVE_INFERIOR_LEFT_X "perspective.inferior.left_x"
#define PROP_PERSPECTIVE_INFERIOR_LEFT_Y "perspective.inferior.left_y"
#define PROP_PERSPECTIVE_INFERIOR_RIGHT_X "perspective.inferior.right_x"
#define PROP_PERSPECTIVE_INFERIOR_RIGHT_Y "perspective.inferior.right_y"
#define PROP_LOGO_PRINCIPAL "images.logos.principal"
#define PROP_LOGO_SECOND "images.logos.second"
#define PROP_ADDRESS "address"
#define PROP_WAIT_KEY_DELAY "window.wait_key_delay"
#define PROP_SHOW_SECOND_WINDOW "window.show_second"
#define PROP_SHOW_PERSPECTIVE "window.show_perspective"
#define PROP_FRAMES_PS "frames_persecond"
#define POINT_LEFT_SUPERIOR_COORNER_X 0
#define POINT_LEFT_SUPERIOR_COORNER_Y 0
#define POINT_RIGHT_SUPERIOR_COORNER_X 640
#define POINT_RIGHT_SUPERIOR_COORNER_Y 0
#define POINT_LEFT_INFERIOR_COORNER_X 0
#define POINT_LEFT_INFERIOR_COORNER_Y 480
#define POINT_RIGHT_INFERIOR_COORNER_X 640
#define POINT_RIGHT_INFERIOR_COORNER_Y 480
typedef struct {
unsigned int width = 640;
unsigned int height = 480;
unsigned int left_counter = 0;
unsigned int right_counter = 0;
unsigned int x_counter[2] = { 50, 50 };
unsigned int y_counter[2] = { 150, 200 };
unsigned int x[4] = { 0, 640, 0, 640 };
unsigned int y[4] = { 0, 0, 480, 480 };
unsigned int x_crop_windowPos = 10;
unsigned int y_crop_windowPos = 60;
unsigned int cropMaxWindowWidth = 350; //640 * CROP_MAX_WINDOW_PERCENT;
unsigned int cropMaxWindowWeight = 300; //480 * CROP_MAX_WINDOW_PERCENT;
unsigned int x_crop[2] = { 0, 640 };
unsigned int y_crop[2] = { 0, 480 };
unsigned int x_interest[2] = { 0, 640 };
unsigned int y_interest[2] = { 0, 480 };
std::string pathImageLogo = "images/logo.jpg";
std::string pathImageCyclist = "images/cyclist.jpg";
std::string address = "Av. Carapinina, 1000";
unsigned int counterDetectedObject = 0;
std::string filePahtDetectedObject = "./images/";
std::string filePrefixDetectedObject = "Bike-";
std::string fileExtensionDetectedObject = "jpg";
unsigned int distanceThreshold = 30;
double contourThreshold = 50;
unsigned int captureFrameDelay = std::min( //.
DEFAULT_CAPTURE_FRAME_DELAY,//.
MAX_CAPTURE_FRAME_DELAY //.
);
unsigned int waitKeyDelay = 30;
std::string fullWindowName = "Janela Principal";
} ConfigData;
enum FileType
: uint8_t {
DEV_FILE = 0x02, REG_FILE = 0x01, UNK_FILE = 0x00
};
class CycloConfig {
private:
std::string source_file = ""; //where data comes from if source is a
//regular file.
int source_device = -1; //where data comes from if source is
//device file.
std::string record_file = ""; //output file.
std::string stream_device = ""; //device file used to stream data.
std::string sensor_device = ""; //sensor device file
FileType source = UNK_FILE;
std::string configFileName = "CycloTracker.conf";
ConfigData data;
unsigned long interaction = 0;
int saveConfigFlag = 0;
int reconfigureFlag = 0;
int showZoomFlag;
int doPerspectiveFlag = false;
CycloConfig();
public:
/* @brief Desabilita Construtor de Cópia gerado pelo compilador
*
*/
CycloConfig(CycloConfig &) = delete;
/* @brief Desabilita Operador de Atribuição gerado pelo compilador
*
*/
CycloConfig &operator=(CycloConfig &) = delete;
virtual ~CycloConfig();
void PersistData(std::string fileName);
void PersistData();
void LoadData();
void parseCommandOptions(int argc, char * const *argv);
void parseCommandOptions(int argc, char * const *argv, option *options);
std::string getOutputDeviceName();
std::string getSensorDeviceName();
std::string getSourceFileName();
std::string getRecordFileName();
int getSourceDeviceId();
bool hasSensorDeviceName();
bool hasOutputDeviceName();
bool hasRecordFileName();
FileType getSourceType();
/** @brief cria uma única instância pra uso em todo o programa (Singelton)
*
* Uma única instância da classe CycloConfig deve existir para toda a
* aplicação assim evita-se bugs e redundância de controle das
* configurações mantendo o sistema padronizado na forma de
* armazena-la.
*
* Para utilizar a classe siga o código abaixo.
* Observe que é obrigatório receber o objeto como ponteiro evitando
* sua duplicação. A tentativa de dúplica-lo irá acarretar erro na compilação.
* @code
* CloseConfig *config = CycloConfig::get();
* @endcode
*/
static CycloConfig *get();
/** @brief Seta o tamanho do frame principal, e mantem referência para criação dos demais frames.
Esta função deve ser usada para armazenar a configuração do
frameprincipal, inicialmente obitido com a capitura do primeiro
frame de video no dispositivo de entrada.
Quando ativado a opção de reconfiguração durante a execução do
programa, esta função tambem dimenciona a area de corte e area de
interesse para que fiquem no mesmo tamanho do frame principal.
@code
@endcode
@see CycloConfig::setCropArea()
@see CycloConfig::SetInterestArea()
*/
void setFullFrameSize(cv::Size size);
void setFullFrameWidth(unsigned int w);
void setFullFrameHeight(unsigned int w);
unsigned int getFullFrameWidth();
unsigned int getFullFrameHeight();
cv::Size getFullFrameSize();
unsigned int getY(int i);
unsigned int getX(int i);
void setX(unsigned int i, int x);
void setY(unsigned int i, int y);
unsigned int getCounterX(int i);
unsigned int getCounterY(int i);
void setCounterX(int i, int x);
void setCounterY(int i, int y);
void clearInteresstArea();
cv::Point GetInterestPos(unsigned int index);
cv::Rect getInterestArea();
unsigned int getInterestX(int i);
unsigned int getInterestY(int i);
unsigned int getInterestWidth();
unsigned int getInterestHeight();
void setInterestX(int i, int x);
void setInterestY(int i, int y);
void setInterestArea(cv::Size size);
void SetInterestPos(unsigned int index, cv::Point pt);
void SetInterestPos(unsigned int index, int x, int y);
void clearCropArea();
cv::Rect getCropArea();
cv::Point GetCropPos(unsigned int index);
unsigned int getCropX(int i);
unsigned int getCropY(int i);
unsigned int getCropWidth();
unsigned int getCropHeight();
unsigned int getCropWindowPosX();
unsigned int getCropWindowPosY();
unsigned int getCropWindowMaxWidth();
unsigned int getCropWindowMaxHeight();
void setCropArea(cv::Size size);
void setCropX(unsigned int i, int x);
void setCropY(unsigned int i, int y);
void setCropWindowX(unsigned int x);
void setCropWindowY(unsigned int y);
void SetCropPos(unsigned int index, cv::Point pt);
void SetCropPos(unsigned int index, int x, int y);
unsigned int GetLeftCounter();
unsigned int GetRightCounter();
void SetLeftCounter(unsigned int counter);
void SetRightCounter(unsigned int counter);
cv::Point GetCounterPos(unsigned int index);
void SetCounterPos(unsigned int index, cv::Point pt);
void SetCounterPos(unsigned int index, int x, int y);
void clearPerspectiveArea();
cv::Point GetPerspectivePos(unsigned int index);
void SetPerspectivePos(unsigned int index, cv::Point pt);
void SetPerspectivePos(unsigned int index, int x, int y);
void setPerspectiveArea(cv::Size size);
std::string getAddress();
void setAddress(std::string a);
std::string getPathPrincipalLogo();
std::string getPathSecondLogo();
void setPathImageLogo(std::string);
void setPathImageCyclist(std::string);
unsigned int getCounterDetectedObject();
void setCounterDetectedObject(unsigned int);
void setFilePahtDetectedObject(std::string);
void setFilePrefixDetectedObject(std::string);
void setFileExtensionDetectedObject(std::string);
std::string getFilePahtDetectedObject();
std::string getFilePrefixDetectedObject();
std::string getFileExtensionDetectedObject();
unsigned int getDistanceThreshold();
double getContourThreshold();
void setDistanceThreshold(unsigned int);
void setContourThreshold(double);
void setReconfigure(bool b);
bool reconfigure();
void setSaveConfig(bool b);
bool getSaveConfig();
unsigned long countInteraction();
unsigned int getCaptureFrameDelay();
void setCaptureFrameDelay(unsigned int);
unsigned int getWaitKeyDelay();
void setWaitKeyDelay(unsigned int d);
std::string getFullWindowName();
void setFullWindowName(std::string n);
bool showZoom();
void setShowZoom(bool b);
void setDoPerspective(bool b);
bool doPerspective();
};
#endif /* CYCLOCONFIG_HPP_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.