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
6980697a
Commit
6980697a
authored
Apr 28, 2018
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Just moved code into dlib namespace.
parent
43e5f42e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
109 additions
and
98 deletions
+109
-98
pybind_utils.h
dlib/python/pybind_utils.h
+59
-55
serialize_pickle.h
dlib/python/serialize_pickle.h
+50
-43
No files found.
dlib/python/pybind_utils.h
View file @
6980697a
...
...
@@ -10,69 +10,73 @@
namespace
py
=
pybind11
;
template
<
typename
T
>
std
::
vector
<
T
>
python_list_to_vector
(
const
py
::
list
&
obj
)
/*!
ensures
- converts a python object into a std::vector<T> and returns it.
!*/
namespace
dlib
{
std
::
vector
<
T
>
vect
(
len
(
obj
));
for
(
unsigned
long
i
=
0
;
i
<
vect
.
size
();
++
i
)
template
<
typename
T
>
std
::
vector
<
T
>
python_list_to_vector
(
const
py
::
list
&
obj
)
/*!
ensures
- converts a python object into a std::vector<T> and returns it.
!*/
{
vect
[
i
]
=
obj
[
i
].
cast
<
T
>
();
std
::
vector
<
T
>
vect
(
len
(
obj
));
for
(
unsigned
long
i
=
0
;
i
<
vect
.
size
();
++
i
)
{
vect
[
i
]
=
obj
[
i
].
cast
<
T
>
();
}
return
vect
;
}
return
vect
;
}
template
<
typename
T
>
py
::
list
vector_to_python_list
(
const
std
::
vector
<
T
>&
vect
)
/*!
ensures
- converts a std::vector<T> into a python list object.
!*/
{
py
::
list
obj
;
for
(
unsigned
long
i
=
0
;
i
<
vect
.
size
();
++
i
)
obj
.
append
(
vect
[
i
]);
return
obj
;
}
template
<
typename
T
>
py
::
list
vector_to_python_list
(
const
std
::
vector
<
T
>&
vect
)
/*!
ensures
- converts a std::vector<T> into a python list object.
!*/
{
py
::
list
obj
;
for
(
unsigned
long
i
=
0
;
i
<
vect
.
size
();
++
i
)
obj
.
append
(
vect
[
i
]);
return
obj
;
}
template
<
typename
T
>
void
extend_vector_with_python_list
(
std
::
vector
<
T
>
&
v
,
const
py
::
list
&
l
)
/*!
ensures
- appends items from a python list to the end of std::vector<T>.
!*/
{
for
(
const
auto
&
item
:
l
)
v
.
push_back
(
item
.
cast
<
T
>
());
}
template
<
typename
T
>
void
extend_vector_with_python_list
(
std
::
vector
<
T
>
&
v
,
const
py
::
list
&
l
)
/*!
ensures
- appends items from a python list to the end of std::vector<T>.
!*/
{
for
(
const
auto
&
item
:
l
)
v
.
push_back
(
item
.
cast
<
T
>
());
}
// ----------------------------------------------------------------------------------------
template
<
typename
T
>
std
::
shared_ptr
<
T
>
load_object_from_file
(
const
std
::
string
&
filename
)
/*!
ensures
- deserializes an object of type T from the given file and returns it.
!*/
{
std
::
ifstream
fin
(
filename
.
c_str
(),
std
::
ios
::
binary
);
if
(
!
fin
)
throw
dlib
::
error
(
"Unable to open "
+
filename
);
auto
obj
=
std
::
make_shared
<
T
>
();
deserialize
(
*
obj
,
fin
);
return
obj
;
template
<
typename
T
>
std
::
shared_ptr
<
T
>
load_object_from_file
(
const
std
::
string
&
filename
)
/*!
ensures
- deserializes an object of type T from the given file and returns it.
!*/
{
std
::
ifstream
fin
(
filename
.
c_str
(),
std
::
ios
::
binary
);
if
(
!
fin
)
throw
dlib
::
error
(
"Unable to open "
+
filename
);
auto
obj
=
std
::
make_shared
<
T
>
();
deserialize
(
*
obj
,
fin
);
return
obj
;
}
}
// ----------------------------------------------------------------------------------------
...
...
dlib/python/serialize_pickle.h
View file @
6980697a
...
...
@@ -8,58 +8,65 @@
#include <sstream>
#include <dlib/vectorstream.h>
template
<
typename
T
>
py
::
tuple
getstate
(
const
T
&
item
)
{
using
namespace
dlib
;
std
::
vector
<
char
>
buf
;
buf
.
reserve
(
5000
);
vectorstream
sout
(
buf
);
serialize
(
item
,
sout
);
return
py
::
make_tuple
(
py
::
handle
(
PyBytes_FromStringAndSize
(
buf
.
size
()
?&
buf
[
0
]
:
0
,
buf
.
size
())));
}
namespace
py
=
pybind11
;
template
<
typename
T
>
T
setstate
(
py
::
tuple
state
)
namespace
dlib
{
using
namespace
dlib
;
if
(
len
(
state
)
!=
1
)
{
PyErr_SetObject
(
PyExc_ValueError
,
py
::
str
(
"expected 1-item tuple in call to __setstate__; got {}"
).
format
(
state
).
ptr
()
);
throw
py
::
error_already_set
();
}
// We used to serialize by converting to a str but the boost.python routines for
// doing this don't work in Python 3. You end up getting an error about invalid
// UTF-8 encodings. So instead we access the python C interface directly and use
// bytes objects. However, we keep the deserialization code that worked with str
// for backwards compatibility with previously pickled files.
T
item
;
py
::
object
obj
=
state
[
0
];
if
(
py
::
isinstance
<
py
::
str
>
(
obj
))
template
<
typename
T
>
py
::
tuple
getstate
(
const
T
&
item
)
{
py
::
str
data
=
state
[
0
].
cast
<
py
::
str
>
();
std
::
string
temp
=
data
;
std
::
istringstream
sin
(
temp
);
deserialize
(
item
,
sin
);
using
namespace
dlib
;
std
::
vector
<
char
>
buf
;
buf
.
reserve
(
5000
);
vectorstream
sout
(
buf
);
serialize
(
item
,
sout
);
return
py
::
make_tuple
(
py
::
handle
(
PyBytes_FromStringAndSize
(
buf
.
size
()
?&
buf
[
0
]
:
0
,
buf
.
size
())));
}
else
if
(
PyBytes_Check
(
py
::
object
(
state
[
0
]).
ptr
()))
template
<
typename
T
>
T
setstate
(
py
::
tuple
state
)
{
using
namespace
dlib
;
if
(
len
(
state
)
!=
1
)
{
PyErr_SetObject
(
PyExc_ValueError
,
py
::
str
(
"expected 1-item tuple in call to __setstate__; got {}"
).
format
(
state
).
ptr
()
);
throw
py
::
error_already_set
();
}
// We used to serialize by converting to a str but the boost.python routines for
// doing this don't work in Python 3. You end up getting an error about invalid
// UTF-8 encodings. So instead we access the python C interface directly and use
// bytes objects. However, we keep the deserialization code that worked with str
// for backwards compatibility with previously pickled files.
T
item
;
py
::
object
obj
=
state
[
0
];
char
*
data
=
PyBytes_AsString
(
obj
.
ptr
());
unsigned
long
num
=
PyBytes_Size
(
obj
.
ptr
());
std
::
istringstream
sin
(
std
::
string
(
data
,
num
));
deserialize
(
item
,
sin
);
}
else
{
throw
error
(
"Unable to unpickle, error in input file."
);
if
(
py
::
isinstance
<
py
::
str
>
(
obj
))
{
py
::
str
data
=
state
[
0
].
cast
<
py
::
str
>
();
std
::
string
temp
=
data
;
std
::
istringstream
sin
(
temp
);
deserialize
(
item
,
sin
);
}
else
if
(
PyBytes_Check
(
py
::
object
(
state
[
0
]).
ptr
()))
{
py
::
object
obj
=
state
[
0
];
char
*
data
=
PyBytes_AsString
(
obj
.
ptr
());
unsigned
long
num
=
PyBytes_Size
(
obj
.
ptr
());
std
::
istringstream
sin
(
std
::
string
(
data
,
num
));
deserialize
(
item
,
sin
);
}
else
{
throw
error
(
"Unable to unpickle, error in input file."
);
}
return
item
;
}
return
item
;
}
#endif // DLIB_SERIALIZE_PiCKLE_Hh_
...
...
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