Smart Parking Inspection System

Using AI to Revolutionize Parking Management

Developed by Nyabiosi Sydiney Nyabiosi - 2024 Capstone Project

Inference Image

The inference image script employs deep learning models and image processing techniques to conduct precise object detection and recognition on individual images, with a focus on license plate identification. Leveraging pretrained models and libraries like PyTorch and OpenCV, the script adeptly analyzes images, identifies relevant objects, including license plates, and extracts their alphanumeric content. Upon plate detection, the script reads the plate's text and prints it to the terminal, offering comprehensive insights for a diverse range of computer vision applications.

Imports

These imports are essential for the functionalities used in the script, including reading and preprocessing images, performing deep learning inference, visualizing results, and extracting text from images.

  1. import os: This imports the `os` module, which provides a portable way to interact with the operating system. It is used for tasks such as file path manipulations (`os.path.join()`).
  2. import torch: This imports the PyTorch library, which is a popular open-source machine learning library used for tasks such as deep learning and neural networks. PyTorch provides functionalities for tensor computations, autograd (automatic differentiation), and building and training neural networks.
  3. import matplotlib.pyplot as plt: This imports the `pyplot` module from the Matplotlib library, which is a plotting library for Python. Matplotlib is used for creating static, interactive, and animated visualizations in Python.
  4. import pytesseract: This imports the `pytesseract` module, which is a Python wrapper for Google's Tesseract-OCR Engine. Tesseract is an open-source OCR engine that can be used to recognize and extract text from images.
  5. import cv2: This imports the `cv2` module from OpenCV, which is an open-source computer vision and machine learning software library. OpenCV provides various tools and functions for image processing, computer vision tasks, and machine learning algorithms.
  6. torchvision.io import read_image: This imports the `read_image` function from the `torchvision.io` module, which is part of the PyTorch Vision library (`torchvision`). This function is used to read an image file and convert it into a PyTorch tensor.
  7. from torchvision import tv_tensors: This imports the `tv_tensors` module from `torchvision`, which provides utilities for manipulating tensors. It is used for converting images to tensors.
  8. torchvision.utils import draw_bounding_boxes: This imports the `draw_bounding_boxes` function from `torchvision.utils`, which is part of the PyTorch Vision library. This function is used for drawing bounding boxes on images.

PlateDetection class

  1. __init__(self, model_path, device='cpu'): This method serves as the constructor for the `PlateDetection` class. When an instance of the class is created, it takes two parameters: `model_path`, which is the path to the trained model file, and `device`, which specifies the device (CPU or GPU) where the model will be loaded. Inside the method, the trained model is loaded using `torch.load()` with the specified path and then moved to the selected device.
  2. load_image(self, image_path): This method is responsible for loading and preprocessing the sample image. It takes the path to the image file (`image_path`) as input. It utilizes `torchvision.io.read_image()` to read the image and converts it into a PyTorch tensor. Additionally, it normalizes the image to the range of [0, 1]. The preprocessed image tensor is then returned after being moved to the specified device.
  3. perform_inference(self, sample_image): In this method, the actual inference is performed using the preprocessed image (`sample_image`). The model stored in `self.model` is used to perform the inference. `torch.no_grad()` is used to disable gradient computation during inference, which reduces memory usage and speeds up computation. The inference result is returned.
  4. visualize_result(self, image, result): This method visualizes the inference result. It takes the original image (`image`) and the inference result (`result`) as inputs. First, it converts the image tensor back to the range of [0, 255] for visualization purposes. Then, it extracts the predicted labels (which represent the confidence scores of the number plate predictions) and bounding box coordinates from the inference result. Bounding boxes are drawn around the detected number plates on the image. Additionally, Tesseract OCR is used to extract text from each plate region. Finally, the image with bounding boxes and extracted plate texts is displayed using Matplotlib.

Execute Script

Overall, this script demonstrates a pipeline for detecting and extracting number plates from vehicle images, combining deep learning for object detection with Tesseract OCR for text extraction.