个人调用OCR

一、自己训练模型

二、调用现成API

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

首页找到OCR 

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

 

 在创建应用后得到API key和 Secret Key

 

 还需要拿到一个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博客 

相关推荐

  1. tesseract ocr 安装/调用/训练

    2024-01-06 07:48:02       19 阅读
  2. 百度OCR api调用代码

    2024-01-06 07:48:02       24 阅读
  3. js vue调用activex ocx

    2024-01-06 07:48:02       27 阅读
  4. chrome中使用AppEmit调用ocx使用文档,vue调用ocx

    2024-01-06 07:48:02       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-06 07:48:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-06 07:48:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-06 07:48:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-06 07:48:02       18 阅读

热门阅读

  1. 金蝶接口调用步骤

    2024-01-06 07:48:02       33 阅读
  2. 网页多文件合并下载成zip

    2024-01-06 07:48:02       41 阅读
  3. Micropython的包管理

    2024-01-06 07:48:02       44 阅读
  4. this的使用(js的问题)

    2024-01-06 07:48:02       40 阅读
  5. LeetCode每日一题 | 1944. 队列中可以看到的人数

    2024-01-06 07:48:02       41 阅读
  6. 1.2 C#基础

    2024-01-06 07:48:02       39 阅读
  7. PHP篇——html+php实现表单提交的一个简单例子

    2024-01-06 07:48:02       42 阅读
  8. Spring Boot 和 Spring Framework 的区别

    2024-01-06 07:48:02       48 阅读
  9. Spring Boot 生产就绪中文文档-下

    2024-01-06 07:48:02       30 阅读
  10. TensorFlow的详细介绍

    2024-01-06 07:48:02       33 阅读