车牌识别 百度api python demo

 get_token.py:

import urllib, urllib2, sys
import ssl

host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=APIkey&client_secret=secretkey'
request = urllib2.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib2.urlopen(request)
content = response.read()
if (content):
    print(content)

plateDetect.py:

# encoding:utf-8
import cv2
import urllib
import urllib2
import urllib2, base64
import time
try:
    from urllib import urlencode
except ImportError:
    from urllib.parse import urlencode
import shutil
import os
import os.path


rootDir = "/home/gaoxiaochuan/plate/general_test/night"#车牌数据集文件夹路径

request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate"

i = 0
j = 0

for lists in os.listdir(rootDir):
    i = i + 1
    time.sleep(0.2)
    path = os.path.join(rootDir, lists)
    print path
    f = open(path,'rb')
    img = base64.b64encode(f.read())
    params={'image':img,'image_type': 'BASE64'}
    params=urlencode(params)
    access_token='xxxx'  #通过get_token.py获取的token值
    request_url = request_url + "?access_token=" + access_token
    request = urllib2.Request(url=request_url, data=params)
    request.add_header('Content-Type', 'application/json')
    response = urllib2.urlopen(request)
    content = response.read()
    if content:
        print (content)
        success = content.find("words_result")
        if (-1 != success):
            j = j + 1
            #print("hahahh")
    print('\r\n')
    print("i = ", i)
    print("j = ", j)

 

你可能感兴趣的:(百度API)