Raspberry-Pi IPcam & Pedestrian detector alarm falseless, with smooth DVR

This guide permit to use a Raspberry-Pi zero(W) as a Pedestrian detector and smooth DVR,
but given the low speed of board max resolution can be 640×480.
Anyhow i’ll describe how use a PI3 and with it resolution can be at least 1280×720.
With wifi connection and PI3 also Ethernet.

What’s you need:
– Raspberry Pi ZeroW or PI3
– Video input as Webcam or ELP camera or original raspberry camera board
– Power supply 5 Volt with at least 2 Ampere out
– At least 8 Gb Sd memory.
Raspian Lite
– Pc debian like as Ubuntu for work around

Start with pc linux terminal, write Raspian.img into Sd with:
sudo dd if=raspian.img of=/dev/sdb
Change path and name of raspian.img as your path and real usb device.

Now last operation with SD on Pc linux for configure Wireless Lan or Ethernet. Edit in the sd card sudo nano /etc/network/interfaces as below:
auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-ssid your-SSID
wpa-psk your-password

Save and put sd into Raspberry, discover IP and if nothing wrong you can be abled to connect to it with SSH client.
In recent version of Raspian SSH Service is disabled at boot, enable it creating an empty file named ssh into /boot/.
Connect at Raspi from pc in SSH with ssh pi@(raspi_IP) password raspberry.

Install Motion
Motion acts as motion detector, script launcher, and raw video file preparer, install it with:
sudo apt-get update
sudo apt-get install motion

Install Opencv
Opencv is the video processor install with:
sudo apt-get install libopencv-dev python-opencv
sudo apt-get install python-pip
sudo pip install imutils
sudo pip install -- upgrade imutils

Install ffmpeg OMX:
FFmpeg omx is advantageous for use GPU Hardware acceleration encoding.
git clone https://github.com/FFmpeg/FFmpeg.git
cd FFmpeg
sudo ./configure --arch=armel --target-os=linux --enable-gpl --enable-omx --enable-omx-rpi --enable-nonfree

These commands download FFmpeg and sets the build configuration.
(RPI 2/3) The next command you want to run is:
sudo make -j4
(RPi 1, ZeroW) The next command you want to run is:
sudo make
This command builds FFmpeg.
The next command you want to run is:
sudo make install
This command installs FFmpeg into your system.

Motion configurations
sudo nano /etc/motion/motion.conf insert/edit:

daemon on
width 640 ###1280 x pi3###
height 480 ###720 x pi3###
framerate 30
threshold 1500 ###2500 x pi3###
output_pictures center
ffmpeg_output_movies off
use_extpipe on
extpipe ffmpeg -y -f rawvideo -pix_fmt yuv420p -video_size 640x480 -framerate %fps -i pipe:0 -c:v h264_omx -b:v 1000k -f mp4 %f.mp4 ###640 x 480###
extpipe ffmpeg -y -f rawvideo -pix_fmt yuv420p -video_size 1280x720 -framerate %fps -i pipe:0 -c:v h264_omx -b:v 1000k -f mp4 %f.mp4 ###1280 x 720###
target_dir /home/pi/motion ###Create it with sudo mkdir /home/pi/motion##
picture_filename image
movie_filename %Y%m%d%H%M%S
stream_port 8081
stream_localhost off
stream_auth_method 1
stream_authentication user:password
on_movie_start su pi -c 'python /home/pi/open2.py --video %f.mp4'
on_movie_end su pi -c 'python /home/pi/end.py'

save and close.

Disk check at boot
To enable disk check at boot edit:
sudo nano /etc/default/rcS

Autostart motion daemon
sudo nano /etc/rc.local
/usr/bin/motion start first of line exit 0
save and close.
Scheduled restart at 9 am and delete video file after 2 days

sudo nano crontab -e
0 9 * * * /sbin/shutdown -r now
*/59 * * * * /etc/init.d/delete.sh 2>&1 > /dev/null

save and close.
sudo nano /etc/init.d/delete.sh
sudo find /home/pi/motion/*.* -mtime +2 -exec rm -R {} \;
save and close.

Python OpenCV script

First python script, open2.py is launched from motion and it write video filename into /home/pi/IOPEN.txt.
create it with nano /home/pi/open2.py
import argparse
import os
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video")
args = vars(ap.parse_args())
videoPath = (args["video"])
out_file = open("/home/pi/IOPEN.txt","w")

save and close.

Second python script end.py work as Pedestrian detector and it is used to launch other alarm and mail notification.
Click here to download end
rename it to end.txt in end.py and copy in /home/pi/

if you want you can only examine an area of ​​the image, in fact in the script are commented the lines about the cropping go here for detailed setting of cropping.
Is possible to set some value as “Winstride, Padding and Scale” also resolution of pedestrian detector can be changed if person not recognized. Follow this page.

Send mail notification
For receive a mail when pedestrian is detected use this script mail , it work with gmail addresses and you need to decrease security of login in that account.
Rename mail.txt in mail.py and save it into /home/pi/

Gpio alarm out
For activate a physics siren alarm out, create in /home/pi/siren.py
import os
import time
import RPi.GPIO as GPIO
sirena = 12
GPIO.setup(sirena, GPIO.OUT, initial=0)
GPIO.output(sirena, True)
print("Siren On")
GPIO.output(sirena, False)

Gpio 12 have to be connected as below:

EXTRA FEATURES coming soon
AP features
remote activation (dynamic dns, app android)

Leave a Reply

Your email address will not be published. Required fields are marked *