Coder Social home page Coder Social logo

amboxer21 / motiondetection Goto Github PK

View Code? Open in Web Editor NEW
25.0 3.0 9.0 23.23 MB

A system that monitors motion with Python's OpenCV from a webcam and allows remote viewing of the webcam from an android app. The system takes pictures when it detects motion then emails those pictures. The android app allows you to remotely view the cam anytime.

License: GNU General Public License v3.0

Python 60.34% Java 21.43% Shell 3.59% HTML 12.46% JavaScript 1.91% CSS 0.27%
opencv python motion-detection android raspberry-pi-3 rpi3 opencv-contrib webcam monitors-motion security-camera

motiondetection's Introduction

[Description]

This highly configurable framework monitors motion from a USB webcam on a Raspberry Pi using the OpenCV API. Once motion is detected by the system, it takes a picture of what set off the framework then E-mails the picture(s) to you. It also affords the ability to remotely view that webcam from an android app from anywhere in the world at anytime. So after you’re notified via E-mail, then you have the option of checking the camera’s live feed if you’d like. If you need an image made, I can make an sdcard for you and send it via USPS(mail). If you need an image or have any questions, send an E-mail to [email protected].

[Notice]

In order for the app to work remotely, you need to forward ports on your router. The default ports for the Android app are port 5000 and port 50050.

[Todo]

(SHOULD BE RESOLVED) As of 2021-01-06 I have found out that the latest data image tarball is broken and needs to be fixed. Using the old tarball will work until the latest is fixed.

Redesign the Android app's UI. See GitHub issue #50.


MotionDetection(CCTV) with Python3 on the Raspberry Pi powered by Gentoo Linux

Note: You can send me an E-mail if you have any trouble and I will be happy to help you - [email protected].

[Videos]

Demo from Aug 9, 2020 - Find a quick demo HERE

Booting - Booting the system after installation HERE

Installing MotionDetection on an sdcard - POC of installation HERE

Android App - POC of Android app HERE


[Credentials]

For use with the installer script.

Username: pi

Password: RASPBERRY pi 3


[Downloads]:

[ LATEST ] Download the Motiondetection system data tarball HERE.

Notice: ^^ This tarball is essential to buillding an sdcard! Without it the system won't work.

[ LATEST ] Download the stage3 tarball for the RPI3 and the RPI4 HERE.

Notice: ^^ This tarball is necessary if you want to roll your own image instead of using mine.


[Installing MotionDetection on your PI]

This is an installer script that needs to be run in the same directory as the pi tarball! The script works on both the Raspberry PI 3b and 4b!!

#!/bin/bash

echo "[ INFO ] Checking for presence of pi.tar.gz tarball.";
if [[ -e $(ls pi.tar.gz 2> /dev/null) ]] ; then
    echo "[ INFO ] pi.tar.gz found.";
else
    echo "[ ERROR ] The pi.tar.gz tarball needs to be in the same directory as this script.";
    exit;
fi

echo "[ INFO ] Checking for presence of sdcard at /dev/mmcblk";
if (( var=$(sudo fdisk -l /dev/mmcblk0 1>& /dev/null) $? == 0 )) ; then
    echo "[ INFO ] Found sdcard.";
else
    echo "[ ERROR ] Cannot find sdcard.";
    exit;
fi

mountpoint=$(mount | awk '/mmcblk0p2/{print $3}');

if [[ $mountpoint ]] ; then
    sudo umount -R $mountpoint;
fi

echo "[ INFO ] Partitioning sdcard.";
for n in {1..4}; do parted -a optimal /dev/mmcblk0 rm $n 2> /dev/null; done
sudo parted -a optimal /dev/mmcblk0 mkpart primary fat32 0% 513MB
sudo parted -a optimal /dev/mmcblk0 mkpart primary ext4 513MB 100%

echo "[ INFO ] Creating FAT32 filesystem on /dev/mmcblk0p1.";
echo 'y' | sudo mkfs.vfat -F32 /dev/mmcblk0p1

echo "[ INFO ] Creating EXT4 filesystem on /dev/mmcblk0p2.";
echo 'y' | sudo mkfs.ext4 /dev/mmcblk0p2

echo "[ INFO ] Checking if mountpoint /mnt/pi exists.";
if [[ -e /mnt/pi ]] ; then
    echo "[ INFO ] Mountpoint /dev/pi exists.";
else
    sudo mkdir -p /mnt/pi;
    echo '[ WARNING ] /mnt/pi doesnt exist - creating it now.';
fi

echo "[ INFO ] Mounting /dev/mmcblk0p2 on /mnt/pi";
sudo mount /dev/mmcblk0p2 /mnt/pi;

echo "[ INFO ] Checking if mountpoint /mnt/pi/boot exists.";
if [[ -e /mnt/pi/boot ]] ; then
    echo "[ INFO ] Mountpoint /dev/pi/boot exists.";
else
    sudo mkdir -p /mnt/pi/boot;
    echo '[ WARNING ] /mnt/pi/boot doesnt exist - creating it now.';
fi

echo "[ INFO ] Mounting /dev/mmcblk0p1 on /mnt/pi/boot";
sudo mount /dev/mmcblk0p1 /mnt/pi/boot;

echo "[ INFO ] Unpacking tarball onto your sdcard.";
tar -xzf pi.tar.gz -C /mnt/pi/ ;

echo "[ INFO ] Unmounting your sdcard now.";
sudo umount -R /mnt/pi ;

Example script output:

root@ghost ~ $ sudo bash make-sdcard.sh 
[ INFO ] Checking for presence of pi.tar.gz tarball.
[INFO] pi.tar.gz found.
[ INFO ] Checking for presence of sdcard at /dev/mmcblk
[ INFO ] Found sdcard.
[ INFO ] Partitioning sdcard.
Information: You may need to update /etc/fstab.                           

Information: You may need to update /etc/fstab.                           

[ INFO ] Creating FAT32 filesystem on /dev/mmcblk0p1.                     
mkfs.fat 4.1 (2017-01-24)
[ INFO ] Creating EXT4 filesystem on /dev/mmcblk0p2.
mke2fs 1.45.5 (07-Jan-2020)
Discarding device blocks: done                            
Creating filesystem with 15511296 4k blocks and 3883008 inodes
Filesystem UUID: fb8a85ff-1c19-4e64-a0e4-80c1e49167f5
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done   

[ INFO ] Checking if mountpoint /mnt/pi exists.
[ INFO ] Mountpoint /dev/pi exists.
[ INFO ] Mounting /dev/mmcblk0p2 on /mnt/pi
[ INFO ] Checking if mountpoint /mnt/pi/boot exists.
[ WARNING ] /mnt/pi/boot doesnt exist - creating it now.
[ INFO ] Mounting /dev/mmcblk0p1 on /mnt/pi/boot
[ INFO ] Unpacking tarball onto your sdcard.
[ INFO ] Unmounting your sdcard now.
root@ghost ~ $

[Rolling a Raspberry Pi image]:

  • Install base system

  • Update your system

  • Emerge rust-bin

  • Emerge ffmpeg

  • Emerge OpenCV

  • Emerge mail utils

emerge -av acct-group/mail acct-user/mail dev-perl/MailTools net-mail/mailbase

NOTE: It is important to emerge rust-bin because compiling the regular rust package takes up a lot of resources and is prone to breaking on the arm arch! You don't want to spend days hacking this install when you can just install the bin version!

Installing base system

Notes:
  1. You must be root while running these commands! sudo -i NOT sudo command
  2. Run the command below seperately from the rest of the script and wait for it to finish before continuing!
umount -R /mnt/gentoo
parted /dev/mmcblk0 mklabel msdos

Continue!

for n in {1..4}; do echo -e 'y' | parted /dev/mmcblk0 rm $n 2>/dev/null; done
parted /dev/mmcblk0 mkpart primary fat32 0% 513MB
parted /dev/mmcblk0 mkpart primary linux-swap 513MB 2561MB
parted /dev/mmcblk0 mkpart primary ext4 2561MB 100%
parted /dev/mmcblk0 p

mkfs.vfat -F32 /dev/mmcblk0p1
mkswap /dev/mmcblk0p2
echo 'y' | mkfs.ext4 /dev/mmcblk0p3

mount /dev/mmcblk0p3 /mnt/gentoo
mkdir /mnt/gentoo/boot
mount /dev/mmcblk0p1 /mnt/gentoo/boot

tar xzvf rpi4b.tar.gz
cd rpi4b
tar xzvf gentoo.tar.gz

time rsync -ra gentoo/* /mnt/gentoo/

wget http://gentoo.osuosl.org/releases/arm/autobuilds/current-stage3-armv7a_hardfp/stage3-armv7a_hardfp-20200509T210605Z.tar.xz
tar xvf stage3-armv7a_hardfp-20200509T210605Z.tar.xz -C /mnt/gentoo/

wget http://distfiles.gentoo.org/snapshots/portage-latest.tar.bz2
tar xjvf portage-latest.tar.bz2 -C /mnt/gentoo/usr

git clone --depth 1 git://github.com/raspberrypi/firmware/
cd firmware/boot
cp -r * /mnt/gentoo/boot/
cp -r ../modules /mnt/gentoo/lib/

echo -e "/dev/mmcblk0p1 /boot auto noauto,noatime 1 2\n/dev/mmcblk0p3 / ext4 noatime 0 1\n/dev/mmcblk0p2 none swap sw 0 0" >> /mnt/gentoo/etc/fstab

echo "dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p3 rootfstype=ext4 elevator=deadline rootwait" > /mnt/gentoo/boot/cmdline.txt

cp /mnt/gentoo/usr/share/zoneinfo/America/New_York /mnt/gentoo/etc/localtime

echo "America/New_York" > /mnt/gentoo/etc/timezone

sed -i 's/^root:.*/root::::::::/' /mnt/gentoo/etc/shadow

cd

umount -R /mnt/gentoo

Update your system

I would suggest installing rust-bin before updating @world!

(1) emerge --sync
(2) emerge -av --deep --newuse --update @world

Note: Before updating your system, create this file and copy these contents into that file to circumvent the gpg syncing errors

anthony@anthony ~ $ cat /etc/portage/repos.conf/gentoo.conf 
[DEFAULT]
main-repo = gentoo

[gentoo]
location = /usr/portage
sync-type = rsync
auto-sync = yes
sync-uri = rsync://rsync.us.gentoo.org/gentoo-portage
sync-rsync-verify-metamanifest = no
anthony@anthony ~ $ 

Emerge rust-bin

emerge -av rust-bin

Compiling OpenCV

Create these file and set the use flags before emerging OpenCV and ffmpeg!

Note: Install ffmpeg before installing OpenCV!

[Package USE Flags]

/etc/portage/package.use/ffmpeg

virtual/ffmpeg encode mp3 threads mp3 threads x264 jack alsa blueray libv4l v4l lzma twolame opengl openssl oss pulseaudio sdl vorbis xvid gsm opus speex vaapi vdpau -theora -truetype -libav
media-video/ffmpeg alsa bzip2 encode gmp gpl hardcoded-tables iconv jack libv4l lzma mp3 network opengl openssl oss postproc pulseaudio sdl threads twolame v4l vorbis x264 x265 xvid zlib bluray cdio chromium cpudetection debug doc fontconfig gcrypt gnutls gsm libass libdrm openh264 opus speex svg vaapi wavpack vdpau -theora -truetype -static-libs -amrenc -amr

/etc/portage/package.use/alsa-plugins

media-plugins/alsa-plugins pulseaudio

/etc/portage/package.use/gst-plugins-base

media-libs/gst-plugins-base -opengl

/etc/portage/package.use/libcrypt

virtual/libcrypt static-libs

/etc/portage/package.use/libglvnd

media-libs/libglvnd X

/etc/portage/package.use/libv4l

media-libs/libv4l jpeg

/etc/portage/package.use/mesa

media-libs/mesa X

/etc/portage/package.use/numpy

dev-python/numpy lapack

/etc/portage/package.use/opencv

media-libs/opencv v4l png jpeg gstreamer python ffmpeg contrib lapack -opengl

/etc/portage/package.use/openssl

dev-libs/openssl sslv3

/etc/portage/package.use/zlib

sys-libs/zlib abi_x86_64 minizip

/etc/portage/package.use/pulseaudio

media-sound/pulseaudio bluetooth jack dbus sox ofono-headset native-headset libsamplerate CPU_FLAGS_ARM: -neon

NOTE: For pulseaudio, the neon flags causes the compilation to crash when emerging if this flag is set.

/etc/portage/package.use/gcc

sys-devel/gcc objc objc++ objc-gc

NOTE: For GCC, I ran into issues compiling cmake and the solution was to recompile gcc with these use flags. I had to recompile quite a few times. Compiling gcc with these USE flags in the beginning could save you the same trouble!

Error message:

-- Checking if compiler supports needed C++17 constructs - yes
-- Checking if compiler supports C++ make_unique
-- Checking if compiler supports C++ make_unique - no
-- Checking if compiler supports C++ unique_ptr
-- Checking if compiler supports C++ unique_ptr - no
CMake Error at CMakeLists.txt:107 (message):
  The C++ compiler does not support C++11 (e.g.  std::unique_ptr).


-- Configuring incomplete, errors occurred!
See also "/var/tmp/portage/dev-util/cmake-3.17.4-r1/work/cmake-3.17.4_build/CMakeFiles/CMakeOutput.log".
See also "/var/tmp/portage/dev-util/cmake-3.17.4-r1/work/cmake-3.17.4_build/CMakeFiles/CMakeError.log".
 * ERROR: dev-util/cmake-3.17.4-r1::gentoo failed (configure phase):
 *   cmake failed

[Masked packages]

/etc/portage/package.mask/cmake

>=dev-util/cmake-3.17.4-r1

[Accept keywords]

/etc/portage/package.accept_keywords/eigen

dev-cpp/eigen ~arm

/etc/portage/package.accept_keywords/opencv

media-libs/opencv ~arm

[make.conf]

# These settings were set by the catalyst build script that automatically
# built this stage.
# Please consult /usr/share/portage/config/make.conf.example for a more
# detailed example.
COMMON_FLAGS="-O2 -pipe -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=hard"
CFLAGS="${COMMON_FLAGS}"
CXXFLAGS="${COMMON_FLAGS}"
FCFLAGS="${COMMON_FLAGS}"
FFLAGS="${COMMON_FLAGS}"
FEATURES="ccache"
CCACHE_DIR="/var/cache/ccache
CPU_FLAGS_ARM="edsp neon thumb vfp vfpv3 vfpv4 vfp-d32 crc32 v4 v5 v6 v7 thumb2"

# WARNING: Changing your CHOST is not something that should be done lightly.
# Please consult https://wiki.gentoo.org/wiki/Changing_the_CHOST_variable before changing.
CHOST="armv7a-unknown-linux-gnueabihf"
USE="bluetooth ffmpeg jpeg png gif curl -X -pulseaudio -gtk -qt -qt5 -qt4 -consolekit -static-libs -cups -systemd"

# NOTE: This stage was built with the bindist Use flag enabled

# This sets the language of build output to English.
# Please keep this setting intact when reporting bugs.
LC_MESSAGES=C

[Building OpenCV from source on x86_64]

OpenCV is built from source so that you can use the opencv_createsamples program which was removed in later versions of OpenCV and is not present in Gentoo's upstream version. The opencv_createsamples command is used to help fascilitate in creating your own custom facial recognition models.

pi@localhost ~ $ sudo -i
Password: 
localhost ~ # cd /usr/src/
localhost /usr/src # git clone https://github.com/opencv/opencv.git -b 3.4
localhost /usr/src # git clone https://github.com/opencv/opencv_contrib.git -b 3.4
localhost /usr/src # cd /usr/src/opencv/opencv
localhost /usr/src/opencv # mkdir build
localhost /usr/src/opencv # cd build
localhost /usr/src/opencv/build # cmake -DCAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local/opencv-3.4 -DINSTALL_PYTHON_EXAMPLES=ON -DWITH_V4L=ON -DWITH_OPENGL=ON -DWITH_OPENCL=OFF -DWITH_VTK=OFF -DWITH_QT=OFF -DOPENCV_EXTRA_MODULES_PATH=/usr/src/opencv_contrib/modules -DBUILD_opencv_apps=ON -DBUILD_EXAMPLES=ON ..

CMake command

cmake -DCAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local/opencv-3.4 -DINSTALL_PYTHON_EXAMPLES=ON -DWITH_V4L=ON -DWITH_OPENGL=ON -DWITH_OPENCL=OFF -DWITH_VTK=OFF -DWITH_QT=OFF -DOPENCV_EXTRA_MODULES_PATH=/usr/src/opencv_contrib/modules -DBUILD_opencv_apps=ON -DBUILD_EXAMPLES=ON ..

[Changing Motiondetection options using a GUI]:

You can change the options that the Motiondetection framework runs with by opening your favorite browser, entering your Rapsberry Pi's IP address - i.e., 192.168.1.232. Here you can change options like the E-mail that the pictures are sent to, burst mode count, etc. A short demonstration can be found HERE.


[Resources]:

stage3-armv7a_hardfp-20200509T210605Z.tar.xz

Raspberry Pi Gentoo wiki

MotionDetection Framework rsync data

Android app to view camera's feed


[Change Log]

[ OLD ][As of 2020-08-03] Currently fixing up this repo's code and working on a new Gentoo based ISO image

[ OLD ][As of 2020-08-03] You can now update the application's configuration options in the GUI using your favorite browser. Now all you have to do is power up the RPI/USB camera, open a browser, navigate to a specified URL, reload, then the program will use your updated options. Watch a short demo of the webconfigurator HERE.

[ OLD ][As of 2020-08-09] The framework that runs on Raspbian has been deprecated in favor of Gentoo Linux. The Raspbian system will no longer be updated but will be left in tact.

[ OLD ][As of 2020-08-09] Daemon support was removed from the framework. You will either have to set it up yourself or roll an image. The image is plug-and-play, power up the RPI4b and it just works.

[ OLD ][As of 2020-08-09] The gentoo install is complete and the system automaticcaly comes up on boot - The system is plug and play.

[ OLD ][As of 2020-08-20] The system would crash after being triggered. A new symlink feature caused the issue and I have since changed that symlink call to a copy(cp) call in the motiondetection.py file in this commit. New rsync data with the new changes has been uploaded this morning.

[ OLD ][As of 2020-10-28] I have written an installer script named make-sdcard.sh that installs an image that works on my my Raspberry PI 3 and 4. The size of the card does not matter either. The idea of a DD'able system image has been put on the back burner for now. The installer script is super simple to use anyway!

[ LATEST ][As of 2020-11-20] I have fixed the broken ANdroid app functionality. The remote veiwing of the live camera feed via android app is now working!


[SCREEN SHOTS]

alt text alt text alt text alt_text



Since Nov 27, 2017

motiondetection's People

Contributors

amboxer21 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

motiondetection's Issues

Too many photos stop the system from working

If the system takes too many pictures the system stops working around 700-800 pictures. My cat sets off the system often during the day while I'm at work and the pictures pile up which results in the system no longer working. I have to figure out a way to circumvent that. Maybe either automatically delete the pictures or compress and email them but leave the choice up to the user.

Infinite scroll for viewing logfile in the webconfigurator

When clicking on the log tab in the webconfigurator, the loading into the view takes too long. There needs to be some kind of asynch non-blocking read coded in or something to prevent that page from hanging if the logs are too large.

Shifted images cause false positives

Sometimes the images shift causing a false positive and this in turn sends an E-mail and resets the framework. I need to check if the image is shifted if motion is detected before sending. (motiondetection.py)

I need a way to stop the app

I need to either stop the app automatically when I am home or add a button to allow me to stop the motion detection system. At the very least I would like to stop the emails from being sent!

New heartbeat server to monitor

Currently the heartbeat monitor for the commands server is being monitored but the remote viewer server is not. This server/port needs to be added to the heart.py script. This server however cannot be checked like the commands server with a simple ping. Instead, if connecting to the remote viewer server fails, we should send a signal to the commands server to reboot. Also, a custom handler to the android app will be needed. It should say re-initializing while the RPI3 is rebooted. I removed any GUI functionality upon booting from the RPI3, so the boot process should be fast!

Add MySQL database support

I need to add MySQL database support. The database will store things like white listed mac addresses, whether or not the system is disabled, etc.

create an ROR(rails) UI to configure the system.

A UI needs to be created using Ruby on rails. I would like to keep the language base the same but I use rails at work and am comfortable with ROR. Maybe in the future it will be converted into DJango or if someone satisfies this issue, they can write it in DJango.

tuple decorator needs to be fixed

The tuple decorator in the accepts class isnt working properly and needs to be fixed so it can work with the server classes message handler. (motiondetection.py)

Ability to record the live stream

The ability to record the live stream via button in the Android app should be added! Whether it be done by screen recording in the Android app or saving the stream server side - Server side would be preferable! (Android App or motiondetection.py)

Better variable names

Better variable names are needed for the connected devices/access list feature! MotionDetection.allowed is not very descriptive!

access list needed

An access list for mac addresses needs to be added. So if the person home has their mac address in the access list the mail and image capture section of the code wont run.

better button design

Better buttons need to be added. SO instead of the text inside of the buttons, I think a triangle for the start button would be better. As well as a square for the stop button. I think the buttons are fine as they are but the text needs to be replaced with these icons. They must be transparent as well and follow the same design as the circle. That being transparent inside with slightly darker in color for the border. (Android App)

Stripped down version of netstat

I need a stripped down version of netstat to solely run the -antp switches and then parse the output. This has to be done in the C programming language and ran as a demon. The idea is the keep track of the amount of processes running and kill the extras.

Android app UI needs updating

The android app was built for a much older version of android and needs to be redesigned. It should also conform to android 11 UI design standard.

default image for when feed isnt live

A default picture needs to be set when the feed isn't live inside of the WebView frame. So maybe taking a picture of whats in the WebView while the feed is live and using that when going back to the app each time until you go live again then repeating the process. Maybe that can be the first thing the app does when viewing live feed?! (Android App)

add guest entry node in the wpa_supplicant.conf file

Add a static guest entry to the wpa_supplicant.conf file in the etc dir. This will allow smeone to configure a guest network on their LAN without having to E-mail me. The credentials will be in the README.

A Gentoo build is needed

I am using Raspbian at the moment and I am having trouble with the file system becoming corrupted. Installing Gentoo on my Raspberry Pi 4b and installing the framework over it is the goal.

config file to pull values from

Use a config script to pull from instead of hard coded values. This way if the email address needs to be changed, you can change it in one place rather than 3.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.