Commit 6c310924 authored by Patrik Huber's avatar Patrik Huber

Added functions to load and save PCA models

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.
parent 81673daa
......@@ -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"
......@@ -34,6 +35,7 @@
#include <array>
#include <random>
#include <cassert>
#include <iostream>
namespace eos {
namespace morphablemodel {
......@@ -292,6 +294,45 @@ private:
};
};
/**
* 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
......
......@@ -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.");
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment