Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vehicle Detection and Counting

Overview

This repository contains a lightweight vehicle detection and counting project built using classical computer-vision techniques. The goal is to detect moving vehicles in video, track them by centroid, and classify their exit routes so counts can be aggregated by direction.

Problem

Count and classify moving vehicles in a scene by tracking object centroids and mapping trajectories to exit zones.

What This Work Does

  • Motion Segmentation: Uses a background subtractor to extract moving foreground regions.
  • Blob Extraction: Filters contours by size and shape to produce candidate vehicle bounding boxes and centroids.
  • Centroid Tracking: Associates detections across frames with a simple centroid-based tracker that maintains object IDs and trajectories.
  • Route Classification: Maps object trajectories to predefined exit zones and aggregates counts per route.

Files

  • vehicle_counter1.py: Main implementation showing the pipeline (motion segmentation, blob extraction, centroid tracker, route classifier) and a runnable run(video_path) entrypoint.
  • vh_count1.py, counter1.py: Additional variants or experiments for vehicle counting (see each file for differences in heuristics and parameters).
  • requirements.txt: Python dependencies used by the project.

How It Works (high level)

  • Background subtraction produces a binary foreground mask.
  • Contours are found in the foreground mask and filtered by area and aspect ratio to identify likely vehicles.
  • Centroids of filtered blobs are tracked using pairwise distance matching; tracks are created, maintained, and removed when objects disappear.
  • Trajectories are inspected to find which exit zone the object ended in; that zone is used to increment the route's count.

Quick Start

  • Create and activate a Python virtual environment, then install dependencies:
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
  • Run the reference script (edit video.mp4 path or pass your own video):
python vehicle_counter1.py

Configuration & Tuning

  • Blob size: Tweak min_area and max_area in the blob extractor to match the scale of vehicles in your video.
  • Background model: Adjust history and varThreshold in the background subtractor for different scene dynamics.
  • Exit zones: Edit the exit_zones rectangles in vehicle_counter1.py to match regions where vehicles leave the frame.
  • Max disappearance: Change max_disappeared in the centroid tracker to tolerate longer occlusions.

Output

  • The reference script prints final route counts to stdout. The repository contains vehicle_count_report.txt with a sample result for reference.

Notes & Next Steps

  • This project uses classic CV techniques (contours, background subtraction, centroid matching). For higher accuracy on crowded scenes consider adding Kalman filters, appearance models, or switching to deep-learning detectors + trackers.
  • The code is intentionally modular: MotionSegmenter, VehicleBlobExtractor, CentroidTracker, and RouteClassifier are easy to experiment with or replace.

About

Detect and count vehicles in video without using object detection models

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages