2015-01-03 14:19:35 +08:00
|
|
|
#!/usr/bin/python
|
2015-01-04 12:33:46 +08:00
|
|
|
#
|
|
|
|
# This example shows how to use find_candidate_object_locations(). The
|
|
|
|
# function takes an input image and generates a set of candidate rectangles
|
|
|
|
# which are expected to bound any objects in the image.
|
|
|
|
# It is based on the paper:
|
|
|
|
# Segmentation as Selective Search for Object Recognition by Koen E. A. van de Sande, et al.
|
|
|
|
#
|
|
|
|
# Typically, you would use this as part of an object detection pipeline.
|
|
|
|
# find_candidate_object_locations() nominates boxes that might contain an
|
|
|
|
# object and you then run some expensive classifier on each one and throw away
|
|
|
|
# the false alarms. Since find_candidate_object_locations() will only generate
|
|
|
|
# a few thousand rectangles it is much faster than scanning all possible
|
|
|
|
# rectangles inside an image.
|
2015-03-08 03:14:47 +08:00
|
|
|
#
|
2015-10-27 20:25:43 +08:00
|
|
|
#
|
|
|
|
# COMPILING/INSTALLING THE DLIB PYTHON INTERFACE
|
|
|
|
# You can install dlib using the command:
|
|
|
|
# pip install dlib
|
|
|
|
#
|
|
|
|
# Alternatively, if you want to compile dlib yourself then go into the dlib
|
|
|
|
# root folder and run:
|
|
|
|
# python setup.py install
|
|
|
|
#
|
|
|
|
# Compiling dlib should work on any operating system so long as you have
|
2018-01-23 08:23:01 +08:00
|
|
|
# CMake installed. On Ubuntu, this can be done easily by running the
|
|
|
|
# command:
|
|
|
|
# sudo apt-get install cmake
|
2015-10-27 20:25:43 +08:00
|
|
|
#
|
2018-04-18 10:49:25 +08:00
|
|
|
# Also note that this example requires Numpy which can be installed
|
2015-03-08 03:14:47 +08:00
|
|
|
# via the command:
|
2018-04-18 10:49:25 +08:00
|
|
|
# pip install numpy
|
2015-01-03 14:19:35 +08:00
|
|
|
|
|
|
|
import dlib
|
|
|
|
|
|
|
|
image_file = '../examples/faces/2009_004587.jpg'
|
2018-04-18 10:49:25 +08:00
|
|
|
img = dlib.load_rgb_image(image_file)
|
2015-01-03 14:19:35 +08:00
|
|
|
|
|
|
|
# Locations of candidate objects will be saved into rects
|
|
|
|
rects = []
|
|
|
|
dlib.find_candidate_object_locations(img, rects, min_size=500)
|
|
|
|
|
2015-01-04 12:33:46 +08:00
|
|
|
print("number of rectangles found {}".format(len(rects)))
|
|
|
|
for k, d in enumerate(rects):
|
|
|
|
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
|
|
|
|
k, d.left(), d.top(), d.right(), d.bottom()))
|