The repo is a toy project based on PyImageSearch Blog post OpenCV Stream Video to Web Browser Html Page meant to learn how to perform super simple motion detection against video stream and display result on web browser.
This repo uses pipenv to setup virtualenv and manage packages, requires Python 3.6 and above
- The pipenv installation steps can be found here
- Run
pipenv install
in root dir of this repo to install packages, includingflask
,opencv
,imutils
,numpy
-
Run
pipenv shell
under root dir of repo to enter virtualenv -
Run
python webstreaming.py -i 0.0.0.0 -p 8000
to start flask server -
Visit
http://127.0.0.1:8000
and there will be a live video stream! -
Press
Ctrl + C
to stop the demo
The approach of motion detection in this repo is a very simple one. It consists three steps basically.
The first step is to collect the background, for example like below, three raw images captured by camera is accumulated averaged using cv2.accumulateWeighted
to get the basic background image.
You can view this case in unit test motion_detection/test_singlemotiondetector.py#test_should_weighted_accumulate_images
The steps of detecting edges of moving object is:
a). The difference between background image and newly captured image is calculated using cv2.absdiff
b). Threshold method is performed to distinguish the static background pixels and moving object pixels using cv2.threshold
c). Basic edge detection method is used to detect edges of moving objects using cv2.erode
& cv2.dilate
Below is the example of detecting edges of moving object
You can view this case in unit test motion_detection/test_singlemotiondetector.py#test_should_detect_moving_object
At last, cv2.findContours
is used to find the contour of moving object and then coordinates of objects is calculated.