热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

HowdoesLinuxdetectyourwebcam?

本文转载至:http:www.netinstructions.comautomating-picture-capture-using-webcams-on-linux

本文转载至: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 85
Writing 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 photo
fswebcam -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 directory
cd /home/stephen/webcamphotos/#Find the most recent jpeg
NEW_JPEG=$(ls -t | grep '\>.jpeg?s=#39; | head -1)#Push it to the remote webserver
scp /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!








推荐阅读
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文介绍了Linux系统中正则表达式的基础知识,包括正则表达式的简介、字符分类、普通字符和元字符的区别,以及在学习过程中需要注意的事项。同时提醒读者要注意正则表达式与通配符的区别,并给出了使用正则表达式时的一些建议。本文适合初学者了解Linux系统中的正则表达式,并提供了学习的参考资料。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 本文介绍了在MFC下利用C++和MFC的特性动态创建窗口的方法,包括继承现有的MFC类并加以改造、插入工具栏和状态栏对象的声明等。同时还提到了窗口销毁的处理方法。本文详细介绍了实现方法并给出了相关注意事项。 ... [详细]
  • Postgresql备份和恢复的方法及命令行操作步骤
    本文介绍了使用Postgresql进行备份和恢复的方法及命令行操作步骤。通过使用pg_dump命令进行备份,pg_restore命令进行恢复,并设置-h localhost选项,可以完成数据的备份和恢复操作。此外,本文还提供了参考链接以获取更多详细信息。 ... [详细]
  • 使用C++编写程序实现增加或删除桌面的右键列表项
    本文介绍了使用C++编写程序实现增加或删除桌面的右键列表项的方法。首先通过操作注册表来实现增加或删除右键列表项的目的,然后使用管理注册表的函数来编写程序。文章详细介绍了使用的五种函数:RegCreateKey、RegSetValueEx、RegOpenKeyEx、RegDeleteKey和RegCloseKey,并给出了增加一项的函数写法。通过本文的方法,可以方便地自定义桌面的右键列表项。 ... [详细]
  • 本文介绍了使用readlink命令获取文件的完整路径的简单方法,并提供了一个示例命令来打印文件的完整路径。共有28种解决方案可供选择。 ... [详细]
  • 本文详细介绍了在Linux虚拟化部署中进行VLAN配置的方法。首先要确认Linux系统内核是否已经支持VLAN功能,然后配置物理网卡、子网卡和虚拟VLAN网卡的关系。接着介绍了在Linux配置VLAN Trunk的步骤,包括将物理网卡添加到VLAN、检查添加的VLAN虚拟网卡信息以及重启网络服务等。最后,通过验证连通性来确认配置是否成功。 ... [详细]
author-avatar
待续爱情2502861755
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有