Python instance calling Baidu API to realize license plate recognition

1. About the author

Wang Shihao, male, School of Electronic Information, Xi'an Polytechnic University, 2020 graduate student, Zhang Hongwei Artificial Intelligence Research Group.
Research direction: machine vision and artificial intelligence.
Email: [email protected]

2. License plate recognition API introduction

Support the identification of blue plates, yellow plates (single and double lines), green plates, large new energy (yellow and green), consulate license plates, police plates, armed police plates (single and double lines), military plates (single and double lines), The regional number and license plate number of Hong Kong and Macao entry and exit license plates, agricultural license plates, and civil aviation license plates, and can identify multiple license plates in the image at the same time.

3. Implementation process

3.1 Call API

First open the Baidu Smart Cloud official website to register and log in.
insert image description here
After logging in, select the text recognition service
insert image description here
to create an application
insert image description here
, then enter the application name, description, and select the application type as personal, and then click the "Create Now" button.
insert image description here
After returning to the application list, you can see the license plate recognition application that has been created. API Key and Secret Key are displayed here, which will be used in the program later.
insert image description here

Note: The free version of Baidu Smart Cloud License Plate Recognition API provides 200 free calls per day, and subsequent calls will fail. It is recommended to keep the API Key and Secret Key properly.

After the application is created, open the interface document https://ai.baidu.com/ai-doc/OCR/ck3h7y191 to get the following useful information.
Request URL: https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate
Header format: Content-Type: application/x-www-form-urlencoded
insert image description here

3.2 Code

Before using the sample code, please remember to replace the sample image address and Key.

# -*- coding: utf-8 -*-
import urllib
import urllib.parse
import urllib.request
import base64
import json

# client_id 为官网获取的API Key, client_secret 为官网获取的Secret Key,将自己应用的Key分别复制在下方即可。
client_id = '*********'
client_secret = '********'

# 获取token
def get_token():
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
    request = urllib.request.Request(host)
    request.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = urllib.request.urlopen(request)
    token_content = response.read()
    if token_content:
        token_info = json.loads(token_content.decode("utf-8"))
        token_key = token_info['access_token']
    return token_key

# 读取图片
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()
        
# 获取车牌号信息
def get_license_plate(path):
    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate"

    f = get_file_content(path)
    access_token = get_token()
    img = base64.b64encode(f)
    params = {
    
    "custom_lib": False, "image": img}
    params = urllib.parse.urlencode(params).encode('utf-8')
    request_url = request_url + "?access_token=" + access_token
    request = urllib.request.Request(url=request_url, data=params)
    request.add_header('Content-Type', 'application/x-www-form-urlencoded')
    response = urllib.request.urlopen(request)
    content = response.read()
    # print("json类型下的返回值:", content)
    if content:
        license_plates = json.loads(content.decode("utf-8"))
        # print("utf-8解码并转化为python类型下的返回值:", license_plates)
        strover = '识别结果:'
        words_result = license_plates['words_result']
        # print("python类型下的返回值:", words_result)
        number = words_result['number']
        strover += '  车牌号:{} \n '.format(number)
        print(strover)
        return content
    else:
        return ''
# 读取图片,自行保存图片,更改路径即可。
image_path = 'D:\桌面\chepai.jpg'
# image_path = 'C:/Users/Wong/chepai.jpg'
# image_path = r'C:\Users\Wong\chepai.jpg'
get_license_plate(image_path)

Locate the folder where the license plate recognition program file is located under the PyCharm or Windows terminal, and run the program to recognize it.
insert image description here

refer to

Baidu Cloud API Documentation
https://cloud.baidu.com/doc/OCR/index.htmlBaidu
Intelligent Cloud License Plate Recognition
https://cloud.baidu.com/doc/OCR/s/ck3h7y191
Call Baidu's License Plate Recognition API
https://cloud.baidu.com/doc/OCR/s/ck3h7y191 ://blog.csdn.net/u011622208/article/details/102999991What
is Token
https://www.jianshu.com/p/24825a2683e6

Guess you like

Origin blog.csdn.net/m0_37758063/article/details/116571433