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.
Let’s do it…
Create a conda environment and install 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, frame.shape)) 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)) except: pass layer_names = net.getLayerNames() output_layers = [layer_names[i - 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 * width) center_y = int(detection * height) w = int(detection * width) h = int(detection * 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 which 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 which 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.
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