From ca568f54dabc05e57f10a8b08475d7d559668f4b Mon Sep 17 00:00:00 2001 From: Patrick Snape Date: Wed, 3 Feb 2016 10:56:59 +0000 Subject: [PATCH 1/4] Add equality to drectangle This is in the main dlib source code (rather than specifically Python). Equality operators were missing for drectangles and so were copied from rectangle. --- dlib/geometry/drectangle.h | 13 +++++++++++++ dlib/geometry/drectangle_abstract.h | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/dlib/geometry/drectangle.h b/dlib/geometry/drectangle.h index e42deb3d6..a4a5f238d 100644 --- a/dlib/geometry/drectangle.h +++ b/dlib/geometry/drectangle.h @@ -195,6 +195,19 @@ namespace dlib return *this; } + bool operator== ( + const drectangle& rect + ) const + { + return (l == rect.l) && (t == rect.t) && (r == rect.r) && (b == rect.b); + } + + bool operator!= ( + const drectangle& rect + ) const + { + return !(*this == rect); + } private: double l; diff --git a/dlib/geometry/drectangle_abstract.h b/dlib/geometry/drectangle_abstract.h index cbec110eb..c6b3a8d9e 100644 --- a/dlib/geometry/drectangle_abstract.h +++ b/dlib/geometry/drectangle_abstract.h @@ -319,6 +319,25 @@ namespace dlib - returns #*this !*/ + bool operator== ( + const drectangle& rect + ) const; + /*! + ensures + - if (top() == rect.top() && left() == rect.left() && + right() == rect.right() && bottom() == rect.bottom()) then + - returns true + - else + - returns false + !*/ + + bool operator!= ( + const drectangle& rect + ) const; + /*! + ensures + - returns !(*this == rect) + !*/ }; // ---------------------------------------------------------------------------------------- From ad882c4a31b467f31ba6304d63a1c690adc67ee3 Mon Sep 17 00:00:00 2001 From: Patrick Snape Date: Wed, 3 Feb 2016 10:57:39 +0000 Subject: [PATCH 2/4] Propagate equality operators through to Python Easy change to allow comparisons on the Python side for both rectangle and drectangle --- tools/python/src/rectangles.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/python/src/rectangles.cpp b/tools/python/src/rectangles.cpp index c15eba67a..2e9f21407 100644 --- a/tools/python/src/rectangles.cpp +++ b/tools/python/src/rectangles.cpp @@ -90,6 +90,8 @@ void bind_rectangles() .def("intersect", &::intersect, (arg("rectangle"))) .def("__str__", &::print_rectangle_str) .def("__repr__", &::print_rectangle_repr) + .def(self == self) + .def(self != self) .def_pickle(serialize_pickle()); } { @@ -112,6 +114,8 @@ void bind_rectangles() .def("intersect", &::intersect, (arg("rectangle"))) .def("__str__", &::print_rectangle_str) .def("__repr__", &::print_rectangle_repr) + .def(self == self) + .def(self != self) .def_pickle(serialize_pickle()); } { From d7cac787579cadb703b8621b410e1a1e59a1ce65 Mon Sep 17 00:00:00 2001 From: Patrick Snape Date: Wed, 3 Feb 2016 10:58:21 +0000 Subject: [PATCH 3/4] Allow serilization and printing of shape_predictor_training_options Add a simple print and serialization scheme for shape_predictor_training_options. This enables you to serialize your training options. --- tools/python/src/shape_predictor.cpp | 4 +- tools/python/src/shape_predictor.h | 69 ++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/tools/python/src/shape_predictor.cpp b/tools/python/src/shape_predictor.cpp index 8affb3695..c05f22b72 100644 --- a/tools/python/src/shape_predictor.cpp +++ b/tools/python/src/shape_predictor.cpp @@ -210,7 +210,9 @@ void bind_shape_predictors() e.g a padding of 0.5 would cause the algorithm to sample pixels from a box that was 2x2 pixels") .add_property("random_seed", &type::random_seed, &type::random_seed, - "The random seed used by the internal random number generator"); + "The random seed used by the internal random number generator") + .def("__str__", &::print_shape_predictor_training_options) + .def_pickle(serialize_pickle()); } { typedef shape_predictor type; diff --git a/tools/python/src/shape_predictor.h b/tools/python/src/shape_predictor.h index 4581aa699..93b13bb65 100644 --- a/tools/python/src/shape_predictor.h +++ b/tools/python/src/shape_predictor.h @@ -45,6 +45,75 @@ namespace dlib std::string random_seed; }; + inline void serialize ( + const shape_predictor_training_options& item, + std::ostream& out + ) + { + try + { + serialize(item.be_verbose,out); + serialize(item.cascade_depth,out); + serialize(item.tree_depth,out); + serialize(item.num_trees_per_cascade_level,out); + serialize(item.nu,out); + serialize(item.oversampling_amount,out); + serialize(item.feature_pool_size,out); + serialize(item.lambda_param,out); + serialize(item.num_test_splits,out); + serialize(item.feature_pool_region_padding,out); + serialize(item.random_seed,out); + } + catch (serialization_error& e) + { + throw serialization_error(e.info + "\n while serializing an object of type shape_predictor_training_options"); + } + } + + inline void deserialize ( + shape_predictor_training_options& item, + std::istream& in + ) + { + try + { + deserialize(item.be_verbose,in); + deserialize(item.cascade_depth,in); + deserialize(item.tree_depth,in); + deserialize(item.num_trees_per_cascade_level,in); + deserialize(item.nu,in); + deserialize(item.oversampling_amount,in); + deserialize(item.feature_pool_size,in); + deserialize(item.lambda_param,in); + deserialize(item.num_test_splits,in); + deserialize(item.feature_pool_region_padding,in); + deserialize(item.random_seed,in); + } + catch (serialization_error& e) + { + throw serialization_error(e.info + "\n while deserializing an object of type shape_predictor_training_options"); + } + } + + string print_shape_predictor_training_options(const shape_predictor_training_options& o) + { + std::ostringstream sout; + sout << "shape_predictor_training_options(" + << "be_verbose=" << o.be_verbose << "," + << "cascade_depth=" << o.cascade_depth << "," + << "tree_depth=" << o.tree_depth << "," + << "num_trees_per_cascade_level=" << o.num_trees_per_cascade_level << "," + << "nu=" << o.nu << "," + << "oversampling_amount=" << o.oversampling_amount << "," + << "feature_pool_size=" << o.feature_pool_size << "," + << "lambda_param=" << o.lambda_param << "," + << "num_test_splits=" << o.num_test_splits << "," + << "feature_pool_region_padding=" << o.feature_pool_region_padding << "," + << "random_seed=" << o.random_seed + << ")"; + return sout.str(); + } + // ---------------------------------------------------------------------------------------- namespace impl From 86e93291a0a2f914cb347297e50d941505ae1cb6 Mon Sep 17 00:00:00 2001 From: Patrick Snape Date: Wed, 3 Feb 2016 10:59:04 +0000 Subject: [PATCH 4/4] Add __version__ to dlib module Only issue is that the strong is hardcoded - otherwise this is actually technically required by PEP 396 --- tools/python/src/dlib.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/python/src/dlib.cpp b/tools/python/src/dlib.cpp index 4d9b279d8..ebc0f91c6 100644 --- a/tools/python/src/dlib.cpp +++ b/tools/python/src/dlib.cpp @@ -29,6 +29,8 @@ BOOST_PYTHON_MODULE(dlib) // since it is full of huge amounts of template clutter. boost::python::docstring_options options(true,true,false); + boost::python::scope().attr("__version__") = "18.18"; + bind_matrix(); bind_vector(); bind_svm_c_trainer();