Building a Number Plate Detector with OpenCV
Depending on where you live, you may have seen speed cameras that take pictures of your car if you’re going above the posted speed limit (image above) or license plate cameras on toll roads. Imagine taking photos of every car that speeds by and then manually writing out the car’s license plate number so that you can issue a ticket. This would be a pretty tedious process. Luckily, we have systems that have automated this entire process, and I replicated a system that detects and scans car number plates with OpenCV through Python.
Computer Vision is a subsection of Artificial Intelligence that allows computers to learn important information from images or videos and possibly carry out actions or provide recommendations based on the computer’s analysis.
I was able to work with computer vision by using the OpenCV library in Python. OpenCV is a library of programming functions used for real-time computer vision to work with live photos and video when developing programs. OpenCV and object detection are also used in autonomous vehicles. See the demo of the model:
Steps:
For this project, I followed a tutorial by Murtaza on Computer Vision and OpenCV and then added some extra pizazz to make it unique.
1. Firstly, import cv2 and pytesseract as these are the two Python libraries we will be using. I used PyCharm as my IDE, and I’d recommend using this if you’re new to programming, as it can get a bit complicated installing packages and libraries the traditional way.
2. After you get your libraries set, you want to set up a system that initiates your computer’s camera for a live photo/video feed. Something that can be used to initialize the computer’s camera is:
frameWidth = 640 # camera frame width
frameHeight = 480 # camera frame height
cap = cv2.VideoCapture(1) # initialize computer camera
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10,150) #customize brightnesswhile True:
success, img = cap.read()
cv2.imshow("Result",img)
if cv2.waitKey(1) & 0xFF == ord('q'): #clicking q breaks loop
break
The first few lines look at customizing the size and brightness of the camera frame, and then a while loop runs and shows the camera results on the screen through cv2.imshow, and the loop can be broken by clicking q.
3. Once we have the camera running, our next step is to bring in the cv2. cascade files that will do a good chunk of work for detecting number plates. This is a pre-trained xml file that is provided with OpenCV. In this step, we will also be defining a few variables that will be used later.
plateCascade = cv2.CascadeClassifier("file path - haarcascade_russian_plate_number.xml")
#import plate detector from cv2 library
minArea = 500 # minimum area that rectangle has to be bigger than
color = (255,0,255) # text color
count = 0 # scan counter (begins from 0)
minArea will be used for placing rectangles as a highlighter for a number plate found, color will be used for the text colour when a number plate is found, and count will be for saving the scans of number plates found.
4. Next, the program will convert the live video feed to grey as this helps build a more accurate model, and the classifier will work on the grey live video feed (this will be underneath the while statement from earlier).
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # convert image to grey
numberPlates = plateCascade.detectMultiScale(imgGray, 1.1, 4) # this will find the number plates in the live feeds
5. In the following steps, the program goes through all of the number plates detected to display rectangles over the plates.
for (x, y, w, h) in numberPlates:
area = w*h
if area > minArea: # if area > minArea, it will declare as rectangle and put text on live photo/video
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 255), 2)
cv2.putText(img, "Number Plate", (x,y-5),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,color,2)
imgRoi = img[y:y+h,x:x+w]
cv2.imshow("ROI", imgRoi)
The for loop goes through each number plate found, then the if statement figures out whether a rectangle will be placed or not. cv2.rectangle places the rectangle over the images, and cv2.putText places the text “number plate” when a plate is found. imgRoi will crop our image to the actual number plate and display it at the corner of our screen for us to see.
6. Finally, in these steps, we’ll be able to see the results on our screen and save the number plates found!
if cv2.waitKey(1) & 0xFF == ord('s'):
cv2.imwrite("saving location path"+str(count)+".jpg",imgRoi)
cv2.rectangle(img,(0,200),(640,300),(0,255,0),cv2.FILLED)
cv2.putText(img,"Scan Saved", (150,265),cv2.FONT_HERSHEY_COMPLEX,2,(0,0,255),2)
cv2.imshow("Result",img)
cv2.waitKey(500)
count +=1
If ‘s’ is pressed on the keyboard, it will save to the saving location path, as seen in line 2 of the code above. Then, lines 3 and 4 print a rectangle on the screen with the text “Scan Saved” to confirm that a scan has been saved. The result is sent over to the saving location folder declared and the counter increases by 1, indicating the number of plates that have already been saved.
7. The central number plate detection portion of the program is complete, but if we want to retrieve the actual letters and numbers of the number plates that we scanned and saved, we could use the following function:
import pytesseractpytesseract.pytesseract.tesseract_cmd = r'path to ocr teseract'print(pytesseract.image_to_string(r'image path'))
We will need to import another library, tesseract, as this is used for retrieving letters and numbers from images. This function works best on Windows as tesseract is easier to use on Windows, so it may not work immediately if you’re trying it out.
Overall, computer vision is a growing field of artificial intelligence and is changing the world around us. Aside from detecting number plates, computer vision is used for self-driving cars, Face ID on your phone, and more. I’m looking forward to seeing what industries are affected as computer vision, and artificial intelligence becomes more prevalent in today’s society.
Thanks for reading!
If you’d like to subscribe to my monthly newsletter, click here, and if you’d like to connect with me on LinkedIn, click here. Visit my Personal Website: jayantarora.ca