百度ocr的简单封装

百度ocr地址

以下代码为对百度ocr的简单封装,实际使用时推荐使用baidu-aip

百度通用ocr

python 复制代码
import base64
from enum import Enum, unique
import requests
import logging as log

@unique
class OcrType(Enum):
    # 标准版
    STANDARD_BASIC = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
    # 标准版含位置
    STANDARD_WITH_LOCATION = "https://aip.baidubce.com/rest/2.0/ocr/v1/general"
    # 高精度版
    ACCURATE_BASIC = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
    # 高精度版含位置
    ACCURATE_WITH_LOCATION = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate"
    # 办公文档识别
    DOC_ANALYSiS_OFFICE = "https://aip.baidubce.com/rest/2.0/ocr/v1/doc_analysis_office"
    # 网络图片文字识别
    WEB_IMAGE = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage"
    # 网络图片文字识别 含位置
    WEB_IMAGE_WITH_LOCATION = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage_loc"
    # 手写文字识别
    HAND_WRITING = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting"
    # 数字识别
    NUMBERS = "https://aip.baidubce.com/rest/2.0/ocr/v1/numbers"
    # 表格文字识别(同步接口)
    FORM_SYNCH = "https://aip.baidubce.com/rest/2.0/ocr/v1/form"
    # 表格文字识别(异步接口)
    FORM_ASYNCH = "https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/request"
    # 二维码识别
    QRCORD = "https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode"


def ocr_help():
    print("==========百度ocr使用说明==========")
    print("本API基于ocr通用识别api编写,官方文档地址:https://cloud.baidu.com/doc/OCR/s/zk3h7xz52")
    print("使用示例:")
    print('''
    baiduOcr = BaiduGeneralOcr(OcrType.STANDARD_BASIC)
    baiduOcr.set_access_token("access_token")
    wordsList = baiduOcr.recoginze(image="D:\\txt1.png",options={})['words_result']
    for word in wordsList:
        print(word)
    ''')


class BaiduGeneralOcr():
    # ocr版本
    ocr_type = 0

    def __init__(self, ocr_type: OcrType):
        """
        ocr_type ocr识别类型 STANDARD_BASIC标准版 标准版含位置STANDARD_WITH_LOCATION 高精读版ACCURATE_BASIC 高精度版含位置ACCURATE_WITH_LOCATION
        @param ocr_type:
        """
        self.options = None
        self.pdf_file = None
        self.url = None
        self.image = None
        self.access_token = None
        self.ocr_type = ocr_type.value

    def gen_access_token(self, api_key, secret_key):
        """
        生成access_token
        @param api_key:
        @param secret_key:
        @return:
        """
        request_url = f'''https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={api_key}&client_secret={secret_key}'''
        response = requests.get(request_url)
        if response:
            print("access_token:%s" %(response.json()['access_token']))
            self.access_token = response.json()['access_token']

    def __check(self):
        """
        参数检查
        @return:
        """
        image = self.image
        url = self.url
        pdf_file = self.pdf_file

        if (image is None or image == "") and (url is None or url == "") and (pdf_file is None or pdf_file == ""):
            raise ValueError("image,url,pdf_file至少传入一项")
        if (image is not None) and (image != ""):
            f = open(image, 'rb')
            self.options['image'] = base64.b64encode(f.read()).decode()
        elif (image is not None or image == "") and (url is not None and url != ""):
            self.options['url'] = url
        else:
            f = open(pdf_file, 'rb')
            self.options['pdf_file'] = base64.b64encode(f.read()).decode()

        keys = self.options.keys()
        if ("image" not in keys) and ("url" not in keys) and ("pdf_file" not in keys):
            raise ValueError("image,url,pdf_file至少传入一项")
        try:
            self.ocr_type.value
        except Exception as e:
            log.info(repr(e))
            self.ocr_type = OcrType.STANDARD_BASIC
            log.info("ocr_type类型,已重置为标准版")

    def __request(self, request_url, data):
        # 设置header
        headers = {'content-type': 'application/x-www-form-urlencoded'}
        # 请求
        print(self.options)
        print(headers)
        return requests.post(request_url + "?access_token=%s" % self.access_token, data=self.options,
                             headers=headers).json()

    def recoginze(self, image: str = None, url: str = None, pdf_file: str = None, options=None):
        """
        识别
        @param image:
        @param url:
        @param pdf_file:
        @param options:
        @return:
        """
        self.image = image
        self.url = url
        self.pdf_file = pdf_file
        self.options = options or {}
        self.__check()
        # 发送请求
        return self.__request(self.ocr_type.value, options)

    def set_access_token(self, access_token):
        """
        设置access_token
        @param access_token:
        @return:
        """
        self.access_token = access_token

百度卡片识别ocr

python 复制代码
import requests
import base64
import json
from cwrpa.log.log import logging as log
from enum import Enum, unique
import keyring


@unique
class OcrType(Enum):
    # 身份证识别
    ID_CARD = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard"
    # 身份证混贴识别
    MULTI_IDCARD = "https://aip.baidubce.com/rest/2.0/ocr/v1/multi_idcard"
    # 身份证识别(金融加密版)
    IDCARD_ENC = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard_enc"
    # 银行卡识别
    BANKCARD = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard"
    # 营业执照识别
    BUSINESS_LICENSE = "https://aip.baidubce.com/rest/2.0/ocr/v1/business_license"
    # 名片识别
    BUSINESS_CARD = "https://aip.baidubce.com/rest/2.0/ocr/v1/business_card"
    # 护照识别
    PASSPORT = "https://aip.baidubce.com/rest/2.0/ocr/v1/passport"
    # 社保卡识别
    SOCIAL_SECURITY_CARD = "https://aip.baidubce.com/rest/2.0/ocr/v1/social_security_card"
    # 港澳通行证识别
    HK_MACAU_EXITENTRYPERMIT = "https://aip.baidubce.com/rest/2.0/ocr/v1/HK_Macau_exitentrypermit"
    # 台湾通行证识别
    TAIWAN_EXITENTRYPERMIT = " https://aip.baidubce.com/rest/2.0/ocr/v1/taiwan_exitentrypermit"
    # 户口本识别
    HOUSEHOLD_REGISTER = "https://aip.baidubce.com/rest/2.0/ocr/v1/household_register"
    # 出生医学识别证明
    BITTH_CERTIFICATE = "https://aip.baidubce.com/rest/2.0/ocr/v1/birth_certificate"
    # 多卡证类别检测
    MULTI_CARD_CLASSIFY = "https://aip.baidubce.com/rest/2.0/ocr/v1/multi_card_classify"


def ocr_help():
    print("==========百度ocr使用说明==========")
    print("本API基于ocr卡证识别api编写,官方文档地址:https://ai.baidu.com/ai-doc/OCR/rk3h7xzck")
    print("使用示例:")
    print('''
    baiduOcr = BaiduCardOcr(OcrType.ID_CARD)
    baiduOcr.set_access_token("access_token")
    wordsList = baiduOcr.recoginze(image="D:\\txt1.png",options={})['words_result']
    for word in wordsList:
        print(word)
    ''')


class BaiduCardOcr:
    # ocr版本
    ocr_type = 0

    def __init__(self, ocr_type: OcrType):
        """
        ocr_type ocr识别类型 STANDARD_BASIC标准版 标准版含位置STANDARD_WITH_LOCATION 高精读版ACCURATE_BASIC 高精度版含位置ACCURATE_WITH_LOCATION
        @param ocr_type:
        """
        self.options = None
        self.pdf_file = None
        self.url = None
        self.image = None
        self.access_token = None
        self.ocr_type = ocr_type.value

    def gen_access_token(self, api_key, secret_key):
        """
        生成access_token
        @param api_key:
        @param secret_key:
        @return:
        """
        request_url = f'''https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={api_key}&client_secret={secret_key}'''
        response = requests.get(request_url)
        if response:
            self.access_token = response.json()['access_token']

    def __check(self):
        """
        参数检查
        @return:
        """
        image = self.image
        url = self.url
        pdf_file = self.pdf_file

        if (image is None or image == "") and (url is None or url == "") and (pdf_file is None or pdf_file == ""):
            raise ValueError("image,url,pdf_file至少传入一项")
        if (image is not None) and (image != ""):
            f = open(image, 'rb')
            self.options['image'] = base64.b64encode(f.read()).decode()
        elif (image is not None or image == "") and (url is not None and url != ""):
            self.options['url'] = url
        else:
            f = open(pdf_file, 'rb')
            self.options['pdf_file'] = base64.b64encode(f.read()).decode()

        keys = self.options.keys()
        if ("image" not in keys) and ("url" not in keys) and ("pdf_file" not in keys):
            raise ValueError("image,url,pdf_file至少传入一项")
        try:
            self.ocr_type.value
        except Exception as e:
            log.info(repr(e))
            self.ocr_type = OcrType.STANDARD_BASIC
            log.info("ocr_type类型,已重置为标准版")

    def __request(self, request_url, data):
        # 设置header
        headers = {'content-type': 'application/x-www-form-urlencoded'}
        # 请求
        print(self.options)
        print(headers)
        return requests.post(request_url + "?access_token=%s" % self.access_token, data=self.options,
                             headers=headers).json()

    def recoginze(self, image: str = None, url: str = None, pdf_file: str = None, options=None):
        """
        识别
        @param image:
        @param url:
        @param pdf_file:
        @param options:
        @return:
        """
        self.image = image
        self.url = url
        self.pdf_file = pdf_file

        self.options = options or {}
        self.__check()
        # 发送请求
        return self.__request(self.ocr_type.value, options)

    def set_access_token(self, access_token):
        """
        设置access_token
        @param access_token:
        @return:
        """
        self.access_token = access_token
相关推荐
我的xiaodoujiao42 分钟前
API 接口自动化测试详细图文教程学习系列16--项目实战演练3
python·学习·测试工具·pytest
ID_180079054731 小时前
Python 实现亚马逊商品详情 API 数据准确性校验(极简可用 + JSON 参考)
java·python·json
时空系1 小时前
第10篇:继承扩展——面向对象编程进阶 python中文编程
开发语言·python·ai编程
CHANG_THE_WORLD2 小时前
python 批量终止进程exe
开发语言·python
liann1192 小时前
3.2_红队攻击框架--MITRE ATT&CK‌
python·网络协议·安全·网络安全·系统安全·信息与通信
云天AI实战派3 小时前
AI 智能体问题排查指南:ChatGPT、API 调用到 Agent 上线失灵的全流程修复手册
大数据·人工智能·python·chatgpt·aigc
我的xiaodoujiao3 小时前
API 接口自动化测试详细图文教程学习系列15--项目实战演练2
python·学习·测试工具·pytest
多思考少编码4 小时前
PAT甲级真题1001 - 1005题详细题解(C++)(个人题解)
c++·python·最短路·pat·算法竞赛
ZhengEnCi4 小时前
M5-markconv自定义CSS样式指南 📝
前端·css·python
ZhengEnCi5 小时前
M4-更新日志v0.1.3-Mermaid图表支持 📝
python