Monitor a Webcam

From SingletonMillerWiki
Revision as of 15:08, 20 June 2012 by Martin (Talk | contribs) (View a live feed)

Jump to: navigation, search


Plug in your webcam

Before you plug in your webcam, try the following command at a console:

ls /dev/video*

I don't have any video devices on my system so I get:

No such file or directory

Now Plug in your webcam

List the USB devices using

lsusb

You should see something like this.

Bus 001 Device 005: ID 1415:2000 Nam Tai E&E Products Ltd. or OmniVision Technologies, Inc. Sony Playstation Eye
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. 
Bus 001 Device 004: ID 05e3:0660 Genesys Logic, Inc. USB 2.0 Hub
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Bus 001 Device 005: shows a USB Playstation Eye has been recognised.

Repeat the first command and make a note of the new devices names.

ls /dev/video*

Now we get

/dev/video0

This means the device has automatically been detected and recognised by the system. It doesn't mean you have a means of watching the video data, not yet.

Video Tools

First we're going to need some video capture tools

MPlayer is capable of displaying a webcam video stream.

MEncoder can record from a webcam to video files. The companion to MPlayer,

FFmpeg is a cross-platform audio/video recording, conversion and streaming application.

sudo apt-get install mplayer mencoder ffmpeg

Wait patiently for the programmes and the dependencies to be installed.

User Permissions

List the current users who can access video devices, using

cat /etc/group | grep video
video:x:44

Modify the group to add your user

gpasswd -a [user] video

replace [user] with your username eg. jack

video:x:44:jack

Group changes only change when you next login, so logout and log back in.

View a live feed

mplayer tv:///dev/video0 

when viewing this via a remote SSH connection you'll need your RPi display on to see the output!

Use q or ctrl-c to terminate the feed.

I noticed that there were a large number of dropped frames reported by mplayer.

MPlayer interrupted by signal 2 in module: filter_video
v4l2: ioctl set mute failed: Invalid argument
v4l2: 416 frames successfully processed, 1225 frames dropped

You can control the frame rate using the -fps switch.

mplayer -fps 3 tv:///dev/video0

The following table was determined whilst running mplayer monitoring the RPi performance using the command top in a seperate terminal (via SSH Enable SSH).

Frame rates of 2 or 3 fps (Hz) seems to strike a balance between performance and CPU load, if you RPi is not expected to be doing much else, crack up the fps to 7.

mplayer FPS performance
fps CPU(%) MEM(%)
1 9.2 - 15.0 7.0
2 22.0 - 28.0 7.0
3 38.8 - 41.2 7.0
4 51.6 - 53.8 7.0
5 60.7 - 66.5 7.0
6 75.0 - 78.4 7.0
7 88.3 - 92.9 7.0
8 94.5 - 98.0 7.0

Save video to a file

ffmpeg -f video4linux2 -s vga -i /dev/video0 out.mpg

You may need to experiment with the format -f video4linux2 or the size -s vga to match your webcam.

Remember to press 'q' to stop the process

Convert the video format

mencoder out.mpg -ovc lavc -lavcopts vcodec=mpeg4 -o out2.avi

This converts the mpeg2 data to mpeg4.

Note, transcoding video formats on your RPi will be slow.

Now you can playback using the hardware accelerated player omxplayer, assuming you have it installed. see Playing Videos

omxplayer out2.avi

omx players mpeg4 using 7% CPU, mplayer require 98% !

For playing streams on the RPi we really need is omxplayer to access the streams directly and the webcam can output in H.264 or MPEG4. Unfortunatly my webcam only does MPEG2, so i can't test this and I'm not really sure if omxplayer can access a video device direcly anyway.

Troubleshooting

  • USB Camera not detected.
  1. The RPi has limited capability to supply power to USB devices, try again using a powered USB hub.
  2. Search google to check if your webcam is support by linux.[1]

Tested

This page has been tested on the following RPi build standards

  1. Linux raspberrypi 3.1.9+ #95 PREEMPT Thu May 31 13:21:40 BST 2012 armv6l GNU/Linux

References

  1. https://help.ubuntu.com/community/Webcam
  2. http://jeremyblythe.blogspot.co.uk/2012/06/battery-powered-wireless-motion.html
  3. http://jeremyblythe.blogspot.co.uk/2012/05/raspberry-pi-webcam.html
  4. http://benosteen.wordpress.com/2012/05/31/raspberrypi-birdfeeder-webcam/