项目作者: amboxer21

项目描述 :
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.
高级语言: Python
项目地址: git://github.com/amboxer21/MotionDetection.git
创建时间: 2017-11-27T19:19:44Z
项目社区:https://github.com/amboxer21/MotionDetection

开源协议:GNU General Public License v3.0

下载


[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 amboxer21@gmail.com.

[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 - amboxer21@gmail.com.

[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!!
```javascript

!/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 ;

  1. Example script output:
  2. ```javascript
  3. root@ghost ~ $ sudo bash make-sdcard.sh
  4. [ INFO ] Checking for presence of pi.tar.gz tarball.
  5. [INFO] pi.tar.gz found.
  6. [ INFO ] Checking for presence of sdcard at /dev/mmcblk
  7. [ INFO ] Found sdcard.
  8. [ INFO ] Partitioning sdcard.
  9. Information: You may need to update /etc/fstab.
  10. Information: You may need to update /etc/fstab.
  11. [ INFO ] Creating FAT32 filesystem on /dev/mmcblk0p1.
  12. mkfs.fat 4.1 (2017-01-24)
  13. [ INFO ] Creating EXT4 filesystem on /dev/mmcblk0p2.
  14. mke2fs 1.45.5 (07-Jan-2020)
  15. Discarding device blocks: done
  16. Creating filesystem with 15511296 4k blocks and 3883008 inodes
  17. Filesystem UUID: fb8a85ff-1c19-4e64-a0e4-80c1e49167f5
  18. Superblock backups stored on blocks:
  19. 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
  20. 4096000, 7962624, 11239424
  21. Allocating group tables: done
  22. Writing inode tables: done
  23. Creating journal (65536 blocks): done
  24. Writing superblocks and filesystem accounting information: done
  25. [ INFO ] Checking if mountpoint /mnt/pi exists.
  26. [ INFO ] Mountpoint /dev/pi exists.
  27. [ INFO ] Mounting /dev/mmcblk0p2 on /mnt/pi
  28. [ INFO ] Checking if mountpoint /mnt/pi/boot exists.
  29. [ WARNING ] /mnt/pi/boot doesnt exist - creating it now.
  30. [ INFO ] Mounting /dev/mmcblk0p1 on /mnt/pi/boot
  31. [ INFO ] Unpacking tarball onto your sdcard.
  32. [ INFO ] Unmounting your sdcard now.
  33. 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!

  1. umount -R /mnt/gentoo
  2. parted /dev/mmcblk0 mklabel msdos

Continue!

  1. for n in {1..4}; do echo -e 'y' | parted /dev/mmcblk0 rm $n 2>/dev/null; done
  2. parted /dev/mmcblk0 mkpart primary fat32 0% 513MB
  3. parted /dev/mmcblk0 mkpart primary linux-swap 513MB 2561MB
  4. parted /dev/mmcblk0 mkpart primary ext4 2561MB 100%
  5. parted /dev/mmcblk0 p
  6. mkfs.vfat -F32 /dev/mmcblk0p1
  7. mkswap /dev/mmcblk0p2
  8. echo 'y' | mkfs.ext4 /dev/mmcblk0p3
  9. mount /dev/mmcblk0p3 /mnt/gentoo
  10. mkdir /mnt/gentoo/boot
  11. mount /dev/mmcblk0p1 /mnt/gentoo/boot
  12. tar xzvf rpi4b.tar.gz
  13. cd rpi4b
  14. tar xzvf gentoo.tar.gz
  15. time rsync -ra gentoo/* /mnt/gentoo/
  16. wget http://gentoo.osuosl.org/releases/arm/autobuilds/current-stage3-armv7a_hardfp/stage3-armv7a_hardfp-20200509T210605Z.tar.xz
  17. tar xvf stage3-armv7a_hardfp-20200509T210605Z.tar.xz -C /mnt/gentoo/
  18. wget http://distfiles.gentoo.org/snapshots/portage-latest.tar.bz2
  19. tar xjvf portage-latest.tar.bz2 -C /mnt/gentoo/usr
  20. git clone --depth 1 git://github.com/raspberrypi/firmware/
  21. cd firmware/boot
  22. cp -r * /mnt/gentoo/boot/
  23. cp -r ../modules /mnt/gentoo/lib/
  24. 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
  25. 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
  26. cp /mnt/gentoo/usr/share/zoneinfo/America/New_York /mnt/gentoo/etc/localtime
  27. echo "America/New_York" > /mnt/gentoo/etc/timezone
  28. sed -i 's/^root:.*/root::::::::/' /mnt/gentoo/etc/shadow
  29. cd
  30. umount -R /mnt/gentoo

Update your system

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

  1. (1) emerge --sync
  2. (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

  1. anthony@anthony ~ $ cat /etc/portage/repos.conf/gentoo.conf
  2. [DEFAULT]
  3. main-repo = gentoo
  4. [gentoo]
  5. location = /usr/portage
  6. sync-type = rsync
  7. auto-sync = yes
  8. sync-uri = rsync://rsync.us.gentoo.org/gentoo-portage
  9. sync-rsync-verify-metamanifest = no
  10. anthony@anthony ~ $

Emerge rust-bin

  1. 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

  1. 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
  2. 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

  1. media-plugins/alsa-plugins pulseaudio

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

  1. media-libs/gst-plugins-base -opengl

/etc/portage/package.use/libcrypt

  1. virtual/libcrypt static-libs

/etc/portage/package.use/libglvnd

  1. media-libs/libglvnd X

/etc/portage/package.use/libv4l

  1. media-libs/libv4l jpeg

/etc/portage/package.use/mesa

  1. media-libs/mesa X

/etc/portage/package.use/numpy

  1. dev-python/numpy lapack

/etc/portage/package.use/opencv

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

/etc/portage/package.use/openssl

  1. dev-libs/openssl sslv3

/etc/portage/package.use/zlib

  1. sys-libs/zlib abi_x86_64 minizip

/etc/portage/package.use/pulseaudio

  1. 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

  1. 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:

  1. -- Checking if compiler supports needed C++17 constructs - yes
  2. -- Checking if compiler supports C++ make_unique
  3. -- Checking if compiler supports C++ make_unique - no
  4. -- Checking if compiler supports C++ unique_ptr
  5. -- Checking if compiler supports C++ unique_ptr - no
  6. CMake Error at CMakeLists.txt:107 (message):
  7. The C++ compiler does not support C++11 (e.g. std::unique_ptr).
  8. -- Configuring incomplete, errors occurred!
  9. See also "/var/tmp/portage/dev-util/cmake-3.17.4-r1/work/cmake-3.17.4_build/CMakeFiles/CMakeOutput.log".
  10. See also "/var/tmp/portage/dev-util/cmake-3.17.4-r1/work/cmake-3.17.4_build/CMakeFiles/CMakeError.log".
  11. * ERROR: dev-util/cmake-3.17.4-r1::gentoo failed (configure phase):
  12. * cmake failed

[Masked packages]

/etc/portage/package.mask/cmake

  1. =dev-util/cmake-3.17.4-r1

[Accept keywords]

/etc/portage/package.accept_keywords/eigen

  1. dev-cpp/eigen ~arm

/etc/portage/package.accept_keywords/opencv

  1. media-libs/opencv ~arm

[make.conf]

  1. # These settings were set by the catalyst build script that automatically
  2. # built this stage.
  3. # Please consult /usr/share/portage/config/make.conf.example for a more
  4. # detailed example.
  5. COMMON_FLAGS="-O2 -pipe -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=hard"
  6. CFLAGS="${COMMON_FLAGS}"
  7. CXXFLAGS="${COMMON_FLAGS}"
  8. FCFLAGS="${COMMON_FLAGS}"
  9. FFLAGS="${COMMON_FLAGS}"
  10. FEATURES="ccache"
  11. CCACHE_DIR="/var/cache/ccache
  12. CPU_FLAGS_ARM="edsp neon thumb vfp vfpv3 vfpv4 vfp-d32 crc32 v4 v5 v6 v7 thumb2"
  13. # WARNING: Changing your CHOST is not something that should be done lightly.
  14. # Please consult https://wiki.gentoo.org/wiki/Changing_the_CHOST_variable before changing.
  15. CHOST="armv7a-unknown-linux-gnueabihf"
  16. USE="bluetooth ffmpeg jpeg png gif curl -X -pulseaudio -gtk -qt -qt5 -qt4 -consolekit -static-libs -cups -systemd"
  17. # NOTE: This stage was built with the bindist Use flag enabled
  18. # This sets the language of build output to English.
  19. # Please keep this setting intact when reporting bugs.
  20. 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.

  1. pi@localhost ~ $ sudo -i
  2. Password:
  3. localhost ~ # cd /usr/src/
  4. localhost /usr/src # git clone https://github.com/opencv/opencv.git -b 3.4
  5. localhost /usr/src # git clone https://github.com/opencv/opencv_contrib.git -b 3.4
  6. localhost /usr/src # cd /usr/src/opencv/opencv
  7. localhost /usr/src/opencv # mkdir build
  8. localhost /usr/src/opencv # cd build
  9. 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

  1. 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