Smart Parking Inspection System
Using AI to Revolutionize Parking Management
Developed by Nyabiosi Sydiney Nyabiosi - 2024 Capstone ProjectInference Video/Live Stream
The provided Python script implements a robust system for license plate detection in videos using deep learning models and computer vision techniques. By leveraging pretrained models, Tesseract OCR, and OpenCV functionalities, the script efficiently identifies license plates in video frames, extracts plate numbers, and annotates bounding boxes, culminating in a comprehensive solution for automated license plate recognition.
Imports
- os This module provides a portable way of interacting with the operating system. It's used for file path operations.
- torch This is the main PyTorch library used for deep learning.
- cv2 This is the OpenCV library used for image and video processing.
- read_image This function from `torchvision.io` is used for reading images.
- draw_bounding_boxes This function from `torchvision.utils` is used for drawing bounding boxes on images.
- numpy as np NumPy is used for numerical operations in Python.
- pytesseract This is a wrapper for Google's Tesseract-OCR Engine, used for text recognition.
LicensePlateDetector Class
This class encapsulates the functionality for detecting license plates in a video.
__init__(self, model_paths, device='cpu')
- This is the constructor method of the class.
- It initializes the LicensePlateDetector object with the paths to the trained deep learning models for license plate detection.
- The `device` parameter specifies whether to use CPU or GPU for inference (default is CPU).
detect_plate(self, frame)
- This method takes a frame from the video as input.
- It iterates over each loaded model and performs license plate detection on the frame using the models.
- For each detected license plate with a confidence score above 0.85, it stores the label and bounding box coordinates.
- Returns the detected labels and bounding boxes for all models.
extract_plate_number(self, frame, box)
- This method extracts the plate number from a detected bounding box region.
- It takes the frame and the bounding box coordinates as input.
- It extracts the plate region, converts it to grayscale, and performs thresholding to get a binary image.
- Then, it uses Tesseract OCR to recognize the text in the binary image.
- It filters out non-alphanumeric characters and ignores plates with three or fewer characters.
- Returns the extracted plate number.
draw_bounding_boxes(self, frame, pred_labels_all, pred_boxes_all)
- This method draws bounding boxes around the detected plates on the frame and annotates them with extracted plate numbers.
- It takes the frame, detected labels, and bounding boxes as input.
- It iterates over each detected plate, draws a bounding box, and annotates it with the plate number.
- Returns the frame with bounding boxes and annotations.
process_video(self, video_file, output_video_path, video_title)
- This method processes a video file for license plate detection.
- It reads each frame of the input video, detects license plates, draws bounding boxes, and annotates them.
- Finally, it writes the processed frames to an output video file.
- It also displays the title on each frame.
play_video(self, video_file)
- This method plays the processed video showing the detected license plates.
- It reads the processed video file frame by frame and displays it.
- It exits the display when 'q' is pressed.
Execute Script
- In the main section, an instance of the LicensePlateDetector class is created.
- The input video file path, output video file path, and video title are specified.
- The video is then processed for license plate detection, and the processed video is played.
Overall, this script demonstrates a comprehensive pipeline for detecting license plates in a video using deep learning models and various image processing techniques.