A simple real-time motion detection system built using Python, OpenCV, and Imutils. The application uses a webcam to monitor movement and identifies moving objects by comparing the current video frame with an initial reference frame.
When significant movement is detected, the system displays "Moving Object Detected" and highlights the moving region with a bounding box.
- 📷 Real-time webcam monitoring
- 🔍 Detects moving objects using frame differencing
- 🖼️ Converts frames to grayscale for processing
- 🌫️ Applies Gaussian Blur to reduce noise
- 📊 Uses thresholding to identify significant changes
- 📦 Draws bounding boxes around detected movement
- ⚡ Lightweight and easy to run
- ⌨️ Press
qto exit the application
- Python
- OpenCV
- Imutils
Motion-Detection/
│
├── motion_detection.py
└── README.md
The project follows a simple computer vision pipeline:
Webcam
↓
Capture Frame
↓
Resize Frame
↓
Convert to Grayscale
↓
Gaussian Blur
↓
Capture Initial Frame
↓
Compare Current Frame with Initial Frame
↓
Calculate Absolute Difference
↓
Threshold the Difference
↓
Dilate Image
↓
Find Contours
↓
Filter Small Movements
↓
Draw Bounding Box
↓
Display Motion Status
The application accesses the system's default webcam using OpenCV.
cam = cv2.VideoCapture(0)Each frame is resized, converted to grayscale, and blurred to make motion detection more stable.
img = imutils.resize(img, width=1000)
grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gaussianImg = cv2.GaussianBlur(grayImg, (21, 21), 0)The first processed frame is stored as the reference frame.
if firstFrame is None:
firstFrame = gaussianImg
continueThe current frame is compared with the reference frame using absolute difference.
imgDiff = cv2.absdiff(firstFrame, gaussianImg)Pixels with significant differences are converted into a binary image.
threshImg = cv2.threshold(
imgDiff, 25, 255, cv2.THRESH_BINARY
)[1]Contours are detected from the thresholded image. Very small contours are ignored to prevent minor noise from being treated as movement.
if cv2.contourArea(c) < area:
continueWhen movement is detected, a rectangle is drawn around the moving object.
cv2.rectangle(
img,
(x, y),
(x + w, y + h),
(0, 255, 0),
2
)The application then displays:
Moving Object Detected
The detection and bounding-box logic are implemented directly in the uploaded Python script.
Make sure Python is installed on your system.
Install the required libraries:
pip install opencv-python imutilsOr:
pip install -r requirements.txtIf you want to use requirements.txt, create the file with:
opencv-python
imutils
Clone the repository:
git clone https://github.com/your-username/Motion-Detection.gitNavigate into the project:
cd Motion-DetectionRun the Python program:
python motion_detection.pyYour webcam should open automatically.
Move an object in front of the camera and the application will detect the movement.
| Key | Action |
|---|---|
q |
Exit the application |
The program listens for the q key and then releases the camera and closes the OpenCV windows.
The main idea behind this project is background/reference-frame subtraction.
Let:
Reference Frame = F₁
Current Frame = F₂
The difference is calculated as:
Difference = |F₁ - F₂|
If the difference is large enough, the system considers that region to contain movement.
A threshold of 25 is used to identify meaningful pixel differences, while contours smaller than an area of 500 are ignored.
This basic motion detection technique can be extended for:
- 🏠 Home security systems
- 📹 CCTV monitoring
- 🚨 Intrusion detection
- 🏢 Office monitoring
- 🚪 Entry/exit monitoring
- 🤖 Computer vision projects
- 📷 Smart surveillance systems
Possible improvements include:
- Add motion detection alerts
- Capture images when movement is detected
- Record video automatically
- Send notifications through email or SMS
- Detect and track specific objects
- Use background subtraction algorithms
- Add a graphical user interface
- Store motion events with timestamps
- Integrate with an IoT security system
This is a basic motion detection implementation and has some limitations:
- It relies on an initial reference frame.
- Changes in lighting can trigger false detections.
- Camera movement can cause incorrect detection.
- Small movements may be ignored because of the contour-area threshold.
- It does not identify what the moving object is; it only detects movement.
This project is open-source and available for learning and educational purposes.
Maria Blessy R J
If you found this project useful, consider giving the repository a ⭐ on GitHub!