Commit c256cf09 authored by Minglangjun Li's avatar Minglangjun Li Committed by Davis E. King

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

parent 22758268
...@@ -7,12 +7,14 @@ ...@@ -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,36 +32,83 @@ void validate_numpy_array_type ( ...@@ -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> template <typename T, int dims>
void get_numpy_ndarray_parts ( void get_numpy_ndarray_parts (
boost::python::object& obj, boost::python::object& obj,
T*& data, 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 | PyBUF_WRITABLE )) if (PyObject_GetBuffer(obj.ptr(), &pybuf, PyBUF_STRIDES | PyBUF_WRITABLE ))
throw dlib::error("Expected contiguous and writable numpy.ndarray."); throw dlib::error("Expected writable numpy.ndarray with shape set.");
try try
{ {
validate_numpy_array_type<T>(obj); 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.");
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) 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(...)
...@@ -76,32 +125,40 @@ template <typename T, int dims> ...@@ -76,32 +125,40 @@ 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(...)
......
...@@ -6,6 +6,7 @@ ...@@ -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: ...@@ -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: ...@@ -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) ...@@ -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: ...@@ -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: ...@@ -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) ...@@ -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&)
{ {
......
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