Real-time instance segmentation of cars and traffic lights in urban street images using a pretrained Mask R-CNN (ResNet-50 FPN backbone) from torchvision. Detections are rendered as colour-coded pixel-level masks overlaid on the original image.
| Original | Segmented |
|---|---|
| Street scene | Cars highlighted in red · Traffic lights in green |
Mask R-CNN with a ResNet-50 + Feature Pyramid Network (FPN) backbone, pretrained on the COCO dataset (80 classes). Only two classes are visualised here:
- Class 3 → Car (red mask)
- Class 10 → Traffic Light (green mask)
Python · PyTorch · torchvision · OpenCV · Pillow · Matplotlib
pip install torch torchvision opencv-python pillow matplotlib
# Add your street images to images/
mkdir images
# Then run:
python segment.py- Model weights are downloaded automatically from torchvision on first run (~170 MB)
- Detection threshold defaults to 0.5 — lower it to catch more objects, raise it to reduce false positives
- Works on CPU; GPU (CUDA) will significantly speed up inference on larger images