Mostrar mensagens com a etiqueta Imaging. Mostrar todas as mensagens
Mostrar mensagens com a etiqueta Imaging. Mostrar todas as mensagens

sábado, março 23, 2013

Project 12 - Raspberry Pi vs Webcam Server vs Mini Web Server vs Old Webcam vs Python


Lack of money and an old webcam found under the bed are the perfect combination for this post lol


This post was planned since I first bought my Pi. I've got an old quickcam lying about at home ever since I can remember . I had to put it to good use. It's old, but it's not unusable...

First I has to test whether my Pi would recognize the cam using lsusb:


So far so good!

Check this command to see if the device is ready :

$ sudo ls /dev/video0

Now that we know that the webcam is completely ready, we should install fswebcam (or any other for that matter)

$ sudo apt-get install fswebcam

Once it’s done, let’s see how it works and type (I've used a low-res setting - 320x240, due to the fact 
that the webcam is really crap...lol) :


Now you should have a screenshot of your webcam on test.jpg:



(I know, I know, the quality is crap. Later on I'll use an IP Cam that I'm using attached to my Synology NAS and the quality will be vastly improved).

To allow the production of photos every 2 seconds, issue the command:

$ sudo fswebcam -r340×240 /dev/video0 test.jpg -l 2 (*)

Next step was to install the web server so to speak. I've chosen a very light one: lighttpd. If you prefer, install Apache. It's quite heavy on the Pi.

$ sudo apt-get install lighttpd

(always run update and upgrade commands first to make sure that we've got the last version kernelwise).

The server will be automatically started and set to start by default after a reboot.

On this post I didn't install PHP. Later on in other posts, depending on the tasks and aim before me, I'll install it (or not). For the task at hand it's not really necessary.

After installing the lighttpd web server, a new directory will be created: /var/www, which is where I'll put my web site (just an html and photo files to check that everything is in order).

Next let's do some html scripting to setup the html file:


In the first red section lies the heart of the "engine". Everything 2 seconds the page will be refreshed with the new file generated with the command (*). The 2nd red section it's just the configuration of the photo dimensions and a link to my public website.

After that just upload the file to the Pi (as usual I've used CyberDuck":


(the "index.lighttpd.html" file was created by the lighttpd install process)

On the browser it looks like this:

(the one-legged puppet in the picture is about to jump off the cliff...lol)

Now we've got a only working version of our web server. If I want to allow viewing from outside my private LAN, it's only a question to permit port forwarding on the router. Depending on particular router brand, the setup it's trivial.

Finally I've delved head first into Python to allow me to analyse daylight by using the photos that are being taken by using the command (*).

This opens up lots of possibilities in terms of Home Automation.

I found a function on the site that was really what I needed for this particular task: Daylight Function

Because I'm working with low-level imaging functions, I had to install the Image module:

$ sudo apt-get install python-imaging

After some tweaking, I came up with the following Python program:


Executing the program "daylight.py":


The first run produces:


Daylight Index 1 =15.23

Two more runs, where the last one was directed at the window:


Daylight Index 2 =15.23 (the same as run Index 1)

Daylight Index 3 =23.53 (more "light"...)

As I stated before, the possibilities are endless in terms of Home Automation.

It'd be nice to upload, using Python, all the generated images to the cloud (CloudPT, Synology, SkyDrive, etc), but that's for another post.

Happy hacking. You don’t learn to hack – you hack to learn…

MAAntão