|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" |
|
"http://www.w3.org/TR/html4/strict.dtd"> |
|
|
|
<html> |
|
<head> |
|
<title></title> |
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
|
<style type="text/css"> |
|
td.linenos { background-color: #f0f0f0; padding-right: 10px; } |
|
span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; } |
|
pre { line-height: 125%; } |
|
body .hll { background-color: #ffffcc } |
|
body { background: #ffffff; } |
|
body .c { color: #008000 } |
|
body .err { border: 1px solid #FF0000 } |
|
body .k { color: #0000ff } |
|
body .ch { color: #008000 } |
|
body .cm { color: #008000 } |
|
body .cp { color: #0000ff } |
|
body .cpf { color: #008000 } |
|
body .c1 { color: #008000 } |
|
body .cs { color: #008000 } |
|
body .ge { font-style: italic } |
|
body .gh { font-weight: bold } |
|
body .gp { font-weight: bold } |
|
body .gs { font-weight: bold } |
|
body .gu { font-weight: bold } |
|
body .kc { color: #0000ff } |
|
body .kd { color: #0000ff } |
|
body .kn { color: #0000ff } |
|
body .kp { color: #0000ff } |
|
body .kr { color: #0000ff } |
|
body .kt { color: #2b91af } |
|
body .s { color: #a31515 } |
|
body .nc { color: #2b91af } |
|
body .ow { color: #0000ff } |
|
body .sa { color: #a31515 } |
|
body .sb { color: #a31515 } |
|
body .sc { color: #a31515 } |
|
body .dl { color: #a31515 } |
|
body .sd { color: #a31515 } |
|
body .s2 { color: #a31515 } |
|
body .se { color: #a31515 } |
|
body .sh { color: #a31515 } |
|
body .si { color: #a31515 } |
|
body .sx { color: #a31515 } |
|
body .sr { color: #a31515 } |
|
body .s1 { color: #a31515 } |
|
body .ss { color: #a31515 } |
|
|
|
</style> |
|
</head> |
|
<body> |
|
<h2></h2> |
|
|
|
<div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/python</span> |
|
<span class="c1"># The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt</span> |
|
<span class="c1">#</span> |
|
<span class="c1"># This example shows how to run a CNN based face detector using dlib. The</span> |
|
<span class="c1"># example loads a pretrained model and uses it to find faces in images. The</span> |
|
<span class="c1"># CNN model is much more accurate than the HOG based model shown in the</span> |
|
<span class="c1"># <a href="face_detector.py.html">face_detector.py</a> example, but takes much more computational power to</span> |
|
<span class="c1"># run, and is meant to be executed on a GPU to attain reasonable speed.</span> |
|
<span class="c1">#</span> |
|
<span class="c1"># You can download the pre-trained model from:</span> |
|
<span class="c1"># http://dlib.net/files/mmod_human_face_detector.dat.bz2</span> |
|
<span class="c1">#</span> |
|
<span class="c1"># The examples/faces folder contains some jpg images of people. You can run</span> |
|
<span class="c1"># this program on them and see the detections by executing the</span> |
|
<span class="c1"># following command:</span> |
|
<span class="c1"># ./<a href="cnn_face_detector.py.html">cnn_face_detector.py</a> mmod_human_face_detector.dat ../examples/faces/*.jpg</span> |
|
<span class="c1">#</span> |
|
<span class="c1">#</span> |
|
<span class="c1"># COMPILING/INSTALLING THE DLIB PYTHON INTERFACE</span> |
|
<span class="c1"># You can install dlib using the command:</span> |
|
<span class="c1"># pip install dlib</span> |
|
<span class="c1">#</span> |
|
<span class="c1"># Alternatively, if you want to compile dlib yourself then go into the dlib</span> |
|
<span class="c1"># root folder and run:</span> |
|
<span class="c1"># python setup.py install</span> |
|
<span class="c1">#</span> |
|
<span class="c1"># Compiling dlib should work on any operating system so long as you have</span> |
|
<span class="c1"># CMake installed. On Ubuntu, this can be done easily by running the</span> |
|
<span class="c1"># command:</span> |
|
<span class="c1"># sudo apt-get install cmake</span> |
|
<span class="c1">#</span> |
|
<span class="c1"># Also note that this example requires Numpy which can be installed</span> |
|
<span class="c1"># via the command:</span> |
|
<span class="c1"># pip install numpy</span> |
|
|
|
<span class="kn">import</span> <span class="nn">sys</span> |
|
<span class="kn">import</span> <span class="nn">dlib</span> |
|
|
|
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span> <span class="o"><</span> <span class="mi">3</span><span class="p">:</span> |
|
<span class="k">print</span><span class="p">(</span> |
|
<span class="s2">"Call this program like this:</span><span class="se">\n</span><span class="s2">"</span> |
|
<span class="s2">" ./<a href="cnn_face_detector.py.html">cnn_face_detector.py</a> mmod_human_face_detector.dat ../examples/faces/*.jpg</span><span class="se">\n</span><span class="s2">"</span> |
|
<span class="s2">"You can get the mmod_human_face_detector.dat file from:</span><span class="se">\n</span><span class="s2">"</span> |
|
<span class="s2">" http://dlib.net/files/mmod_human_face_detector.dat.bz2"</span><span class="p">)</span> |
|
<span class="nb">exit</span><span class="p">()</span> |
|
|
|
<span class="n">cnn_face_detector</span> <span class="o">=</span> <span class="n">dlib</span><span class="o">.</span><span class="n">cnn_face_detection_model_v1</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> |
|
<span class="n">win</span> <span class="o">=</span> <span class="n">dlib</span><span class="o">.</span><span class="n">image_window</span><span class="p">()</span> |
|
|
|
<span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">:]:</span> |
|
<span class="k">print</span><span class="p">(</span><span class="s2">"Processing file: {}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">f</span><span class="p">))</span> |
|
<span class="n">img</span> <span class="o">=</span> <span class="n">dlib</span><span class="o">.</span><span class="n">load_rgb_image</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> |
|
<span class="c1"># The 1 in the second argument indicates that we should upsample the image</span> |
|
<span class="c1"># 1 time. This will make everything bigger and allow us to detect more</span> |
|
<span class="c1"># faces.</span> |
|
<span class="n">dets</span> <span class="o">=</span> <span class="n">cnn_face_detector</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> |
|
<span class="sd">'''</span> |
|
<span class="sd"> This detector returns a mmod_rectangles object. This object contains a list of mmod_rectangle objects.</span> |
|
<span class="sd"> These objects can be accessed by simply iterating over the mmod_rectangles object</span> |
|
<span class="sd"> The mmod_rectangle object has two member variables, a dlib.rectangle object, and a confidence score.</span> |
|
<span class="sd"> </span> |
|
<span class="sd"> It is also possible to pass a list of images to the detector.</span> |
|
<span class="sd"> - like this: dets = cnn_face_detector([image list], upsample_num, batch_size = 128)</span> |
|
|
|
<span class="sd"> In this case it will return a mmod_rectangless object.</span> |
|
<span class="sd"> This object behaves just like a list of lists and can be iterated over.</span> |
|
<span class="sd"> '''</span> |
|
<span class="k">print</span><span class="p">(</span><span class="s2">"Number of faces detected: {}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">dets</span><span class="p">)))</span> |
|
<span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">d</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">dets</span><span class="p">):</span> |
|
<span class="k">print</span><span class="p">(</span><span class="s2">"Detection {}: Left: {} Top: {} Right: {} Bottom: {} Confidence: {}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> |
|
<span class="n">i</span><span class="p">,</span> <span class="n">d</span><span class="o">.</span><span class="n">rect</span><span class="o">.</span><span class="n">left</span><span class="p">(),</span> <span class="n">d</span><span class="o">.</span><span class="n">rect</span><span class="o">.</span><span class="n">top</span><span class="p">(),</span> <span class="n">d</span><span class="o">.</span><span class="n">rect</span><span class="o">.</span><span class="n">right</span><span class="p">(),</span> <span class="n">d</span><span class="o">.</span><span class="n">rect</span><span class="o">.</span><span class="n">bottom</span><span class="p">(),</span> <span class="n">d</span><span class="o">.</span><span class="n">confidence</span><span class="p">))</span> |
|
|
|
<span class="n">rects</span> <span class="o">=</span> <span class="n">dlib</span><span class="o">.</span><span class="n">rectangles</span><span class="p">()</span> |
|
<span class="n">rects</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">d</span><span class="o">.</span><span class="n">rect</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">dets</span><span class="p">])</span> |
|
|
|
<span class="n">win</span><span class="o">.</span><span class="n">clear_overlay</span><span class="p">()</span> |
|
<span class="n">win</span><span class="o">.</span><span class="n">set_image</span><span class="p">(</span><span class="n">img</span><span class="p">)</span> |
|
<span class="n">win</span><span class="o">.</span><span class="n">add_overlay</span><span class="p">(</span><span class="n">rects</span><span class="p">)</span> |
|
<span class="n">dlib</span><span class="o">.</span><span class="n">hit_enter_to_continue</span><span class="p">()</span> |
|
</pre></div> |
|
</body> |
|
</html> |
|
|