Fun With Webcams

Prototype Webcam Setup

I started off with a low cost Logitech USB camera and a cheap desktop tripod. A trip to the hardware store also for epoxy to affix a regular nut to the arm on the camera to create a mount point for use with a tripod. I was fairly happy with the end result, which allows me to use the camera in either landscape or portrait orientation.


I used this blog article as a guide to get up and running with Python 3 and OpenCV on my MacBook. The tutorial series provided by OpenCV is great for showing off what OpenCV has to offer, being mindful of differences between Python and OpenCV versions.


Motion Dection In Video Output

The basic logic for working with a simple OpenCV application for image capturing and processing involves using an application loop, where the actions are performed within the loop over and over again until execution is ended. The following code demonstrates using an event loop to perform simple motion detection.

In the code above, I'm capturing an image from the camera and applying it to a background subtraction filter on each iteration of the application loop. The subtraction filter is used to compare each frame to the previous succession of frames, yielding a mask which draws attention to the portion of the image where containing motion. From the mask I find contours in order to detect areas in the image with motion use them to draw rectangles in the output image around the area where the motion is detected. You'll notice these rectangles in green in the following video.




Raspberry Pi Live Video Streaming

My prototype camera worked fine with my laptop, but one of my goals was streaming video to my iPhone. One way to accomplish this on the Raspberry Pi's limited hardware is to use a camera with a hardware h264 encoder. The Logitech c920 has this feature, and as a bonus has the proper hardware for mounting on a tripod. Getting my hands on this camera was a nice step up from my previous prototype. Once I had the streaming 'server' built out on the Raspberry Pi, I was able to throw together a 'viewer' app for my iPhone to view the live video stream.

I built out a solution for video streaming from the Pi to my iPhone using HLS. To accomplish this, I needed a toolchain to get the video from the camera, create the hls files for the player to view, and serve the video files over HTTP to the mobile player. The result of my effort is a set of scripts I created to install and configure all of this software on a Raspberry Pi, which can be found in this github repository. The README provides a complete explanation of what and how the script is used.

Here's a look at the video streaming in action, running the iPhone app in the simulator on my laptop.




published on 2016-02-11
updated on 2016-02-11