How does Linux detect your webcam?

来源:互联网 发布:昆仑虚麒麟臂升阶数据 编辑:程序博客网 时间:2024/06/04 23:25

本文转载至:http://www.netinstructions.com/automating-picture-capture-using-webcams-on-linuxubuntu/

Well if it's a USB webcam, plug it in. If it's an integrated webcam built into the laptop, there's nothing to plug in. Ubuntu should automatically detect and install drivers for the webcam. To see if it's detected, let's run some commands:

Here's a command to see if any video device nodes exist:

stephen@ubuntu:~$ ls -l /dev/video*  crw-rw----+ 1 root video 81, 0 Mar 18 20:29 /dev/video0  crw-rw----+ 1 root video 81, 1 Apr 2 08:03 /dev/video1

And here's another command to find out about the devices:

stephen@ubuntu:~$ lsusb  Bus 001 Device 003: ID 046d:0990 Logitech, Inc. QuickCam Pro 9000  Bus 001 Device 002: ID 13d3:5111 IMC Networks Integrated Webcam  Bus 004 Device 002: ID 0b05:1788 ASUSTek Computer, Inc.  Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub  Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub  Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub  Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

More information on lsusb can be found by reading the lsusb 'man page' (otherwise known as the manual). You can also use the lsusb command to learn more about the resolution of the webcams. Just change the Bus and Device numbers that you found above.


stephen@ubuntu:~$ lsusb -s 001:002 -v | egrep "Width|Height"  wWidth 640  wHeight 480  wWidth 800  wHeight 600  wWidth 1024  wHeight 768  wWidth 1280  wHeight 720
If for some reason you plug in the webcam but it doesn't show up (in /dev/video* or lsusb), try looking at the recent driver messages for clues:

stephen@ubuntu:~$ dmesg|tail

fswebcam, ffmpeg, MPlayer, and VLC



There are a few command line tools that will let you take a picture using your webcam. I've tried three different tools and found that I liked fswebcam the most, but I've listed all of the options here:


Note that you might need to change your device from /dev/video0 to perhaps /dev/video1! Check the above section to see what webcam is detected.



ffmpeg

ffmpeg -f video4linux2 -i /dev/video0 -vframes 1 test.jpg  ffmpeg -f video4linux2 -s 640x480 -i /dev/video1 -vframes 1 /home/stephen/webcamphotos/$(date +\%Y\%m\%d\%H\%M).jpg
  • ffmpeg man page
  • ffmpeg documentation

fswebcam

fswebcam -d /dev/video0 -r 640x480 --jpeg 85 -F 5 test.jpg

This uses a compression factor of 85 (good tradeoff of quality/size) and captures 5 frames (for less noise in image).

  • fswebcam man page

MPlayer

mplayer tv:// -tv driver=v4l2:device=/dev/video0:width=640:height=480 -frames 3 -vo jpeg
  • MPlayer documentation
  • archLinux article on MPlayer

VLC

vlc -I dummy v4l2:///dev/video0 --video-filter scene --no-audio --scene-path /home/stoppal/test --scene-prefix image_prefix --scene-format png vlc://quit --run-time=1

Some notes about the Logitech Quickcam Pro 9000

logitech-quickcam-pro-9000

If you are using the Logitech Quickcam Pro 9000 it has an advertised maximum resolution of 1600x1200. Let's try to run that with fswebcam.

stephen@ubuntu:~$ fswebcam -d /dev/video1 -r 1600x1200 --jpeg 85 -F 5 /home/stephen/webcamphotos/$(date +\%Y\%m\%d\%H\%M).jpeg--- Opening /dev/video1...Trying source module v4l2.../dev/video1 opened.No input was specified, using the first.Adjusting resolution from 1600x1200 to 960x720.--- Capturing 5 frames...Captured 5 frames in 0.40 seconds. (12 fps)--- Processing captured image...Setting output format to JPEG, quality 85Writing JPEG image to ~/home/stephen/webcamphotos/201304051633.jpeg.

Wait a second! Why did it adjust the resolution to 960x720?

It turns out we need to force it to use a YUYV palette instead of the default

stephen@ubuntu:~$ fswebcam -d /dev/video1 -p YUYV -r 1600x1200 --jpeg 85 -F 5 /home/stephen/webcamphotos/$(date +\%Y\%m\%d\%H\%M).jpeg

Configure crontab (make a cronjob) to take a picture every minute or hour

Crontab is a popular *nix utility that executes a command on a user defined interval. Maybe you just want to take a picture every minute, or maybe you want to shutdown your computer Monday through Friday at 10pm. Or maybe you want to run some scripts that backup your data once every 3 months. If you want to run multiple commands you can do so by chaining them with the && keyword, but it's also sometimes worth making a bash script (or maybe a simple Python/Perl/Ruby script) that gets executed as part of the cronjob.

To view the current cron jobs for the current user, type crontab -e. To view the current cron jobs for the super user, type sudo crontab -e.

Here are some cronjobs I have set up:

# To take a picture every minute  # */1 * * * * streamer -f jpeg -s 1024x768 -o /home/stephen/timelap/$(date +\%m\%d\%k\%M).jpeg# To take a picture every hour on the 15 minute mark using a different tool# 15 * * * * fswebcam -r 1024x768 --jpeg 85 -D 4 -F 10 /home/stephen/webcamphotos/$(date +\%Y\%m\%d\%k\%M).jpeg# Take a picture and upload it to the webserver every hour@hourly bash /home/stephen/scripts/take_photo_and_push.sh

The last cronjob calls a bash script that looks like this:

#!/bin/bash  #Take a picture, then push it to a remote webserver#Take a photofswebcam -d /dev/video1 -p YUYV -r 1600x1200 --jpeg 85 -D 2 -F 15 /home/stephen/webcamphotos/$(date +\%Y\%m\%d\%H\%M).jpeg#Navigate to the directorycd /home/stephen/webcamphotos/#Find the most recent jpegNEW_JPEG=$(ls -t | grep '\>.jpeg' | head -1)#Push it to the remote webserverscp /home/stephen/webcamphotos/$NEW_JPEG stephen@netinstructions.com:/home/stephen/netinstructions.com/homeserver/latest.jpeg

For more information on cronjobs and crontab, take a look at this guide.

If you want to view any logs for the the cron job you can view the logs by typing:

$ grep CRON /var/log/syslog

Viewing/Transferring the Pictures

Okay, so you found a command line utility that takes pictures, and perhaps a cronjob that runs that command every 5 minutes or 10 minutes or every hour or once a day, but how do you look at the picture?

There are a couple of ways of doing this. If you're using the desktop version of Ubuntu (with a nice graphical user interface) you just double click on the photo. For the rest of us who are SSH'ing in to a remote machine or are using the server version of Ubuntu or some other Linx distro, we have a few options:

  • FileZilla to grab the files and transfer them to our local machines
  • If you have a web server (Apache, ngnix, or something else) on the server, move the file to the web directory
  • SCP the file to a remote web server. For example, I have a few websites (such as this one) hosted by Dreamhost, and they provide shell access

The command to securely transfer a file on one machine to another looks like this:

scp /home/stephen/webcamphotos/$NEW_JPEG stephen@netinstructions.com:/home/stephen/netinstructions.com/homeserver/latest.jpeg

Good luck!





0 0
原创粉丝点击