From e11b68d680bd231b1abc4badf5fbf79fc5ed0ccb Mon Sep 17 00:00:00 2001 From: Charles Otto Date: Fri, 8 Nov 2013 12:02:23 -0500 Subject: [PATCH] Add support for a simple append flag for enroll --- openbr/core/core.cpp | 28 ++++++++++++++++++++++++---- openbr/openbr_plugin.cpp | 11 +++++++++++ openbr/openbr_plugin.h | 1 + openbr/plugins/gallery.cpp | 37 ++++++++++++++++++++++++++++++++++++- openbr/plugins/misc.cpp | 2 -- 5 files changed, 72 insertions(+), 7 deletions(-) diff --git a/openbr/core/core.cpp b/openbr/core/core.cpp index 372723b..0d20c2c 100644 --- a/openbr/core/core.cpp +++ b/openbr/core/core.cpp @@ -121,6 +121,8 @@ struct AlgorithmCore FileList enroll(File input, File gallery = File()) { + FileList files; + qDebug("Enrolling %s%s", qPrintable(input.flat()), gallery.isNull() ? "" : qPrintable(" to " + gallery.flat())); @@ -128,12 +130,29 @@ struct AlgorithmCore if (input.name.isEmpty()) return FileList(); else gallery = getMemoryGallery(input); } + TemplateList data(TemplateList::fromGallery(input)); + + if (gallery.contains("append")) + { + // Remove any templates which are already in the gallery + QScopedPointer g(Gallery::make(gallery)); + files = g->files(); + QStringList names = files.names(); + for (int i = data.size() - 1; i>=0; i--) { + if (names.contains(data[i].file.name)) + { + data.removeAt(i); + } + } + } + + if (data.empty()) + return files; - TemplateList i(TemplateList::fromGallery(input)); // Trust me, this makes complete sense. // We're just going to make a pipe with a placeholder first transform - QString pipeDesc = "Identity+ProgressCounter("+QString::number(i.length())+")+GalleryOutput("+gallery.flat()+")+Discard"; + QString pipeDesc = "Identity+ProgressCounter("+QString::number(data.length())+")+GalleryOutput("+gallery.flat()+")+Discard"; QScopedPointer basePipe(Transform::make(pipeDesc,NULL)); CompositeTransform * downcast = dynamic_cast(basePipe.data()); @@ -157,9 +176,10 @@ struct AlgorithmCore // and get the final stream's stages by reinterpreting the pipe. Perfectly straightforward. wrapper->init(); - wrapper->projectUpdate(i,i); + wrapper->projectUpdate(data,data); - return i.files(); + files.append(data.files()); + return files; } void enroll(TemplateList &data) diff --git a/openbr/openbr_plugin.cpp b/openbr/openbr_plugin.cpp index 990251e..9322f5d 100644 --- a/openbr/openbr_plugin.cpp +++ b/openbr/openbr_plugin.cpp @@ -1153,6 +1153,17 @@ Gallery *Gallery::make(const File &file) return gallery; } +// Default init -- if the file contains "append", read the existing +// data and immediately write it +void Gallery::init() +{ + if (file.exists() && file.contains("append")) + { + TemplateList data = this->read(); + this->writeBlock(data); + } +} + /* Transform - public methods */ Transform::Transform(bool _independent, bool _trainable) { diff --git a/openbr/openbr_plugin.h b/openbr/openbr_plugin.h index 6472337..d96c71e 100644 --- a/openbr/openbr_plugin.h +++ b/openbr/openbr_plugin.h @@ -1060,6 +1060,7 @@ public: void writeBlock(const TemplateList &templates); /*!< \brief Serialize a template list. */ virtual void write(const Template &t) = 0; /*!< \brief Serialize a template. */ static Gallery *make(const File &file); /*!< \brief Make a gallery to/from a file on disk. */ + void init(); private: QSharedPointer next; diff --git a/openbr/plugins/gallery.cpp b/openbr/plugins/gallery.cpp index 1549bcd..4cc2221 100644 --- a/openbr/plugins/gallery.cpp +++ b/openbr/plugins/gallery.cpp @@ -73,6 +73,11 @@ class arffGallery : public Gallery arffFile.write(qPrintable(OpenCVUtils::matrixToStringList(t).join(','))); arffFile.write(qPrintable(",'" + t.file.get("Label") + "'\n")); } + + void init() + { + // + } }; BR_REGISTER(Gallery, arffGallery) @@ -94,7 +99,12 @@ class galGallery : public Gallery if (file.get("remove", false)) gallery.remove(); QtUtils::touchDir(gallery); - if (!gallery.open(QFile::ReadWrite | QFile::Append)) + QFile::OpenMode mode = QFile::ReadWrite; + + if (file.contains("append")) + mode |= QFile::Append; + + if (!gallery.open(mode)) qFatal("Can't open gallery: %s", qPrintable(gallery.fileName())); stream.setDevice(&gallery); } @@ -579,6 +589,11 @@ class templateGallery : public Gallery (void) t; qFatal("No supported."); } + + void init() + { + // + } }; BR_REGISTER(Gallery, templateGallery) @@ -737,6 +752,11 @@ class dbGallery : public Gallery (void) t; qFatal("Not supported."); } + + void init() + { + // + } }; BR_REGISTER(Gallery, dbGallery) @@ -790,6 +810,11 @@ class googleGallery : public Gallery (void) t; qFatal("Not supported."); } + + void init() + { + // + } }; BR_REGISTER(Gallery, googleGallery) @@ -883,6 +908,11 @@ class FDDBGallery : public Gallery (void) t; qFatal("Not implemented."); } + + void init() + { + // + } }; BR_REGISTER(Gallery, FDDBGallery) @@ -927,6 +957,11 @@ class landmarksGallery : public Gallery (void) t; qFatal("Not implemented."); } + + void init() + { + // + } }; BR_REGISTER(Gallery, landmarksGallery) diff --git a/openbr/plugins/misc.cpp b/openbr/plugins/misc.cpp index 77579d9..a47baa4 100644 --- a/openbr/plugins/misc.cpp +++ b/openbr/plugins/misc.cpp @@ -520,8 +520,6 @@ class EventTransform : public UntrainableMetaTransform BR_REGISTER(Transform, EventTransform) -//ProgressCounter+Output - class GalleryOutputTransform : public TimeVaryingTransform { Q_OBJECT -- libgit2 0.21.4