28 #include <boost/program_options.hpp>
29 #include <boost/algorithm/string/predicate.hpp>
86 namespace po = boost::program_options;
87 namespace fs = boost::filesystem;
101 m_list.push_back(group);
110 m_list.push_back(source);
127 ::setenv(
"LC_ALL",
"C", 1);
141 std::make_shared<SourceWithOnDemandPropertiesFactory>(task_provider);
143 std::make_shared<SourceGroupWithOnDemandPropertiesFactory>(task_provider);
151 bool config_initialized =
false;
157 : plugin_manager { task_factory_registry, output_registry,
config_manager_id, plugin_path, plugin_list } {
164 if (!config_initialized) {
176 segmentation_factory.reportConfigDependencies(config_manager);
177 partition_factory.reportConfigDependencies(config_manager);
178 grouping_factory.reportConfigDependencies(config_manager);
179 deblending_factory.reportConfigDependencies(config_manager);
180 measurement_factory.reportConfigDependencies(config_manager);
181 output_factory.reportConfigDependencies(config_manager);
182 background_level_analyzer_factory.reportConfigDependencies(config_manager);
184 config_parameters.add(config_manager.closeRegistration());
185 config_initialized =
true;
187 return config_parameters;
192 auto options = getConfigParameters();
195 "List the possible output properties for the given input parameters and exit");
197 "Show the columns created for each property");
199 "Show the columns created for each property, for the given configuration");
201 "Dump parameters with default values into a configuration file");
202 progress_printer_factory.addOptions(options);
205 po::positional_options_description p;
206 p.add(
"python-arg", -1);
212 template <
typename T>
214 out << opt.long_name() <<
'=' << boost::any_cast<T>(default_value) <<
std::endl;
218 template <
typename T>
220 auto values = boost::any_cast<std::vector<T>>(default_value);
221 if (values.empty()) {
222 out << opt.long_name() <<
'=' <<
std::endl;
225 for (
const auto& v : values)
226 out << opt.long_name() <<
'=' << v <<
std::endl;
234 {
typeid(bool), &writeDefault<bool>},
235 {
typeid(int), &writeDefault<int>},
236 {
typeid(double), &writeDefault<double>},
240 decltype(printers)::const_iterator printer;
242 auto config_parameters = getConfigParameters();
243 for (
const auto& p : config_parameters.options()) {
244 boost::any default_value;
247 if (!p->semantic()->apply_default(default_value)) {
250 else if ((printer = printers.find(default_value.type())) == printers.end()) {
251 std::cout <<
'#' << p->long_name() <<
"=<Unknown type " << default_value.type().name() <<
'>' <<
std::endl;
254 printer->second(
std::cout, *p, default_value);
290 if (args.
find(
"config-file") != args.
end()) {
291 auto cfg_file = args.
at(
"config-file").as<fs::path>();
292 if (cfg_file !=
"" && !fs::exists(cfg_file)) {
293 throw Elements::Exception() <<
"The configuration file '" << cfg_file <<
"' does not exist";
298 progress_printer_factory.configure(args);
299 auto progress_mediator = progress_printer_factory.createProgressMediator();
303 config_manager.initialize(args);
306 auto memory_config = config_manager.getConfiguration<
MemoryConfig>();
308 memory_config.getTileSize(), memory_config.getTileMaxMemory());
312 task_factory_registry->
configure(config_manager);
315 segmentation_factory.configure(config_manager);
316 partition_factory.configure(config_manager);
317 grouping_factory.configure(config_manager);
318 deblending_factory.configure(config_manager);
319 measurement_factory.configure(config_manager);
320 output_factory.configure(config_manager);
321 background_level_analyzer_factory.configure(config_manager);
328 auto detection_image = config_manager.getConfiguration<
DetectionImageConfig>().getDetectionImage();
329 auto detection_image_path = config_manager.getConfiguration<
DetectionImageConfig>().getDetectionImagePath();
330 auto weight_image = config_manager.getConfiguration<
WeightImageConfig>().getWeightImage();
331 bool is_weight_absolute = config_manager.getConfiguration<
WeightImageConfig>().isWeightAbsolute();
332 auto weight_threshold = config_manager.getConfiguration<
WeightImageConfig>().getWeightThreshold();
334 auto detection_image_coordinate_system = config_manager.getConfiguration<
DetectionImageConfig>().getCoordinateSystem();
336 auto detection_image_saturation = config_manager.getConfiguration<
DetectionImageConfig>().getSaturation();
338 auto segmentation = segmentation_factory.createSegmentation();
339 auto partition = partition_factory.getPartition();
340 auto source_grouping = grouping_factory.createGrouping();
346 auto sorter = std::make_shared<Sorter>();
350 segmentation->Observable<ProcessSourcesEvent>::addObserver(source_grouping);
352 source_grouping->addObserver(deblending);
355 sorter->addObserver(output);
357 segmentation->Observable<SegmentationProgress>::addObserver(progress_mediator->getSegmentationObserver());
359 deblending->
addObserver(progress_mediator->getDeblendingObserver());
360 measurement->
addObserver(progress_mediator->getMeasurementObserver());
380 auto interpolation_gap = config_manager.getConfiguration<
DetectionImageConfig>().getInterpolationGap();
381 auto detection_frame = std::make_shared<DetectionImageFrame>(detection_image, weight_image,
382 weight_threshold, detection_image_coordinate_system, detection_image_gain,
383 detection_image_saturation, interpolation_gap);
384 detection_frame->setLabel(boost::filesystem::basename(detection_image_path));
386 auto background_analyzer = background_level_analyzer_factory.createBackgroundAnalyzer();
387 auto background_model = background_analyzer->analyzeBackground(detection_frame->getOriginalImage(), weight_image,
394 detection_frame->setBackgroundLevel(background_model.getLevelMap());
396 if (weight_image !=
nullptr) {
397 if (is_weight_absolute) {
398 detection_frame->setVarianceMap(weight_image);
401 detection_frame->setVarianceMap(scaled_image);
404 detection_frame->setVarianceMap(background_model.getVarianceMap());
409 const auto& background_config = config_manager.getConfiguration<
BackgroundConfig>();
412 if (background_config.isBackgroundLevelAbsolute()) {
414 detection_image->getWidth(), detection_image->getHeight(), background_config.getBackgroundLevel());
416 detection_frame->setBackgroundLevel(background);
420 if (background_config.isDetectionThresholdAbsolute()) {
421 detection_frame->setDetectionThreshold(background_config.getDetectionThreshold());
432 segmentation->processFrame(detection_frame);
435 logger.
error() <<
"Failed to process the frame! " <<
e.what();
449 size_t n_writen_rows = output->
flush();
451 progress_mediator->done();
453 if (n_writen_rows > 0) {
454 logger.
info() << n_writen_rows <<
" sources detected";
468 m_plugin_path(plugin_path), m_plugin_list(plugin_list) {
474 auto& config_manager = ConfigManager::getInstance(conf_man_id);
476 auto options = config_manager.closeRegistration();
478 options.add_options()(
"*", po::value<std::vector<std::string>>());
483 auto& config_manager = ConfigManager::getInstance(conf_man_id);
484 config_manager.initialize(args);
486 m_plugin_path =
conf.getPluginPath();
487 m_plugin_list =
conf.getPluginList();
507 if (local_env[
"ELEMENTS_CONF_PATH"].empty()) {
508 local_env[
"ELEMENTS_CONF_PATH"] =
".:/etc";
510 local_env[
"ELEMENTS_CONF_PATH"] =
".:" + local_env[
"ELEMENTS_CONF_PATH"] +
":/etc";
527 plugin_options_input.emplace_back(
"--log-level");
528 plugin_options_input.emplace_back(
"ERROR");
529 for (
int i = 0; i < argc; ++i) {
531 if (option ==
"--config-file") {
532 plugin_options_input.emplace_back(
"--config-file");
533 plugin_options_input.emplace_back(
std::string{argv[i + 1]});
535 if (boost::starts_with(option,
"--config-file=")) {
536 plugin_options_input.emplace_back(option);
538 if (option ==
"--plugin-directory") {
539 plugin_options_input.emplace_back(
"--plugin-directory");
540 plugin_options_input.emplace_back(
std::string{argv[i + 1]});
542 if (boost::starts_with(option,
"--plugin-directory=")) {
543 plugin_options_input.emplace_back(option);
545 if (option ==
"--plugin") {
546 plugin_options_input.emplace_back(
"--plugin");
547 plugin_options_input.emplace_back(
std::string{argv[i + 1]});
549 if (boost::starts_with(option,
"--plugin=")) {
550 plugin_options_input.emplace_back(option);
554 int argc_tmp = plugin_options_input.
size();
556 for (
unsigned int i = 0; i < plugin_options_input.size(); ++i) {
557 auto& option_str = plugin_options_input[i];
558 argv_tmp[i] = option_str.
data();
562 plugin_options_program.run(argc_tmp,
const_cast<char **
>(argv_tmp.
data()));