A comprehensive computer vision project focused on vehicular safety and traffic monitoring. This repository contains multiple modules for detecting vehicles, identifying lanes, predicting collisions, and detecting accidents using OpenCV and Deep Learning techniques.
This project utilizes computer vision to analyze video feeds for traffic scenarios. It combines classical image processing techniques (Background Subtraction, Canny Edge Detection, Hough Transform) with modern Deep Learning models (Faster R-CNN and YOLO) to address real-world traffic safety challenges.
Key objectives:
- Vehicle detection and classification
- Lane detection and tracking
- Collision warning based on vehicle proximity
- Accident detection through motion and anomaly analysis
- Uses a pre-trained Faster R-CNN (ResNet50) model from
torchvision - High-accuracy detection of traffic participants
Detects: Cars, buses, trucks, motorcycles, bicycles
Tech Stack: PyTorch, Torchvision, OpenCV
- Prototype system for detecting anomalies in traffic flow
- Uses frame differencing and background subtraction
- Displays "Accident happened" on anomaly detection
- Includes a logic stub for WhatsApp alerts using
pywhatkit
Input: accident.mp4
- Detects and highlights lane markings
- Converts frames to HSV color space for color masking
- Applies Canny Edge Detection and Hough Transform
Input: road_car_view.mp4
- Monitors distance between moving vehicles
- Uses MOG2 background subtraction for motion detection
- Computes Euclidean distance between object centroids
- Triggers "Collision Warning!" when vehicles are too close
Input: vi.mp4
- Lightweight vehicle detection using classical computer vision
- Uses BackgroundSubtractorMOG2 with morphological operations
Input: video.mp4
This section documents experimental work, including dataset selection, model comparisons, and performance evaluation.
- Sources: Custom dashcam footage combined with samples from the COCO dataset (Car, Bus, Truck classes) and the KITTI Vision Benchmark Suite
- Preprocessing: Frame resizing and normalization
- Augmentation: Rotation and brightness adjustments to improve robustness under varying lighting conditions
Two architectures were evaluated to balance accuracy and real-time performance:
- Used for high-accuracy benchmarking
- Strong performance on small objects
- Slower inference on CPU
Performance: ~5–7 FPS (CPU)
- Primary model for real-time deployment
- Single-pass detection enables fast inference
Versions Tested: YOLOv5 / YOLOv8 (Nano & Small variants)
- Faster R-CNN: 0.82
- YOLO: 0.78 (acceptable trade-off for speed)
- Faster R-CNN: ~140 ms/frame
- YOLO: ~25 ms/frame (real-time capable)
Recall Strategy: High recall was prioritized to avoid missing potential accident scenarios, even with some false positives.
- Occlusion: Vehicles partially hidden behind larger vehicles
- Lighting Variations: Sudden brightness changes affected HSV-based lane detection
- False Positives: Shadows occasionally detected as vehicles in background subtraction
- Dynamic HSV filter tuning based on lighting conditions
- Pixel-to-meter calibration for improved collision distance estimation
- Model quantization (FP16) to reduce memory usage in YOLO deployments
# Example YOLO inference command
python detect.py --weights yolov5s.pt --source video.mp4 --conf 0.4 --iou 0.45Python Version: Python 3.x (Python 3.9 recommended)
git clone https://github.com/your-username/opencv-project.git
cd opencv-project/vanetpip install opencv-python numpy torch torchvision pywhatkitDependencies:
opencv-python– Image processing and video capturenumpy– Numerical computationstorch,torchvision– Deep learning modelspywhatkit– Optional WhatsApp alert integration
Ensure required video files are present or update paths in the scripts:
video.mp4accident.mp4road_car_view.mp4vi.mp4
Deep Learning Object Detection
python va.pyLane Detection
python laneDetection.pyAccident Detection
python accidentDetection.pyCollision Warning System
python cardetect.pyPress q or Esc to exit the video window.
- Real-time webcam and IP camera integration
- Enable live WhatsApp/SMS alerts
- Vehicle speed estimation from frame displacement
- Kalman filter-based object tracking
- Persistent vehicle ID assignment across frames
This project is open-source. You are free to use, modify, and improve the