百度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
相关推荐
用户83562907805110 分钟前
从一维到二维:用Spire.XLS轻松将Python列表导出到Excel
后端·python
l木本I41 分钟前
uv 技术详解
人工智能·python·深度学习·机器学习·uv
宁大小白1 小时前
pythonstudy Day31
python·机器学习
江上鹤.1481 小时前
Day34模块和库的导入
python
我爱鸢尾花1 小时前
第十四章聚类方法理论及Python实现
大数据·python·机器学习·数据挖掘·数据分析·聚类
言之。2 小时前
Dropbear远程连接
python
dhdjjsjs3 小时前
Day34 PythonStudy
python
一个java开发3 小时前
Dask 配置文件加载机制说明
大数据·python
bj_zhb3 小时前
图片的base64表示
python·llm
飞Link3 小时前
【Django】Django 调用外部 Python 程序的完整指南
后端·python·django·sqlite