Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
bff364dc
Commit
bff364dc
authored
Feb 07, 2014
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added some functions for converting a numpy array into a pointer and its
dimensions.
parent
746f4282
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
118 additions
and
0 deletions
+118
-0
numpy.h
dlib/python/numpy.h
+118
-0
No files found.
dlib/python/numpy.h
0 → 100644
View file @
bff364dc
// Copyright (C) 2014 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_PYTHON_NuMPY_H__
#define DLIB_PYTHON_NuMPY_H__
#include <boost/python.hpp>
#include <dlib/error.h>
#include <dlib/algs.h>
#include <dlib/string.h>
// ----------------------------------------------------------------------------------------
template
<
typename
T
>
void
validate_numpy_array_type
(
boost
::
python
::
object
&
obj
)
{
using
namespace
boost
::
python
;
const
char
ch
=
extract
<
char
>
(
obj
.
attr
(
"dtype"
).
attr
(
"char"
));
if
(
dlib
::
is_same_type
<
T
,
double
>::
value
&&
ch
!=
'd'
)
throw
dlib
::
error
(
"Expected numpy.ndarray of float64"
);
if
(
dlib
::
is_same_type
<
T
,
float
>::
value
&&
ch
!=
'f'
)
throw
dlib
::
error
(
"Expected numpy.ndarray of float32"
);
if
(
dlib
::
is_same_type
<
T
,
dlib
::
int32
>::
value
&&
ch
!=
'i'
)
throw
dlib
::
error
(
"Expected numpy.ndarray of int32"
);
if
(
dlib
::
is_same_type
<
T
,
unsigned
char
>::
value
&&
ch
!=
'B'
)
throw
dlib
::
error
(
"Expected numpy.ndarray of uint8"
);
}
// ----------------------------------------------------------------------------------------
template
<
typename
T
,
int
dims
>
void
get_numpy_ndarray_parts
(
boost
::
python
::
object
&
obj
,
T
*&
data
,
long
(
&
shape
)[
dims
]
)
/*!
ensures
- extracts the pointer to the data from the given numpy ndarray. Stores the shape
of the array into #shape.
!*/
{
Py_buffer
pybuf
;
if
(
PyObject_GetBuffer
(
obj
.
ptr
(),
&
pybuf
,
PyBUF_ND
|
PyBUF_WRITABLE
))
throw
dlib
::
error
(
"Expected contiguous and writable numpy.ndarray."
);
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."
);
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
(
const
boost
::
python
::
object
&
obj
,
const
T
*&
data
,
long
(
&
shape
)[
dims
]
)
/*!
ensures
- extracts the pointer to the data from the given numpy ndarray. Stores the shape
of the array into #shape.
!*/
{
Py_buffer
pybuf
;
if
(
PyObject_GetBuffer
(
obj
.
ptr
(),
&
pybuf
,
PyBUF_ND
))
throw
dlib
::
error
(
"Expected contiguous numpy.ndarray."
);
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."
);
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
);
}
// ----------------------------------------------------------------------------------------
#endif // DLIB_PYTHON_NuMPY_H__
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