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
6c313b89
Commit
6c313b89
authored
Apr 27, 2013
by
Davis King
Browse files
Just moving code around.
parent
e0c9bb65
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
169 additions
and
158 deletions
+169
-158
tools/python/CMakeLists.txt
tools/python/CMakeLists.txt
+2
-0
tools/python/src/basic.cpp
tools/python/src/basic.cpp
+132
-0
tools/python/src/dlib.cpp
tools/python/src/dlib.cpp
+5
-158
tools/python/src/other.cpp
tools/python/src/other.cpp
+30
-0
No files found.
tools/python/CMakeLists.txt
View file @
6c313b89
...
...
@@ -9,4 +9,6 @@ add_python_module(dlib
src/vector.cpp
src/svm_c_trainer.cpp
src/decision_funcions.cpp
src/other.cpp
src/basic.cpp
)
tools/python/src/basic.cpp
0 → 100644
View file @
6c313b89
#include <boost/python.hpp>
#include <dlib/matrix.h>
#include <sstream>
#include <string>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
#include <boost/python/suite/indexing/indexing_suite.hpp>
#include <boost/shared_ptr.hpp>
#include <dlib/string.h>
#include "serialize_pickle.h"
using
namespace
std
;
using
namespace
dlib
;
using
namespace
boost
::
python
;
boost
::
shared_ptr
<
std
::
vector
<
double
>
>
array_from_object
(
object
obj
)
{
extract
<
long
>
thesize
(
obj
);
if
(
thesize
.
check
())
{
long
nr
=
thesize
;
boost
::
shared_ptr
<
std
::
vector
<
double
>
>
temp
(
new
std
::
vector
<
double
>
(
nr
));
return
temp
;
}
else
{
const
long
nr
=
len
(
obj
);
boost
::
shared_ptr
<
std
::
vector
<
double
>
>
temp
(
new
std
::
vector
<
double
>
(
nr
));
for
(
long
r
=
0
;
r
<
nr
;
++
r
)
{
(
*
temp
)[
r
]
=
extract
<
double
>
(
obj
[
r
]);
}
return
temp
;
}
}
string
array__str__
(
const
std
::
vector
<
double
>&
v
)
{
std
::
ostringstream
sout
;
for
(
unsigned
long
i
=
0
;
i
<
v
.
size
();
++
i
)
{
sout
<<
v
[
i
];
if
(
i
+
1
<
v
.
size
())
sout
<<
"
\n
"
;
}
return
sout
.
str
();
}
string
array__repr__
(
const
std
::
vector
<
double
>&
v
)
{
std
::
ostringstream
sout
;
sout
<<
"dlib.array(["
;
for
(
unsigned
long
i
=
0
;
i
<
v
.
size
();
++
i
)
{
sout
<<
v
[
i
];
if
(
i
+
1
<
v
.
size
())
sout
<<
", "
;
}
sout
<<
"])"
;
return
sout
.
str
();
}
string
pair__str__
(
const
std
::
pair
<
unsigned
long
,
double
>&
p
)
{
std
::
ostringstream
sout
;
sout
<<
p
.
first
<<
": "
<<
p
.
second
;
return
sout
.
str
();
}
string
pair__repr__
(
const
std
::
pair
<
unsigned
long
,
double
>&
p
)
{
std
::
ostringstream
sout
;
sout
<<
"dlib.pair("
<<
p
.
first
<<
", "
<<
p
.
second
<<
")"
;
return
sout
.
str
();
}
string
sparse_vector__str__
(
const
std
::
vector
<
std
::
pair
<
unsigned
long
,
double
>
>&
v
)
{
std
::
ostringstream
sout
;
for
(
unsigned
long
i
=
0
;
i
<
v
.
size
();
++
i
)
{
sout
<<
v
[
i
].
first
<<
": "
<<
v
[
i
].
second
;
if
(
i
+
1
<
v
.
size
())
sout
<<
"
\n
"
;
}
return
sout
.
str
();
}
string
sparse_vector__repr__
(
const
std
::
vector
<
std
::
pair
<
unsigned
long
,
double
>
>&
v
)
{
std
::
ostringstream
sout
;
sout
<<
"< dlib.sparse_vector containing:
\n
"
<<
sparse_vector__str__
(
v
)
<<
" >"
;
return
sout
.
str
();
}
void
bind_basic_types
()
{
class_
<
std
::
vector
<
double
>
>
(
"array"
,
init
<>
())
.
def
(
vector_indexing_suite
<
std
::
vector
<
double
>
>
())
.
def
(
"__init__"
,
make_constructor
(
&
array_from_object
))
.
def
(
"__str__"
,
array__str__
)
.
def
(
"__repr__"
,
array__repr__
)
.
def_pickle
(
serialize_pickle
<
std
::
vector
<
double
>
>
());
class_
<
std
::
vector
<
matrix
<
double
,
0
,
1
>
>
>
(
"vectors"
)
.
def
(
vector_indexing_suite
<
std
::
vector
<
matrix
<
double
,
0
,
1
>
>
>
())
.
def_pickle
(
serialize_pickle
<
std
::
vector
<
matrix
<
double
,
0
,
1
>
>
>
());
typedef
pair
<
unsigned
long
,
double
>
pair_type
;
class_
<
pair_type
>
(
"pair"
,
"This object is used to represent the elements of a sparse_vector."
,
init
<>
()
)
.
def
(
init
<
unsigned
long
,
double
>
())
.
def_readwrite
(
"first"
,
&
pair_type
::
first
,
"This field represents the index/dimension number."
)
.
def_readwrite
(
"second"
,
&
pair_type
::
second
,
"This field contains the value in a vector at dimension specified by the first field."
)
.
def
(
"__str__"
,
pair__str__
)
.
def
(
"__repr__"
,
pair__repr__
)
.
def_pickle
(
serialize_pickle
<
pair_type
>
());
class_
<
std
::
vector
<
pair_type
>
>
(
"sparse_vector"
)
.
def
(
vector_indexing_suite
<
std
::
vector
<
pair_type
>
>
())
.
def
(
"__str__"
,
sparse_vector__str__
)
.
def
(
"__repr__"
,
sparse_vector__repr__
)
.
def_pickle
(
serialize_pickle
<
std
::
vector
<
pair_type
>
>
());
class_
<
std
::
vector
<
std
::
vector
<
pair_type
>
>
>
(
"sparse_vectors"
)
.
def
(
vector_indexing_suite
<
std
::
vector
<
std
::
vector
<
pair_type
>
>
>
())
.
def_pickle
(
serialize_pickle
<
std
::
vector
<
std
::
vector
<
pair_type
>
>
>
());
}
tools/python/src/dlib.cpp
View file @
6c313b89
#include <boost/python.hpp>
#include <dlib/matrix.h>
#include <sstream>
#include <string>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
#include <boost/python/suite/indexing/indexing_suite.hpp>
#include <boost/shared_ptr.hpp>
#include <dlib/string.h>
#include "serialize_pickle.h"
using
namespace
std
;
using
namespace
dlib
;
using
namespace
boost
::
python
;
#include <boost/python.hpp>
void
bind_matrix
();
void
bind_vector
();
void
bind_svm_c_trainer
();
void
bind_decision_functions
();
void
bind_basic_types
();
void
bind_other
();
boost
::
shared_ptr
<
std
::
vector
<
double
>
>
array_from_object
(
object
obj
)
{
extract
<
long
>
thesize
(
obj
);
if
(
thesize
.
check
())
{
long
nr
=
thesize
;
boost
::
shared_ptr
<
std
::
vector
<
double
>
>
temp
(
new
std
::
vector
<
double
>
(
nr
));
return
temp
;
}
else
{
const
long
nr
=
len
(
obj
);
boost
::
shared_ptr
<
std
::
vector
<
double
>
>
temp
(
new
std
::
vector
<
double
>
(
nr
));
for
(
long
r
=
0
;
r
<
nr
;
++
r
)
{
(
*
temp
)[
r
]
=
extract
<
double
>
(
obj
[
r
]);
}
return
temp
;
}
}
string
array__str__
(
const
std
::
vector
<
double
>&
v
)
{
std
::
ostringstream
sout
;
for
(
unsigned
long
i
=
0
;
i
<
v
.
size
();
++
i
)
{
sout
<<
v
[
i
];
if
(
i
+
1
<
v
.
size
())
sout
<<
"
\n
"
;
}
return
sout
.
str
();
}
string
array__repr__
(
const
std
::
vector
<
double
>&
v
)
{
std
::
ostringstream
sout
;
sout
<<
"dlib.array(["
;
for
(
unsigned
long
i
=
0
;
i
<
v
.
size
();
++
i
)
{
sout
<<
v
[
i
];
if
(
i
+
1
<
v
.
size
())
sout
<<
", "
;
}
sout
<<
"])"
;
return
sout
.
str
();
}
string
pair__str__
(
const
std
::
pair
<
unsigned
long
,
double
>&
p
)
{
std
::
ostringstream
sout
;
sout
<<
p
.
first
<<
": "
<<
p
.
second
;
return
sout
.
str
();
}
string
pair__repr__
(
const
std
::
pair
<
unsigned
long
,
double
>&
p
)
{
std
::
ostringstream
sout
;
sout
<<
"dlib.pair("
<<
p
.
first
<<
", "
<<
p
.
second
<<
")"
;
return
sout
.
str
();
}
string
sparse_vector__str__
(
const
std
::
vector
<
std
::
pair
<
unsigned
long
,
double
>
>&
v
)
{
std
::
ostringstream
sout
;
for
(
unsigned
long
i
=
0
;
i
<
v
.
size
();
++
i
)
{
sout
<<
v
[
i
].
first
<<
": "
<<
v
[
i
].
second
;
if
(
i
+
1
<
v
.
size
())
sout
<<
"
\n
"
;
}
return
sout
.
str
();
}
string
sparse_vector__repr__
(
const
std
::
vector
<
std
::
pair
<
unsigned
long
,
double
>
>&
v
)
{
std
::
ostringstream
sout
;
sout
<<
"< dlib.sparse_vector containing:
\n
"
<<
sparse_vector__str__
(
v
)
<<
" >"
;
return
sout
.
str
();
}
tuple
get_training_data
()
{
typedef
matrix
<
double
,
0
,
1
>
sample_type
;
std
::
vector
<
sample_type
>
samples
;
std
::
vector
<
double
>
labels
;
sample_type
samp
(
3
);
samp
=
1
,
2
,
3
;
samples
.
push_back
(
samp
);
labels
.
push_back
(
+
1
);
samp
=
-
1
,
-
2
,
-
3
;
samples
.
push_back
(
samp
);
labels
.
push_back
(
-
1
);
return
make_tuple
(
samples
,
labels
);
}
BOOST_PYTHON_MODULE
(
dlib
)
{
...
...
@@ -123,51 +15,6 @@ BOOST_PYTHON_MODULE(dlib)
bind_vector
();
bind_svm_c_trainer
();
bind_decision_functions
();
class_
<
std
::
vector
<
double
>
>
(
"array"
,
init
<>
())
.
def
(
vector_indexing_suite
<
std
::
vector
<
double
>
>
())
.
def
(
"__init__"
,
make_constructor
(
&
array_from_object
))
.
def
(
"__str__"
,
array__str__
)
.
def
(
"__repr__"
,
array__repr__
)
.
def_pickle
(
serialize_pickle
<
std
::
vector
<
double
>
>
());
class_
<
std
::
vector
<
matrix
<
double
,
0
,
1
>
>
>
(
"vectors"
)
.
def
(
vector_indexing_suite
<
std
::
vector
<
matrix
<
double
,
0
,
1
>
>
>
())
.
def_pickle
(
serialize_pickle
<
std
::
vector
<
matrix
<
double
,
0
,
1
>
>
>
());
typedef
pair
<
unsigned
long
,
double
>
pair_type
;
class_
<
pair_type
>
(
"pair"
,
"This object is used to represent the elements of a sparse_vector."
,
init
<>
()
)
.
def
(
init
<
unsigned
long
,
double
>
())
.
def_readwrite
(
"first"
,
&
pair_type
::
first
,
"This field represents the index/dimension number."
)
.
def_readwrite
(
"second"
,
&
pair_type
::
second
,
"This field contains the value in a vector at dimension specified by the first field."
)
.
def
(
"__str__"
,
pair__str__
)
.
def
(
"__repr__"
,
pair__repr__
)
.
def_pickle
(
serialize_pickle
<
pair_type
>
());
class_
<
std
::
vector
<
pair_type
>
>
(
"sparse_vector"
)
.
def
(
vector_indexing_suite
<
std
::
vector
<
pair_type
>
>
())
.
def
(
"__str__"
,
sparse_vector__str__
)
.
def
(
"__repr__"
,
sparse_vector__repr__
)
.
def_pickle
(
serialize_pickle
<
std
::
vector
<
pair_type
>
>
());
class_
<
std
::
vector
<
std
::
vector
<
pair_type
>
>
>
(
"sparse_vectors"
)
.
def
(
vector_indexing_suite
<
std
::
vector
<
std
::
vector
<
pair_type
>
>
>
())
.
def_pickle
(
serialize_pickle
<
std
::
vector
<
std
::
vector
<
pair_type
>
>
>
());
def
(
"get_training_data"
,
get_training_data
);
/*
def("tomat",tomat);
def("add_to_map", add_to_map);
def("getpair", getpair);
def("getmatrix", getmatrix);
def("yay", yay);
def("sum", sum_mat);
def("getmap", getmap);
def("go", go);
def("append_to_vector", append_to_vector);
*/
bind_basic_types
();
bind_other
();
}
tools/python/src/other.cpp
0 → 100644
View file @
6c313b89
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
#include <dlib/matrix.h>
using
namespace
dlib
;
using
namespace
std
;
using
namespace
boost
::
python
;
tuple
get_training_data
()
{
typedef
matrix
<
double
,
0
,
1
>
sample_type
;
std
::
vector
<
sample_type
>
samples
;
std
::
vector
<
double
>
labels
;
sample_type
samp
(
3
);
samp
=
1
,
2
,
3
;
samples
.
push_back
(
samp
);
labels
.
push_back
(
+
1
);
samp
=
-
1
,
-
2
,
-
3
;
samples
.
push_back
(
samp
);
labels
.
push_back
(
-
1
);
return
make_tuple
(
samples
,
labels
);
}
void
bind_other
()
{
def
(
"get_training_data"
,
get_training_data
);
}
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