Save/Load vocabulary options can read/write in session binary format

This commit is contained in:
matlabbe 2018-08-30 12:43:16 -04:00
parent 9d0c03f661
commit ade5dd6729
4 changed files with 77 additions and 16 deletions

View File

@ -91,7 +91,7 @@ bool FindObject::loadSession(const QString & path)
Settings::setParameter(iter.key(), iter.value()); Settings::setParameter(iter.key(), iter.value());
} }
// save vocabulary // load vocabulary
vocabulary_->load(in); vocabulary_->load(in);
// load objects // load objects
@ -156,7 +156,26 @@ bool FindObject::saveSession(const QString & path)
bool FindObject::saveVocabulary(const QString & filePath) const bool FindObject::saveVocabulary(const QString & filePath) const
{ {
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 vocabulary_->save(filePath);
}
return false;
} }
bool FindObject::loadVocabulary(const QString & filePath) bool FindObject::loadVocabulary(const QString & filePath)
@ -166,6 +185,27 @@ 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 " 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."); "be cleared at the time the objects are updated.");
} }
if(QFile::exists(filePath) && !filePath.isEmpty() && QFileInfo(filePath).suffix().compare("bin") == 0)
{
//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(vocabulary_->load(filePath))
{ {
if(objects_.size()) if(objects_.size())
@ -174,6 +214,7 @@ bool FindObject::loadVocabulary(const QString & filePath)
} }
return true; return true;
} }
}
return false; return false;
} }

View File

@ -627,7 +627,7 @@ void MainWindow::loadVocabulary()
if(Settings::getGeneral_vocabularyFixed() && if(Settings::getGeneral_vocabularyFixed() &&
Settings::getGeneral_invertedSearch()) 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(!path.isEmpty())
{ {
if(findObject_->loadVocabulary(path)) if(findObject_->loadVocabulary(path))
@ -651,10 +651,12 @@ void MainWindow::saveVocabulary()
QMessageBox::warning(this, tr("Saving vocabulary..."), tr("Vocabulary is empty!")); QMessageBox::warning(this, tr("Saving vocabulary..."), tr("Vocabulary is empty!"));
return; 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(!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"); path.append(".yaml");
} }

View File

@ -70,10 +70,18 @@ void Vocabulary::clear()
indexedDescriptors_ = cv::Mat(); indexedDescriptors_ = cv::Mat();
} }
void Vocabulary::save(QDataStream & streamSessionPtr) const void Vocabulary::save(QDataStream & streamSessionPtr, bool saveVocabularyOnly) const
{ {
// save index // save index
if(saveVocabularyOnly)
{
QMultiMap<int, int> dummy;
streamSessionPtr << dummy;
}
else
{
streamSessionPtr << wordToObjects_; streamSessionPtr << wordToObjects_;
}
// save words // save words
qint64 dataSize = indexedDescriptors_.elemSize()*indexedDescriptors_.cols*indexedDescriptors_.rows; qint64 dataSize = indexedDescriptors_.elemSize()*indexedDescriptors_.cols*indexedDescriptors_.rows;
@ -84,10 +92,20 @@ void Vocabulary::save(QDataStream & streamSessionPtr) const
streamSessionPtr << QByteArray((char*)indexedDescriptors_.data, dataSize); streamSessionPtr << QByteArray((char*)indexedDescriptors_.data, dataSize);
} }
void Vocabulary::load(QDataStream & streamSessionPtr) void Vocabulary::load(QDataStream & streamSessionPtr, bool loadVocabularyOnly)
{ {
// load index // load index
if(loadVocabularyOnly)
{
QMultiMap<int, int> dummy;
streamSessionPtr >> dummy;
// clear index
wordToObjects_.clear();
}
else
{
streamSessionPtr >> wordToObjects_; streamSessionPtr >> wordToObjects_;
}
// load words // load words
int rows,cols,type; int rows,cols,type;

View File

@ -49,8 +49,8 @@ public:
const QMultiMap<int, int> & wordToObjects() const {return wordToObjects_;} const QMultiMap<int, int> & wordToObjects() const {return wordToObjects_;}
const cv::Mat & indexedDescriptors() const {return indexedDescriptors_;} const cv::Mat & indexedDescriptors() const {return indexedDescriptors_;}
void save(QDataStream & streamSessionPtr) const; void save(QDataStream & streamSessionPtr, bool saveVocabularyOnly = false) const;
void load(QDataStream & streamSessionPtr); void load(QDataStream & streamSessionPtr, bool loadVocabularyOnly = false);
bool save(const QString & filename) const; bool save(const QString & filename) const;
bool load(const QString & filename); bool load(const QString & filename);