OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Detecting and recognizing a car license plate from a video in real-time involves multiple steps, and can be approached using various methods. In this tutorial, I'll guide you through a basic approach using OpenCV and Python:
Install the necessary libraries if you haven't already:
pip install opencv-python
For detecting license plates, you can use a Haar cascade classifier:
import cv2 # Load the pre-trained Haar cascades from OpenCV plate_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_russian_plate_number.xml') cap = cv2.VideoCapture('path_to_video.mp4') while cap.isOpened(): ret, frame = cap.read() if not ret: break gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) plates = plate_cascade.detectMultiScale(gray, 1.1, 10) for (x, y, w, h) in plates: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) roi_gray = gray[y:y + h, x:x + w] roi_color = frame[y:y + h, x:x + w] # Here you can add the recognition part cv2.imshow('Video', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
License plate recognition is more complex and typically involves Optical Character Recognition (OCR). A popular tool for this is Tesseract. You can install it via pip:
pip install pytesseract
Make sure you also have Tesseract installed on your system.
After setting up Tesseract, you can recognize license plates as follows:
import pytesseract # ... # Inside the for loop, after detecting the license plate: text = pytesseract.image_to_string(roi_gray, config='--psm 8') cv2.putText(frame, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2) # ...
This code captures license plates from a video and displays them in real-time. Adjust the parameters of detectMultiScale
and the configurations of Tesseract as needed to improve accuracy.
Car license plate recognition with OpenCV in Python:
import cv2 # Load the pre-trained license plate classifier plate_cascade = cv2.CascadeClassifier('haarcascade_license_plate.xml') # Read an image img = cv2.imread('car_image.jpg') # Convert to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Detect license plates in the image plates = plate_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) # Draw rectangles around the license plates for (x, y, w, h) in plates: cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2) # Display the image with detected license plates cv2.imshow('License Plates', img) cv2.waitKey(0) cv2.destroyAllWindows()
Real-time license plate detection and recognition using OpenCV:
import cv2 # Load pre-trained license plate classifier and OCR engine plate_cascade = cv2.CascadeClassifier('haarcascade_license_plate.xml') ocr = cv2.text.OCRTesseract_create() # Open a connection to the webcam (0 represents the default camera) cap = cv2.VideoCapture(0) while True: # Read a frame from the webcam ret, frame = cap.read() # Convert to grayscale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect license plates in the frame plates = plate_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) # Recognize license plates using OCR for (x, y, w, h) in plates: plate_roi = frame[y:y+h, x:x+w] plate_text = ocr.run(plate_roi) cv2.putText(frame, plate_text, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # Display the frame with license plate recognition cv2.imshow('License Plate Recognition', frame) # Break the loop if 'q' key is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the webcam and close the window cap.release() cv2.destroyAllWindows()