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

目前接触到了一个新的内容,识别图片上的文字,以下是这个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