Page 1 of 1
Motion detection in AVI surveillance movie?
Posted: 2010-04-21T08:32:47-07:00
by BrianP
Hi,
I have a jerk of a neighbor who lets his dogs out at random times to crap on my lawn. I have seen him and them do it numerous times. He denies it and gets verbally abusive. I need photographic evidence or a second witness to get the cops to take action and fine him up to $500 per occurrence.
I have a Canon G9 (wonderful camera) which takes time lapse films at 1 or 2 second intervals. It generates an AVI movie. I point it out of my garage and onto my front lawn. But viewing the entire movie is very boring and time consuming.
I want to nuke all of the frames which are near dups (noise) of the previous one and show only the frames with motion. I have scoured the web for motion detection freeware or source and can't find anything suitable. Am I missing any good, off the shelf solutions?
I used mplayer -vo jpeg MVI_2421.AVI to generate one jpg per frame. I could use ImageMagick to convert them to raw, RGB format and write a C program to sums up the RGB vector differences between each pixel and its corresponding pixel in the next frame. If the sum of differences is below some noise level, nuke the earlier frame. Then create a movie from what's left, 100% action packed!
Or, I could read frame i and frame i+1 with ImageMagick and do the RGB vector subtraction frame to frame and accumulate the results.
Or, I could use GD, which has a very low level C language interface to do the same thing.
Any ideas on which approach would be the best?
Thanks,
BrianP
Re: Motion detection in AVI surveillance movie?
Posted: 2010-04-21T08:39:34-07:00
by snibgo
Imagemagic can compare frames, eg:
compare -metric RMSE frame0021.jpg frame0022.jpg null: 2>comp21-22.txt
It generates numbers where larger means more different. I would create a script that did all the comparisons and looked for unusually large numbers.
Re: Motion detection in AVI surveillance movie?
Posted: 2010-04-21T10:28:12-07:00
by BrianP
Snibog,
BRILLIANT! That's exactly what I need.
I presume the RMSE is the root mean square error of the rgb vector difference of each 2 corresponding points in the 2 images?
Thanks 1E6!
BrianP
Re: Motion detection in AVI surveillance movie?
Posted: 2010-04-21T10:38:41-07:00
by snibgo
That's it, more or less, but I don't know the details. See
http://www.imagemagick.org/script/comma ... php#metric for other metrics. It's a clever command: take a photo of the dog, another of the garden, and it will try to find the dog in the garden.
Re: Motion detection in AVI surveillance movie?
Posted: 2010-04-21T16:28:43-07:00
by BrianP
This function will copy all of the action frames to a motion subdirectory while ignoring the boring frames which are nearly the same as the adjacent frames. If it is windy, tree sway will count as motion. Even beetles crawling in front of the lens will show up in the motion directory. Set the mindif parameter to make it as sensitive as you like. Too low, you miss motion. Too high, you see leaves falling. Experiment. Get the newest ImageMagick as the old one I had gave me weird 410 errors. Compile IM and PerlMagick from latest source.
Part of the STDOUT can be stuffed into a CSV file and graphed in a spreadsheet to show you the spikes in activity. Kind of an error check.
Use ffmgeg to generate the jpgs from the movie amazingly fast. Unfortunately, it does not seem to work to reassemble the movement frames into a movie. From the web site, Turn X images to a video sequence: ffmpeg -f image2 -i image%d.jpg video.mpg == NFG. Some stupid error message. One trick pony, but useful.
This reduced 7255 frames down to 168, or 2.3% or a 43:1 data reduction, which is vastly more interesting than watching the whole 2 hour (mostly boring) movie. My Canon G9 runs out of batteries after 2 hrs of time lapse filming. Very useful. Need to figure out how to hotwire it without paying $51 for a $5 AC adapter. Any bright ideas? The whole movie was only 416 MB when the battery died and I have a 4 GB memory card so I am definitely battery, not memory limited.
I found a new cat stalking my front yard!
QED,
BrianP
<pre> Sorry if the IM web knuckleheads mangle the pretty Perl formatting.
================================================================
WARNING! CODE UNTESTED AND WRITTEN BY HALF-WITTED PROGRAMMER
WILL PROBABLY MELT YOUR COMPUTER AND WARP YOUR BRAIN
USE AT YOUR OWN RISK
ZERO WARRANY
# Given a directory, look at all of the jpgs, compare the previous
# one to the current one and copy all of the moving ones
# to a motion directory. Use the following command to generate
# all of the jpg files from the movie.
# ffmpeg -i MVI_2438.AVI f%05d.jpg
sub motion() {
use Image::Magick;
$dir = "/tera/pic/new/20100421b";
`mkdir $dir/motion 2> /dev/null`;
$maxdif = 0;
$mindif = 0.02; # Ignore anything below this noise level.
$old = new Image::Magick; # Old image.
$cmd = "ls $dir/*jpg";
@jpg = `$cmd`;
chomp @jpg;
printf("Found %d JPGs with cmd \"$cmd\".\n", scalar @jpg);
$err = $old->Read($jpg[0]); # Read the Fully Qualified Path.
&err($err) if $err;
for($ii = 0; $ii < scalar @jpg; $ii++) {
$new = new Image::Magick; # New image.
$err = $new->Read($jpg[$ii]); # Read the Fully Qualified Path.
&err($err) if $err;
$comp = $new->Compare(image => $old, metric => 'rmse');
$dif = $comp->Get('error');
$dif[$ii] = $dif; # Push onto dif arry.
$maxdif = $dif if $dif > $maxdif; # Find largest diff.
if($dif > $mindif) {
$cmd = sprintf("cp %s $dir/motion", $jpg[$ii]);
`$cmd`;
# Make sure we have the previous frame too.
unless(-f "$jpg[$ii-1]") {
$cmd = sprintf("cp %s $dir/motion", $jpg[$ii-1]);
`$cmd`;
}
}
undef $old; # Free memory for last image.
$old = $new; # Demote the used up new image to old.
}
print("Max Dif = $maxdif.\n");
$ii = 0;
foreach $dif (@dif) {
printf("$ii,$dif\n ");
$ii++;
}
}
</pre>
Re: Motion detection in AVI surveillance movie?
Posted: 2010-04-21T18:52:57-07:00
by snibgo
Good stuff. When assembling movies, ffmpeg wants frames with consecutive numbers (EDIT: starting from one), so your copy could change to suit.
Re: Motion detection in AVI surveillance movie?
Posted: 2010-04-22T06:08:08-07:00
by BrianP
Snibgo,
I definitely owe you a beer. Thanks for the exact info I needed!
BrianP
Re: Motion detection in AVI surveillance movie?
Posted: 2010-04-22T07:51:18-07:00
by Bonzo
Expensive charger as you say !
I would get a piece of plastic about the correct shape and size.
Get a charger - the canon one looks a bit like a laptop charger.
Chop the end off the charger lead ( from memory the black with white strip is posative ).
Glue or tape the wires to the side of the plastic and over one end and make a terminal - just blobs of solder or solder to a couple of pices of metal and glue or screw to the plastic.
I suppose wood would do instead of plastic and it looks like there is a little flap the lead will cme out of.
Of course there is always a chance of breaking your camera
