Add detection threshold adjustment to object detection python interface (#140)

* Add cmake option to use external libjpeg on Mac OS

* Add adjust_threshold to python object detector

* Add cmake option to use external libjpeg on Mac OS

* Add adjust_threshold to python object detector

* Revert "Add cmake option to use external libjpeg on Mac OS"

This reverts commit 01f7fd13ea.

* Update detector example to set adjust_threshold
pull/141/head
nxwhite-str 8 years ago committed by Davis E. King
parent ea9cba7eeb
commit b53e9cf010

@ -71,11 +71,13 @@ for f in sys.argv[1:]:
# Finally, if you really want to you can ask the detector to tell you the score # Finally, if you really want to you can ask the detector to tell you the score
# for each detection. The score is bigger for more confident detections. # for each detection. The score is bigger for more confident detections.
# The third argument to run is an optional adjustment to the detection threshold,
# where a negative value will return more detections and a positive value fewer.
# Also, the idx tells you which of the face sub-detectors matched. This can be # Also, the idx tells you which of the face sub-detectors matched. This can be
# used to broadly identify faces in different orientations. # used to broadly identify faces in different orientations.
if (len(sys.argv[1:]) > 0): if (len(sys.argv[1:]) > 0):
img = io.imread(sys.argv[1]) img = io.imread(sys.argv[1])
dets, scores, idx = detector.run(img, 1) dets, scores, idx = detector.run(img, 1, -1)
for i, d in enumerate(dets): for i, d in enumerate(dets):
print("Detection {}, score: {}, face_type:{}".format( print("Detection {}, score: {}, face_type:{}".format(
d, scores[i], idx[i])) d, scores[i], idx[i]))

@ -328,7 +328,7 @@ ensures \n\
detector. If you don't know how many times you want to upsample then \n\ detector. If you don't know how many times you want to upsample then \n\
don't provide a value for upsample_num_times and an appropriate \n\ don't provide a value for upsample_num_times and an appropriate \n\
default will be used.") default will be used.")
.def("run", run_rect_detector, (arg("image"), arg("upsample_num_times")=0), .def("run", run_rect_detector, (arg("image"), arg("upsample_num_times")=0, arg("adjust_threshold")=0.0),
"requires \n\ "requires \n\
- image is a numpy ndarray containing either an 8bit grayscale or RGB \n\ - image is a numpy ndarray containing either an 8bit grayscale or RGB \n\
image. \n\ image. \n\
@ -350,7 +350,7 @@ ensures \n\
.def("__init__", make_constructor(&load_object_from_file<type>), .def("__init__", make_constructor(&load_object_from_file<type>),
"Loads a simple_object_detector from a file that contains the output of the \n\ "Loads a simple_object_detector from a file that contains the output of the \n\
train_simple_object_detector() routine.") train_simple_object_detector() routine.")
.def("__call__", &type::run_detector1, (arg("image"), arg("upsample_num_times")), .def("__call__", &type::run_detector1, (arg("image"), arg("upsample_num_times"), arg("adjust_threshold")=0.0),
"requires \n\ "requires \n\
- image is a numpy ndarray containing either an 8bit grayscale or RGB \n\ - image is a numpy ndarray containing either an 8bit grayscale or RGB \n\
image. \n\ image. \n\

@ -37,6 +37,7 @@ namespace dlib
dlib::simple_object_detector& detector, dlib::simple_object_detector& detector,
boost::python::object img, boost::python::object img,
const unsigned int upsampling_amount, const unsigned int upsampling_amount,
const double adjust_threshold,
std::vector<double>& detection_confidences, std::vector<double>& detection_confidences,
std::vector<double>& weight_indices std::vector<double>& weight_indices
) )
@ -51,7 +52,7 @@ namespace dlib
array2d<unsigned char> temp; array2d<unsigned char> temp;
if (upsampling_amount == 0) if (upsampling_amount == 0)
{ {
detector(numpy_gray_image(img), rect_detections, 0.0); detector(numpy_gray_image(img), rect_detections, adjust_threshold);
split_rect_detections(rect_detections, rectangles, split_rect_detections(rect_detections, rectangles,
detection_confidences, weight_indices); detection_confidences, weight_indices);
return rectangles; return rectangles;
@ -66,7 +67,7 @@ namespace dlib
pyramid_up(temp); pyramid_up(temp);
} }
detector(temp, rect_detections, 0.0); detector(temp, rect_detections, adjust_threshold);
for (unsigned long i = 0; i < rect_detections.size(); ++i) for (unsigned long i = 0; i < rect_detections.size(); ++i)
rect_detections[i].rect = pyr.rect_down(rect_detections[i].rect, rect_detections[i].rect = pyr.rect_down(rect_detections[i].rect,
upsampling_amount); upsampling_amount);
@ -81,7 +82,7 @@ namespace dlib
array2d<rgb_pixel> temp; array2d<rgb_pixel> temp;
if (upsampling_amount == 0) if (upsampling_amount == 0)
{ {
detector(numpy_rgb_image(img), rect_detections, 0.0); detector(numpy_rgb_image(img), rect_detections, adjust_threshold);
split_rect_detections(rect_detections, rectangles, split_rect_detections(rect_detections, rectangles,
detection_confidences, weight_indices); detection_confidences, weight_indices);
return rectangles; return rectangles;
@ -96,7 +97,7 @@ namespace dlib
pyramid_up(temp); pyramid_up(temp);
} }
detector(temp, rect_detections, 0.0); detector(temp, rect_detections, adjust_threshold);
for (unsigned long i = 0; i < rect_detections.size(); ++i) for (unsigned long i = 0; i < rect_detections.size(); ++i)
rect_detections[i].rect = pyr.rect_down(rect_detections[i].rect, rect_detections[i].rect = pyr.rect_down(rect_detections[i].rect,
upsampling_amount); upsampling_amount);
@ -116,19 +117,23 @@ namespace dlib
dlib::simple_object_detector& detector, dlib::simple_object_detector& detector,
boost::python::object img, boost::python::object img,
const unsigned int upsampling_amount const unsigned int upsampling_amount
) )
{ {
std::vector<double> detection_confidences; std::vector<double> detection_confidences;
std::vector<double> weight_indices; std::vector<double> weight_indices;
const double adjust_threshold = 0.0;
return run_detector_with_upscale1(detector, img, upsampling_amount, return run_detector_with_upscale1(detector, img, upsampling_amount,
adjust_threshold,
detection_confidences, weight_indices); detection_confidences, weight_indices);
} }
inline boost::python::tuple run_rect_detector ( inline boost::python::tuple run_rect_detector (
dlib::simple_object_detector& detector, dlib::simple_object_detector& detector,
boost::python::object img, boost::python::object img,
const unsigned int upsampling_amount) const unsigned int upsampling_amount,
const double adjust_threshold)
{ {
boost::python::tuple t; boost::python::tuple t;
@ -137,6 +142,7 @@ namespace dlib
std::vector<rectangle> rectangles; std::vector<rectangle> rectangles;
rectangles = run_detector_with_upscale1(detector, img, upsampling_amount, rectangles = run_detector_with_upscale1(detector, img, upsampling_amount,
adjust_threshold,
detection_confidences, weight_indices); detection_confidences, weight_indices);
return boost::python::make_tuple(rectangles, return boost::python::make_tuple(rectangles,

Loading…
Cancel
Save