Raspberry Pi2 as surveillance cam?

Non Joggler related discussion.
Post Reply
User avatar
dwl99
Posts: 845
Joined: Fri Mar 04, 2011 7:38 am
Location: Glasgow

Raspberry Pi2 as surveillance cam?

Post by dwl99 » Sun Nov 15, 2015 7:35 pm

I've been meaning to get around to rigging up an IP cam at my front door, ideally something that will start recording when it detects motion. I have a Pi2, a Pi camera module and a spare Logitech HD webcam that is supposed to be compatible with Linux. Can anyone give me some pointers? The last time I looked at this there was no graphical GUI for the camera module and the command line inputs looked a bit beyond my capabilities.

jim_lewis1
Posts: 145
Joined: Wed Mar 26, 2014 4:44 pm

Re: Raspberry Pi2 as surveillance cam?

Post by jim_lewis1 » Sun Nov 15, 2015 9:52 pm

There's a good write up on instructables.com for one
I searched pi surveillance camera

I like your sig pic!

User avatar
dwl99
Posts: 845
Joined: Fri Mar 04, 2011 7:38 am
Location: Glasgow

Re: Raspberry Pi2 as surveillance cam?

Post by dwl99 » Sun Nov 15, 2015 10:10 pm

Thanks, I had come up with a few options such as Raspbian, Motion Pie and Zoneminder but was interested in what people thought. Raspbian & ZoneMinder probably look a bit too complex for me.

User avatar
pete
Posts: 2525
Joined: Mon Aug 01, 2011 6:33 am
Location: Time Traveler

Re: Raspberry Pi2 as surveillance cam?

Post by pete » Tue Nov 17, 2015 10:58 pm

Yup here purchased a few Logitech webcams for Joggler playing. They work well with Skype.

I wanted to build a Dick Tracey watch but the Logitech and Joggler wouldn't fit on my wrist.

Their are a few Ubuntu programs out there that see the Logitech web cam from the get go.

Been playing a bit with a time lapse video thing which works well on the Joggler / RPi2.

The compression pieces from mpeg to mp4 are a bit heavy on the CPU.

I have only tested this stuff on my IP cameras. It works well and I share the time lapse videos out to KODI. It is fun to watch.

Been a Zoneminder user here for many years. With the new HD IP cameras you do need more CPU / Memory these days.

There have been some folks running it on the RPi2 these days.

It is a python script.

Here are are my installation Python files. I installed everything in the root drive /timelapse and share the drive out via SAMBA.
Add the font file right to the directory.


1 - sudo apt-get install python-imaging * need this for the imaging pieces
2 - Main python capture script:

Code: Select all

#!/usr/bin/python

# Number of seconds between frames:
LAPSE_TIME = 30

# Name of truetype font file to use for timestamps (should be a monospace font!)
FONT_FILENAME = "UbuntuMono-B.ttf"

# Format of timestamp on each frame
TIMESTAMP_FORMAT = "%Y-%m-%d %H:%M:%S"

# Command to batch convert mjpeg to mp4 files:
# for f in *.mjpeg; do echo $f ; avconv -r 30000/1001 -i "$f" "${f%mjpeg}mp4" 2>/dev/null ; done

import urllib
import sys, time, datetime
import StringIO
import Image, ImageDraw, ImageFont

class Camera:
def __init__(self, name, url, filename):
self.name = name
self.url = url
self.filename = filename

def CaptureImage(self):
camera = urllib.urlopen(self.url)
image_buffer = StringIO.StringIO()
image_buffer.write(camera.read())
image_buffer.seek(0)
image = Image.open(image_buffer)
camera.close()
return image

def TimestampImage(self, image):
draw_buffer = ImageDraw.Draw(image)
font = ImageFont.truetype(FONT_FILENAME, 16)
timestamp = datetime.datetime.now()
stamptext = "{0} - {1}".format(timestamp.strftime(TIMESTAMP_FORMAT), self.name)
draw_buffer.text((5, 5), stamptext, font=font)

def SaveImage(self, image):
with open(self.filename, "a+b") as video_file:
image.save(video_file, "JPEG")
video_file.flush()

def Update(self):
image = self.CaptureImage()
self.TimestampImage(image)
self.SaveImage(image)
print("Captured image from {0} camera to {1}".format(self.name, self.filename))


if __name__ == "__main__":
cameras = []
cameras.append(Camera("porch", "http://username:[email protected]<script data-cfhash='f9e31' type="text/javascript">
/*  */</script>/SnapshotJPEG?Resolution=640x480&Quality=Clarity", "cam1.mjpeg"))
cameras.append(Camera("driveway", "http://username:[email protected]
/*  *//SnapshotJPEG?Resolution=640x480&Quality=Clarity", "cam2.mjpeg"))
cameras.append(Camera("backyard", "http://username:[email protected]
/*  *//SnapshotJPEG?Resolution=640x480&Quality=Clarity", "cam3.mjpeg"))
cameras.append(Camera("sideyard", "http://10.17.42.176/image/jpeg.cgi", "cam4.mjpeg"))
cameras.append(Camera("stairway", "http://10.17.42.175/image/jpeg.cgi", "cam5.mjpeg"))

print("Capturing images from {0} cameras every {1} seconds...".format(len(cameras), LAPSE_TIME))

try:
while (True):
for camera in cameras:
camera.Update()

time.sleep(LAPSE_TIME)

except KeyboardInterrupt:
print("\nExit requested, terminating normally")
sys.exit(0)
4 - Convert to MP4 script:

Code: Select all

#!/bin/bash

echo "Removing old files..."
rm -fv *.mp4

echo "Converting files to mp4..."
for f in *.mjpeg ; do
t=${f%mjpeg}mp4
echo " Converting $f to $t"
avconv -r 30000/1001 -i "$f" -q 5 "$t" 2>/dev/null
done

echo "Done!"
- Pete

Auto mater

User avatar
dwl99
Posts: 845
Joined: Fri Mar 04, 2011 7:38 am
Location: Glasgow

Re: Raspberry Pi2 as surveillance cam?

Post by dwl99 » Tue Nov 17, 2015 11:08 pm

Thanks Pete, this looks a little advanced for me, I'm more in the market for an off-the-peg solution for the hard-of thinking :-)

User avatar
pete
Posts: 2525
Joined: Mon Aug 01, 2011 6:33 am
Location: Time Traveler

Re: Raspberry Pi2 as surveillance cam?

Post by pete » Wed Nov 18, 2015 2:06 am

Zoneminder should work for you.

Here are the instructions for running it on a n RPi2

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install apache2
sudo apt-get install zoneminder
sudo apt-get install v4l-utils

# Allow www-data to access webcam:
sudo usermod -aG video www-data

# Add CGI ScriptAlias if not present after installing:
sudo nano /etc/zm/apache.conf

# Correct ownership/give write+read+execute dir access to www-data group under /var/cache/zoneminder/*
sudo chown -R root:www-data /var/cache/zoneminder/*
sudo find /var/cache/zoneminder/ -type d -exec chmod 775 {} +

# Correct Perl memory bug in zoneminder code if present:
# REF : http://www.freshports.org/multimedia/zo ... mory.pm.in
sudo nano /usr/share/perl5/ZoneMinder/Memory.pm

# line 130 - Change from:
-our $arch = int(3.2*length(~0));
# To:
+our $arch = 32;

# Find formats of webcam for zoneminder config:
zmu -d /dev/video0 -vqV2
# If that does not work or fails to enumerate, try:
v4l2-ctl --list-formats

4. Kernel shared memory settings:

# Set shared memory for 512MB RPi board:
# 128MB shhmax shared:
sudo su -
echo "kernel.shmmax = 134217728" >> /etc/sysctl.conf
exit
# 2MB shmall pages:
sudo su -
echo "kernel.shmall = 2097152" >> /etc/sysctl.conf
exit
- Pete

Auto mater

User avatar
dwl99
Posts: 845
Joined: Fri Mar 04, 2011 7:38 am
Location: Glasgow

Re: Raspberry Pi2 as surveillance cam?

Post by dwl99 » Wed Nov 18, 2015 7:09 am

Thanks for this - I assume the starting point is Raspian? I'll take a deep breath & give it a go.

User avatar
pete
Posts: 2525
Joined: Mon Aug 01, 2011 6:33 am
Location: Time Traveler

Re: Raspberry Pi2 as surveillance cam?

Post by pete » Wed Nov 18, 2015 3:16 pm

I just googled the above so I am not sure. You cannot break anything writing an RPi2 image to an microSD card.

I currently am using two RPi2 Wheezys. One is running CumulusMX and the other is running Homeseer. Both utilize mono.

While the RPi2 is much nicer than the RPi1; its still rather a bit underpowered. ZM captures jpg stills to make videos; a bit primitive and works well.

Here is a short timelapse video that I uploaded to MS using the RPi2.

http://1drv.ms/1LBTG7h

It is easiest to download it then play it back. It is a few days worth.
- Pete

Auto mater

Post Reply