Fixes #128. Added support to discontiguous Numpy arrays (#155)

This commit is contained in:
Minglangjun Li 2016-07-20 22:05:30 +08:00 committed by Davis E. King
parent 22758268fc
commit c256cf0999
2 changed files with 88 additions and 24 deletions

View File

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

View File

@ -6,6 +6,7 @@
#include "numpy.h" #include "numpy.h"
#include <dlib/pixel.h> #include <dlib/pixel.h>
#include <dlib/matrix.h> #include <dlib/matrix.h>
#include <dlib/array.h>
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
@ -18,7 +19,7 @@ public:
numpy_gray_image (boost::python::object& img) numpy_gray_image (boost::python::object& img)
{ {
long shape[2]; long shape[2];
get_numpy_ndarray_parts(img, _data, shape); get_numpy_ndarray_parts(img, _data, _contig_buf, shape);
_nr = shape[0]; _nr = shape[0];
_nc = shape[1]; _nc = shape[1];
} }
@ -32,6 +33,7 @@ public:
private: private:
unsigned char* _data; unsigned char* _data;
dlib::array<unsigned char> _contig_buf;
long _nr; long _nr;
long _nc; long _nc;
}; };
@ -51,7 +53,8 @@ inline bool is_gray_python_image (boost::python::object& img)
{ {
try try
{ {
numpy_gray_image temp(img); long shape[2];
get_numpy_ndarray_shape(img, shape);
return true; return true;
} }
catch (dlib::error&) catch (dlib::error&)
@ -70,7 +73,7 @@ public:
numpy_rgb_image (boost::python::object& img) numpy_rgb_image (boost::python::object& img)
{ {
long shape[3]; long shape[3];
get_numpy_ndarray_parts(img, _data, shape); get_numpy_ndarray_parts(img, _data, _contig_buf, shape);
_nr = shape[0]; _nr = shape[0];
_nc = shape[1]; _nc = shape[1];
if (shape[2] != 3) if (shape[2] != 3)
@ -87,6 +90,7 @@ public:
private: private:
dlib::rgb_pixel* _data; dlib::rgb_pixel* _data;
dlib::array<dlib::rgb_pixel> _contig_buf;
long _nr; long _nr;
long _nc; long _nc;
}; };
@ -107,8 +111,11 @@ inline bool is_rgb_python_image (boost::python::object& img)
{ {
try try
{ {
numpy_rgb_image temp(img); long shape[3];
return true; get_numpy_ndarray_shape(img, shape);
if (shape[2] == 3)
return true;
return false;
} }
catch (dlib::error&) catch (dlib::error&)
{ {