图片文字识别aip的一个小Demo

本文详细介绍如何使用百度AI平台的文字识别服务进行图片文字提取,包括创建应用获取密钥、安装SDK、调用API实现文字识别及结果展示,适合初学者快速上手。

目前接触到了一个新的内容,识别图片上的文字,以下是这个Demo

  • 首先需要在需要在百度云-管理中心创建应用
    • 地址:http://console.bce.baidu.com/ai/#/ai/ocr/app/list,如果没有账号主要注册百度账号
    • 点击创建应用同时保存记录AppID,API Key,Secret Key
  • 安装aip的安装包:pip install baidu-aip

 

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author:jiyanjiao

from aip import AipOcr
import cv2

APP_ID = '创建应用后生成的id'
API_KEY = '创建应用后生成的key'
SECRET_KEY = '创建应用后生成的secretkey'

fname = 'picture/T5.jpg'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)


""" 读取图片 """


def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


image = get_file_content(fname)


""" 调用通用文字识别, 图片参数为本地图片 """
words_results = client.general(image)
results = words_results["words_result"]

img = cv2.imread(fname)
for result in results:
    text = result["words"]
    location = result["location"]

    print("截取出的文本为:",text)   # 画矩形框
    cv2.rectangle(img, (location["left"],location["top"]), (location["left"]+location["width"],location["top"]+location["height"]), (0,255,0), 2)

cv2.imwrite(fname[:-4]+"_result.jpg", img)
  • 接下来我们来说一些这些方法的返回值
    • aip的官方文档:https://cloud.baidu.com/doc/OCR/OCR-Python-SDK.html#.E7.AE.80.E4.BB.8B
    • client.general(image)会返回一个字典,包含了唯一标识log_id,words_result_num就是这个words_result的数量
    •    words_results["words_result"] 提取 words_result这个列表,然后逐一遍历,去取每个字符串,即完成了图片上文字的提取

 

转载于:https://www.cnblogs.com/jiyanjiao-702521/p/9984242.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值