Surveillance camera

This project implements a Raspberry Pi based surveillance camera with image based motion detection and upload of picture showing a change to an FTP server. The projects uses a listing from the journal Hardware Guide, Raspberry Pi Projekte with some modifications.

camera.py. The main program, to be saved in the home directory.

#!/usr/bin/python

import io
import os
import picamera
import time
import sys
from datetime import datetime
from PIL import Image
from ftplib import FTP

camera = picamera.PiCamera()

difference = 20
pixels = 100

width = 1280
height = 960

swidth = 100
sheight = 75

imgcounter = 0
imgmax = 400

def compare():
camera.resolution = (swidth, sheight)
stream = io.BytesIO()
camera.capture(stream, format = ‚bmp‘)
stream.seek(0)
im = Image.open(stream)
buffer = im.load()
stream.close()
return im, buffer

def newimage(width, height, counter):
time = datetime.now()
#filename = „img-%04d%02d%02d-%02d%02d%02d.jpg“ % (time.year, time.month, time.day, time.hour, time.minute, time.second)
timestr = „%04d%02d%02d-%02d%02d%02d“ % (time.year, time.month, time.day, time.hour, time.minute, time.second)
filename = „img-%04d.jpg“ % (counter)
tmpfn = „/tmp/img.jpg“
camera.resolution = (width, height)
camera.capture(tmpfn)
print „Captured %s at %s“ % (tmpfn, timestr)
try:
ftp = FTP(‚ftp.site.com‘, ‚user‘, ‚password‘)
try:
ftp.cwd(‚img‘)
ftp.cwd(‚camera‘)
ftp.storbinary(‚STOR ‚ + filename, open(tmpfn))
#ftp.retrlines(‚LIST‘)
ftp.close()
print „Uploaded %s“ % filename
except:
print „FTP upload error(2):“, filename
ftp.close()
pass
except:
print „FTP upload error(1):“, filename
pass

image1, buffer1 = compare()

timestamp = time.time()

while (True):
image2, buffer2 = compare()

changedpixels = 0
for x in xrange(0, swidth):
for y in xrange(0, sheight):
pixdiff = abs(buffer1[x, y][1] – buffer2[x, y][1])
if pixdiff > difference:
changedpixels += 1

if changedpixels > pixels:
timestamp = time.time()
newimage(width, height, imgcounter)
imgcounter = (imgcounter + 1) % imgmax

image1 = image2
buffer1 = buffer2
time.sleep(1)

run.sh:

#!/bin/sh
./camera.py
/usr/bin/sudo /sbin/reboot

Changes in fstab. We don’t want that the Pi is writes to the SD card again and again. We use a memory mapped /tmp file system instead.

sudo nano /etc/fstab
tmpfs /tmp tmpfs nodev,nosuid,relatime,size=50M 0 0

Changes in crontab to reboot at regular intervals each Monday, Wednesday, Friday. Linux should run stable but better to do some reboots from time to time.

sudo crontab -e
0 16 * * 1,3,5 /sbin/reboot

Changes in inittab to auto-start the camera app:

sudo nano /etc/inittab
Change
1:2345:respawn:/sbin/getty 115200 tty1
to
#1:2345:respawn:/sbin/getty 115200 tty1
Add
1:2345:respawn:/bin/login -f pi tty1 /dev/tty1 2>&1

Changes in .profile to auto-start the camera app and distinguish between interactive and non-interactive shells:

if [ $(tty) == /dev/tty1 ]; then
date > last_login.txt
./run.sh&
fi

Don’t forget to protect the path when downloading to FTP. Create .htaccess file in resp. folder:

AuthType Basic
AuthName „Password Protected Area“
AuthUserFile /complete_path/.htpasswd
Require valid-user

Create .htpasswd in ssh:

htpasswd -c .htpasswd user
Connect with: