1#!/usr/bin/python
2# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
3#
4# This example shows how to use the correlation_tracker from the dlib Python
5# library. This object lets you track the position of an object as it moves
6# from frame to frame in a video sequence. To use it, you give the
7# correlation_tracker the bounding box of the object you want to track in the
8# current video frame. Then it will identify the location of the object in
9# subsequent frames.
10#
11# In this particular example, we are going to run on the
12# video sequence that comes with dlib, which can be found in the
13# examples/video_frames folder. This video shows a juice box sitting on a table
14# and someone is waving the camera around. The task is to track the position of
15# the juice box as the camera moves around.
16#
17#
18# COMPILING/INSTALLING THE DLIB PYTHON INTERFACE
19# You can install dlib using the command:
20# pip install dlib
21#
22# Alternatively, if you want to compile dlib yourself then go into the dlib
23# root folder and run:
24# python setup.py install
25#
26# Compiling dlib should work on any operating system so long as you have
27# CMake installed. On Ubuntu, this can be done easily by running the
28# command:
29# sudo apt-get install cmake
30#
31# Also note that this example requires Numpy which can be installed
32# via the command:
33# pip install numpy
34
35import os
36import glob
37
38import dlib
39
40# Path to the video frames
41video_folder = os.path.join("..", "examples", "video_frames")
42
43# Create the correlation tracker - the object needs to be initialized
44# before it can be used
45tracker = dlib.correlation_tracker()
46
47win = dlib.image_window()
48# We will track the frames as we load them off of disk
49for k, f in enumerate(sorted(glob.glob(os.path.join(video_folder, "*.jpg")))):
50 print("Processing Frame {}".format(k))
51 img = dlib.load_rgb_image(f)
52
53 # We need to initialize the tracker on the first frame
54 if k == 0:
55 # Start a track on the juice box. If you look at the first frame you
56 # will see that the juice box is contained within the bounding
57 # box (74, 67, 112, 153).
58 tracker.start_track(img, dlib.rectangle(74, 67, 112, 153))
59 else:
60 # Else we just attempt to track from the previous frame
61 tracker.update(img)
62
63 win.clear_overlay()
64 win.set_image(img)
65 win.add_overlay(tracker.get_position())
66 dlib.hit_enter_to_continue()
67