This article was contributed to the Roboflow blog by Abirami Vina.

We spend a lot of our time commuting from place to place, and parking is always a hassle. For example, finding a parking spot at the mall when you go shopping can be a frustrating task. With that said, cameras and computer vision algorithms can work together in parking lots to know if a vehicle has occupied a parking space or not. 

Monitoring empty and occupied spaces with computer vision.

In this article, we will walk through how object detection can be used in parking management systems. Through this step-by-step tutorial, we'll explore the power of computer vision to make parking your vehicle a less tedious task. Let's get started!

Using Object Detection for Parking Lot Management

Object detection is a computer vision technique that identifies where certain objects are in an image. It can be used to detect empty and occupied spots in a parking lot to help you park. 

Using computer vision for parking management provides multiple advantages over traditional methods of searching for a parking spot. It is more efficient than circling around the parking lot for spots, in the process increasing fuel consumption and emissions.

This application of computer vision can be integrated with the CCTV cameras in the parking lot that are already deployed for security monitoring. The camera feeds can be processed with a computer vision model that tracks the number of empty and occupied spaces. This helps provide real-time updates on the parking spaces and is more precise than human-based systems. 

Now, we’ll go through the steps of creating a system that allows users to identify the number of empty and occupied spots in a parking space, and visualize where their vehicle could be parked.

A Trained Object Detection Model

Before we dive into the steps to build this system, let’s take a look at the object detection model we’ll be using. We'll be using a model trained to detect parking spots from Roboflow Universe. Roboflow Universe is a computer vision platform with open-source datasets and models. Roboflow Universe boasts a vast collection, offering over 200,000 datasets and 50,000 models ready for use. 

To get started, create a Roboflow account and navigate to the model page on the Car Space Detection model hosted on Roboflow Universe.

As you go through the page, you'll see a code snippet (as shown in the image below) demonstrating how to use the API with this model.

In the code, you'll find the model ID and version number, it's important to note them. In our example, the model is identified as "car-space-find" and it is the second version of the model. These details will help us when we put together our inference script.

Code Walkthrough

Our aim is to be able to analyze an image of a parking lot and identify the number of empty and occupied spaces. 

We’ll be using an image downloaded from the image dataset for the model we are using. But, you can use any relevant image like the image shown below.

Step 1: Setting Up the Requirements

To begin, let's install the necessary dependencies. Run the following command:

pip install roboflow

Step 2: Running an Inference using the Trained Model

Next, we’ll import the Roboflow library and load a pre-trained model. Remember to replace ROBOFLOW_API_KEY with your Roboflow API key. You can refer to the Roboflow documentation for more instructions on how to retrieve your API key. The code then uses this model to perform object detection on a local image.

from roboflow import Roboflow

#Replace ROBOFLOW_API_KEY with your Roboflow API Key

rf = Roboflow(api_key="ROBOFLOW_API_KEY")
project = rf.workspace().project("car-space-find")
model = project.version(2).model

# infer on a local image
output = model.predict("your_image.jpg", confidence=40, overlap=30).json()

Step 3: Understanding the Model Predictions

Then, we’ll use the output of the inference and extract the predicted class labels from the results.

for prediction in output['predictions']:
    predicted_class = prediction['class']

Step 4: Counting the Parking Spots

The following code snippet will help us count the number of empty and occupied parking spots.

occupied_count = 0
empty_count = 0

for prediction in output['predictions']:
    predicted_class = prediction['class']
    
    # Increment counts based on predicted classes
    if predicted_class == 'occupied':
        occupied_count += 1
    elif predicted_class == 'empty':
        empty_count += 1

Step 5: Display & Visualize Your Output

We can print the total counts, and save an image with the output visualized to your local files using the following code.

print("Total Occupied Spaces in the Parking Lot:", occupied_count)
print("Total Empty Spaces in the Parking Lot:", empty_count)
total_spots = occupied_count + empty_count
print("Total Spots in the Parking Lot:",total_spots)

# Visualize your prediction
model.predict("your_image.jpg", confidence=40, overlap=30).save("prediction.jpg")

Step 6: Putting it All Together

Finally, after putting steps 1-5 together, your code will look like as follows.

from roboflow import Roboflow

#Replace ROBOFLOW_API_KEY with your Roboflow API Key
rf = Roboflow(api_key="ROBOFLOW_API_KEY")
project = rf.workspace().project("car-space-find")
model = project.version(2).model

output = model.predict("your_image.jpg", confidence=40, overlap=30).json()
occupied_count = 0
empty_count = 0

for prediction in output['predictions']:
    predicted_class = prediction['class']
    
    # Increment counts based on predicted classes
    if predicted_class == 'occupied':
        occupied_count += 1
    elif predicted_class == 'empty':
        empty_count += 1

# Print total counts
print("Total Occupied Spaces in the Parking Lot:", occupied_count)
print("Total Empty Spaces in the Parking Lot:", empty_count)
total_spots = occupied_count + empty_count
print("Total Spots in the Parking Lot:",total_spots)

# Visualize your prediction
model.predict("your_image.jpg", confidence=40, overlap=30).save("prediction.jpg")

When you run the code above, you’ll see the following output in the terminal.

The image below, saved to our computer, shows the results from our model:

The model successfully identifies empty spaces with the “empty” label and occupied spaces with the “occupied” label. This model could be improved using more data representative of the parking lot in which it is deployed. To learn more about training a custom vision mode with your own data, refer to the Roboflow Getting Started guide.

This model could be combined with other business data you have (i.e. the location of a camera) to identify in what zones of a parking lot are spaces. You could build display information about the number of free spaces in each zone at parking lot entry points. This information will help drivers evaluate where they should go to park.

Impact and Future Potential

Parking lot management systems fit into the vision of smart cities. Computer vision can be seamlessly integrated with security cameras and IoT (Internet of Things) devices to enable functionalities such as automated parking guidance and payment systems.

In recent years, computer vision has played a significant role in parking management, with cities like San Francisco, London, and Singapore implementing smart parking systems. The use of AI and computer vision-based solutions is expected to propel the global smart parking market size to reach billions of dollars by 2028.

Conclusion

Overall, using object detection transforms parking lot management by allowing you to understand how many people are parked in parking lots. As computer vision technologies continue to be developed and deployed, the vision of hassle-free parking experiences and reduced congestion become more of a reality.