Skip to content

nhandang02/Vehicle-Segmentation-Traffic-Analysis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vehicle Segmentation & Traffic Congestion Analysis

A traffic congestion analysis system using YOLO Segmentation to detect and segment vehicles, then calculate congestion percentage within a Region of Interest (ROI).

Features

  • Vehicle Segmentation: Detect and segment vehicles using trained YOLO model
  • ROI Selection: Select region of interest by clicking 4 points on the image
  • Congestion Calculation: Calculate congestion percentage based on vehicle area within ROI
  • Visualization: Display results with mask overlay, ROI, and congestion information
  • Model Comparison: Compare results between trained and pretrained models

Results

Example 1

Original Trained Model Segmentation
Original Prediction Segmentation

Example 2

Original Trained Model Segmentation
Original Prediction Segmentation

Model Comparison (Pretrained vs Trained)

Compare the performance between YOLO11x-seg pretrained model and our custom trained model.

Example 1 Comparison

Original Pretrained Model Trained Model
Original Pretrained Trained

Example 2 Comparison

Original Pretrained Model Trained Model
Original Pretrained Trained

Model Training

Want to train your own YOLO model for vehicle segmentation? Check out our training guide:

🔗 YOLOv11 Vehicle Segmentation Training

The guide includes:

  • Dataset preparation and annotation
  • Training configuration
  • Model evaluation
  • Export trained model

Project Structure

Vehicle-Segmentation/
├── main.py                      # Main traffic analysis script
├── compare_models.py            # Compare two YOLO models
├── models/
│   ├── best.pt                  # Trained YOLO model
│   └── yolo_model.py            # YOLO model wrapper
├── services/
│   └── congestion_analyzer.py   # Congestion analysis
├── ui/
│   └── roi_selector.py          # ROI selection interface
├── utils/
│   ├── geometry.py              # Geometry calculations
│   ├── load_image.py            # Image loader
│   └── show_result.py           # Result display
├── visualization/
│   └── renderer.py              # Result renderer
├── data_predict/                # Test images (10 samples)
└── result/                      # Result images

Requirements

  • Python 3.8+
  • OpenCV
  • Ultralytics YOLO
  • NumPy

Installation

1. Clone repository

git clone <repository-url>
cd Vehicle-Segmentation

2. Create virtual environment (recommended)

python -m venv venv

# macOS/Linux
source venv/bin/activate

# Windows
venv\Scripts\activate

3. Install dependencies

pip install opencv-python ultralytics numpy

Usage

Run Traffic Analysis

python main.py

Steps:

  1. Select an image from file dialog
  2. Click 4 points to define ROI
  3. View results showing:
    • Original image
    • Image with ROI, masks, and congestion percentage
    • Segmentation masks only

Compare Models

python compare_models.py

Compare trained model (best.pt) vs pretrained YOLO11x-seg model.

How It Works

Load Image → Select ROI → YOLO Prediction → Calculate Intersection → Compute Congestion → Display Results

Congestion Percentage = (Total Vehicle Area in ROI / ROI Area) × 100


About

Traffic congestion analyzer using YOLOv11 segmentation with ROI-based vehicle detection and density calculation

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages