Time-saving preview of surveillance videos

Me-Too Algorithm

The algorithm borrowed from OpenCV Essentials [2] uses the move_test() function (lines 9-45) to call the OpenCV goodFeaturesToTrack() function (line 12) to detect points of interest in the old frame (oframe); the maximum number points of interest is limited to 500 by the constant MAX_FEATURES (line 6). Line 27 then calls calcOpticalFlowPyrLK() and returns a number of areas in the new_features variable that have apparently shifted, compared with ofeatures in the last frame.

The for loop (lines 34-43) iterates across the areas and finds the range that covered the longest path. If one of them exceeds the value of 100, line 44 returns the value 1 from move_test(), thus indicating that movement must have occurred.

Spice It Up and Show Me!

Listing 1 thus outputs lines of integer values that represent the values in seconds for times at which something in the video moved from one frame to the next. It's now time for the code in Listing 3 to spice up this raw data, generate thumbnails for the appropriate scenes, and summarize the whole thing in an overview, as shown in Figure 4.

Listing 3



Figure 4: The motion filter only shows the seconds of video in which something actually happened.

It uses the good old, all-around mplayer tool for the thumbnails, fast forwarding to the specified video second with the -ss option and storing the frame in a temporary directory $tmpdir. The -frames 1 option stipulates that mplayer terminates right after reading a single frame. The move() function from the CPAN File::Copy module then renames the file in the temporary directory to one in the current directory and uses the CPAN DateTime::Format::Duration module to convert movie seconds to the hh:mm:ss format. The frame at second 64 thus becomes the file 00:01:04.jpg.

The Ubuntu perlmagick package adds the CPAN Image::Magick module to the system, which you can use to create montages from multiple image files (i.e., virtual contact sheets) in the format shown in Figure 4. The call

$ max-movement-lk test.mp4 | ./motion-meta test.mp4

glues the two parts of the pipeline together and produces the contact sheet in motion-meta.jpg.

The first part analyzes the frames in the video and prints the values (in seconds) during which movement has occurred. The second part grabs the movie second values, deduplicates them, looks up the associated thumbnail in the video, and mounts all of them to create a contact sheet – using nothing but the raw still image file names, conveniently chosen to reflect the time in the video in minutes and seconds.

10 Million for a Specialist

Detecting moving objects in video streams is used not only with surveillance videos, but with self-driving cars to distinguish endangered pedestrians from stationary road signs. Learning these techniques could pay off in terms of your career: According to ex-Googler Sebastian Thrun, companies in this field are currently outdoing each other and paying about $10 million for specialists [5]. Now who could afford it to say no to that?


  1. Arlo Security System: https://www.amazon.com/dp/B00P7EVST6
  2. Deniz Suarez, O., M. del Milagro Fernandez Carrobles, N. Vallez Enano, G. Bueno Garcia, and I. Serrano Gracia. OpenCV Essentials. Packt Publishing, 2014. https://www.amazon.com/OpenCV-Essentials-Oscar-Suarez-2014-08-25/dp/B017YC0EHA
  3. Lucas-Kanade method: https://en.wikipedia.org/wiki/Lucas%E2%80%93Kanade_method
  4. Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/magazine/195/
  5. Post by Sebastian Thrun: http://www.recode.net/2016/9/17/12943214/sebastian-thrun-self-driving-talent-pool

The Author

Mike Schilli works as a software engineer in the San Francisco Bay Area. He can be contacted at mailto:mschilli@perlmeister.com. Mike's homepage can be found at http://perlmeister.com.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Motion Detection

    The motion detector software, Motion, monitors the video signal from one or multiple cameras and is able to detect whether a significant part of the picture has changed, record and track movement, or launch arbitrary external commands to trigger other actions.

  • Blender 3D Animation

    Blender not only generates realistic single frames; it is also capable of capturing the natural movements of people and animals. We’ll introduce you to some of Blender’s animation features.

  • Howdy

    Howdy brings the convenience of facial authentication to Linux.

  • Embed Elements into Your Clips Using Natron

    Tracking is good for stabilizing video clips, and it helps you put stuff in scenes that wasn't there in the first place.

  • Tutorials – Natron

    Natron allows you to create eye-catching effects and combine different video clips in surprising ways, letting you build up your clips like a pro.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95