利用百度AI平台识别12306验证码

简单的利用百度AI平台做一个12306的验证码识别。

利用百度AI平台识别12306验证码_第1张图片

 

 

第一步:先进行图片预处理 

提取文字部分


import numpy as np

#分割文字
def get_img_text(img,offset=0):
    return img[3:22,120+offset:177+offset]

#分割图片
def _get_imgs(img):
    interval = 5
    length = 66
    img_array = np.empty((8,length,length,3))
    i = 0
    for x in range(40, img.shape[0] - length, interval + length):
        for y in range(interval, img.shape[1] - length, interval + length):
                imgs = img[x:x+length,y:y+length]
                img_array[i,] = imgs
                i += 1
                

    return img_array

第二步,借用百度AI平台

说明文档地址:https://ai.baidu.com/docs#/ImageClassify-API/top

import base64
import cv2
import numpy as np
import requests
import sys
import urllib




#ocr
AK = '换成自己的'
SK = '换成自己的'
#img
AK_img = '换成自己的'
SK_img = '换成自己的'

#获取token
def get_token(ak, sk):

    # http://ai.baidu.com/docs#/Auth/top

    url = 'https://aip.baidubce.com/oauth/2.0/token'
    params = {

        'grant_type': 'client_credentials',

        'client_id': ak,

        'client_secret': sk,

    }
    r = requests.post(url, params=params)
    return r.json()['access_token']

TOKEN = get_token(AK, SK)
TOKEN_img = get_token(AK_img,SK_img)

def ocr(img):
    # 文件名
    if isinstance(img, str):
        img = open(img, 'rb').read()
    # 或cv2图像
    elif isinstance(img, np.ndarray):
        _, img = cv2.imencode('.jpg', img)
    # https://ai.baidu.com/docs#/OCR-API/e1bd77f3
    url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic'
    params = {'access_token': TOKEN}
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    img = base64.b64encode(img)
    data = {'image': img}
    r = requests.post(url, data=data, params=params, headers=headers)
    # 该项目只需要一个词
    return r.json()['words_result'][0]['words']

def img_classify(img):

    request_url = 'https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general'
    #以图像打开
    _, img = cv2.imencode('.jpg', img)
    img = base64.b64encode(img)
    params = {"image": img}
    params = urllib.parse.urlencode(params).encode(encoding='UTF8')
    request_url = request_url + '?access_token=' + TOKEN_img
    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()
    if content:
        result = content.decode('UTF8')
        result = eval(result)
        return result['result'][0]['keyword']



第三步 主函数

import cv2
import pretreatment
import baidu

#图片地址
img_path = 'xx'
length = 66

def main(img_path):
    img = cv2.imread(img_path)
    text = pretreatment.get_img_text(img)
    text = baidu.ocr(text)
    img_array = pretreatment._get_imgs(img)
    results = []
    for i in range(8):
        img = img_array[i, :, :, :]
        img = cv2.resize(img, (length, length))
        result = baidu.img_classify(img)
        results.append(result)
    return text,results

text,results = main(img_path)
print(text,results)

效果如下:

利用百度AI平台识别12306验证码_第2张图片 

 

你可能感兴趣的:(利用百度AI平台识别12306验证码)