BirdCam, Round Two

In the October 2013 issue, I described the hardware and software I used to create my "BirdTopia Monitoring Station", more commonly called BirdCam. If you've been visiting BirdCam recently, which a surprising number of folks have been doing, you'll notice quite a few changes (Figure 1). In this article, I describe the upgrades, the changes and some of the challenges along the way. If you like fun projects like these involving Linux, please read on and join in my birdy obsession!

Figure 1. BirdCam has changed a lot. Here, the biggest changes are highlighted. Also, look at all those birds!

Slicing and Dicing

One of the first changes I wanted to make to BirdCam was to zoom in a bit on the feeders. Yes, the enormous photo provided by the Galaxy S2 phone mounted in the window is nice, but for displaying on a computer screen (or HDTV, as I'll talk about later), a 1920x1080-size snapshot is really ideal. Unfortunately, when I crop the photo, it leaves out the birdbath. Because I spent the money on a heated birdbath this winter, I didn't want to miss out on any candid water shots. You can see in Figure 2 how I planned to zoom in on the bird feeders and then relocate the birdbath onto what was left of the photo. Although it took a bit of trial and error, the code for doing this was remarkably easy. I used the convert program from the ImageMagick suite. It might be possible to include the crop and relocate into a single command, but I just created a temp file and then overlayed that temp file later on:


convert /dev/shm/original.jpg -crop 640x360+1800+1425 \
        /dev/shm/birdbath.jpg
convert /dev/shm/original.jpg -crop 1920x1080+220+130 \
        /dev/shm/birdbath.jpg -gravity southeast -composite \
        /dev/shm/final.jpg

Figure 2. My old cell phone takes really high-resolution photos. I was able to clip out the birdbath and overlay it nicely to get a closeup of the feeders.

In the code snippet above, I crop out the small birdbath photo from the original camera photo and save it as birdbath.jpg. Then, with another convert command, I crop the original photo to that 1080p size I mentioned earlier and overlay the birdbath onto the photo with the -composite flag. In this little example, I use the -gravity flag to put the birdbath in the corner. You can be more precise with the -geometry flag, which you'll see in my final script (see Resources).

Time and Temp

The original BirdCam article showed how to add a timestamp to the top of the photo, but I didn't mention how I got the temperature. I've since added sunrise and sunset information to the annotation, and made it a little more readable. (Figure 1 shows the annotation in the upper-left corner.)

Although the method for overlaying the information isn't much different (you can check it out in the final script—see Resources for the link), getting the temperature and sunrise/sunset information was challenging. To get the current temperature, I use a little command-line program called weather-util, available in most distro repositories. The program actually gives much more information than is required, so to extract just the temperature, you need to do a little grep-fu:


weather-util -i KPLN | grep Temperature | awk '{print $2}'

This requires a bit of explanation. You need to figure out what your four-letter weather ID is. The best place to find that is at http://weather.noaa.gov. Find the closest location to you, and then look in the URL at the top for the four-letter code (usually an airport). For example, the Pellston Regional Airport is my closest, so my ID is KPLN. If you're thinking it would be better to get the actual temperature from my backyard, I agree. In fact, I'll be working on setting up my own weather station in the months to come for that very reason.

The rest of the code does two things. Grepping for "Temperature" returns the line of the weather-util results containing the temperature, and then the awk command extracts just the Fahrenheit temperature from that line. I set up a cron job to save the temperature into a text file every few minutes, and I use that file to annotate the BirdCam photo. The sunrise/sunset information is similar, but for that, I use the Yahoo weather information. You'll need your WOEID information, which again is available in the URL when you go to http://weather.yahoo.com and enter your location information. For example, my weather URL is http://weather.yahoo.com/united-states/michigan/indian-river-2426936, so my WOEID is 2426936. The rest is fairly easy using more grep-fu. Here's the code for sunrise:


echo `l=2426936;curl -s http://weather.yahooapis.com/
↪forecastrss?w=$l|grep astronomy| awk -F\" '{print $2}'`

And the code for sunset:


echo `l=2426936;curl -s http://weather.yahooapis.com/
↪forecastrss?w=$l|grep astronomy| awk -F\" '{print $4}'`

Much like the temperature, I have a cron job that stores these values into a text file that gets used during the annotation of the final BirdCam graphic. Once I get a local weather station from which I can pull information, I might add wind speed and such. For now, I think it's a useful bit of information.

The WindowCam

If you've been following my blog or my Twitter feed, you may have seen a few different iterations of WindowCam. I tried weatherproofing a USB Webcam (Figure 3), which worked until the wind packed snow into the "hood" I created with a Dixie cup. For a long time, I leaned an old iPhone against the window and just extracted a photo to embed into the final BirdCam photo. The iPhone's resolution was only 640x480, so it was fairly simple to embed that image (downloaded directly from the iPhone) using convert.

Figure 3. My Dixie-cup weatherproofing did an okay job, until the snow flew. Now it's inside looking through the window.

But of course, I wanted more. My current window camera is a Logitech C-920 USB Webcam connected to a computer running Linux. At first I connected it to a Raspberry Pi, but the RPi couldn't do more than five frames per second. I wanted 15 frames per second, full HD, so I'm using a full-blown computer. See, not only do I want to embed the window camera into BirdCam, but I also want to have an archive video of the birds that visit my feeder every day.

The Magic of Motion

Much of the feedback I got about my original BirdCam article included "why didn't you use motion?" Quite simply, I didn't have a computer nearby I could attach to a camera. Now I do. Like I mentioned earlier, I started with a Raspberry Pi. If you have simple needs, or a low-resolution camera, the RPi might be plenty of horsepower. Since the procedures are the same regardless of what computer you're using, just pick whatever makes sense for you.

Motion is a frustratingly powerful program. By that, I mean it will do so many things, it's often difficult to know where to begin. Thankfully, the default configuration file is commented really well, and very few tweaks are required to get things going. For my WindowCam, I have motion do three things:

  1. Save a snapshot every second. This is for BirdCam, so that it can embed a thumbnail of WindowCam in the corner. (See Figure 1, with the huge Mourning Dove in the corner.)

  2. When motion is detected, save full-resolution images, 15 frames every second. Note, this is a lot of photos, especially with a busy WindowCam. I easily get 100,000 photos or more a day.

  3. Record videos of motion detected. I actually don't do this anymore, because it's hard to deal with hundreds of short videos. I just take the photos saved in step two and create a daily archival video (more on that later).

Motion will do much, much more. It will support multiple cameras. It can handle IP cameras. It can fire off commands when motion is detected (turn on lights, or alarms, or text you and so on). But I'll leave those things for BirdCam 3.0! Let's configure motion.

______________________

Shawn Powers is an Associate Editor for Linux Journal. You might find him chatting on the IRC channel, or Twitter

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix