It is almost Halloween, and while standing in my kitchen, I was looking at my door thinking about the kids that will be trick or treating there soon. I also thought about my Raspberry Pi. How nice it would be if I could see who is at the door from my computer or tablet? Very nice indeed, and not only for this holiday, but to check for deliveries and general security.
I had bought a discount MicroSoft web cam earlier this year, as I had intended to set up a "kitty cam", but being the professional procrastinator I am, that didn't happen. I've checked it with the Linux video HCL, and it is supported.
Motion
Doing some research on this subject, the most popular software package to use is Motion. So after downloading NOOBS from raspberrypi.org, I booted up and chose Rasbian. (I do want to learn about Arch at one point, but that is for another day). I had earlier in the day chosen to boot to desktop GUI, however I saw the CPU meter being maxed out.
What I am finding about the Motion package, is that it is not compatible with any browser other than FireFox, and occasionally Safari on my iPhone. This is a bit problematic, as I prefer Chrome on both my desktop and my mobile platforms. Added to that issue, it seems to work for a while, and randomly cut out. Either it will stop responding to requests from the browser, or it will put up an error of "unable to open video device". When it works, it works amazingly well.
I like what motion does, and unfortunately it is not going to meet my needs for this project.
fswebcam
I searched around for a more custom and scripted way to have a steady webcam, and found this post from James Bruce describing how he set up a Pi with attached camera(s) to snap a pic every minute for a given counter. I tried his script and it worked with my MicroSoft cam, so I set about modifying it for my needs.
#!/bin/bash
# Timelapse controller for USB webcam
DIR=/home/pi/timelapse
x=1
while [ $x -le 4 ]; do
filename=$(date +"%d%m%Y_%H%M-%S").jpg
mv /var/www/test.jpg /var/www/camarch/$filename
fswebcam -d /dev/video0 -r 800x600 --flip v --flip h --top-banner --title "Door Cam Mark 0.5" /var/www/test.jpg
x=$(( $x + 1 ))
sleep 10;
done;
Based on James' code, I changed down from 1440 or so to 4, and added a cron job to fire it every minute. This produces 4 pictures per minute. Next, I grab the date/time to attach to the file name that I am going to move. Then I move the file "test.jpg" to an archive folder, renaming it to that minute, with a counter. (This does however, create wrong file names, with a file actually taken at 13:11 having a file name of 13:12 for example. To be fixed in later versions.)
The next line is the actual capture command, which I have modified by making the image size larger, and adding the --flip for both vertical and horizontal, as my cam will be mounted upside down in the top corner of the door's window. Also, I moved the banner to the top of the image, and titled it with "Door Cam Mark 0.5" (I'm lookin' at you Tony Stark ;) )
Then it increments the counter and sleeps for 10 seconds. (I chose 4 pictures to ensure that the process would not overlap the 1 minute cron job, as it sometimes takes fswebcam a few seconds to process the picture)
As you can imagine, taking 4 frames a minute for 24 hours creates a major amount of files. I decided that to deal with this, I would make time lapse videos of the day. So I created a cron job at 23:59 daily, that creates a list the JPG files in the "camarch" directory, then runs a mencoder command. This creates a nice AVI file of the events in the time lapse, looking exactly like all the other time lapse videos you've ever seen. After this video is created, I delete the JPG files from the previous day.
date=$(date +"%d%m%Y)
ls /var/www/camarch/*.jpg > /var/www/camarch/list.txt
mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=640:480 -o /var/www/camarch/$(date +"%d%m%Y").avi -mf type=jpeg:fps=10 mf://@/var/www/camarch/list.txt
rm /var/www/camarch/$date*.jpg
I intend to set a weekly cron job to combine the week's daily videos and name it something like "week-of-DDMM-DDMM".
I have learned a few interesting things during this experiment, and I want to be able to use the motion package in a semi-interactive Halloween/other holiday video system. I may need to actually learn programming in order to do this, however.
Currently, this project is 90% working, however that last 10% is a fairly vital part: the WiFi connection. I know this WiFi dongle works, and works under Linux, as it was running my RaspBMC. With the Pi being nice and small, only needing the power cable and the webcam's USB cable, it isn't a "big deal" right now to pick it up and put it back on my workbench to copy the day's files, though it does create a hole. I am going to install RaspBMC on my other Pi and then replicate the fswebcam setup after verifying it does in face connect to WiFi. Bonus for this; the layout of my apartment is such that I will be putting the Pi next to my projector/Xbox so I could use the XBMC features again, while maintaining the "who's there" webcam. :)

No comments:
Post a Comment