Commit c4cf31a7 authored by Patrick Snape's avatar Patrick Snape

Preparing for correlation tracker - add drectangle

The correlation tracker deals with the drectangle
(double rectangle) class which currently isn't wrapped. Therefore,
I add the drectangle class and refactor rectangles into their
own file. I also added a load of methods on rectangle that might
be useful such as intersection/contains/area etc.
parent 8568c262
...@@ -18,6 +18,7 @@ set(python_srcs ...@@ -18,6 +18,7 @@ set(python_srcs
src/sequence_segmenter.cpp src/sequence_segmenter.cpp
src/svm_struct.cpp src/svm_struct.cpp
src/image.cpp src/image.cpp
src/rectangles.cpp
src/object_detection.cpp src/object_detection.cpp
src/shape_predictor.cpp src/shape_predictor.cpp
) )
......
// Copyright (C) 2013 Davis E. King (davis@dlib.net) // Copyright (C) 2015 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include <boost/python.hpp> #include <boost/python.hpp>
...@@ -14,6 +14,7 @@ void bind_cca(); ...@@ -14,6 +14,7 @@ void bind_cca();
void bind_sequence_segmenter(); void bind_sequence_segmenter();
void bind_svm_struct(); void bind_svm_struct();
void bind_image_classes(); void bind_image_classes();
void bind_rectangles();
void bind_object_detection(); void bind_object_detection();
void bind_shape_predictors(); void bind_shape_predictors();
...@@ -38,6 +39,7 @@ BOOST_PYTHON_MODULE(dlib) ...@@ -38,6 +39,7 @@ BOOST_PYTHON_MODULE(dlib)
bind_sequence_segmenter(); bind_sequence_segmenter();
bind_svm_struct(); bind_svm_struct();
bind_image_classes(); bind_image_classes();
bind_rectangles();
bind_object_detection(); bind_object_detection();
bind_shape_predictors(); bind_shape_predictors();
#ifndef DLIB_NO_GUI_SUPPORT #ifndef DLIB_NO_GUI_SUPPORT
......
// Copyright (C) 2014 Davis E. King (davis@dlib.net) // Copyright (C) 2015 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license. // License: Boost Software License See LICENSE.txt for the full license.
#include <dlib/python.h> #include <dlib/python.h>
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#include <boost/python/args.hpp> #include <boost/python/args.hpp>
#include <dlib/geometry.h> #include <dlib/geometry.h>
#include <dlib/image_processing/frontal_face_detector.h> #include <dlib/image_processing/frontal_face_detector.h>
#include "indexing.h"
#include "simple_object_detector.h" #include "simple_object_detector.h"
#include "simple_object_detector_py.h" #include "simple_object_detector_py.h"
#include "conversion.h" #include "conversion.h"
...@@ -26,29 +25,6 @@ string print_simple_test_results(const simple_test_results& r) ...@@ -26,29 +25,6 @@ string print_simple_test_results(const simple_test_results& r)
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
long left(const rectangle& r) { return r.left(); }
long top(const rectangle& r) { return r.top(); }
long right(const rectangle& r) { return r.right(); }
long bottom(const rectangle& r) { return r.bottom(); }
long width(const rectangle& r) { return r.width(); }
long height(const rectangle& r) { return r.height(); }
string print_rectangle_str(const rectangle& r)
{
std::ostringstream sout;
sout << r;
return sout.str();
}
string print_rectangle_repr(const rectangle& r)
{
std::ostringstream sout;
sout << "rectangle(" << r.left() << "," << r.top() << "," << r.right() << "," << r.bottom() << ")";
return sout.str();
}
// ----------------------------------------------------------------------------------------
inline simple_object_detector_py train_simple_object_detector_on_images_py ( inline simple_object_detector_py train_simple_object_detector_on_images_py (
const boost::python::list& pyimages, const boost::python::list& pyimages,
const boost::python::list& pyboxes, const boost::python::list& pyboxes,
...@@ -153,55 +129,45 @@ inline void find_candidate_object_locations_py ( ...@@ -153,55 +129,45 @@ inline void find_candidate_object_locations_py (
void bind_object_detection() void bind_object_detection()
{ {
using boost::python::arg; using boost::python::arg;
{
class_<simple_object_detector_training_options>("simple_object_detector_training_options", typedef simple_object_detector_training_options type;
class_<type>("simple_object_detector_training_options",
"This object is a container for the options to the train_simple_object_detector() routine.") "This object is a container for the options to the train_simple_object_detector() routine.")
.add_property("be_verbose", &simple_object_detector_training_options::be_verbose, .add_property("be_verbose", &type::be_verbose,
&simple_object_detector_training_options::be_verbose, &type::be_verbose,
"If true, train_simple_object_detector() will print out a lot of information to the screen while training.") "If true, train_simple_object_detector() will print out a lot of information to the screen while training.")
.add_property("add_left_right_image_flips", &simple_object_detector_training_options::add_left_right_image_flips, .add_property("add_left_right_image_flips", &type::add_left_right_image_flips,
&simple_object_detector_training_options::add_left_right_image_flips, &type::add_left_right_image_flips,
"if true, train_simple_object_detector() will assume the objects are \n\ "if true, train_simple_object_detector() will assume the objects are \n\
left/right symmetric and add in left right flips of the training \n\ left/right symmetric and add in left right flips of the training \n\
images. This doubles the size of the training dataset.") images. This doubles the size of the training dataset.")
.add_property("detection_window_size", &simple_object_detector_training_options::detection_window_size, .add_property("detection_window_size", &type::detection_window_size,
&simple_object_detector_training_options::detection_window_size, &type::detection_window_size,
"The sliding window used will have about this many pixels inside it.") "The sliding window used will have about this many pixels inside it.")
.add_property("C", &simple_object_detector_training_options::C, .add_property("C", &type::C,
&simple_object_detector_training_options::C, &type::C,
"C is the usual SVM C regularization parameter. So it is passed to \n\ "C is the usual SVM C regularization parameter. So it is passed to \n\
structural_object_detection_trainer::set_c(). Larger values of C \n\ structural_object_detection_trainer::set_c(). Larger values of C \n\
will encourage the trainer to fit the data better but might lead to \n\ will encourage the trainer to fit the data better but might lead to \n\
overfitting. Therefore, you must determine the proper setting of \n\ overfitting. Therefore, you must determine the proper setting of \n\
this parameter experimentally.") this parameter experimentally.")
.add_property("epsilon", &simple_object_detector_training_options::epsilon, .add_property("epsilon", &type::epsilon,
&simple_object_detector_training_options::epsilon, &type::epsilon,
"epsilon is the stopping epsilon. Smaller values make the trainer's \n\ "epsilon is the stopping epsilon. Smaller values make the trainer's \n\
solver more accurate but might take longer to train.") solver more accurate but might take longer to train.")
.add_property("num_threads", &simple_object_detector_training_options::num_threads, .add_property("num_threads", &type::num_threads,
&simple_object_detector_training_options::num_threads, &type::num_threads,
"train_simple_object_detector() will use this many threads of \n\ "train_simple_object_detector() will use this many threads of \n\
execution. Set this to the number of CPU cores on your machine to \n\ execution. Set this to the number of CPU cores on your machine to \n\
obtain the fastest training speed."); obtain the fastest training speed.");
}
class_<simple_test_results>("simple_test_results")
.add_property("precision", &simple_test_results::precision)
.add_property("recall", &simple_test_results::recall)
.add_property("average_precision", &simple_test_results::average_precision)
.def("__str__", &::print_simple_test_results);
{ {
typedef rectangle type; typedef simple_test_results type;
class_<type>("rectangle", "This object represents a rectangular area of an image.") class_<type>("simple_test_results")
.def(init<long,long,long,long>( (arg("left"),arg("top"),arg("right"),arg("bottom")) )) .add_property("precision", &type::precision)
.def("left", &::left) .add_property("recall", &type::recall)
.def("top", &::top) .add_property("average_precision", &type::average_precision)
.def("right", &::right) .def("__str__", &::print_simple_test_results);
.def("bottom", &::bottom)
.def("width", &::width)
.def("height", &::height)
.def("__str__", &::print_rectangle_str)
.def("__repr__", &::print_rectangle_repr)
.def_pickle(serialize_pickle<type>());
} }
// Here, kvals is actually the result of linspace(start, end, num) and it is different from kvals used // Here, kvals is actually the result of linspace(start, end, num) and it is different from kvals used
...@@ -398,14 +364,6 @@ ensures \n\ ...@@ -398,14 +364,6 @@ ensures \n\
.def("save", save_simple_object_detector_py, (arg("detector_output_filename")), "Save a simple_object_detector to the provided path.") .def("save", save_simple_object_detector_py, (arg("detector_output_filename")), "Save a simple_object_detector to the provided path.")
.def_pickle(serialize_pickle<type>()); .def_pickle(serialize_pickle<type>());
} }
{
typedef std::vector<rectangle> type;
class_<type>("rectangles", "An array of rectangle objects.")
.def(vector_indexing_suite<type>())
.def("clear", &type::clear)
.def("resize", resize<type>)
.def_pickle(serialize_pickle<type>());
}
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
// Copyright (C) 2015 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#include <dlib/python.h>
#include <boost/python/args.hpp>
#include <dlib/geometry.h>
#include "indexing.h"
using namespace dlib;
using namespace std;
using namespace boost::python;
// ----------------------------------------------------------------------------------------
long left(const rectangle& r) { return r.left(); }
long top(const rectangle& r) { return r.top(); }
long right(const rectangle& r) { return r.right(); }
long bottom(const rectangle& r) { return r.bottom(); }
long width(const rectangle& r) { return r.width(); }
long height(const rectangle& r) { return r.height(); }
unsigned long area(const rectangle& r) { return r.area(); }
double dleft(const drectangle& r) { return r.left(); }
double dtop(const drectangle& r) { return r.top(); }
double dright(const drectangle& r) { return r.right(); }
double dbottom(const drectangle& r) { return r.bottom(); }
double dwidth(const drectangle& r) { return r.width(); }
double dheight(const drectangle& r) { return r.height(); }
double darea(const drectangle& r) { return r.area(); }
template <typename rect_type>
bool is_empty(const rect_type& r) { return r.is_empty(); }
template <typename rect_type>
point center(const rect_type& r) { return center(r); }
template <typename rect_type>
point dcenter(const rect_type& r) { return dcenter(r); }
template <typename rect_type>
bool contains(const rect_type& r, const point& p) { return r.contains(p); }
template <typename rect_type>
bool contains_xy(const rect_type& r, const long x, const long y) { return r.contains(point(x, y)); }
template <typename rect_type>
bool contains_rec(const rect_type& r, const rect_type& r2) { return r.contains(r2); }
template <typename rect_type>
rect_type intersect(const rect_type& r, const rect_type& r2) { return r.intersect(r2); }
template <typename rect_type>
string print_rectangle_str(const rect_type& r)
{
std::ostringstream sout;
sout << r;
return sout.str();
}
template <typename rect_type>
string print_rectangle_repr(const rect_type& r)
{
std::ostringstream sout;
sout << "rectangle(" << r.left() << "," << r.top() << "," << r.right() << "," << r.bottom() << ")";
return sout.str();
}
// ----------------------------------------------------------------------------------------
void bind_rectangles()
{
using boost::python::arg;
{
typedef rectangle type;
class_<type>("rectangle", "This object represents a rectangular area of an image.")
.def(init<long,long,long,long>( (arg("left"),arg("top"),arg("right"),arg("bottom")) ))
.def("area", &::area)
.def("left", &::left)
.def("top", &::top)
.def("right", &::right)
.def("bottom", &::bottom)
.def("width", &::width)
.def("height", &::height)
.def("is_empty", &::is_empty<type>)
.def("center", &::center<type>)
.def("dcenter", &::dcenter<type>)
.def("contains", &::contains<type>, arg("point"))
.def("contains", &::contains_xy<type>, (arg("x"), arg("y")))
.def("contains", &::contains_rec<type>, (arg("rectangle")))
.def("intersect", &::intersect<type>, (arg("rectangle")))
.def("__str__", &::print_rectangle_str<type>)
.def("__repr__", &::print_rectangle_repr<type>)
.def_pickle(serialize_pickle<type>());
}
{
typedef drectangle type;
class_<type>("drectangle", "This object represents a rectangular area of an image with floating point coordinates.")
.def(init<double,double,double,double>( (arg("left"),arg("top"),arg("right"),arg("bottom")) ))
.def("area", &::darea)
.def("left", &::dleft)
.def("top", &::dtop)
.def("right", &::dright)
.def("bottom", &::dbottom)
.def("width", &::dwidth)
.def("height", &::dheight)
.def("is_empty", &::is_empty<type>)
.def("center", &::center<type>)
.def("dcenter", &::dcenter<type>)
.def("contains", &::contains<type>, arg("point"))
.def("contains", &::contains_xy<type>, (arg("x"), arg("y")))
.def("contains", &::contains_rec<type>, (arg("rectangle")))
.def("intersect", &::intersect<type>, (arg("rectangle")))
.def("__str__", &::print_rectangle_str<type>)
.def("__repr__", &::print_rectangle_repr<type>)
.def_pickle(serialize_pickle<type>());
}
{
typedef std::vector<rectangle> type;
class_<type>("rectangles", "An array of rectangle objects.")
.def(vector_indexing_suite<type>())
.def("clear", &type::clear)
.def("resize", resize<type>)
.def_pickle(serialize_pickle<type>());
}
}
// ----------------------------------------------------------------------------------------
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment