个人调用OCR

一、自己训练模型

二、调用现成API

此处介绍百度智能云API,因为有免费次数。(原来一些网址在百度不是默认显示网址的,而是自己的网站名字)

个人调用OCR_第1张图片

首页找到OCR 

个人调用OCR_第2张图片

个人调用OCR_第3张图片

 每个人每月能用1K次。(有详细的API文档说明,不过跟着我的步骤来也足够)

个人调用OCR_第4张图片

 

 在创建应用后得到API key和 Secret Key个人调用OCR_第5张图片

 

 还需要拿到一个access token

#官方代码
import requests
import json

def main():
        
    url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=xxxxxx&client_secret=xxxxx"
    
    payload = ""
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)
    
    print(response.text)

if __name__ == '__main__':
    main()

# 更改为个人使用版本
def get_token(self):
    url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s' \
           '&client_secret=%s' % (self.apikey,self.apisecret)
    payload = ""
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
        }
    response = requests.request("POST", url, headers=headers, data=payload)
    if response:
        print(response.json())
        # 存储token
        try:
            self.redis1["baidutoken"]=response.json()["access_token"]
        except Exception as e:
            print("请求报错,无法获取token")

全代码

import requests
import base64
# 封装百度类
class Baidu:
    def __init__(self):
        # apikey 
        self.apikey = "12345"
        # api secretkey
        self.apisecret = "12345"
        self.redis = {} 
    # 文字图片识别
    def cor(self, filename=r"C:\Users\eqwimg\test.png"):
        # 定义请求地址
        request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
        # 读取图片
        f = open(filename, 'rb')
        # base64编码
        img = base64.b64encode(f.read())
        # 定义请求参数
        params = {"image": img}
        self.get_token()
        access_token = self.redis.get("baidutoken")
 
        request_url = request_url + "?access_token=" +access_token
        headers = {'content-type': 'application/x-www-form-urlencoded'}
        # 发起请求
        response = requests.post(request_url, data=params, headers=headers)
        if response:
            print(response.json())
            # 获取识别的结果
            num = ""
            for x in response.json()["words_result"]:
                num += x["words"]
            return num
    # 获取token
    def get_token(self):
        host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s' \
               '&client_secret=%s' % (self.apikey,self.apisecret)
        response = requests.get(host)
        payload = ""
        headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
            }
        response = requests.request("POST", url, headers=headers, data=payload)
        if response:
            print(response.json())
            # 存储token
            try:
                self.redis["baidutoken"]=response.json()["access_token"]
            except Exception as e:
                print("请求报错,无法获取token")
def cor():
    # 实例化对象
    baidu = Baidu()
    num = baidu.cor()
    times = 0
    success = False
    while times<3 and not success:#最大识别3次
        num = baidu.cor()
        if len(num) == 4:  #默认是4为识别码
            success = True
        else:
            break
        times += 1
    if success:
        return num
    else:
        return None
cor()

借鉴:

https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu

selenium--自动化识别图片验证码并输入_selenium识别验证码图片-CSDN博客 

你可能感兴趣的:(小练习,ocr,python)