Home Security Camera With Raspberry Pi and a Webcam

Lately, I’ve been thinking about ways to beef up my home security using Raspberry Pi. The Raspberry Pi is a pretty capable, dirt cheap little board, and it can connect to all kinds of sensors and systems. I’ve been working on adding a Raspberry Pi camera module, but for those who don’t want to shell out another $30, there’s an even simpler solution.

The Raspberry Pi has full USB support, and runs a fully featured Linux operating system. The Raspberry Pi OS includes support for Video for Linux (V4L), which can interface with webcams. Everyone has a webcam sitting around in their closet. You know, from the 90s? Why not combine a Raspberry Pi and a webcam together to make a super cheap home security camera (ala Dropcam), but for a fraction of the price?

To try this out, I went to my own closet. Sure enough, I found a webcam; in this case, an old Ipevo document camera. Ipevo cameras are nice, because they come with a stand which allows you to reposition them however you like, and they have pretty decent optics, making for a pretty clear picture.

Connecting the Ipevo to the Raspberry Pi was simple as can be. You just plug it into the USB port.

Raspberry Pi.jpgRaspberry Pi.jpg

But how do you go about actually taking pictures? I like Python, so I wanted to find a Pythonic solution. It turns out that the easiest one is to use Pygame. Pygame is usually used to design simple games, but it’s great for this surveillance application too, since it has webcam support and comes pre-loaded on most Raspberry Pi boards.

I Googled around a bit, and in just a couple minutes, I was able to hack together some Python code which connects to the webcam, takes a picture each second for 10 secconds, and saves them to the disk.

cam = pygame.camera.Camera(pygame.camera.list_cameras()[0], (1280,720))
print "Camera Started"
i = 0
while i < 10:
    img = cam.get_image()
    print "Image captured"
    import pygame.image
    print "Image package imported"
    pygame.image.save(img, "webcam_photos/photo%s.jpg" % i)
    print "Image saved"
    i += 1

Here’s an example picture. In this case, I caught my dog in the act of…walking away from the camera.

The scandal!The scandal!

The scandal!

The neat thing about this is that I can connect the camera to my Raspberry Pi home automation server, and since the server is wireless, I could place the camera anywhere in my house. Instant diy surveillance cam.

My next step is to set up the camera for remote access. I’ll probably add in some simple Python code to upload the images to a web server, so I can access them from elsewhere. I’d also like to add in motion detection, and ultimately facial recognition, so my home automation system can know when someone familiar shows up at the door. More on these projects soon.

Leave a Reply