Commit 70db61c5 authored by Patrick Snape's avatar Patrick Snape

First attempt at adding to the boost API

Exposes the test method, but takes in images and bounding boxes.
Seems fairly simply to extend the API
parent 8cd11003
...@@ -222,20 +222,14 @@ string print_simple_test_results(const simple_test_results& r) ...@@ -222,20 +222,14 @@ string print_simple_test_results(const simple_test_results& r)
return sout.str(); return sout.str();
} }
inline void train_simple_object_detector_on_images_py ( void python_detections_to_dlib(
const object& pyimages, const object& pyimages,
const object& pyboxes, const object& pyboxes,
const std::string& detector_output_filename, dlib::array<array2d<rgb_pixel> >& images,
const simple_object_detector_training_options& options std::vector<std::vector<rectangle> >& boxes
) )
{ {
const unsigned long num_images = len(pyimages); const unsigned long num_images = len(pyimages);
if (num_images != len(pyboxes))
throw dlib::error("The length of the boxes list must match the length of the images list.");
// We never have any ignore boxes for this version of the API.
std::vector<std::vector<rectangle> > ignore(num_images), boxes(num_images);
dlib::array<array2d<rgb_pixel> > images(num_images);
// Now copy the data into dlib based objects so we can call the trainer. // Now copy the data into dlib based objects so we can call the trainer.
for (unsigned long i = 0; i < num_images; ++i) for (unsigned long i = 0; i < num_images; ++i)
{ {
...@@ -251,10 +245,45 @@ inline void train_simple_object_detector_on_images_py ( ...@@ -251,10 +245,45 @@ inline void train_simple_object_detector_on_images_py (
else else
throw dlib::error("Unsupported image type, must be 8bit gray or RGB image."); throw dlib::error("Unsupported image type, must be 8bit gray or RGB image.");
} }
}
inline void train_simple_object_detector_on_images_py (
const object& pyimages,
const object& pyboxes,
const std::string& detector_output_filename,
const simple_object_detector_training_options& options
)
{
const unsigned long num_images = len(pyimages);
if (num_images != len(pyboxes))
throw dlib::error("The length of the boxes list must match the length of the images list.");
// We never have any ignore boxes for this version of the API.
std::vector<std::vector<rectangle> > ignore(num_images), boxes(num_images);
dlib::array<array2d<rgb_pixel> > images(num_images);
python_detections_to_dlib(pyimages, pyboxes, images, boxes);
train_simple_object_detector_on_images("", images, boxes, ignore, detector_output_filename, options); train_simple_object_detector_on_images("", images, boxes, ignore, detector_output_filename, options);
} }
inline simple_test_results test_simple_object_detector_with_images_py (
const object& pyimages,
const object& pyboxes,
const std::string& detector_filename
)
{
const unsigned long num_images = len(pyimages);
if (num_images != len(pyboxes))
throw dlib::error("The length of the boxes list must match the length of the images list.");
// We never have any ignore boxes for this version of the API.
std::vector<std::vector<rectangle> > ignore(num_images), boxes(num_images);
dlib::array<array2d<rgb_pixel> > images(num_images);
python_detections_to_dlib(pyimages, pyboxes, images, boxes);
return test_simple_object_detector_with_images(images, boxes, ignore, detector_filename);
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
void bind_object_detection() void bind_object_detection()
...@@ -410,7 +439,7 @@ ensures \n\ ...@@ -410,7 +439,7 @@ ensures \n\
def("test_simple_object_detector", test_simple_object_detector, def("test_simple_object_detector", test_simple_object_detector,
(arg("dataset_filename"), arg("detector_filename")), (arg("dataset_filename"), arg("detector_filename")),
"ensures \n\ "ensures \n\
- Loads an image dataset from dataset_filename. We assume dataset_filename is \n\ - Loads an image dataset from dataset_filename. We assume dataset_filename is \n\
a file using the XML format written by save_image_dataset_metadata(). \n\ a file using the XML format written by save_image_dataset_metadata(). \n\
- Loads a simple_object_detector from the file detector_filename. This means \n\ - Loads a simple_object_detector from the file detector_filename. This means \n\
...@@ -438,6 +467,23 @@ ensures \n\ ...@@ -438,6 +467,23 @@ ensures \n\
!*/ !*/
); );
def("test_simple_object_detector", test_simple_object_detector_with_images_py,
(arg("images"), arg("boxes"), arg("detector_filename")),
"requires \n\
- len(images) == len(boxes) \n\
- images should be a list of numpy matrices that represent images, either RGB or grayscale. \n\
- boxes should be a list of lists of dlib.rectangle object. \n\
ensures \n\
- Loads a simple_object_detector from the file detector_filename. This means \n\
detector_filename should be a file produced by the train_simple_object_detector() \n\
routine. \n\
- This function tests the detector against the dataset and returns the \n\
precision, recall, and average precision of the detector. In fact, The \n\
return value of this function is identical to that of dlib's \n\
test_object_detection_function() routine. Therefore, see the documentation \n\
for test_object_detection_function() for a detailed definition of these \n\
metrics. "
);
{ {
typedef simple_object_detector_py type; typedef simple_object_detector_py type;
class_<type>("simple_object_detector", class_<type>("simple_object_detector",
......
...@@ -252,15 +252,14 @@ namespace dlib ...@@ -252,15 +252,14 @@ namespace dlib
double average_precision; double average_precision;
}; };
inline const simple_test_results test_simple_object_detector ( template <typename image_array>
const std::string& dataset_filename, inline const simple_test_results test_simple_object_detector_with_images (
image_array& images,
std::vector<std::vector<rectangle> >& boxes,
std::vector<std::vector<rectangle> >& ignore,
const std::string& detector_filename const std::string& detector_filename
) )
{ {
dlib::array<array2d<rgb_pixel> > images;
std::vector<std::vector<rectangle> > boxes, ignore;
ignore = load_image_dataset(images, boxes, dataset_filename);
simple_object_detector detector; simple_object_detector detector;
int version = 0; int version = 0;
unsigned int upsample_amount = 0; unsigned int upsample_amount = 0;
...@@ -284,6 +283,18 @@ namespace dlib ...@@ -284,6 +283,18 @@ namespace dlib
return ret; return ret;
} }
inline const simple_test_results test_simple_object_detector (
const std::string& dataset_filename,
const std::string& detector_filename
)
{
dlib::array<array2d<rgb_pixel> > images;
std::vector<std::vector<rectangle> > boxes, ignore;
ignore = load_image_dataset(images, boxes, dataset_filename);
return test_simple_object_detector_with_images(images, boxes, ignore, detector_filename);
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
} }
......
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