Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Motion-Detection/OpenCvPROJECT.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import cv2
import time
import imutils

cam = cv2.VideoCapture(0)
time.sleep(0)
firstFrame = None
area = 500
frameUpdateInterval = 100
frameCount = 0

while True:
ret, img = cam.read()
if not ret:
break

text = "Normal"
img = imutils.resize(img, width=600)
grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gaussianImg = cv2.GaussianBlur(grayImg, (21, 21), 0)

if firstFrame is None or frameCount % frameUpdateInterval == 0:
firstFrame = gaussianImg
frameCount = 0

frameCount += 1

imgDiff = cv2.absdiff(firstFrame, gaussianImg)

threshImg = cv2.threshold(imgDiff, 25, 255, cv2.THRESH_BINARY)[1]
threshImg = cv2.dilate(threshImg, None, iterations=2)

cnts = cv2.findContours(threshImg.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

for c in cnts:
if cv2.contourArea(c) < area:
continue

(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
text = "Moving Object detected"

cv2.putText(img, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.imshow("cameraFeed", img)

key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break

cam.release()
cv2.destroyAllWindows()
57 changes: 57 additions & 0 deletions Motion-Detection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 🎥 Motion Detection Using OpenCV 🕵️‍♂️

This project demonstrates a **basic motion detection system** using OpenCV and Python. It captures video from a camera, processes the frames to detect moving objects, and highlights detected motion areas in real-time. 🚀

---

## ✨ Features

- 📹 Capture video feed from the webcam.
- 🎨 Convert frames to grayscale and apply Gaussian blur for noise reduction.
- 🔍 Detect motion by comparing the current frame with the first frame.
- 🔲 Highlight moving objects with bounding rectangles.
- 📝 Display status text indicating whether motion is detected.
- ❌ Press **`q`** to quit the application.

---

## 🛠️ Requirements

- Python 3.x 🐍
- OpenCV (`cv2`) 🖼️
- Imutils 🔧
- Time module (standard Python library) ⏰

---

## 📦 Installation

Install required libraries using pip:

```bash
pip install opencv-python imutils
```

▶️ Usage

Run the script to start the motion detection:

```bash
python motion_detection.py
```

⚙️ How It Works

🥇 The first frame is captured and used as a reference.

🖤 Each new frame is converted to grayscale and blurred.

➖ The absolute difference between the current frame and the reference frame is calculated.

⚫ Thresholding and dilation are applied to highlight differences.

🔎 Contours are detected in the thresholded image.

✅ If contours exceed a minimum area, bounding boxes are drawn, and motion is detected.

📺 The video feed with detected motion highlighted is displayed.