Skip to content

Commit

Permalink
Added functions to load and save PCA models
Browse files Browse the repository at this point in the history
This is useful when one wants to load or save a single PCA model, for example when combining different shape and albedo models.
Also added python bindings for both functions.
  • Loading branch information
patrikhuber committed Mar 13, 2017
1 parent 81673da commit 6c31092
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
41 changes: 41 additions & 0 deletions include/eos/morphablemodel/PcaModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "cereal/access.hpp"
#include "cereal/types/array.hpp"
#include "cereal/types/vector.hpp"
#include "cereal/archives/binary.hpp"

#include "Eigen/Core"

Expand All @@ -34,6 +35,7 @@
#include <array>
#include <random>
#include <cassert>
#include <iostream>

namespace eos {
namespace morphablemodel {
Expand Down Expand Up @@ -292,6 +294,45 @@ class PcaModel
};
};

/**
* Helper method to load a PCA model from
* a cereal::BinaryInputArchive from the harddisk.
*
* Usually, morphablemodel::load_model(std::string) should be used to directly
* load a MorphableModel. This function can be useful when it is necessary to just
* load a PCA model.
*
* @param[in] filename Filename to a model.
* @return The loaded PCA model.
* @throw std::runtime_error When the file given in \c filename fails to be opened (most likely because the file doesn't exist).
*/
inline PcaModel load_pca_model(std::string filename)
{
PcaModel model;

std::ifstream file(filename, std::ios::binary);
if (file.fail()) {
throw std::runtime_error("Error opening given file: " + filename);
}
cereal::BinaryInputArchive input_archive(file);
input_archive(model);

return model;
};

/**
* Helper method to save a PCA model to the
* harddrive as cereal::BinaryOutputArchive.
*
* @param[in] model The model to be saved.
* @param[in] filename Filename for the model.
*/
inline void save_pca_model(PcaModel model, std::string filename)
{
std::ofstream file(filename, std::ios::binary);
cereal::BinaryOutputArchive output_archive(file);
output_archive(model);
};

/**
* Takes an orthonormal PCA basis matrix (a matrix consisting
Expand Down
6 changes: 4 additions & 2 deletions python/generate-python-bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ PYBIND11_PLUGIN(eos) {
* Bindings for the eos::morphablemodel namespace:
* - PcaModel
* - MorphableModel
* - load_model()
* - save_model()
* - load_model(), save_model()
* - load_pca_model(), save_pca_model()
*/
py::module morphablemodel_module = eos_module.def_submodule("morphablemodel", "Functionality to represent a Morphable Model, its PCA models, and functions to load models and blendshapes.");

Expand Down Expand Up @@ -111,6 +111,8 @@ PYBIND11_PLUGIN(eos) {

morphablemodel_module.def("load_model", &morphablemodel::load_model, "Load a Morphable Model from a cereal::BinaryInputArchive (.bin) from the harddisk.", py::arg("filename"));
morphablemodel_module.def("save_model", &morphablemodel::save_model, "Save a Morphable Model as cereal::BinaryOutputArchive.", py::arg("model"), py::arg("filename"));
morphablemodel_module.def("load_pca_model", &morphablemodel::load_pca_model, "Load a PCA model from a cereal::BinaryInputArchive (.bin) from the harddisk.", py::arg("filename"));
morphablemodel_module.def("save_pca_model", &morphablemodel::save_pca_model, "Save a PCA model as cereal::BinaryOutputArchive.", py::arg("model"), py::arg("filename"));

/**
* - Blendshape
Expand Down

0 comments on commit 6c31092

Please sign in to comment.