Time-saving preview of surveillance videos
Jerky Action
Fast-motion playback of the movie, for example, with the help of the fps
(frames per second) parameter in MPlayer, would be the easiest option:
mplayer -framedrop -fps 150 video.mp4
The -framedrop
parameter simply throws away frames if the CPU fails to keep pace when decoding. The result is a movie that runs roughly five times as fast as normal, which looks like something from the early days of movie making, like the old hand-cranked takes of Charlie Chaplin.
This procedure requires a human reviewer, whereas I was looking for an automated process that lists the most important seconds of the retrieved video as metadata, along with illustrative thumbnails, much like a photographer's contact sheet.
Motion Detection
Much has happened in the field of pattern recognition in image processing in the last few years, and the OpenCV [2] package even offers some highly scientific routines as an open source program. To determine whether someone or something is moving through the video, a program needs to read the frames in the video stream and determine which pixels have shifted from the (x, y) coordinates to the (x + Δx, y + Δy) coordinates. If you find a large contiguous area for which this is true, then you can assume that a movement took place between two frames.
One of the procedures included with OpenCV is called Lucas-Kanade [3]; it attempts to find optical flow – areas around central points that jointly move from one frame to the next – in a video. To do so, it first determines a number of interesting areas, where monitoring promises success and that another algorithm can extract from an image by focusing on image points in areas with a recognizable structure or on the edges of objects.
To perform the Lucas-Kanade (LK) analysis, the OpenCV package (on Ubuntu, this is libopencv-dev) provides the calcOpticalFlowPyrLK()
function with no fewer than 11 parameters.
Acrobatics with Cmake
The C++ program in Listing 1 [4] reads in a video file and detects any movement of objects between frames. Converting it into an executable program requires some compilation acrobatics with include
files and link libraries; your easiest approach here is to use cmake
and its meta Makefile in Listing 2.
Listing 1
max-movement-lk.cpp
Listing 2
CMakeLists.txt
Typing the cmake .
command (the dot stands for the current directory, in which the CMakeLists.txt
file resides) followed by the make
command starts the lengthy compilation process, which finally produces the max-movement-lk
binary. It expects a video file and outputs movie location values in seconds for scenes containing motion.
To do this, the main()
function reads the video file name from the command line and starts VideoCapture
provided by the OpenCV package in line 57. The frame rate is read from the video file in line 63 and stored in the fps
variable. Because the LK algorithm works best with grayscale images, lines 68 and 75 bleed the color out of every frame to be analyzed.
A while
loop iterates across all the frames, and the move_test()
function checks in line 76 whether any motion occurred between the last frame read, oframe
, and the current frame
. If so, line 77 divides the counter value by the FPS value of the video and thus computes the time at which the motion occurred in the video as a value in seconds.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
News
-
Red Hat Migrates RHEL from Xorg to Wayland
If you've been wondering when Xorg will finally be a thing of the past, wonder no more, as Red Hat has made it clear.
-
PipeWire 1.0 Officially Released
PipeWire was created to take the place of the oft-troubled PulseAudio and has finally reached the 1.0 status as a major update with plenty of improvements and the usual bug fixes.
-
Rocky Linux 9.3 Available for Download
The latest version of the RHEL alternative is now available and brings back cloud and container images for ppc64le along with plenty of new features and fixes.
-
Ubuntu Budgie Shifts How to Tackle Wayland
Ubuntu Budgie has yet to make the switch to Wayland but with a change in approaches, they're finally on track to making it happen.
-
TUXEDO's New Ultraportable Linux Workstation Released
The TUXEDO Pulse 14 blends portability with power, thanks to the AMD Ryzen 7 7840HS CPU.
-
AlmaLinux Will No Longer Be "Just Another RHEL Clone"
With the release of AlmaLinux 9.3, the distribution will be built entirely from upstream sources.
-
elementary OS 8 Has a Big Surprise in Store
When elementary OS 8 finally arrives, it will not only be based on Ubuntu 24.04 but it will also default to Wayland for better performance and security.
-
OpenELA Releases Enterprise Linux Source Code
With Red Hat restricting the source for RHEL, it was only a matter of time before those who depended on that source struck out on their own.
-
StripedFly Malware Hiding in Plain Sight as a Cryptocurrency Miner
A rather deceptive piece of malware has infected 1 million Windows and Linux hosts since 2017.
-
Experimental Wayland Support Planned for Linux Mint 21.3
As with most Linux distributions, the migration to Wayland is in full force. While some distributions have already made the move, Linux Mint has been a bit slower to do so.