Tag Archives: motion

Permissions on /dev/video0 running motion on Raspberry pi

It pretty much happens that every time I setup motion on a new build that it doesn’t work right away. There’s usually a few things I miss. Usually it’s an error reading from the camera, the logs reporting something like:

Feb 17 10:51:21 pi2 motion: [1] [NTC] [ALL] motion_init: Thread 1 started , motion detection Enabled
Feb 17 10:51:21 pi2 motion: [1] [NTC] [VID] vid_v4lx_start: Using videodevice /dev/video0 and input -1
Feb 17 10:51:21 pi2 motion: [1] [ALR] [VID] vid_v4lx_start: Failed to open video device /dev/video0:

which invariably means the camera is broken or I’ve misconfigured my motion setup.

This one was puzzling me a little though. It’s on a raspberry pi, imaginatively titled pi2, and it was failing to read from the camera. The reason I was puzzled was that the hardware combination had worked before. What had gone wrong was the micro-SD card, and I’d done a new raspbian build, copying over the relevant motion configuration files from the old card.

Clearly the difference had to be something to do with the OS. So what was different? I’d taken the opportunity of the SD failure to download and install the latest version of raspbian and everything looked good to go.

Running motion as root worked fine, both against the vanilla configuration file, and the customised config file I wanted to use. So let’s have a look at the video file:

root@pi2:~# ls -l /dev/video0
crw-rw----+ 1 root video 81, 0 Feb 17 11:17 /dev/video0

Looks about right. Ah, the video group. I need to be in the video group. That might be it:

root@pi2:~# usermod -G video dougie

Restart motion and try again. Nope. Let’s have a closer look at that file.

On pi2 (with a new version of raspbian):

crw-rw----+ 1 root video 81, 0 Feb 17 11:17 /dev/video0

and on pi1 (another rpi, running motion fine, with a slightly older version of raspbian):

dougie@pi1:~ $ ls -l /dev/video0
crw-rw---- 1 root video 81, 0 Jan 8 22:54 /dev/video0

Very similar, but not identical. The newer version of the device file has and extra + at the end of the permissions bit, which means the file has extra security permissions set. I’ve not had cause to use Access Control Lists (ACLs) before, and it was a temptation just to chmod 777 on the file as a quick and dirty, and lazy, fix, but I thought it’d be better to take a closer look. Using the getfacl command:

root@pi2:~# getfacl /dev/video0
getfacl: Removing leading '/' from absolute path names
# file: dev/video0
# owner: root
# group: video

I could see that I (me: dougie) did not appear on the list, although the default rpi user pi does. I rarely use the pi account. One of the first things I do is change its password, create my own user, and use that instead. So it looks like the default install for raspbian allows user pi to access /dev/video0. It also looks like I can’t access the file, despite being a member of the group video.

I found a good command summary on the centos documentation website, and using that gave myself access:

root@pi2:~# setfacl -m u:dougie:rw /dev/video0

That did the trick.



Sticky Backed Plastic …

… well, not quite. But a big long bit of wood, a (really) heavy planter (which I usually use to sit on), and some parcel tape. Nothing that the BBC Springwatch tech-crew will feel too threatened by but I’m feeling pretty pleased with myself.

Sometimes thinking about things too much is a bad idea. I wanted the webcam, away from the building, pointing at the nestbox. And I couldn’t work out how to do it. So I went for a walk. Around the garden, and around the garage. And 10 minutes later …

Looking good but despite tweaking with mask files and experimenting with the configuration for motion I still end up with over 10,000 images at the end of the day, a random snapshot of which get upload to my flickr photostream.
[slickr-flickr tag=”djnwebcam2″ items=”20″]

housekeeping script to keep jpegs manageable

This crops up periodically and it surprises me that with all the free tools you get with linux that there’s nothing (that I can find, at least) that will allow me to keep a folder tree within a manageable size. I did write a shell script to do this but I’ve lost it. With a webcam using motion often triggering over 5000 images a day (four times that if it’s windy) I need to have some automated housekeeping to purge the older images.

I think what I had before was something that had a user-definable value (e.g. 5GB) and it would iteratively recurse a directory tree, deleting the older directories until the space taken up dropped below the threshhold. There are problems with this (e.g. accidentally Tagging or touching an old jpeg) that causes the directory time stamp to be updated. But all these thousands of jpegs are piling up and I’ll have to address this sometime soon.

Mask files, sizes, and motion.conf tweaks

The tweaks don’t seem to be having much effect. Even with the lightswitch option set to 80, and the smart_mask_speed set to 10, it’s still pretty wild and windy out there.

# Dynamically create a mask file during operation (default: 0)
# Adjust speed of mask changes from 0 (off) to 10 (fast)
smart_mask_speed 10

# Ignore sudden massive light intensity changes given as a percentage of the picture
# area that changed intensity. Valid range: 0 - 100 , default: 0 = disabled
lightswitch 80

So it has to be the mask file. The way the mask file works is by using an identically sized image as the webcam and making all areas to be monitored white, and all not to be monitored black. Or is it the other way around? I did this using the gimp and after a couple of false starts it turned out to be pretty easy. Here’s my rough notes:

  1. Make a copy of an existing webcam image to use as the template. This way the dimensions of the image will be correct
  2. Use the Free Select Tool (it looks like the laso) to select an area that you want to be monitored for movement.
  3. Select Bucket Fill. Under the section for Affected Area make sure it’s ticked for Fill Whole Selection. Make sure your foreground colour is white (I kept getting this wrong. It doesn’t matter. Just click on the arrows to reverse foreground/background and do it again).
  4. Click on the area to be filled and it should fill with white.
  5. For the background, go to Select -> Invert, then swap your foreground and background colours. Click on the area you want to be black

You need to save this as a ‘pgm’ file. For some reason a pgm file is huge compared to a jpeg, e.g.

-rw-r--r-- 1 dougie dougie 691254 2011-05-25 14:24 mask1 - 25May2011_1407.54-00.pgm
-rw-r--r-- 1 dougie dougieĀ  12639 2011-05-25 14:26 mask1 - 25May2011_1407.54-00.jpg

although I’m sure it’s possible to reduce the size there doesn’t seem much need at the moment.

Mask File for motion

mask file

Now it’s just a matter of pointing the appropriate option in motion.conf at the mask file and seeing what happens.

Here’s my first attempt:



motion.conf options

Hmmmm, there are a few options in the motion.conf file I haven’t seen before. Or at least, that I can’t recall having seeing before. These look promising:

# Dynamically create a mask file during operation (default: 0)
# Adjust speed of mask changes from 0 (off) to 10 (fast)
smart_mask_speed 0

# Ignore sudden massive light intensity changes given as a percentage of the picture
# area that changed intensity. Valid range: 0 - 100 , default: 0 = disabled
lightswitch 0

The trouble with creating a mask file is that it assumes the camera stays fixed in the same position, and that’s pretty unlikely. So I’ll try tweaking these settings and see what happens. Far less effort. Currently running at about 1000 images an hour. Let’s see if I can get that down a bit …


controlling the loco-motion

So here’s the problem:

[slickr-flickr tag=”webcam2″ type=”gallery”]

This is a USB webcam pointing at the nestbox. The software is motion, running on a Linux Mint box, and uploading to my flickr photostream using flickr_upload. There are a couple of homebrew shell scripts involved too.

The trouble is, even though the motion config file can be tweaked and various things twiddled to adjust various thresholds, most of which are black magic, all it needs is a windy day and I find I’ve got 40,000 or so JPEGS on my harddrive. All of blades of grass blowing in the wind.

So I think I’m going to have to create a mask file. I can’t remember how to do it, even though I’ve done it many times before. I’ll do that later. And I’ll write it down.