|
<!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 use the correlation_tracker from the dlib Python</span> |
|
<span class="c1"># library. This object lets you track the position of an object as it moves</span> |
|
<span class="c1"># from frame to frame in a video sequence. To use it, you give the</span> |
|
<span class="c1"># correlation_tracker the bounding box of the object you want to track in the</span> |
|
<span class="c1"># current video frame. Then it will identify the location of the object in</span> |
|
<span class="c1"># subsequent frames.</span> |
|
<span class="c1">#</span> |
|
<span class="c1"># In this particular example, we are going to run on the</span> |
|
<span class="c1"># video sequence that comes with dlib, which can be found in the</span> |
|
<span class="c1"># examples/video_frames folder. This video shows a juice box sitting on a table</span> |
|
<span class="c1"># and someone is waving the camera around. The task is to track the position of</span> |
|
<span class="c1"># the juice box as the camera moves around.</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">os</span> |
|
<span class="kn">import</span> <span class="nn">glob</span> |
|
|
|
<span class="kn">import</span> <span class="nn">dlib</span> |
|
|
|
<span class="c1"># Path to the video frames</span> |
|
<span class="n">video_folder</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">".."</span><span class="p">,</span> <span class="s2">"examples"</span><span class="p">,</span> <span class="s2">"video_frames"</span><span class="p">)</span> |
|
|
|
<span class="c1"># Create the correlation tracker - the object needs to be initialized</span> |
|
<span class="c1"># before it can be used</span> |
|
<span class="n">tracker</span> <span class="o">=</span> <span class="n">dlib</span><span class="o">.</span><span class="n">correlation_tracker</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="c1"># We will track the frames as we load them off of disk</span> |
|
<span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">f</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">video_folder</span><span class="p">,</span> <span class="s2">"*.jpg"</span><span class="p">)))):</span> |
|
<span class="k">print</span><span class="p">(</span><span class="s2">"Processing Frame {}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">k</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"># We need to initialize the tracker on the first frame</span> |
|
<span class="k">if</span> <span class="n">k</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> |
|
<span class="c1"># Start a track on the juice box. If you look at the first frame you</span> |
|
<span class="c1"># will see that the juice box is contained within the bounding</span> |
|
<span class="c1"># box (74, 67, 112, 153).</span> |
|
<span class="n">tracker</span><span class="o">.</span><span class="n">start_track</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">dlib</span><span class="o">.</span><span class="n">rectangle</span><span class="p">(</span><span class="mi">74</span><span class="p">,</span> <span class="mi">67</span><span class="p">,</span> <span class="mi">112</span><span class="p">,</span> <span class="mi">153</span><span class="p">))</span> |
|
<span class="k">else</span><span class="p">:</span> |
|
<span class="c1"># Else we just attempt to track from the previous frame</span> |
|
<span class="n">tracker</span><span class="o">.</span><span class="n">update</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">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">tracker</span><span class="o">.</span><span class="n">get_position</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> |
|
|