python3百度文字ocr识别

"""
    读取文件
"""


def read_file(image_path):
    f = None
    try:
        f = open(image_path, 'rb')
        return f.read()
    except:
        print('read image file fail')
        return None
    finally:
        if f:
            f.close()


# 2. 接入百度智云文字识别服务
#如果已安装pip,执行pip install baidu-aip即可。
# 从aip中导入AipOcr
from aip import AipOcr

# 替换你的APP_ID,API_KEY,SECRET_KEY
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
# 新建一个AipOcr,并赋值给变量client
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

# 调用通用文字识别
# 如果有可选参数
# 创建字典options,并将可选参数detect_direction的值设置为"true"
options = {"detect_direction": "true"}
# 调用通用文字识别接口并把结果赋值给result
image = read_file("D:/image/verifyCode.png")
result = client.basicAccurate(image, options)
# 输出result
# print(result)
ending = result["words_result"]
print(ending)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用百度云提供的文字识别 API 来实现Python中的文字识别。首先,您需要在百度云平台上创建一个账号,并获取到文字识别的 API Key 和 Secret Key。 接下来,您可以使用Python中的requests库向百度云 API 发送请求。以下是一个简单的示例代码: ```python import requests # 设置API Key和Secret Key api_key = 'your_api_key' secret_key = 'your_secret_key' # 上传图片并调用文字识别API def baidu_ocr(image_path): url = 'https://aip.baidubce.com/oauth/2.0/token' data = { 'grant_type': 'client_credentials', 'client_id': api_key, 'client_secret': secret_key } response = requests.post(url, data=data) access_token = response.json().get('access_token') ocr_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic' headers = {'Content-Type': 'application/x-www-form-urlencoded'} params = {'access_token': access_token} image = open(image_path, 'rb').read() response = requests.post(ocr_url, headers=headers, params=params, data=image) result = response.json() if 'error_code' in result: print('文字识别出错:', result['error_msg']) else: for word in result['words_result']: print(word['words']) # 使用示例 image_path = 'path/to/your/image.jpg' baidu_ocr(image_path) ``` 请确保将上述代码中的`your_api_key`和`your_secret_key`替换为您在百度云平台上创建的 API Key 和 Secret Key。同时,将`image_path`替换为您要识别的图片的路径。 这样,您就可以使用Python调用百度云的文字识别 API 来实现图片中文字识别了。希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值