The traffic police also think it's wonderful - Python recognizes the license plate

License plate recognition has a wide range of applications in highways, such as our common electronic toll collection (ETC) system and the detection of traffic violation vehicles, in addition to community or underground

Garage access control will also be used, basically wherever the identity of the vehicle needs to be detected.

Please add image description

Some background:

License plate recognition system (Vehicle License Plate Recognition) is an application of computer video image recognition technology in vehicle license plate recognition.

The system mainly includes the following four parts:

• Vehicle image acquisition

• License plate location

• License plate character segmentation

• License plate character recognition

Let's take a look at the description of license plate recognition technology in Wikipedia:

License plate recognition technology requires the ability to extract and recognize moving license plates from complex backgrounds, through license plate extraction, image preprocessing, feature extraction, and license plate character recognition.

Different technologies can identify information such as vehicle license plates, colors, etc. The latest technology level is that the recognition rate of letters and numbers can reach 99.7%, and the recognition rate of Chinese characters can reach 99%.

Please add image description

Method to realize

We do not do too complicated vehicle dynamic recognition here, but only demonstrate the recognition of license plate information from images. There are two ways to implement the license plate recognition function. One is to write it yourself.

Code implementation, the other is through third-party API interface implementation.

implement it yourself

If we want to implement the license plate recognition function manually through Python, we can use some Python libraries, such as: OpenCV, TensorFlow, etc.

Because each function point needs to be coded and implemented by ourselves, it will be relatively complicated. On the other hand, if we want to ensure the accuracy of recognition, we may need to make it larger.

amount of experimentation, which means that it will take more time.

third-party interface

Now some third-party platforms have implemented the function of license plate recognition, and they have provided API interfaces, we only need to call the interfaces provided by them.

This method is relatively simple to implement, and usually the accuracy of the interface functions provided by the interface provider can basically be guaranteed. The reason is very simple. If the interface

If the function is too poor, firstly, you will be slapped in the face, and basically no one will use it, so the value provided by the interface will be lost, and third-party interfaces may be charged.

Take a certain fee, therefore, if we implement it in reality, we must consider it comprehensively.
Please add image description

Implementation

Based on the above situation, we use a third-party interface to realize the function of license plate recognition. As the interface provider, we choose the interface provided by Baidu Cloud and the Baidu Cloud interface.

A free quota is provided. Simply put, it is how many times you can use it for free every day. If you exceed this number of times, you need to pay or something. The document address is:

https://cloud.baidu.com/doc/OCR/index.html, let's take a look at the specific implementation process.

SDK installation

Baidu Cloud SDK provides support for multiple languages, such as: Python, Java, C++, IOS, Android, etc. Here we install the Python version of the SDK, which is very easy to install.

Simple, just use the pip install baidu-aip command. The SDK supports Python versions: 2.7+ and 3.x. The SDK directory structure is as follows:

Python学习交流Q群:660193417####
├── README.md
├── aip                   // SDK 目录│
├── __init__.py       // 导出类│   
├── base.py           // aip 基类│   
├── http.py           // http 请求│   
└── ocr.py //OCR└── setup.py              // setuptools 安装

Create an app

After the SDK is installed, we need to create an application. Here we need a Baidu account or Baidu Cloud account. If you don't have one, you can register one yourself, log in and register

The address is: https://login.bce.baidu.com/?redirect=http%3A%2F%2Fcloud.baidu.com%2Fcampaign%2Fcampus-

2018%2Findex.html, after logging in, we move the mouse to the position of the login avatar, and then click the user center in the pop-up menu, as shown in the following figure:
Please add image description
Please add image description
After the information is checked, click the save button.

Then move the mouse to the left column > Symbol position, and then select artificial intelligence and text recognition in turn, as shown in the following figure:
Please add image description
After clicking, you will enter the following figure:
Please add image description
We click Create an application, and enter the following figure:
Please add image description
Here we only need Fill in the application name and application description below, and click Create Now after filling in.

After creation, we return to the application list, as shown in the following figure:
Please add image description
Here we need to use three values: AppID, API Key and Secret Key.
Please add image description

Implementation

After the application is created, we can call the interface to realize the license plate recognition function.

First, we need to create AipOcr. AipOcr is the Python SDK client of OCR. It provides a series of interactive methods for developers using OCR.

It is also relatively simple, as follows:

from aip import AipOcr
# 自己的 APPID AK SKAPP_
ID = '自己的 App 
ID'API_KEY = '自己的 Api Key'
SECRET_KEY = '自己的 Secret Key'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

In the above code, the constants APP_ID, API_KEY and SECRET_KEY are the constant values ​​we need to use when viewing the application list. These values ​​are all characters

String, used to identify the user and perform signature verification for access.

If we need to configure the network request parameters of AipOcr, we can call the interface to set the parameters after constructing AipOcr. Currently, two parameters are supported. Let's take a look at the code implementation:

# 建立连接的超时时间,单位为毫秒
client.setConnectionTimeoutInMillis(5000)
# 通过打开的连接传输数据的超时时间,单位为毫秒client.setSocketTimeoutInMillis(5000)

In general, it is relatively simple to realize the license plate recognition function through the interface. Take the following figure as an example:
Please add image description

The implementation code is as follows:

from aip import AipOcrAPP_ID = '自己的 App ID'API_KEY = '自己的 Api Key'SECRET_KEY = '自己的 Secret Key'# 创建客户端对象client = AipOcr(APP_ID, API_KEY, SECRET_KEY)# 建立连接的超时时间,单位为毫秒client.setConnectionTimeoutInMillis(5000)# 通过打开的连接传输数据的超时时间,单位为毫秒client.setSocketTimeoutInMillis(5000)# 读取图片def get_file_content(filePath):    with open(filePath, 'rb') as fp:        return fp.read()image = get_file_content('car.jpeg')res = client.licensePlate(image)print('车牌号码:' + res['words_result']['number'])print('车牌颜色:' + res['words_result']['color'])

Results of the:

车牌号码:川QK9777车牌颜色:blue

The above code realizes the recognition of a license plate in a picture. Of course, the interface also supports the recognition of multiple license plates in a picture, just use

licensePlate(image, options) is enough, take the following figure as an example:
Please add image description
the implementation code is as follows:

from aip import AipOcr
APP_ID = '自己的 App ID'API_KEY = '自己的 Api Key'SECRET_KEY = '自己的 Secret Key'# 创建客户端对象client = AipOcr(APP_ID, API_KEY, SECRET_KEY)# 建立连接的超时时间,单位为毫秒client.setConnectionTimeoutInMillis(5000)# 通过打开的连接传输数据的超时时间,单位为毫秒client.setSocketTimeoutInMillis(5000)
# 读取图片def get_file_content(filePath):    with open(filePath, 'rb') as fp:        return fp.read()
image = get_file_content('cars.png')options = {}# 参数 multi_detect 默认为 falseoptions['multi_detect'] = 'true'res = client.licensePlate(image, options)for wr in res['words_result']:    print('车牌号码:' + wr['number'])    print('车牌颜色:' + wr['color'])

Results of the:

车牌号码:京N6HZ61
车牌颜色:blue
车牌号码:鲁NS1A26
车牌颜色:blue

At last

In this article, we first introduce some license plate recognition, and then use the Baidu cloud interface to realize the recognition function of single and multiple license plates. Through this article, we can recognize license plates.

Have some understanding of the related concepts and specific implementations. Today's sharing ends here, see you in the next chapter! ! !
Please add image description

Guess you like

Origin blog.csdn.net/m0_67575344/article/details/124331900