Commit 6980697a authored by Davis King's avatar Davis King

Just moved code into dlib namespace.

parent 43e5f42e
...@@ -10,69 +10,73 @@ ...@@ -10,69 +10,73 @@
namespace py = pybind11; namespace py = pybind11;
template <typename T> namespace dlib
std::vector<T> python_list_to_vector (
const py::list& obj
)
/*!
ensures
- converts a python object into a std::vector<T> and returns it.
!*/
{ {
std::vector<T> vect(len(obj));
for (unsigned long i = 0; i < vect.size(); ++i) template <typename T>
std::vector<T> python_list_to_vector (
const py::list& obj
)
/*!
ensures
- converts a python object into a std::vector<T> and returns it.
!*/
{ {
vect[i] = obj[i].cast<T>(); std::vector<T> vect(len(obj));
for (unsigned long i = 0; i < vect.size(); ++i)
{
vect[i] = obj[i].cast<T>();
}
return vect;
} }
return vect;
}
template <typename T> template <typename T>
py::list vector_to_python_list ( py::list vector_to_python_list (
const std::vector<T>& vect const std::vector<T>& vect
) )
/*! /*!
ensures ensures
- converts a std::vector<T> into a python list object. - converts a std::vector<T> into a python list object.
!*/ !*/
{ {
py::list obj; py::list obj;
for (unsigned long i = 0; i < vect.size(); ++i) for (unsigned long i = 0; i < vect.size(); ++i)
obj.append(vect[i]); obj.append(vect[i]);
return obj; return obj;
} }
template <typename T> template <typename T>
void extend_vector_with_python_list ( void extend_vector_with_python_list (
std::vector<T> &v, std::vector<T> &v,
const py::list &l const py::list &l
) )
/*! /*!
ensures ensures
- appends items from a python list to the end of std::vector<T>. - appends items from a python list to the end of std::vector<T>.
!*/ !*/
{ {
for (const auto &item : l) for (const auto &item : l)
v.push_back(item.cast<T>()); v.push_back(item.cast<T>());
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template <typename T> template <typename T>
std::shared_ptr<T> load_object_from_file ( std::shared_ptr<T> load_object_from_file (
const std::string& filename const std::string& filename
) )
/*! /*!
ensures ensures
- deserializes an object of type T from the given file and returns it. - deserializes an object of type T from the given file and returns it.
!*/ !*/
{ {
std::ifstream fin(filename.c_str(), std::ios::binary); std::ifstream fin(filename.c_str(), std::ios::binary);
if (!fin) if (!fin)
throw dlib::error("Unable to open " + filename); throw dlib::error("Unable to open " + filename);
auto obj = std::make_shared<T>(); auto obj = std::make_shared<T>();
deserialize(*obj, fin); deserialize(*obj, fin);
return obj; return obj;
}
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
...@@ -8,58 +8,65 @@ ...@@ -8,58 +8,65 @@
#include <sstream> #include <sstream>
#include <dlib/vectorstream.h> #include <dlib/vectorstream.h>
template<typename T> namespace py = pybind11;
py::tuple getstate(const T& item)
{
using namespace dlib;
std::vector<char> buf;
buf.reserve(5000);
vectorstream sout(buf);
serialize(item, sout);
return py::make_tuple(py::handle(
PyBytes_FromStringAndSize(buf.size()?&buf[0]:0, buf.size())));
}
template<typename T> namespace dlib
T setstate(py::tuple state)
{ {
using namespace dlib;
if (len(state) != 1)
{
PyErr_SetObject(PyExc_ValueError,
py::str("expected 1-item tuple in call to __setstate__; got {}").format(state).ptr()
);
throw py::error_already_set();
}
// We used to serialize by converting to a str but the boost.python routines for template<typename T>
// doing this don't work in Python 3. You end up getting an error about invalid py::tuple getstate(const T& item)
// UTF-8 encodings. So instead we access the python C interface directly and use
// bytes objects. However, we keep the deserialization code that worked with str
// for backwards compatibility with previously pickled files.
T item;
py::object obj = state[0];
if (py::isinstance<py::str>(obj))
{ {
py::str data = state[0].cast<py::str>(); using namespace dlib;
std::string temp = data; std::vector<char> buf;
std::istringstream sin(temp); buf.reserve(5000);
deserialize(item, sin); vectorstream sout(buf);
serialize(item, sout);
return py::make_tuple(py::handle(
PyBytes_FromStringAndSize(buf.size()?&buf[0]:0, buf.size())));
} }
else if(PyBytes_Check(py::object(state[0]).ptr()))
template<typename T>
T setstate(py::tuple state)
{ {
using namespace dlib;
if (len(state) != 1)
{
PyErr_SetObject(PyExc_ValueError,
py::str("expected 1-item tuple in call to __setstate__; got {}").format(state).ptr()
);
throw py::error_already_set();
}
// We used to serialize by converting to a str but the boost.python routines for
// doing this don't work in Python 3. You end up getting an error about invalid
// UTF-8 encodings. So instead we access the python C interface directly and use
// bytes objects. However, we keep the deserialization code that worked with str
// for backwards compatibility with previously pickled files.
T item;
py::object obj = state[0]; py::object obj = state[0];
char* data = PyBytes_AsString(obj.ptr()); if (py::isinstance<py::str>(obj))
unsigned long num = PyBytes_Size(obj.ptr()); {
std::istringstream sin(std::string(data, num)); py::str data = state[0].cast<py::str>();
deserialize(item, sin); std::string temp = data;
} std::istringstream sin(temp);
else deserialize(item, sin);
{ }
throw error("Unable to unpickle, error in input file."); else if(PyBytes_Check(py::object(state[0]).ptr()))
{
py::object obj = state[0];
char* data = PyBytes_AsString(obj.ptr());
unsigned long num = PyBytes_Size(obj.ptr());
std::istringstream sin(std::string(data, num));
deserialize(item, sin);
}
else
{
throw error("Unable to unpickle, error in input file.");
}
return item;
} }
return item;
} }
#endif // DLIB_SERIALIZE_PiCKLE_Hh_ #endif // DLIB_SERIALIZE_PiCKLE_Hh_
......
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