opencv + svm realizes license plate recognition (with complete code)

1. The purpose of the experiment

Through a photo of a car with a license plate, segment the license plate and identify the license plate number of the car on the picture

2. Specific content

  1. License plate positioning
  2. License plate character segmentation
  3. License plate character recognition

Three, the experimental process

1. License plate location The
specific process:
1. Grayscale conversion: Convert a color picture to a grayscale image, the common R=G=B=pixel average value.
2. Gaussian smoothing and median filtering: remove noise.
3. Binarization processing: the image is converted into black and white, usually the pixel is larger than 127 and set to 255, and smaller than is set to 0.
4. Canny edge detection
5. Expansion and refinement: enlarge the image outline and convert it into regions, which contain license plates.
6. Select the appropriate license plate position through the algorithm, usually filter out the smaller area or look for the blue bottom area.
7. Mark the position of the license plate and extract the license plate

Read in the original image
Insert picture description here

Convert BGR to grayscale image
Insert picture description here

Image binarization processing

Insert picture description here

canny edge detection
Insert picture description here

Perform closed and open operations to eliminate small areas and reserve large areas to locate the license plate position

Insert picture description here

Insert picture description here

Insert picture description here

Find the upper left and lower right points of the contour, and calculate its area to length ratio. Find the three largest areas.
In the three largest areas, color recognition is used to find the area that most resembles the license plate.
Currently, it supports the recognition of three-color license plates, and traverses all pixels to determine the color according to the interval of the HSV value of each point.
Count the color values ​​of all points, and determine the color
HSV color space range of the license plate according to the number of all yellow, blue, and green points counted :
Insert picture description here

Color recognition result:

Insert picture description here
Insert picture description here

Insert picture description here

2. License plate character segmentation
Since there are only black and white pixels in the image, the characters can be separated by the white pixels and black pixels of the image. That is, the characters are located by judging the positions of the black and white pixel values ​​in each row and each column.

Insert picture description here

Segmentation result:
to grayscale
Insert picture description here

Binarization

Insert picture description here

Split:

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

3. License plate character recognition

The SVM model of the machine learning framework that comes with OpenCv is used here.
Two svm models are trained to recognize the Chinese province abbreviation of license plates and the letters and numbers that
follow. Opencv's svm framework uses:

model = cv2.ml.SVM_create()#Generate an SVM model

model.setGamma(gamma) #Set the Gamma parameter, 0.5 in the demo

model.setC©# Set penalty items, as: 1

model.setKernel(cv2.ml.SVM_RBF)#Set the kernel function

model.setType(cv2.ml.SVM_C_SVC)#Set the model type of SVM: SVC is a classification model, SVR is a regression model

model.train(samples, cv2.ml.ROW_SAMPLE, responses)#训练

model.predict(samples)#predict

model.load(filepath)#Load the trained model from the file

model.save(filepath)#Save the trained model to a file

Training data set:
The training samples used in SVM training come from github.
The training data set is divided into two categories, and two models are trained separately
to recognize the province abbreviation and the English characters and numbers
0-9 on the right, and the data set from A to Z:

Insert picture description here
Insert picture description here

The province abbreviated data set of the license plate:

Insert picture description here

Insert picture description here

Training process:
Use the name of the folder as the label, and the picture in the file as the data.
Use the os.walk method, which is mainly used to traverse the subdirectories and subfiles in a directory.
You can get a triple (root, dirs, files),
root refers to the address of the folder that is currently being traversed,
dirs is a list, and the content is the names of all directories in the folder (not including sub Directory)
files is also a list, the content is all the files in the folder (not including subdirectories)
os.path.basename(), returns the last name of the path, as the label
cv2.imread(filepath) to read the training data and
mark it Once you have the training data and labels, you can "feed" to the classifier:
model.train(chars_train, chars_label)
Finally, save the trained model to a file, next time you can use
model.save("module\svm.dat ")

Insert picture description here

Insert picture description here

Recognition process:
1. Load the previously trained model first

Insert picture description here

2. Traverse all the segmented character pictures and call model.predict(part_card) to
sequentially identify all the character pictures segmented by the license plate. The
first character calls the Chinese svm recognition and the
other subsequent characters call the alphanumeric svm recognition

Insert picture description here

Recognition result:

Insert picture description here

Insert picture description here

4. Experimental results

Some of the results are as follows:

Insert picture description here

Insert picture description here
Insert picture description here

Results:
.Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here

Five, summary

The current program has some defects, for example, if the color of the car is blue-green or yellow, the position of the license plate cannot be determined by the color. The training data set is limited, and the recognition of Chinese characters is not very accurate.

Project complete code github address

Guess you like

Origin blog.csdn.net/qq_41784749/article/details/112705813