Skip to content

y-india/project-RealTimeObjectTracker-opencv

Repository files navigation

🎯 Real-Time Object Tracker & Counter


🤝 Want to Use or Customize This Project?

If you’d like to use, modify, or integrate this Real-Time Object Tracker in your system
(e.g., for CCTV monitoring, vehicle counting, factory automation, or custom analytics dashboards),

📩 Reach out to me directly:

Email: y.india.main@gmail.com

I’d be happy to:

  • Help you customize it for your use case
  • Integrate it with your camera setup or backend system
  • Add new object categories (cars, bikes, people, etc.)
  • Provide advanced ML upgrades (YOLOv8, motion tracking, etc.)

💬 Let’s collaborate and build something powerful together.


🔍 Overview

The Real-Time Object Tracker and Counter is a powerful computer vision system built using OpenCV that can detect, track, and count moving objects in real time from a live camera or video feed.
It supports multiple color, shape, and object categories, making it useful across industries such as traffic monitoring, retail analytics, factory automation, and security systems.


🚀 Features

✅ Real-time object detection using color and shape filters
✅ Object counting when crossing a defined line (region of interest)
✅ Supports 30+ predefined colors (from colors.txt)
✅ Detects multiple shapes (circle, rectangle, triangle, etc.)
✅ Extendable to detect cars, bikes, people, and more
✅ Modular design — easily modify settings via .txt files
✅ Saves real-time logs and counts to CSV for reporting
✅ Works with webcam or video files


🧩 Project Structure

real_time_object_tracker/
│
├── main.py                 # main car detection script
├── utils.py                # helper functions
├── haarcascade_car.xml     # downloaded vehicle cascade
├── colors.txt              # color definitions
├── shapes.txt              # shape filters
├── objects.txt             # object type definitions
├── README.md               # documentation
│
└── logs/
    └── car_log.csv         # saved logs (will be auto-generated)


👨‍💻 Author

Author: Yuvraj
Skills: Python, OpenCV, Pandas, NumPy, Matplotlib, Seaborn, Streamlit, scikit-learn


🧠 Problem It Solves

In many industries, manual counting and monitoring of moving objects is inefficient and prone to human error.
This project solves that problem by providing an automated, camera-based system that detects and counts objects accurately — 24/7, in real time.

💼 Real-World Applications

Domain Example Use Case Benefit
🚗 Traffic Monitoring Count vehicles crossing an intersection Analyze traffic flow automatically
🏭 Factories Count products on a conveyor belt Improve production tracking
🛍️ Retail Stores Track customer entries/exits Analyze footfall data
📦 Warehouses Count boxes moving through gates Automate inventory movement logs
🚨 Security Systems Detect movement in restricted areas Trigger alerts or logs

🧰 Skills & Technologies Used

Category Tools
🐍 Programming Language Python 3
👁️ Computer Vision OpenCV
🔢 Data Handling NumPy, Pandas
🧠 ML Extensions (future) YOLOv8, TensorFlow, scikit-learn
💾 Logging CSV File Output
⚙️ Configuration Custom .txt files (for easy modification)

⚙️ How It Works

  1. Video Capture

    • Reads live feed from a webcam (cv2.VideoCapture(0)) or a video file.
  2. Color Detection

    • Converts frames from BGR → HSV.
    • Applies masks using predefined HSV ranges from colors.txt.
  3. Contour Detection

    • Finds object boundaries using cv2.findContours.
    • Filters noise and small areas using area thresholds.
  4. Shape Filtering

    • Calculates circularity and aspect ratio for each contour.
    • Matches parameters from shapes.txt.
  5. Object Counting

    • Draws a counting line (line_y) across the frame.
    • Counts each object crossing that line once.
  6. Logging & Display

    • Real-time visualization with bounding boxes and counters.
    • Saves timestamps and counts to logs/object_counts.csv.

🗂️ Configuration Files Explained

🎨 colors.txt

  • Contains 30+ color definitions with lower and upper HSV ranges.
  • You can add or edit colors easily.
  • Example:
red1,0,120,70,10,255,255
blue,90,100,100,130,255,255
gold,20,100,150,30,255,255
  • Modify color_to_detect in main.py to switch target color.

🔺 shapes.txt

  • Defines circularity and aspect ratio for each shape.
  • Example:
circle,0.7,1.2,0.8,1.2
square,0.7,1.2,0.8,1.2
rectangle,0.5,0.9,1.3,5.0
triangle,0.4,0.7,0.6,1.4
ellipse,0.6,1.3,0.8,1.5
  • Modify shape_to_detect in main.py to target a specific shape.

🚗 objects.txt

  • Lists real-world object categories (car, bike, person, ball, etc.).
  • Specifies detection type: cv (OpenCV) or ml (machine learning).
  • Example:
ball,Detect circular colored objects,cv
car,Detect moving cars using ML or color+motion,ml
bottle,Detect tall rectangular shapes,cv
  • Use this file to extend your project with ML models later.

💡 How to Run

1️⃣ Install Dependencies

pip install opencv-python numpy pandas

2️⃣ Run the Script

python main.py

3️⃣ Adjust Settings

  • Change color_to_detect and shape_to_detect in main.py.
  • Modify colors.txt and shapes.txt for custom color and shape definitions.
  • Modify objects.txt for custom object definitions.

3️⃣ Quit Program

Press q to exit the program.