From 95a0108bd8da3cefe8ea976e3febfdd2d1b5db1c Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Mon, 21 Mar 2016 16:51:55 -0400 Subject: [PATCH] Using a default alprd.conf file with an override in /etc/openalpr/alprd.conf --- config/{alprd.conf => alprd.conf.defaults} | 12 +++-- config/alprd.conf.user | 4 ++ src/CMakeLists.txt | 14 ++++- src/daemon.cpp | 58 ++++++--------------- src/daemon/daemonconfig.cpp | 60 ++++++++++++++++++++++ src/daemon/daemonconfig.h | 31 +++++++++++ 6 files changed, 130 insertions(+), 49 deletions(-) rename config/{alprd.conf => alprd.conf.defaults} (68%) create mode 100644 config/alprd.conf.user create mode 100644 src/daemon/daemonconfig.cpp create mode 100644 src/daemon/daemonconfig.h diff --git a/config/alprd.conf b/config/alprd.conf.defaults similarity index 68% rename from config/alprd.conf rename to config/alprd.conf.defaults index 493b992..0e62337 100644 --- a/config/alprd.conf +++ b/config/alprd.conf.defaults @@ -1,17 +1,19 @@ [daemon] -; country determines the training dataset used for recognizing plates. Valid values are: us, eu +; country determines the training dataset used for recognizing plates. +; Valid values are: us, eu, au, auwide, gb, kr, mx, sg country = us ; text name identifier for this location -site_id = your-unique-sitename +; site_id = your-unique-sitename ; Declare each stream on a separate line ; each unique stream should be defined as stream = [url] -stream = http://127.0.0.1/example_video_stream.mjpeg -;stream = http://127.0.0.1/example_second_stream.mjpeg -;stream = webcam +; Example stream config: +; stream = http://127.0.0.1/example_video_stream.mjpeg +; stream = http://127.0.0.1/example_second_stream.mjpeg +; stream = webcam ; topn is the number of possible plate character variations to report topn = 10 diff --git a/config/alprd.conf.user b/config/alprd.conf.user new file mode 100644 index 0000000..4c51ad3 --- /dev/null +++ b/config/alprd.conf.user @@ -0,0 +1,4 @@ +; This configuration file overrides the default values specified +; in ${CMAKE_INSTALL_PREFIX}/share/openalpr/config/alprd.defaults.conf +[daemon] + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4d11a61..53178f3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -156,7 +156,12 @@ ENDIF() # Compile the alprd library on Unix-based OS IF (WITH_DAEMON) - ADD_EXECUTABLE( alprd daemon.cpp daemon/beanstalk.c daemon/beanstalk.cc ) + ADD_EXECUTABLE( alprd + daemon.cpp + daemon/daemonconfig.cpp + daemon/beanstalk.c + daemon/beanstalk.cc +) FIND_PACKAGE( log4cplus REQUIRED ) @@ -212,8 +217,13 @@ CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/../config/openalpr.conf.defaults ${CMAKE_CURR install (FILES ${CMAKE_CURRENT_BINARY_DIR}/share/openalpr/config/openalpr.defaults.conf DESTINATION ${CMAKE_INSTALL_PREFIX}/share/openalpr/config COMPONENT config) IF (WITH_DAEMON) + CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/../config/alprd.conf.user ${CMAKE_CURRENT_BINARY_DIR}/config/alprd.conf) + install (FILES ${CMAKE_CURRENT_BINARY_DIR}/config/alprd.conf DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/openalpr/ COMPONENT config) + + CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/../config/alprd.conf.defaults ${CMAKE_CURRENT_BINARY_DIR}/share/openalpr/config/alprd.defaults.conf) + install (FILES ${CMAKE_CURRENT_BINARY_DIR}/share/openalpr/config/alprd.defaults.conf DESTINATION ${CMAKE_INSTALL_PREFIX}/share/openalpr/config COMPONENT config) + install (TARGETS alprd DESTINATION ${CMAKE_INSTALL_PREFIX}/bin) - install (FILES ${CMAKE_SOURCE_DIR}/../config/alprd.conf DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/openalpr COMPONENT config) ENDIF() diff --git a/src/daemon.cpp b/src/daemon.cpp index f248b03..8446b56 100644 --- a/src/daemon.cpp +++ b/src/daemon.cpp @@ -6,10 +6,10 @@ #include "daemon/beanstalk.hpp" #include "video/logging_videobuffer.h" +#include "daemon/daemonconfig.h" #include "tclap/CmdLine.h" #include "alpr.h" -#include "openalpr/simpleini/simpleini.h" #include "openalpr/cjson.h" #include "support/tinythread.h" #include @@ -167,73 +167,47 @@ int main( int argc, const char** argv ) LOG4CPLUS_INFO(logger, "Running OpenALPR daemon in the foreground."); } - CSimpleIniA ini; - ini.SetMultiKey(); + LOG4CPLUS_INFO(logger, "Using: " << daemonConfigFile << " for daemon configuration"); - ini.LoadFile(daemonConfigFile.c_str()); - - std::vector stream_urls; - - - CSimpleIniA::TNamesDepend values; - ini.GetAllValues("daemon", "stream", values); + std::string daemon_defaults_file = INSTALL_PREFIX "/share/openalpr/config/alprd.defaults.conf"; + DaemonConfig daemon_config(daemonConfigFile, daemon_defaults_file); - // sort the values into the original load order - values.sort(CSimpleIniA::Entry::LoadOrder()); - - // output all of the items - CSimpleIniA::TNamesDepend::const_iterator i; - for (i = values.begin(); i != values.end(); ++i) { - stream_urls.push_back(i->pItem); - } - - if (stream_urls.size() == 0) + if (daemon_config.stream_urls.size() == 0) { LOG4CPLUS_FATAL(logger, "No video streams defined in the configuration."); return 1; } - std::string country = ini.GetValue("daemon", "country", "us"); - int topn = ini.GetLongValue("daemon", "topn", 20); - - bool storePlates = ini.GetBoolValue("daemon", "store_plates", false); - std::string imageFolder = ini.GetValue("daemon", "store_plates_location", "/tmp/"); - bool uploadData = ini.GetBoolValue("daemon", "upload_data", false); - std::string upload_url = ini.GetValue("daemon", "upload_address", ""); - std::string company_id = ini.GetValue("daemon", "company_id", ""); - std::string site_id = ini.GetValue("daemon", "site_id", ""); - - LOG4CPLUS_INFO(logger, "Using: " << daemonConfigFile << " for daemon configuration"); - LOG4CPLUS_INFO(logger, "Using: " << imageFolder << " for storing valid plate images"); + LOG4CPLUS_INFO(logger, "Using: " << daemon_config.imageFolder << " for storing valid plate images"); pid_t pid; - for (int i = 0; i < stream_urls.size(); i++) + for (int i = 0; i < daemon_config.stream_urls.size(); i++) { pid = fork(); if (pid == (pid_t) 0) { // This is the child process, kick off the capture data and upload threads CaptureThreadData* tdata = new CaptureThreadData(); - tdata->stream_url = stream_urls[i]; + tdata->stream_url = daemon_config.stream_urls[i]; tdata->camera_id = i + 1; tdata->config_file = openAlprConfigFile; - tdata->output_images = storePlates; - tdata->output_image_folder = imageFolder; - tdata->country_code = country; - tdata->company_id = company_id; - tdata->site_id = site_id; - tdata->top_n = topn; + tdata->output_images = daemon_config.storePlates; + tdata->output_image_folder = daemon_config.imageFolder; + tdata->country_code = daemon_config.country; + tdata->company_id = daemon_config.company_id; + tdata->site_id = daemon_config.site_id; + tdata->top_n = daemon_config.topn; tdata->clock_on = clockOn; tthread::thread* thread_recognize = new tthread::thread(streamRecognitionThread, (void*) tdata); - if (uploadData) + if (daemon_config.uploadData) { // Kick off the data upload thread UploadThreadData* udata = new UploadThreadData(); - udata->upload_url = upload_url; + udata->upload_url = daemon_config.upload_url; tthread::thread* thread_upload = new tthread::thread(dataUploadThread, (void*) udata ); } diff --git a/src/daemon/daemonconfig.cpp b/src/daemon/daemonconfig.cpp new file mode 100644 index 0000000..f6aeb4d --- /dev/null +++ b/src/daemon/daemonconfig.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2016 OpenALPR Technology, Inc. + * Open source Automated License Plate Recognition [http://www.openalpr.com] + * + * This file is part of OpenALPR. + * + * OpenALPR is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License + * version 3 as published by the Free Software Foundation + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . +*/ + +#include "daemonconfig.h" +#include "config_helper.h" + +using namespace alpr; + +DaemonConfig::DaemonConfig(std::string config_file, std::string config_defaults_file) { + CSimpleIniA ini; + ini.SetMultiKey(); + ini.LoadFile(config_file.c_str()); + + CSimpleIniA defaultIni; + defaultIni.SetMultiKey(); + defaultIni.LoadFile(config_defaults_file.c_str()); + + // Stream will only be in the user override config, never in the defaults + CSimpleIniA::TNamesDepend values; + ini.GetAllValues("daemon", "stream", values); + + // sort the values into the original load order + values.sort(CSimpleIniA::Entry::LoadOrder()); + + // output all of the items + CSimpleIniA::TNamesDepend::const_iterator i; + for (i = values.begin(); i != values.end(); ++i) { + stream_urls.push_back(i->pItem); + } + + country = getString(&ini, &defaultIni, "daemon", "country", "us"); + topn = getInt(&ini, &defaultIni, "daemon", "topn", 20); + + storePlates = getBoolean(&ini, &defaultIni, "daemon", "store_plates", false); + imageFolder = getString(&ini, &defaultIni, "daemon", "store_plates_location", "/tmp/"); + uploadData = getBoolean(&ini, &defaultIni, "daemon", "upload_data", false); + upload_url = getString(&ini, &defaultIni, "daemon", "upload_address", ""); + company_id = getString(&ini, &defaultIni, "daemon", "company_id", ""); + site_id = getString(&ini, &defaultIni, "daemon", "site_id", ""); +} + +DaemonConfig::~DaemonConfig() { +} + diff --git a/src/daemon/daemonconfig.h b/src/daemon/daemonconfig.h new file mode 100644 index 0000000..766563e --- /dev/null +++ b/src/daemon/daemonconfig.h @@ -0,0 +1,31 @@ + +#ifndef OPENALPR_DAEMONCONFIG_H +#define OPENALPR_DAEMONCONFIG_H + +#include +#include +#include "simpleini/simpleini.h" + +class DaemonConfig { +public: + DaemonConfig(std::string config_file, std::string config_defaults_file); + virtual ~DaemonConfig(); + + std::vector stream_urls; + + std::string country; + + int topn; + bool storePlates; + std::string imageFolder; + bool uploadData; + std::string upload_url; + std::string company_id; + std::string site_id; + +private: + +}; + +#endif /* OPENALPR_DAEMONCONFIG_H */ +