• Stars
    star
    149
  • Rank 248,619 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Windows, Unix, Raspberry Pi Motion Tracking Demo. Tracks movement in camera view and returns X, Y Position of Largest Moving Contour in Camera View - See Moved GitHub Project Links

MOTION TRACK - Windows, Unix, Raspberry Pi Demo

Uses python2/3, Opencv2/3 to do Real Time x,y tracking of largest moving object in camera view.

Motion Track Demo YouTube Video

Introduction

I did quite a bit of searching on the internet, GitHub, etc, but could not at the time find a similar python picamera implementation that returns x,y coordinates of the most dominate moving object in the frame although some came close.

Prerequisites

Requires a Raspberry Pi computer with a Web Camera or RPI Camera Module and Recent Raspbian
or
MS Windows, Unix computer with a Web Camera with recent python installed

If you wish to use a web camera that is plugged into a usb port. Set WEBCAM = True in config.py otherwise, WEBCAM = False will use a connected raspberry pi camera module video stream. When running under Windows or a non RPI unix distro import of pi-camera python libraries will fail and Web camera setting WEBCAM = True will automatically be configured.

The Raspberry Pi or Debian dependencies and code files can be installed per the motion-track-install.sh curl script below.
if you are using MS Win or Non Debian Unix Repo, select the GitHub Clone or download green button at top right of web page and select desired method.

Quick Install or Upgrade

Easy Install of motion-track-demo onto Debian or Raspberry Pi Computer with latest Raspbian.

Step 1 With mouse left button highlight curl command in code box below. Right click mouse in highlighted area and Copy.
Step 2 On RPI putty SSH or terminal session right click, select paste then Enter to download and run script.

curl -L https://raw.github.com/pageauc/motion-track/master/motion-track-install.sh | bash

Command above will install all code and dependencies
Note - a raspbian sudo apt-get update and upgrade will be performed as part of install so it may take some time if these are not up-to-date

Manual Install

From logged in RPI SSH session or console terminal perform the following.

wget https://raw.github.com/pageauc/motion-track/master/motion-track-install.sh
chmod +x motion-track-install.sh
./motion-track-install.sh

How to Run

Default is console only display. Use Nano or text editor to Edit config.py variable window_on = True to display the opencv tracking window on GUI desktop. See other variables and descriptions for additional variable customization settings. From SSH session, console or GUI desktop terminal session execute the following commands

cd ~/motion-track-demo
./motion-track.py

On Windows make sure you have the latest python installed from https://www.python.org/downloads/ Run motion-track.py from IDLE or if file association exists it can also be run from cmd prompt by double clicking on motion-track.py. Use a text editor to modify config.py to view opencv window(s) and set other configuration variables.

Trouble Shooting

if you get an opengl error then see this article about installing opengl on
a RPI P2 https://www.raspberrypi.org/blog/another-new-raspbian-release/

Otherwise install opengl support library per following command then reboot.

sudo apt-get install libgl1-mesa-dri

Edit the config.py file and set variable window_on = True so the opencv status windows can display camera motion images and a circle marking x,y coordinates as well as the threshold images. The circle diameter can be change using CIRCLE_SIZE variable. You can set window_on = False if you need to run from SSH session. If debug= True then status information will be displayed without a GUI desktop session.

Credits

Some of this code is based on a YouTube tutorial by
Kyle Hounslow using C here https://www.youtube.com/watch?v=X6rPdRZzgjg

Thanks to Adrian Rosebrock jrosebr1 at http://www.pyimagesearch.com
for the PiVideoStream Class code available on github at
https://github.com/jrosebr1/imutils/blob/master/imutils/video/pivideostream.py

motion-track.py

Motion Track Demo - Basic concept of tracking moving objects. This Demo program detects motion in the field of view and uses opencv to calculate the largest contour above a minimum size and return its x,y coordinate.

Track Multiple Objects

This code example only tracks the largest moving object since it is done in real time. If you are interested in tracking multiple objects you might want to take a look at this github repo for multitracker code example.
https://github.com/opencv/opencv_contrib/blob/master/modules/tracking/samples/multitracker.py My suggestion would be to cut contours and pass this as the ROI (Rectangle of Interest). Read the issues on this since there will be issues regarding objects that leave, enter and stop that will need to have new ROI's initialized.

Project links

---------- Other Projects of Mine Based on Motion Tracking ------------

track-inout.py

Track Enter and Leave Activity that cross a vert or horiz center line This project Runs on Windows or Unix using Web Cam or Raspberry Pi using Web Cam or pi camera module and Uses Python2/3, OpenCV2/3 to track motion and record object enter or leave camera view when they cross a trigger centerline. Has options to take image and/or record data in a csv file

speed-camera.py

Windows, Unix vehicle (object) speed camera using motion tracking Tracks vehicle speeds or other moving objects in real time and records image and logs data. Now improved using threading for video stream and clipping of area of interest for greater performance.

cam-track.py

Tracks camera pan/tilt motion position based on opencv template matching Uses a clipped search image rectangle to search subsequent video stream images and returns the location. Can be used for tracking camera x y movements for stabilization, robotics, Etc.

hotspot-game.py

A simple motion tracking game with motion activated menus The game play involves motion tracking of body motion (hands) to get as many hits as possible inside shrinking boxes that randomly move around the screen. Position the camera so you can see body motions either close or standing. Pretty simple but I think kids would have fun with it and they just might take a look at the code to see how it works, change variables or game logic.

sonic-track.py

Send Motion Tracking Data to Sonic Pi to play notes and/or music samples This demo app sends camera motion tracking data to the sonic-pi music program. sonic-track sends data to sonic-pi via psonic.py and pythonosc. You will need a pi camera and a powered speaker connected to the Raspberry Pi audio/video plug via appropriate cables or you can attach RPI to an HDMI TV via HDMI cable and sound will redirect to TV speakers.

----------------------------------------------------------------------------

Have Fun Claude Pageau
YouTube Channel https://www.youtube.com/user/pageaucp
GitHub Repo https://github.com/pageauc

More Repositories

1

speed-camera

A Unix, Windows, Raspberry Pi Object Speed Camera using python, opencv, video streaming, motion tracking. Includes a Standalone Web Server Interface, Image Search using opencv template match and a whiptail Admin Menu Interface Includes picam and webcam Plugins for motion track security camera configuration including rclone sync script. watch-app allows remotely controller camera configuration from a remote storage service name. Uses sqlite3 and gnuplot for reporting. Recently added openalpr license plate reader support.
Python
935
star
2

pi-timolo

Raspberry PI-TIMOLO ( PI-TImelapse, MOtion, LOwLight ) uses RPI picamera and OpenCV for Remote Headless Security Monitoring using Motion Tracking, Rclone Auto Sync files with remote storage services. Auto Twilight Transitions and Low Light Camera Settings. Panoramic images using PanTiltHat and More. This project is featured on GitHub Awesome software.
Python
547
star
3

rclone4pi

rclone4pi - Easy Install of rclone to Raspberry Pi Computer with automated folder sync demo
Shell
96
star
4

picamera-motion

Raspberry Pi python PiCamera Lightweight Motion Detection. Includes easy curl script install/upgrade, whiptail admin menu system, single file web server and Rclone for uploading to a variety of web storage services.
Python
89
star
5

opencv3-setup

Raspberry Pi whiptail Menu driven Easy Install and Compile of opencv3 python from source files.
Shell
51
star
6

pi-motion-lite

Raspberry Pi python motion detection using picamera python module
Python
45
star
7

face-track-demo

Raspberry Pi python PiCamera, OpenCV Face and Motion Tracking using pan/tilt assembly via servo controller or gpiozero pwm
Python
38
star
8

cam-track

Windows, Unix, Raspberry Pi Computer python program to Track Camera X, Y Movements and Convert to Camera Pointing Position. Useful for Stabilization or Robotics Course Correction
Python
38
star
9

sonic-track

Uses a raspberry pi camera or web cam and python opencv to track motion in camera view. Sends motion contour data to sonic-pi via osc interface to produce and control notes/sample. Includes ability to use onscreen menu areas to change synthesizer, octaves Etc.
Python
30
star
10

pi-motion

Raspberry Pi and Camera Module raspistill Python Motion Capture
Shell
23
star
11

FileBrowser

This is a bash whiptail file selection menu sample code
Shell
15
star
12

track-inout

Windows, Unix, Raspberry Pi program uses opencv and python to track moving objects that enter and leave camera view and cross a vert or horz trigger center line. Can be configured to take an image and log data to a csv file. Includes a standalone web server for viewing image files. At this point it is a demo since it would need more work but the basic operation is functional.
Python
12
star
13

webserver

A Raspberry Pi Simple python Stand Alone Local Network Web Server. Automatically reads folder and file entries into a right pane selection list. Selected folder opens a new browser tab and Selected file displays content in browser left pane iframe. Right pane list can be ordered by date/time or alphabetic ascending or descending via settings.py file. A menubox.sh makes it easier to operate and change the webserver settings.
Python
12
star
14

pi-motion-orig

Raspberry Pi camera python motion detect and grive sync images to google drive
Python
10
star
15

panopi

Create pano timelapse images from multiple RPI computers using picamera modules.
Python
9
star
16

pifan

Control Raspberry Pi case fan on/off using CPU temperature
Python
7
star
17

waveshare.pantilthat

Waveshare PanTiltHat python2 and 3 Driver and demo. Uses curl install scripts. One for Demo and Driver and one just for driver. Demo's scripts support Pimoroni.
Python
6
star
18

enviro-pi

Use RPI with Sensehat to monitor humidity, temperature, barometric pressure. View data and graphs on a web browser. Optionally Upload data to Weather Underground Personal Weather Station
Python
6
star
19

pokeme

A Motion Tracking Demo with Motion Activated Menus and Image Clips
Python
3
star
20

MoTrack-Picam2-Demo

Test of Picamera2 python library
Python
3
star
21

hotspot-game

A Windows, Unix or Raspberry Pi Computer Motion Tracking Game using a Web Cam or RPI Camera Video Streaming. Uses Body Motion for Menus and Game Play for One or Two Players.. Get High Score by getting most motion hits in moving boxes that get smaller..
Python
3
star
22

rpi-shutdown

Safely shutdown a raspberry pi using a momentary switch
Python
2
star
23

webcam-video

A Bash script to take short video sequences using avconv and upload (sync) to google drive. Runs on Raspberry Pi or other Unix Distros
Shell
1
star