Using Python license plate recognition technology, several lines of code

License plate recognition is widely used in expressways, such as our common electronic toll collection (ETC) system and the detection of traffic violation vehicles. In addition, access control in residential areas or underground garages will also be used. Basically, it will be used in all places where vehicle identity detection is required.

brief introduction

Vehicle License Plate Recognition is an application of computer video image recognition technology in Vehicle License Plate Recognition. Generally, a license plate recognition system mainly includes the following four parts:

  • Vehicle image acquisition
  • License plate location
  • License plate character segmentation
  • License plate character recognition

Let's take another look at the description of license plate recognition technology in Encyclopedia:

License plate recognition technology requires that the moving vehicle license plate can be extracted and recognized from the complex background. Through license plate extraction, image preprocessing, feature extraction, license plate character recognition and other technologies, the vehicle brand, color and other information can be recognized. At present, the latest technical level is that the recognition rate of initials and numbers can reach 99.7%, and the recognition rate of Chinese characters can reach 99%.
PS note: Here's a reminder. Many people will encounter all kinds of troublesome problems in the process of learning python. It's easy to give up if no one answers them. To this end, I have set up a python learning resource group, which has the latest learning materials. If you want to learn python, you can follow me and send me a private letter in the background to get the latest Python tutorial materials automatically! And the old driver answers!

Implementation mode

We don't do too complicated vehicle dynamic recognition here. We only demonstrate the recognition of license plate information from images. The realization of license plate recognition function is roughly divided into two ways: one is to write code by ourselves, and the other is to realize it with the help of third-party API interface.

Self realization

If we want to realize the license plate recognition function through Python's own manual coding, we can use some Python libraries, such as OpenCV, TensorFlow, etc. in this way, because each function point needs our own coding, all of which will be relatively complex. On the other hand, if we want to ensure the accuracy of recognition, we may need to do a lot of experiments, In other words, it will take more time.

Third party interface

At present, some third-party platforms have realized 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, and generally, the accuracy of the interface functions provided by the interface providers can be basically guaranteed. The reason is very simple, If the function of the interface is too poor, one is to make a face for ourselves, and the other is that no one will use it, which will lose the value provided by the interface. In addition, the third-party interface may charge a certain fee. Therefore, if we implement it in reality, we should consider it comprehensively.

Concrete implementation

Based on the above situation, we use the third-party interface to realize the function of license plate recognition. The interface provider we choose is the interface provided by Baidu cloud. Baidu cloud interface provides a free quota, which is simply how many times you can use it for free every day. If you exceed this number, you need to pay 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 supports many languages, such as python, Java, C + +, IOS, Android, etc. Here we install the python version of SDK. The installation is very simple. You can use the PIP install Baidu AIP command. The SDK supports Python versions: 2.7 + and 3.5 x. The SDK directory structure is as follows:

├── README.md├── aip                   // SDK directory │♪__ init__.py / / export class │♪ base Py / / AIP base class │♪ -- http Py / / HTTP request │ └ -- OCR py //OCR└── setup. Py / / setuptools installation

Create application

After the SDK is installed, we need to create an application. Here we need a baidu account or Baidu cloud account. If not, you can register one yourself. The login and registration address is: https://login.bce.baidu.com/?redirect=http%3A%2F%2Fcloud.baidu.com%2Fcampaign%2Fcampus-2018%2Findex.html, after login, we move the mouse to the login avatar position, and then click the user center in the pop-up menu, as shown in the following figure:

system

If it is the first time to enter, check the corresponding information, as shown in the figure below:

system

After checking the information, click the Save button.

Then move the mouse to the position of > symbol in the left column, and then select AI and character recognition in turn, as shown in the following figure:

method

Click to enter the following figure:

layout

Click create application to enter the following figure:

information

Here we only need to fill in the application name and the application description below. After filling in, click create now.

After creation, we will return to the application list, as shown in the following figure:

information

Here we need to use three values: AppID, API Key and Secret Key.

Concrete 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, which provides a series of interaction methods for developers using OCR, and the code implementation is relatively simple, as shown below:

from aip import AipOcr
# Own APPID AK SKAPP_ID = 'own app ID'_ Key = 'own Api Key'SECRET_KEY = 'own Secret Key'client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

In the above code, the constant APP_ID,API_KEY and SECRET_KEY is the constant value that we need to use when viewing the application list. These values are strings, which are used to identify the user and verify the signature for access.

If we need to configure the network request parameters of AipOcr, we can call the interface setup parameters after constructing AipOcr. At present, we support two parameters to see the code implementation:

# The timeout for establishing a connection, in milliseconds setConnectionTimeoutInMillis(5000)
# Timeout for data transmission over an open connection, in milliseconds client setSocketTimeoutInMillis(5000)

Generally speaking, it is relatively simple to realize the license plate recognition function through the interface, as shown in the following figure:

License plate

The implementation code is as follows:

from aip import AipOcrAPP_ID = 'own App ID'API_KEY = 'own Api Key'SECRET_KEY = 'own Secret Key'
# Create client object client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# The timeout for establishing a connection, in milliseconds setConnectionTimeoutInMillis(5000)
# Timeout for data transmission over an open connection, in milliseconds client setSocketTimeoutInMillis(5000)
# Read picture 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('license plate:' + res['words_result']['number'])
print('License plate color:' + res['words_result']['color'])

Execution result:

License plate No.: Sichuan QK9777 License plate color: 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 the licensePlate(image, options), as shown in the following figure:

License plate

The implementation code is as follows:

from aip import AipOcrAPP_ID = 'own App ID'API_KEY = 'own Api Key'SECRET_KEY = 'own Secret Key'
# Create client object client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
# The timeout for establishing a connection, in milliseconds setConnectionTimeoutInMillis(5000)
# Timeout for data transmission over an open connection, in milliseconds client setSocketTimeoutInMillis(5000)
# Read picture def get_file_content(filePath):   
 with open(filePath, 'rb') as fp:        
return fp.read()image = get_file_content('cars.png')options = {}
# Parameter multi_ Detect defaults to false options ['multi_detect '] ='true'res = client licensePlate(image, options)
for wr in res['words_result']:    
print('license plate:' + wr['number'])    
print('License plate color:' + wr['color'])

Execution result:

License plate No.: Beijing N6HZ61 License plate color: blue License plate No.: Lu NS1A26 License plate color: blue

summary

In this paper, we first introduce license plate recognition, and then use Baidu cloud interface to realize the recognition function of single and multiple license plates. Through this paper, we can have some understanding of the relevant concepts and specific implementation of license plate recognition. Note: Here's a reminder. Many people will encounter all kinds of troublesome problems in the process of learning python. It's easy to give up if no one answers them. To this end, I have set up a python learning resource group, which has the latest learning materials. If you want to learn python, you can follow me and send me a private letter in the background to get the latest Python tutorial materials automatically! And the old driver answers!

Keywords: Python Programmer AI crawler Computer Vision

Added by npereira on Fri, 11 Feb 2022 05:59:14 +0200