Howto: Automatic webcam image upload to remote server

Status
Not open for further replies.
Joined
Aug 7, 2008
Messages
60
Reaction score
0
I had this idea planned out before I even bought my one. I run my own webserver and I now have a webcam so why not put them to work? :D

Prereqs:
Web server
linux
ruby interpreter (yum install ruby/aptitude install ruby, ect)
working web cam (need module uvcvideo loaded)
uvccapture

I am running debian testing and to get the webcam running I had to install linux-uvc-source and then run module-assistant and select, build, and install linux-uvc

uvccapture works good with this webcam driver:
Code:
uvccapture -q97 -x320 -y240 -o/home/user/cam/snap.jpg

-q is the percent jpg quality. 97% gives ~35kb size image and 100% gives about 52kb size, couldn't really tell the difference between the two. You can also capture 640x480 images. uvccapture is kinda buggy so you will have to play with it ;)

Next up I am using a ruby script to upload the image to remote host:

cam.rb
Code:
#! /usr/bin/ruby


require 'net/sftp'

  Net::SFTP.start('host','user',:port => '22') do |sftp|
    # upload a file or directory to the remote host
    sftp.upload!("/local/cam/snap.jpg", "/remote/snap.jpg")
end

You will need ruby gems net::ssh and net::sftp installed. In debian you can install rubygems which is a front end for downloading and installing gems but it wouldn't work right so I did it the manual way. Just download the tar.gz and run the setup.rb:
http://rubyforge.org/frs/download.php/3 ... 0.3.tar.gz
http://rubyforge.org/frs/download.php/3 ... 0.1.tar.gz

Note that no password is supplied, everything works fine with ssh shared keys. Yea can use a password if you want though, more syntax here: http://net-ssh.rubyforge.org/

Now to add all this to cron. You can just run crontab -e as a regular user to open an editor and install a crontab to /var/spool/crontabs/

Code:
*/5 * * * * uvccapture -q97 -x320 -y240 -o/home/user/cam/snap.jpg && ruby /home/user/cam.rb

Also note, just to be fancy I stuck /home/user/cam onto a 100kb ram disk just to keep any disk activity from slowing my poor slow intel ssd down more. Easy to do, just add to fstab:
Code:
tmpfs		/home/user/cam	tmpfs	user,size=100k	0	0
(#note user parm denotes any user can mount/unmount)

And yet another note, in the directory on my webserver I added in the .htaccess file:

Code:
Options -Indexes
<Files *>
Header set Cache-Control: "private, pre-check=0, post-check=0, max-age=0"
Header set Expires: 0
Header set Pragma: no-cache
</Files>

to prevent the image from be cached into a browser. For that to work you have to make sure that you have the apache header module loaded.

Now testing 1..2...3....

snap.jpg


whoa spooky! :mrgreen:

From as far as I can tell very little system resources are used!
 
Status
Not open for further replies.
Back
Top