Python 利用百度文字识别 API 识别并提取图片中文字

浏览885

利用百度 AI 开发平台的 OCR 文字识别 API 识别并提取图片中的文字。首先需注册获取 API 调用的 ID 和 key,步骤如下:

打开百度AI开放平台,进入控制台中的文字识别应用(需要有百度账号)。 

创建一个应用,并进入管理应用,记下 AppID, API Key, Secrect Key,调用 API需用到。

最后安装 python 的百度ai接口的的库 
pip install baidu-aip 

	        
from aip import AipOcr

class Ocr(object):
    def __init__(self):
        pass

    def test(self,path):    #D:\#pythonCode\ocr\ocr.png
        """ 你的 APPID AK SK """
        APP_ID = 'xxx'
        API_KEY = 'xxx'
        SECRECT_KEY = 'xxx'
        client = AipOcr(APP_ID, API_KEY, SECRECT_KEY)

        i = open(path, 'rb')
        img = i.read()
        message = client.basicGeneral(img)  # 通用文字识别,每天 50 000 次免费
        # message = client.basicAccurate(img)   # 通用文字高精度识别,每天 800 次免费
        i.close()
        # 输出文本内容
        word = ''
        for text in message.get('words_result'):
            word = text.get('words')
            print("识别文字:",text.get('words'))
        return word

if __name__ == '__main__':
    Ocr().test()			
	      

    


  • 暂无任何回答