Residential safety solutions with YOLO-v5

Aayushmaan Jain
3 min readJul 26, 2022

--

With increasing challenges to safety in society, unknown vehicles can be a potential danger and to combat that problem, I have tried to create a safety solution which detects the license plates of vehicles and registers them in a database along with the timestamp they were caught on camera.

Motivation behind the project

Unknown vehicles in residential buildings can cause inconvenience to the residents, even though there can be guests who are anyways authenticated at the lobby, and are assisted for finding parking spots, unknown vehicles can be a potential threat to safety and to combat that, I have tried to build a residential safety solution which keeps a track of the movement of vehicles in and out of the society

Getting started

  1. Finding the right data

I found an awesome data source on Kaggle which had over four hundred annotated images of license plates of cars.

2. Finding the right model

To detect the license plates accurately and effectively with high speed, I chose to use YOLOv5 algorithm from ultralytics as YOLO algorithms are known for their speed and effectiveness due to their single forward pass through the model to detect the object (as the name suggests — You Only Look Once)

3. Data preprocessing

The main challenge with the annotations was that it was annotated in the Pascal-VOC format which was incompatible with YOLO so I had to convert them into YOLO compatible format by using the following code

4. Creating training, validation and test sets

Since the data is now compatible with YOLO, we need to create training, validation and test sets to train, tune and evaluate the model using the following code.

5. Training the model

Since the data is now partitioned, we need to train the model. To do so, we use the ultralytics yolov5 implementation. The following code to train the model is inspired from the ultralytics yolov5 train on custom data documentation.

6. Selecting the best model

Since the model is now trained, we select the best version of the model from the latest run by using the following code.

7. Creating a database

Since now we have trained the model and loaded the best model, it is time to do some detections and to store the results from the detections, we need to create a database in our preferred database software, in this case, I prefer to use MySQL Community Edition

Now to connect to our database from python, we need to store the credentials in a config.py file so that we can connect to it from python

And now since we have our database ready, we can use it to store the authenticated members of the residential society and the results from the detections.

8. Create a web UI for the residents to register

To authenticate the members of the residential society, we can create a simple web UI where the secretary of the society can fill in the details after authenticating the documents. To create a web UI, I have used Flask, the choice of HTML templates and CSS is up to the user, I have included the link to the github repository of this project at the end of this article so the readers can refer to the code.

9. Perform license plate detection

And now to detect the license plates, we can load the model from torch hub, supply custom weights and can detect the license plates from the footage using OpenCV. To read the text from the license plate image, we can use various OCR libraries available, personally I prefer using EasyOCR for this purpose. The code to implement the object detection is given below

Github Repository

Kaggle Notebook to train the model

Future prospects of the project that I am currently working on are:

  1. Improve the speed of performing the detections and OCR, thereby reducing latency in detections
  2. Improve accuracy of OCR

Suggestions and Corrections are warmly welcome.

Please star the repository on github and upvote and comment on kaggle if you liked my work

About me:

I am Aayushmaan Jain, an aspiring data scientist pursuing a bachelor’s degree in Data Science

I am particularly interested in the field of Machine Learning, Deep Learning, Natural Language Processing and Computer Vision. I am eager to learn. I believe that the best way to learn a particular concept is to apply it in solving various real-world problems.

--

--

Aayushmaan Jain
Aayushmaan Jain

Written by Aayushmaan Jain

A data science enthusiast currently pursuing a bachelor's degree in data science

No responses yet