|
|
|
@ -7,12 +7,14 @@
|
|
|
|
|
#include <dlib/error.h>
|
|
|
|
|
#include <dlib/algs.h>
|
|
|
|
|
#include <dlib/string.h>
|
|
|
|
|
#include <dlib/array.h>
|
|
|
|
|
#include <dlib/pixel.h>
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
void validate_numpy_array_type (
|
|
|
|
|
boost::python::object& obj
|
|
|
|
|
const boost::python::object& obj
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
using namespace boost::python;
|
|
|
|
@ -30,36 +32,83 @@ void validate_numpy_array_type (
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
template <int dims>
|
|
|
|
|
void get_numpy_ndarray_shape (
|
|
|
|
|
const boost::python::object& obj,
|
|
|
|
|
long (&shape)[dims]
|
|
|
|
|
)
|
|
|
|
|
/*!
|
|
|
|
|
ensures
|
|
|
|
|
- stores the shape of the array into #shape.
|
|
|
|
|
- the dimension of the given numpy array is not greater than #dims.
|
|
|
|
|
!*/
|
|
|
|
|
{
|
|
|
|
|
Py_buffer pybuf;
|
|
|
|
|
if (PyObject_GetBuffer(obj.ptr(), &pybuf, PyBUF_STRIDES ))
|
|
|
|
|
throw dlib::error("Expected numpy.ndarray with shape set.");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (pybuf.ndim > dims)
|
|
|
|
|
throw dlib::error("Expected array with " + dlib::cast_to_string(dims) + " dimensions.");
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < dims; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (i < pybuf.ndim)
|
|
|
|
|
shape[i] = pybuf.shape[i];
|
|
|
|
|
else
|
|
|
|
|
shape[i] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch(...)
|
|
|
|
|
{
|
|
|
|
|
PyBuffer_Release(&pybuf);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
PyBuffer_Release(&pybuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
template <typename T, int dims>
|
|
|
|
|
void get_numpy_ndarray_parts (
|
|
|
|
|
boost::python::object& obj,
|
|
|
|
|
T*& data,
|
|
|
|
|
dlib::array<T>& contig_buf,
|
|
|
|
|
long (&shape)[dims]
|
|
|
|
|
)
|
|
|
|
|
/*!
|
|
|
|
|
ensures
|
|
|
|
|
- extracts the pointer to the data from the given numpy ndarray. Stores the shape
|
|
|
|
|
of the array into #shape.
|
|
|
|
|
- the dimension of the given numpy array is not greater than #dims.
|
|
|
|
|
- #shape[#dims-1] == pixel_traits<T>::num when #dims is greater than 2
|
|
|
|
|
!*/
|
|
|
|
|
{
|
|
|
|
|
Py_buffer pybuf;
|
|
|
|
|
if (PyObject_GetBuffer(obj.ptr(), &pybuf, PyBUF_ND | PyBUF_WRITABLE ))
|
|
|
|
|
throw dlib::error("Expected contiguous and writable numpy.ndarray.");
|
|
|
|
|
if (PyObject_GetBuffer(obj.ptr(), &pybuf, PyBUF_STRIDES | PyBUF_WRITABLE ))
|
|
|
|
|
throw dlib::error("Expected writable numpy.ndarray with shape set.");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
validate_numpy_array_type<T>(obj);
|
|
|
|
|
data = (T*)pybuf.buf;
|
|
|
|
|
|
|
|
|
|
if (pybuf.ndim > dims)
|
|
|
|
|
throw dlib::error("Expected array with " + dlib::cast_to_string(dims) + " dimensions.");
|
|
|
|
|
get_numpy_ndarray_shape(obj, shape);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < dims; ++i)
|
|
|
|
|
if (dlib::pixel_traits<T>::num > 1 && dlib::pixel_traits<T>::num != shape[dims-1])
|
|
|
|
|
throw dlib::error("Expected numpy.ndarray with " + dlib::cast_to_string(dlib::pixel_traits<T>::num) + " channels.");
|
|
|
|
|
|
|
|
|
|
if (PyBuffer_IsContiguous(&pybuf, 'C'))
|
|
|
|
|
data = (T*)pybuf.buf;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (i < pybuf.ndim)
|
|
|
|
|
shape[i] = pybuf.shape[i];
|
|
|
|
|
else
|
|
|
|
|
shape[i] = 1;
|
|
|
|
|
contig_buf.resize(pybuf.len);
|
|
|
|
|
if (PyBuffer_ToContiguous(&contig_buf[0], &pybuf, pybuf.len, 'C'))
|
|
|
|
|
throw dlib::error("Can't copy numpy.ndarray to a contiguous buffer.");
|
|
|
|
|
data = &contig_buf[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch(...)
|
|
|
|
@ -76,32 +125,40 @@ template <typename T, int dims>
|
|
|
|
|
void get_numpy_ndarray_parts (
|
|
|
|
|
const boost::python::object& obj,
|
|
|
|
|
const T*& data,
|
|
|
|
|
dlib::array<T>& contig_buf,
|
|
|
|
|
long (&shape)[dims]
|
|
|
|
|
)
|
|
|
|
|
/*!
|
|
|
|
|
ensures
|
|
|
|
|
- extracts the pointer to the data from the given numpy ndarray. Stores the shape
|
|
|
|
|
of the array into #shape.
|
|
|
|
|
- the dimension of the given numpy array is not greater than #dims.
|
|
|
|
|
- #shape[#dims-1] == pixel_traits<T>::num when #dims is greater than 2
|
|
|
|
|
!*/
|
|
|
|
|
{
|
|
|
|
|
Py_buffer pybuf;
|
|
|
|
|
if (PyObject_GetBuffer(obj.ptr(), &pybuf, PyBUF_ND ))
|
|
|
|
|
throw dlib::error("Expected contiguous numpy.ndarray.");
|
|
|
|
|
if (PyObject_GetBuffer(obj.ptr(), &pybuf, PyBUF_STRIDES ))
|
|
|
|
|
throw dlib::error("Expected numpy.ndarray with shape set.");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
validate_numpy_array_type<T>(obj);
|
|
|
|
|
data = (const T*)pybuf.buf;
|
|
|
|
|
|
|
|
|
|
if (pybuf.ndim > dims)
|
|
|
|
|
throw dlib::error("Expected array with " + dlib::cast_to_string(dims) + " dimensions.");
|
|
|
|
|
get_numpy_ndarray_shape(obj, shape);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < dims; ++i)
|
|
|
|
|
if (dlib::pixel_traits<T>::num > 1 && dlib::pixel_traits<T>::num != shape[dims-1])
|
|
|
|
|
throw dlib::error("Expected numpy.ndarray with " + dlib::cast_to_string(dlib::pixel_traits<T>::num) + " channels.");
|
|
|
|
|
|
|
|
|
|
if (PyBuffer_IsContiguous(&pybuf, 'C'))
|
|
|
|
|
data = (const T*)pybuf.buf;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (i < pybuf.ndim)
|
|
|
|
|
shape[i] = pybuf.shape[i];
|
|
|
|
|
else
|
|
|
|
|
shape[i] = 1;
|
|
|
|
|
contig_buf.resize(pybuf.len);
|
|
|
|
|
if (PyBuffer_ToContiguous(&contig_buf[0], &pybuf, pybuf.len, 'C'))
|
|
|
|
|
throw dlib::error("Can't copy numpy.ndarray to a contiguous buffer.");
|
|
|
|
|
data = &contig_buf[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch(...)
|
|
|
|
|