Thursday, August 18, 2016

Raspberry Pi Experiments - Remote Web Cam


IMG_20160815_092029-picsay



One of my initial ideas for using Raspberry Pi was to remotely view my sons activities when ever I was away from him. So as a start I checkout my old web cam (which is a Zebatronics Ecam 2009 model) it was working and finally followed the steps in the below link to setup the webcam in my raspberry pi.
https://www.raspberrypi.org/documentation/usage/webcams/

Here I will give a brief rundown of steps that I did to setup the Web Cam.

1. Check if your web cam is a USB Webcam and the make, mine was Zebatronics Ecam 2009 model
2. Connect it to the Raspberry Pi USB port and see if the lights of the Web Cam are glowing.
3. Check if the Web Cam is detected using lsusb For me it shows the below for the webcam:
pi@raspberrypi:~ $ lsusb
Bus 001 Device 004: ID 0c45:614a Microdia


Note: The trick to know that if webcam is connected is to lsusb before and after connecting the webcam if its detected an additional USB device will show. Additionally in /dev folder check if there any device like /dev/video0

pi@raspberrypi:~ $ ls /dev/video*
/dev/video0

4. Install fswebcam and check if the webcam capture works sudo apt-get install fswebcam

pi@raspberrypi:~/webcam $ fswebcam image.jpg
--- Opening /dev/video0...                                                     
Trying source module v4l2...                                                   
/dev/video0 opened.                                                            
No input was specified, using the first.                                       
Adjusting resolution from 384x288 to 352x288.                                  
--- Capturing frame...                                                         
Captured frame in 0.00 seconds.                                                
--- Processing captured image...                                               
Writing JPEG image to 'image.jpg'.


5. Next I developed a script to capture the image and save to a folder /home/pi/webcam


mkdir /home/pi/webcam

Created a script webcam.sh


#!/bin/bash

DATE=$(date +"%Y-%m-%d_%H%M")

fswebcam -r 1280x720 --no-banner /home/pi/webcam/$DATE.jpg
 
 6. Then setup cron to capture images every minute:
 
crontab -e
 
Added this line in the editor: 
 
* * * * * /home/pi/webcam.sh 2>&1
   
7. The output can be seen as below where the script runs every one minute:
pi@raspberrypi:~ $ ls ~/webcam2016-08-18_1644.jpg 2016-08-18_1707.jpg 2016-08-18_1730.jpg2016-08-18_1645.jpg 2016-08-18_1708.jpg 2016-08-18_1731.jpg2016-08-18_1646.jpg 2016-08-18_1709.jpg 2016-08-18_1732.jpg2016-08-18_1647.jpg 2016-08-18_1710.jpg 2016-08-18_1733.jpg2016-08-18_1648.jpg 2016-08-18_1711.jpg 2016-08-18_1734.jpg2016-08-18_1649.jpg 2016-08-18_1712.jpg 2016-08-18_1735.jpg2016-08-18_1650.jpg 2016-08-18_1713.jpg 2016-08-18_1736.jpg2016-08-18_1651.jpg 2016-08-18_1714.jpg 2016-08-18_1737.jpg2016-08-18_1652.jpg 2016-08-18_1715.jpg 2016-08-18_1738.jpg2016-08-18_1653.jpg 2016-08-18_1716.jpg 2016-08-18_1739.jpg2016-08-18_1654.jpg 2016-08-18_1717.jpg 2016-08-18_1740.jpg2016-08-18_1655.jpg 2016-08-18_1718.jpg 2016-08-18_1741.jpg2016-08-18_1656.jpg 2016-08-18_1719.jpg 2016-08-18_1742.jpg2016-08-18_1657.jpg 2016-08-18_1720.jpg 2016-08-18_1743.jpg2016-08-18_1658.jpg 2016-08-18_1721.jpg 2016-08-18_1744.jpg2016-08-18_1659.jpg 2016-08-18_1722.jpg 2016-08-18_1745.jpg2016-08-18_1700.jpg 2016-08-18_1723.jpg 2016-08-18_1746.jpg2016-08-18_1701.jpg 2016-08-18_1724.jpg 2016-08-18_1747.jpg2016-08-18_1702.jpg 2016-08-18_1725.jpg 2016-08-18_1748.jpg2016-08-18_1703.jpg 2016-08-18_1726.jpg 2016-08-18_1749.jpg

8. Finally I created a script to generate a time lapse video of these images, for this first we need to install avconv which is a fork of ffmpeg.

pi@raspberrypi:~ $ sudo apt-get install avconv
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package avconv
Then created a script conv2vid.sh to generate the time lapse video from the images and flush the images once the video is created:


#!/bin/bash
DATE=$(date +"%Y-%m-%d_%H%M")
mv /home/pi/webcam/*.jpg /home/pi/webcam/video
cat /home/pi/webcam/video/*.jpg | avconv -r 10 -f image2pipe -codec:v mjpeg -i --pix_fmt yuvj420p -r 30 -c:v libx264 -vf scale=480:300 -y /home/pi/webcam/video/$DATE.mp4
rm /home/pi/webcam/video/*.jpg
Please find the below video for the Time lapse video

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...