Parking Spaces

October 23, 2023 (1y ago)

Detecting Empty Parking Spaces Using Computer Vision

Method 1: Histogram of Oriented Gradients (HOG)

Example Code for HOG Feature Extraction

import cv2
from skimage.feature import hog
from skimage import exposure
 
# Load the image
image = cv2.imread('input_image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 
# Compute HOG features and visualize them
features, hog_image = hog(gray, visualize=True)
 
# Rescale the HOG image
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))
 
# Display the original and HOG image
cv2.imshow('Original Image', image)
cv2.imshow('HOG Image', hog_image_rescaled)
cv2.waitKey(0)
cv2.destroyAllWindows()

Method 2: Convolutional Neural Network (CNN)

Example Code for CNN Object Detection

import tensorflow as tf
 
# Load a pre-trained CNN model (e.g., MobileNet)
model = tf.keras.applications.MobileNetV2(weights='imagenet')
 
# Load and preprocess the input image
input_image = cv2.imread('input_image.jpg')
input_image = cv2.resize(input_image, (224, 224))
input_image = tf.keras.applications.mobilenet_v2.preprocess_input(input_image)
input_tensor = tf.convert_to_tensor(input_image)
input_tensor = tf.expand_dims(input_tensor, axis=0)
 
# Run inference
predictions = model.predict(input_tensor)

Method 3: Newer Deep Learning Approaches

Example Code for Object Detection with YOLO

import cv2
import numpy as np
 
# Load YOLO
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
layer_names = net.getLayerNames()
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
 
# Load the image
image = cv2.imread('input_image.jpg')
height, width, _ = image.shape
 
# Prepare the image for YOLO
blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
 
# Process the detections
for out in outs:
    for detection in out:
        scores = detection[5:]
        class_id = np.argmax(scores)
        confidence = scores[class_id]
        if confidence > 0.5:
            # Object detected
            center_x = int(detection * width)
            center_y = int(detection * height)
            w = int(detection * width)
            h = int(detection * height)
            # Draw bounding box
            cv2.rectangle(image, (center_x, center_y), (center_x + w, center_y + h), (0, 255, 0), 2)
 
# Show the output
cv2.imshow("Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Detecting Empty Parking Spaces

Example Code for IoU Calculation

def calculate_iou(boxA, boxB):
    # Determine the coordinates of the intersection rectangle
    xA = max(boxA, boxB)
    yA = max(boxA, boxB)
    xB = min(boxA, boxB)
    yB = min(boxA, boxB)
 
    # Compute the area of intersection rectangle
    interArea = max(0, xB - xA) * max(0, yB - yA)
 
    # Compute the area of both the prediction and ground-truth rectangles
    boxAArea = (boxA - boxA) * (boxA - boxA)
    boxBArea = (boxB - boxB) * (boxB - boxB)
 
    # Compute the intersection over union by taking the intersection area and dividing it by the sum of prediction + ground-truth areas - the intersection area
    iou = interArea / float(boxAArea + boxBArea - interArea)
 
    return iou

Sending a Text

pip3 install twilio

Example Code for Sending SMS with Twilio

from twilio.rest import Client
 
# Your Twilio credentials
account_sid = 'your_account_sid'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)
 
# Send SMS
message = client.messages.create(
    body="A parking space is now available!",
    from_='+1234567890',  # Your Twilio phone number
    to='+0987654321'       # Your phone number
)
 
print(f"Message sent: {message.sid}")

GitHub Code Link