How to Build a Real‑Time Parking Spot Detector with Mask R‑CNN and Python

This tutorial walks through using a webcam, Mask R‑CNN, and Python to automatically detect available parking spaces, track stationary vehicles, compute Intersection‑over‑Union to confirm emptiness, and send SMS alerts via Twilio, providing full code snippets and practical tips.

21CTO
21CTO
21CTO
How to Build a Real‑Time Parking Spot Detector with Mask R‑CNN and Python

Problem Overview

During the busy Chinese New Year period, finding a parking space in crowded streets or malls becomes a major hassle.

Solution Overview

Software engineer Adam Geitgey created a Python application that uses a webcam and a Mask R‑CNN instance‑segmentation model to locate free parking spots and notify the user via SMS.

Decomposing the Task

The overall problem is split into four simple sub‑tasks: (1) detect potential parking spaces in each video frame, (2) detect all cars, (3) determine which spaces are occupied by matching cars to spaces, and (4) send a notification when a space remains empty for several consecutive frames.

Detecting Parking Spaces

Instead of manually labeling each spot, the tutorial suggests using visual cues such as parking‑meter poles or painted curb markings, but ultimately recommends treating any region where a car is stationary as a parking space.

Detecting Cars

Car detection is a standard object‑detection problem. Various methods are listed, from classic HOG detectors to modern deep‑learning models such as YOLO, Faster R‑CNN, and Mask R‑CNN. For this project, Mask R‑CNN is chosen because it provides both bounding boxes and segmentation masks.

Train a custom detector (time‑consuming) or use a pre‑trained COCO model.

Mask R‑CNN returns object class, confidence score, bounding box, and mask for each detection.

Mask R‑CNN Example Output

The model detects cars, people, traffic lights, and even a tree.

Extracting Detection Data

Cars found in frame of video:
Car:  [492 871 551 961]
Car:  [450 819 509 913]
Car:  [411 774 470 856]

Determining Empty Spots with IoU

Intersection‑over‑Union (IoU) measures overlap between a car’s bounding box and a parking‑space box. Low IoU (e.g., 0.15) indicates the car does not occupy the space, while high IoU (e.g., 0.6) means the spot is taken.

The Matterport Mask R‑CNN library provides mrcnn.utils.compute_overlaps() to compute IoU matrices.

[[1.0 0.0704 0.0 0.0]
 [0.0704 1.0 0.0767 0.0]
 [0.0 0.0 0.0233 0.0]]

Rows with all near‑zero values correspond to empty spots.

Sending SMS Alerts

When a spot remains empty for several frames, the script uses Twilio’s Python client to send an SMS. Install the library with pip3 install twilio and provide your account SID, auth token, and phone numbers.

from twilio.rest import Client

client = Client(account_sid, auth_token)
message = client.messages.create(
    body="Parking spot is free!",
    from_="+1234567890",
    to="+0987654321"
)

A flag ensures the message is sent only once per free‑spot event.

Packaging the Workflow

Each step is implemented as a separate Python script; the full source is available on Medium. Required dependencies include Python 3.6+, the Matterport Mask R‑CNN repository, and OpenCV.

By swapping the target object IDs, the same pipeline can be adapted to other domains such as ski‑jump detection or wildlife counting.

Related article: https://medium.com/@ageitgey/snagging-parking-spaces-with-mask-r-cnn-and-python-955f2231c400
computer visionPythonTwilioIoUMask R-CNNparking detection
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.