From ade5dd6729221f41e8b38370f858f3eea5894b00 Mon Sep 17 00:00:00 2001 From: matlabbe Date: Thu, 30 Aug 2018 12:43:16 -0400 Subject: [PATCH] Save/Load vocabulary options can read/write in session binary format --- src/FindObject.cpp | 55 ++++++++++++++++++++++++++++++++++++++++------ src/MainWindow.cpp | 8 ++++--- src/Vocabulary.cpp | 26 ++++++++++++++++++---- src/Vocabulary.h | 4 ++-- 4 files changed, 77 insertions(+), 16 deletions(-) diff --git a/src/FindObject.cpp b/src/FindObject.cpp index 75460d5f..dc359546 100644 --- a/src/FindObject.cpp +++ b/src/FindObject.cpp @@ -91,7 +91,7 @@ bool FindObject::loadSession(const QString & path) Settings::setParameter(iter.key(), iter.value()); } - // save vocabulary + // load vocabulary vocabulary_->load(in); // load objects @@ -156,7 +156,26 @@ bool FindObject::saveSession(const QString & path) bool FindObject::saveVocabulary(const QString & filePath) const { - return vocabulary_->save(filePath); + if(!filePath.isEmpty() && QFileInfo(filePath).suffix().compare("bin") == 0) + { + QFile file(filePath); + file.open(QIODevice::WriteOnly); + QDataStream out(&file); + + // ignore parameters + out << ParametersMap(); + + // save vocabulary + vocabulary_->save(out, true); + + file.close(); + return true; + } + else + { + return vocabulary_->save(filePath); + } + return false; } bool FindObject::loadVocabulary(const QString & filePath) @@ -166,14 +185,36 @@ bool FindObject::loadVocabulary(const QString & filePath) UWARN("Doesn't make sense to load a vocabulary if \"General/vocabularyFixed\" and \"General/invertedSearch\" are not enabled! It will " "be cleared at the time the objects are updated."); } - if(vocabulary_->load(filePath)) + + if(QFile::exists(filePath) && !filePath.isEmpty() && QFileInfo(filePath).suffix().compare("bin") == 0) { - if(objects_.size()) - { - updateVocabulary(); - } + //binary format (from session format) + QFile file(filePath); + file.open(QIODevice::ReadOnly); + QDataStream in(&file); + + ParametersMap parameters; + // ignore parameters + in >> parameters; + + // load vocabulary + vocabulary_->load(in, true); + file.close(); + return true; } + else + { + //yaml/xml format + if(vocabulary_->load(filePath)) + { + if(objects_.size()) + { + updateVocabulary(); + } + return true; + } + } return false; } diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 3c908a1e..13a805f9 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -627,7 +627,7 @@ void MainWindow::loadVocabulary() if(Settings::getGeneral_vocabularyFixed() && Settings::getGeneral_invertedSearch()) { - QString path = QFileDialog::getOpenFileName(this, tr("Load vocabulary..."), Settings::workingDirectory(), "Data (*.yaml *.xml)"); + QString path = QFileDialog::getOpenFileName(this, tr("Load vocabulary..."), Settings::workingDirectory(), "Data (*.yaml *.xml *.bin)"); if(!path.isEmpty()) { if(findObject_->loadVocabulary(path)) @@ -651,10 +651,12 @@ void MainWindow::saveVocabulary() QMessageBox::warning(this, tr("Saving vocabulary..."), tr("Vocabulary is empty!")); return; } - QString path = QFileDialog::getSaveFileName(this, tr("Save vocabulary..."), Settings::workingDirectory(), "Data (*.yaml *.xml)"); + QString path = QFileDialog::getSaveFileName(this, tr("Save vocabulary..."), Settings::workingDirectory(), "Data (*.yaml *.xml *.bin)"); if(!path.isEmpty()) { - if(QFileInfo(path).suffix().compare("yaml") != 0 && QFileInfo(path).suffix().compare("xml") != 0) + if( QFileInfo(path).suffix().compare("yaml") != 0 && + QFileInfo(path).suffix().compare("xml") != 0 && + QFileInfo(path).suffix().compare("bin") != 0) { path.append(".yaml"); } diff --git a/src/Vocabulary.cpp b/src/Vocabulary.cpp index f69f26c9..e67eb55a 100644 --- a/src/Vocabulary.cpp +++ b/src/Vocabulary.cpp @@ -70,10 +70,18 @@ void Vocabulary::clear() indexedDescriptors_ = cv::Mat(); } -void Vocabulary::save(QDataStream & streamSessionPtr) const +void Vocabulary::save(QDataStream & streamSessionPtr, bool saveVocabularyOnly) const { // save index - streamSessionPtr << wordToObjects_; + if(saveVocabularyOnly) + { + QMultiMap dummy; + streamSessionPtr << dummy; + } + else + { + streamSessionPtr << wordToObjects_; + } // save words qint64 dataSize = indexedDescriptors_.elemSize()*indexedDescriptors_.cols*indexedDescriptors_.rows; @@ -84,10 +92,20 @@ void Vocabulary::save(QDataStream & streamSessionPtr) const streamSessionPtr << QByteArray((char*)indexedDescriptors_.data, dataSize); } -void Vocabulary::load(QDataStream & streamSessionPtr) +void Vocabulary::load(QDataStream & streamSessionPtr, bool loadVocabularyOnly) { // load index - streamSessionPtr >> wordToObjects_; + if(loadVocabularyOnly) + { + QMultiMap dummy; + streamSessionPtr >> dummy; + // clear index + wordToObjects_.clear(); + } + else + { + streamSessionPtr >> wordToObjects_; + } // load words int rows,cols,type; diff --git a/src/Vocabulary.h b/src/Vocabulary.h index 9c754d48..948e2bf1 100644 --- a/src/Vocabulary.h +++ b/src/Vocabulary.h @@ -49,8 +49,8 @@ public: const QMultiMap & wordToObjects() const {return wordToObjects_;} const cv::Mat & indexedDescriptors() const {return indexedDescriptors_;} - void save(QDataStream & streamSessionPtr) const; - void load(QDataStream & streamSessionPtr); + void save(QDataStream & streamSessionPtr, bool saveVocabularyOnly = false) const; + void load(QDataStream & streamSessionPtr, bool loadVocabularyOnly = false); bool save(const QString & filename) const; bool load(const QString & filename);