diff --git a/dlib/image_processing/object_detector.h b/dlib/image_processing/object_detector.h index 9092102fa..92e5d6a4a 100644 --- a/dlib/image_processing/object_detector.h +++ b/dlib/image_processing/object_detector.h @@ -45,6 +45,14 @@ namespace dlib const image_type& img ) const; + template < + typename image_type + > + void operator() ( + const image_type& img, + std::vector >& final_dets + ) const; + template friend void serialize ( const object_detector& item, @@ -72,6 +80,19 @@ namespace dlib return false; } + bool overlaps_any_box ( + const std::vector >& rects, + const dlib::rectangle& rect + ) const + { + for (unsigned long i = 0; i < rects.size(); ++i) + { + if (boxes_overlap(rects[i].second, rect)) + return true; + } + return false; + } + overlap_tester_type boxes_overlap; matrix w; mutable image_scanner_type scanner; @@ -221,6 +242,41 @@ namespace dlib return final_dets; } +// ---------------------------------------------------------------------------------------- + + template < + typename image_scanner_type, + typename overlap_tester_type + > + template < + typename image_type + > + void object_detector:: + operator() ( + const image_type& img, + std::vector >& final_dets + ) const + { + final_dets.clear(); + if (w.size() != 0) + { + std::vector > dets; + const double thresh = w(scanner.get_num_dimensions()); + + scanner.load(img); + scanner.detect(w, dets, thresh); + + for (unsigned long i = 0; i < dets.size(); ++i) + { + if (overlaps_any_box(final_dets, dets[i].second)) + continue; + + dets[i].first -= thresh; + final_dets.push_back(dets[i]); + } + } + } + // ---------------------------------------------------------------------------------------- } diff --git a/dlib/image_processing/object_detector_abstract.h b/dlib/image_processing/object_detector_abstract.h index d44e03d0e..bf76a41cb 100644 --- a/dlib/image_processing/object_detector_abstract.h +++ b/dlib/image_processing/object_detector_abstract.h @@ -99,6 +99,29 @@ namespace dlib element 1 the next best, and so on. !*/ + template < + typename image_type + > + void operator() ( + const image_type& img, + std::vector >& dets + ) const; + /*! + requires + - img == an object which can be accepted by image_scanner_type::load() + ensures + - performs object detection on the given image and stores the + detected objects into #dets. In particular, we will have that: + - #dets is sorted such that the highest confidence detections + come first. E.g. element 0 is the best detection, element 1 + the next best, and so on. + - #dets.size() == the number of detected objects. + - #dets[i].first gives the "detection confidence", of the i-th + detection. This is the detection value output by the scanner + minus the threshold, therefore this is a value > 0. + - #dets[i].second == the bounding box for the i-th detection. + !*/ + }; // ----------------------------------------------------------------------------------------