Skip to content

ananas304/Vehicle-Parking-Automation-Using-Machine-Learning

Repository files navigation

Vehicle Parking Automation Using Machine Learning

Overview

The Vehicle Parking Automation project is designed to streamline the parking process using Machine Learning (ML) techniques, particularly focusing on image recognition to identify car license plates. The system aims to automate the allocation of parking spots by detecting car numbers through captured images.

Project Structure

Files and Directories

  1. image_detection.ipynb: Jupyter Notebook for image detection and OCR.
  2. detection.py: Script for detecting license plates.
  3. detect.py: Script for performing OCR and displaying results.
  4. mainCar.py: Main script for managing the overall parking system.
  5. mes.py: Supporting script for handling miscellaneous tasks.
  6. images/: Directory containing sample images for testing.
  7. data.csv: CSV file for logging detected license plates and their accuracy.

Primary Goal:

To efficiently allocate parking spots in a parking facility using image recognition technology.

Key Features

  1. Detects vehicle license plates from images using OpenCV and EasyOCR.
  2. Automatically extracts vehicle numbers and displays them on the image.
  3. Assigns parking slots based on the detected vehicle number (simulated).
  4. Maintains a log of detected license plates and timestamps in a CSV file.

Installation

1. Clone the Repository

git clone https://github.com/ananas304/your-repo.git

2. Install Dependencies

Create a virtual environment (optional but recommended):
python -m venv env
Activate the virtual environment:
  • On Windows:
    .\env\Scripts\activate
  • On macOS/Linux:
    source env/bin/activate
install the required packages:
pip install -r requirements.txt

3. requirements.txt should include:

easyocr
opencv-python
imutils
matplotlib
pandas

Usage

1. Image Preprocessing

The initial step involves loading and preprocessing the image.

import cv2
from matplotlib import pyplot as plt
#Load image
image = cv2.imread('images/4.jpg')
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
#Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.imshow(cv2.cvtColor(gray, cv2.COLOR_BGR2RGB))

2. Edge Detection

Apply Gaussian blur and Canny edge detection to preprocess the image for contour detection.

blur = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blur, 10, 200)
plt.imshow(cv2.cvtColor(edged, cv2.COLOR_BGR2RGB))

3. Contour Detection

Find contours and identify the license plate contour.

contours, _ = cv2.findContours(edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:5]

n_plate_cnt = None
for c in contours:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)
    if len(approx) == 4:
        n_plate_cnt = approx
        break

if n_plate_cnt is None:
    print("No contour detected")

4. Optical Character Recognition (OCR)

Perform OCR on the detected license plate using EasyOCR.

from easyocr import Reader

reader = Reader(['en'])
detection = reader.readtext(license_plate)

if len(detection) == 0:
    text = "Unable to detect"
else:
    text = detection[0][1]
    accu = f"{detection[0][2] * 100:.2f}%"
    final_text = f"{text} {accu}"

5. Display Results and Save Data

Display the processed images and save the detected text to a CSV file.

if len(detection) == 0:
    cv2.putText(image, "Unable to detect", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 3)
else:
    cv2.drawContours(image, [n_plate_cnt], -1, (0, 255, 0), 3)
    cv2.putText(image, final_text, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 3)

#Save results
raw_data = {'date': [time.asctime(time.localtime(time.time()))], 'v_number': [text]}
df = pd.DataFrame(raw_data, columns=['date', 'v_number'])
df.to_csv('data.csv', mode='a', index=False, header=False)

6. Batch Processing

Define a function to process multiple images and run the detection.

def imageDetection(img_path):
    # Include all the previous steps here
    pass

for i in range(1, 16):
    imageDetection(f'images/{i}.jpg')

Screenshots

- License plates detected with the number displayed on the image.
image of car 1 image of car 2 image of car 3 image of car 4
- Cars being assigned parking slots and exiting.
image of car being slotted image of car being exited

Notes

  • Ensure that the images in the images/ folder are named 1.jpg, 2.jpg, etc.
  • Adjust the parameters in Gaussian blur and Canny edge detector as needed based on your images.

Acknowledgements

  • EasyOCR: For Optical Character Recognition.
  • OpenCV: For image processing.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks