So guys in this blog we will see how we can implement Helmet and Number Plate Detection and Recognition in Python using YOLOv3 and some other Computer Vision techniques. This is a very advanced project which you can use for your college minor projects as well as major projects. So without wasting any further time.
Our main motive behind Helmet and Number Plate Detection and Recognition were to first detect if someone is wearing a helmet or not, if he is wearing it, no problem, but if not, detect his number plate and send an e-challan to him.
This is going to be a very interesting blog, so without any due, Let’s do it…
Create a conda environment and install the required libraries
conda create -n hnpdr python=3.9 conda activate hnpdr pip install opencv-python numpy tensorflow imutils
Code for Helmet and Number Plate Detection and Recognition…
import cv2 import numpy as np import random import os from PIL import Image import time import imutils from tensorflow.keras.models import load_model os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true' net = cv2.dnn.readNet("yolov3-custom_7000.weights", "yolov3-custom.cfg") net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA) net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA) model = load_model('helmet-nonhelmet_cnn.h5') print('model loaded!!!') cap = cv2.VideoCapture('testing videos/test2.mp4') COLORS = [(0,255,0),(0,0,255)] fourcc = cv2.VideoWriter_fourcc(*"XVID") writer = cv2.VideoWriter('output.avi', fourcc, 5,(888,500)) writer = VideoWriter('output.avi',(frame.shape[1], frame.shape[0])) writer.open() def helmet_or_nohelmet(helmet_roi): try: helmet_roi = cv2.resize(helmet_roi, (224, 224)) helmet_roi = np.array(helmet_roi,dtype='float32') helmet_roi = helmet_roi.reshape(1, 224, 224, 3) helmet_roi = helmet_roi/255.0 return int(model.predict(helmet_roi)[0][0]) except: pass layer_names = net.getLayerNames() output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] ret = True while ret: ret, img = cap.read() img = imutils.resize(img,height=500) # img = cv2.imread('test.png') height, width = img.shape[:2] blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False) net.setInput(blob) outs = net.forward(output_layers) confidences = [] boxes = [] classIds = [] for out in outs: for detection in out: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.3: center_x = int(detection[0] * width) center_y = int(detection[1] * height) w = int(detection[2] * width) h = int(detection[3] * height) x = int(center_x - w / 2) y = int(center_y - h / 2) boxes.append([x, y, w, h]) confidences.append(float(confidence)) classIds.append(class_id) indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) for i in range(len(boxes)): if i in indexes: x,y,w,h = boxes[i] color = [int(c) for c in COLORS[classIds[i]]] # green --> bike # red --> number plate if classIds[i]==0: #bike helmet_roi = img[max(0,y):max(0,y)+max(0,h)//4,max(0,x):max(0,x)+max(0,w)] else: #number plate x_h = x-60 y_h = y-350 w_h = w+100 h_h = h+100 cv2.rectangle(img, (x, y), (x + w, y + h), color, 7) # h_r = img[max(0,(y-330)):max(0,(y-330 + h+100)) , max(0,(x-80)):max(0,(x-80 + w+130))] if y_h>0 and x_h>0: h_r = img[y_h:y_h+h_h , x_h:x_h +w_h] c = helmet_or_nohelmet(h_r) cv2.putText(img,['helmet','no-helmet'][c],(x,y-100),cv2.FONT_HERSHEY_SIMPLEX,2,(0,255,0),2) cv2.rectangle(img, (x_h, y_h), (x_h + w_h, y_h + h_h),(255,0,0), 10) writer.write(img) cv2.imshow("Image", img) if cv2.waitKey(1) == 27: break writer.release() cap.release() cv2.waitKey(0) cv2.destroyAllWindows()
- Line 1-8 – Importing required libraries.
- Line 10 – Allow TensorFlow to use GPU.
- Line 12 – Read the network weights from yolo files. These ‘yolo.weights’ is the file that we trained just to detect bikes and number plates.
- Line 13-14 – If you want to use GPU, set the backend and target to CUDA.
- Line 17-18 – Load the CNN model we trained to detect helmets.


- Line 20 – VideoCapture object to read frames from the video feed.
- Line 21 – A color array that we will use later.
- Line 23-27 – This writer will help write our output frames to a video file using cv2.VideoWriter().
- Line 29-37 – This function will be provided with an image and the model will predict whether there is a helmet or not in the image.
- Line 39-40 – Get the output layers to take the output from them in further steps.
- Line 46-49 – Read the frames from the video file, resize them and get their height and width.
- Line 51 – Use cv2.dnn.blobFromImage() to create a blob from the image and to use this blob as input to our network.
- Line 53 – Set this blob as input to our network.
- Line 54 – Flow this blob through the network and get the outputs.
- Line 60-76 – Get the outputs from the network.
- Line 78 – Get the indexes of the boxes which we will not consider due to Non-Maximum Suppression.
- Line 80-99 – Draw a green box around bikes and a red box around number plates. Detect if there is a helmet or not and print that.
- Line 102-103 – Write the output images as a video.
- Line 105-106 – Break the code if someone hits the ESC key.
Final results…
Download Source Code from here…
Do let me know if there’s any query regarding Helmet and Number Plate Detection and Recognition by contacting me on email or LinkedIn.
So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…
Read my previous post: HEALTHCURE – AN ALL IN ONE MEDICAL SOLUTION – MEDICAL PROJECT – 7 DISEASE DETECTIONS
Check out my other machine learning projects, deep learning projects, computer vision projects, NLP projects, Flask projects at machinelearningprojects.net.
can you please share the weight and config file related to it or can you share the github link in order to run it in our system.
I have uploaded the source code 🙂
Can I get the code for output data connected with any database system.
We are not storing anything in this project…
Although you can do that…
Do you have any idea on that, for my mini project have to connect with database , if u have idea please share it to me
How to extract the number plate number and screenshot?
Sir in which plot from we execute this code (Python) Jupeter, R studies, Oracle etc in python
I didn’t get what you are asking…
sir can u do a tutorial video ?
Sir can you please share your email id..
Find that on Contact Page.
Interesting post, thanks for sharing.
Thanks 🙂
Can I get that cctv video?
Sir..how to find the count of the people who are not wearing the helmet??
In Line 97 we are predicting whether the person is wearing the helmet or not. It returns either 0 or 1. 0 means helmet and 1 means no helmet. You can add an if statement there. Initialize a counter and if c==1, increment the counter.
sir…you also mentioned about the e-challan generation can I get the code for that please?
For that, you need the government api.
Hello Sir,Can you please share the steps for how to run project from starting
Just follow the steps in the blog…
can u share it briefly sir ?
Sir, How to read the numbers from ROI on number plate region ?
You can use any OCR(e.g. PyTesseract) for that.
what are the softwares required to be installed to run this??
No specific software is required.
Just some python packages are required which are mentioned in the project…
sir how to add my traffic clip video please help me
Just paste your video in the folder and name it test2.mp4
Hey, Can you share the drive link for your traffic clip please !!
Hey ,
R Gowtham can you share your traffic clip
Thank you so much!!Working perfectly fine
Can you please say how to add a GUI for this
What type of GUI?
GUI(using flask/django/tkinter/gradio) where user can input his video(input video in the code)
Sir how can I get the number plate photos so i can extract the number?
In the code we are getting the number plate ROI, you can pass that through an OCR to extract the number.
Also can i get more sample input videos or references to the videos?
Bro can you provide the dataset used?
same problem have you got the dataset
how to run it in windows bro please help
can i get the dataset for this…….Good one by the way.
thanks in advance.
The dataset is created by randomly taking out frames from the video and annotating them manually…
have you got the dataset bro?
hi can you share how to store number plates which are been scanned
you can save number_plate_roi image in any folder…
Sir could you please provide me the dataset for training ?
how to get more data set
Hi,
Please can you share the source github link.