Building a Real-Time Parking Space Detection System with OpenCV
Learn how OpenCV, NumPy, and classical computer vision techniques can detect parking space availability in real time without machine learning.
Building a Real-Time Parking Space Detection System with OpenCV
Introduction
Computer vision allows machines to interpret and analyze visual information from the world. While modern discussions often focus on deep learning and neural networks, many real-world problems can still be solved efficiently using classical image processing techniques.
In this project, I built a real-time parking space monitoring system using OpenCV, NumPy, and CVZone. The system analyzes a parking lot video feed and determines whether each parking bay is free or occupied. The results are then displayed directly on the video stream using colored overlays and a live availability counter.
Unlike sensor-based parking systems that require hardware installation for every parking space, this approach uses a single camera and software-based image analysis to determine occupancy.
Project Link : https://github.com/TheODDYSEY/Parking-OpenCv
Project Overview
The application processes every frame from a parking lot video feed and classifies each parking space as either:
- Free
- Occupied
The system uses a sequence of image-processing operations to progressively transform raw camera footage into a format suitable for classification.
Key capabilities include:
- Real-time parking occupancy detection
- Live parking availability counter
- Visual overlays showing occupied and free spaces
- Reusable parking position storage using Pickle
- Lightweight image-processing pipeline without machine learning
Technology Stack
- OpenCV Video capture, image processing, display
- NumPy Array and matrix operations
- Pickle Storing parking space coordinates
- CVZone Enhanced overlays and UI components
How the System Works
The parking lot camera remains fixed in position while monitoring predefined parking spaces.
For every frame:
- Capture video frame
- Convert to grayscale
- Reduce noise
- Apply thresholding
- Perform morphological operations
- Extract parking-space regions
- Count active pixels
- Classify occupancy
- Overlay results on screen
The complete processing pipeline runs continuously in real time.
Stage 1: Frame Capture
The application begins by reading frames from a parking lot video.
cap = cv2.VideoCapture('media/carPark.mp4')Each frame is represented as a NumPy array containing pixel values.
When the video reaches the end, playback automatically loops back to the beginning.
if not ret:
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)Stage 2: Grayscale Conversion
Color information is not necessary for parking occupancy detection.
The frame is converted from BGR to grayscale:
imgGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)This reduces computational overhead while preserving intensity information.
Stage 3: Gaussian Blur
Raw images often contain sensor noise.
A Gaussian blur smooths the image and removes small variations that could interfere with later processing stages.
imgBlur = cv2.GaussianBlur(imgGray, (3,3), 1)Using a small kernel helps reduce noise while maintaining important structural details.
Stage 4: Adaptive Thresholding
Parking lots often contain uneven lighting caused by sunlight, shadows, and reflections.
Adaptive thresholding calculates a local threshold for every region of the image.
imgThresh = cv2.adaptiveThreshold(
imgBlur,
255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV,
25,
16
)
The output becomes a binary image where important structures are highlighted.
Stage 5: Median Blur
After thresholding, isolated noise pixels may still exist.
Median filtering removes salt-and-pepper noise while preserving object boundaries.
imgMedian = cv2.medianBlur(imgThresh, 5)
This improves classification accuracy.
Stage 6: Dilation
Dilation expands white regions and fills small gaps.
kernel = np.ones((3,3), np.uint8)
imgDilate = cv2.dilate(
imgMedian,
kernel,
iterations=1
)
This step strengthens parking-space boundaries and improves pixel-count consistency.
Stage 7: Parking Space Extraction
Parking coordinates are manually defined using a helper tool.
These coordinates are stored in a Pickle file.
with open('CarParkPos', 'rb') as f:
posList = pickle.load(f)
Each parking space is extracted from the processed image as a Region of Interest (ROI).
imgCrop = imgDilate[
y:y+SPACE_H,
x:x+SPACE_W
]
Stage 8: Occupancy Classification
The classification logic is intentionally simple and efficient.
The number of white pixels within each parking-space crop is counted.
count = cv2.countNonZero(imgCrop)
If the count falls below a threshold, the parking space is considered free.
THRESHOLD = 900
if count < THRESHOLD:
FREE
else:
OCCUPIED
This threshold was determined experimentally using the parking-lot footage.
Stage 9: Visualization
The final step overlays results on the original frame.
Green rectangles indicate available spaces.
Red rectangles indicate occupied spaces.
cv2.rectangle(
frame,
(x, y),
(x + W, y + H),
color,
2
)
A live availability counter is displayed using CVZone.
cvzone.putTextRect(
frame,
f'Free: {free}/{len(posList)}',
(50, 60),
scale=3
)
This provides operators with an instant overview of parking availability.
Why Classical Computer Vision Works Here
Many parking-monitoring systems use machine learning models for occupancy detection.
However, this project demonstrates that traditional computer vision techniques remain highly effective when:
- Camera positions are fixed
- Parking spaces are predefined
- Environmental conditions are relatively stable
Because of these constraints, a lightweight pixel-counting approach can achieve reliable results without requiring model training or expensive hardware.
Challenges and Limitations
Although effective, the system has some limitations.
Camera Movement
If the camera position changes, parking coordinates become invalid.
Lighting Conditions
Heavy shadows, glare, or extreme weather may affect thresholding quality.
Threshold Calibration
Different parking lots may require different occupancy thresholds.
Manual Setup
Parking-space coordinates must be defined manually.
Future Improvements
Several enhancements could further improve the system:
Deep Learning Detection
Replace threshold-based classification with:
- YOLOv8
- Faster R-CNN
- EfficientDet
Web Dashboard
Stream occupancy information to a web application for remote monitoring.
Analytics
Generate parking usage statistics such as:
- Peak occupancy periods
- Average utilization rates
- Vehicle turnover rates
Smart Parking APIs
Expose parking availability through REST APIs for mobile applications and smart-city integrations.
Conclusion
This project demonstrates how classical computer vision techniques can solve practical real-world problems without requiring machine learning.
By combining grayscale conversion, adaptive thresholding, morphological operations, and pixel analysis, the system accurately identifies available parking spaces in real time using only a camera feed and lightweight processing.
The result is a cost-effective parking monitoring solution that showcases the power of OpenCV and traditional image-processing techniques in modern computer vision applications.
Comments