diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 0b0681616..e3ae4383e 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -122,6 +122,7 @@ if (NOT USING_OLD_VISUAL_STUDIO_COMPILER) add_gui_example(dnn_mmod_dog_hipsterizer) add_gui_example(dnn_imagenet_ex) add_gui_example(dnn_mmod_find_cars_ex) + add_gui_example(dnn_mmod_find_cars2_ex) add_example(dnn_mmod_train_find_cars_ex) if (NOT MSVC) # Don't try to compile this program using Visual Studio since it causes the diff --git a/examples/dnn_mmod_find_cars2_ex.cpp b/examples/dnn_mmod_find_cars2_ex.cpp new file mode 100644 index 000000000..65a919e11 --- /dev/null +++ b/examples/dnn_mmod_find_cars2_ex.cpp @@ -0,0 +1,96 @@ +// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt +/* + This example shows how to run a CNN based vehicle detector using dlib. The + example loads a pretrained model and uses it to find the front and rear ends + of cars in an image. The model used by this example was trained by the + dnn_mmod_train_find_cars_ex.cpp example program on this dataset: + http://dlib.net/files/data/dlib_front_and_rear_vehicles_v1.tar + + Users who are just learning about dlib's deep learning API should read + the dnn_introduction_ex.cpp and dnn_introduction2_ex.cpp examples to learn + how the API works. For an introduction to the object detection method you + should read dnn_mmod_ex.cpp. + + You can also see a video of this vehicle detector running on YouTube: + https://www.youtube.com/watch?v=OHbJ7HhbG74 +*/ + + +#include +#include +#include +#include +#include + +using namespace std; +using namespace dlib; + + + +// The rear view vehicle detector network +template using con5d = con; +template using con5 = con; +template using downsampler = relu>>>>>>>>; +template using rcon5 = relu>>; +using net_type = loss_mmod>>>>>>>; + +// ---------------------------------------------------------------------------------------- + +int main() try +{ + net_type net; + shape_predictor sp; + // You can get this file from http://dlib.net/files/mmod_front_and_rear_end_vehicle_detector.dat.bz2 + // This network was produced by the dnn_mmod_train_find_cars_ex.cpp example program. + // As you can see, the file also includes a separately trained shape_predictor. To see + // a generic example of how to train those refer to train_shape_predictor_ex.cpp. + deserialize("mmod_front_and_rear_end_vehicle_detector.dat") >> net >> sp; + + matrix img; + load_image(img, "../mmod_cars_test_image2.jpg"); + + image_window win; + win.set_image(img); + + // Run the detector on the image and show us the output. + for (auto&& d : net(img)) + { + // We use a shape_predictor to refine the exact shape and location of the detection + // box. This shape_predictor is trained to simply output the 4 corner points of + // the box. So all we do is make a rectangle that tightly contains those 4 points + // and that rectangle is our refined detection position. + auto fd = sp(img,d); + rectangle rect; + for (unsigned long j = 0; j < fd.num_parts(); ++j) + rect += fd.part(j); + + if (d.label == "rear") + win.add_overlay(rect, rgb_pixel(255,0,0), d.label); + else + win.add_overlay(rect, rgb_pixel(255,255,0), d.label); + } + + + + + cout << "Hit enter to end program" << endl; + cin.get(); +} +catch(image_load_error& e) +{ + cout << e.what() << endl; + cout << "The test image is located in the examples folder. So you should run this program from a sub folder so that the relative path is correct." << endl; +} +catch(serialization_error& e) +{ + cout << e.what() << endl; + cout << "The correct model file can be obtained from: http://dlib.net/files/mmod_front_and_rear_end_vehicle_detector.dat.bz2" << endl; +} +catch(std::exception& e) +{ + cout << e.what() << endl; +} + + + + diff --git a/examples/mmod_cars_test_image2.jpg b/examples/mmod_cars_test_image2.jpg new file mode 100644 index 000000000..16aa30eb7 Binary files /dev/null and b/examples/mmod_cars_test_image2.jpg differ