Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
c256cf09
Commit
c256cf09
authored
Jul 20, 2016
by
Minglangjun Li
Committed by
Davis E. King
Jul 20, 2016
Browse files
Fixes #128. Added support to discontiguous Numpy arrays (#155)
parent
22758268
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
22 deletions
+86
-22
dlib/python/numpy.h
dlib/python/numpy.h
+74
-17
dlib/python/numpy_image.h
dlib/python/numpy_image.h
+12
-5
No files found.
dlib/python/numpy.h
View file @
c256cf09
...
...
@@ -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
(
i
<
pybuf
.
ndim
)
shape
[
i
]
=
pybuf
.
shape
[
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
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
(
i
<
pybuf
.
ndim
)
shape
[
i
]
=
pybuf
.
shape
[
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
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
(...)
...
...
dlib/python/numpy_image.h
View file @
c256cf09
...
...
@@ -6,6 +6,7 @@
#include "numpy.h"
#include <dlib/pixel.h>
#include <dlib/matrix.h>
#include <dlib/array.h>
// ----------------------------------------------------------------------------------------
...
...
@@ -18,7 +19,7 @@ public:
numpy_gray_image
(
boost
::
python
::
object
&
img
)
{
long
shape
[
2
];
get_numpy_ndarray_parts
(
img
,
_data
,
shape
);
get_numpy_ndarray_parts
(
img
,
_data
,
_contig_buf
,
shape
);
_nr
=
shape
[
0
];
_nc
=
shape
[
1
];
}
...
...
@@ -32,6 +33,7 @@ public:
private:
unsigned
char
*
_data
;
dlib
::
array
<
unsigned
char
>
_contig_buf
;
long
_nr
;
long
_nc
;
};
...
...
@@ -51,7 +53,8 @@ inline bool is_gray_python_image (boost::python::object& img)
{
try
{
numpy_gray_image
temp
(
img
);
long
shape
[
2
];
get_numpy_ndarray_shape
(
img
,
shape
);
return
true
;
}
catch
(
dlib
::
error
&
)
...
...
@@ -70,7 +73,7 @@ public:
numpy_rgb_image
(
boost
::
python
::
object
&
img
)
{
long
shape
[
3
];
get_numpy_ndarray_parts
(
img
,
_data
,
shape
);
get_numpy_ndarray_parts
(
img
,
_data
,
_contig_buf
,
shape
);
_nr
=
shape
[
0
];
_nc
=
shape
[
1
];
if
(
shape
[
2
]
!=
3
)
...
...
@@ -87,6 +90,7 @@ public:
private:
dlib
::
rgb_pixel
*
_data
;
dlib
::
array
<
dlib
::
rgb_pixel
>
_contig_buf
;
long
_nr
;
long
_nc
;
};
...
...
@@ -107,8 +111,11 @@ inline bool is_rgb_python_image (boost::python::object& img)
{
try
{
numpy_rgb_image
temp
(
img
);
long
shape
[
3
];
get_numpy_ndarray_shape
(
img
,
shape
);
if
(
shape
[
2
]
==
3
)
return
true
;
return
false
;
}
catch
(
dlib
::
error
&
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment