Python實現圖片轉為文字------利用百度AI 文字識別
阿新 • • 發佈:2019-01-27
準備工作
1.安裝aip:pip install baidu-aip
參考文件
http://ai.baidu.com/docs#/OCR-Python-SDK/top
程式碼
from aip import AipOcr print('---歡迎使用圖片轉文字的高階應用---') # 定義常量 APP_ID = '11526380' API_KEY = 'x0m94pNN7TGb7rfBwTXHWbNl' SECRET_KEY = 'wVyXcZmE9ZC25qYkWVWkSv4iZiNauw4G' # 初始化文字識別分類器 client = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 讀取圖片 def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() path=input('請輸入要轉為文字的圖片路徑:') image = get_file_content(path) # 呼叫通用文字識別, 圖片引數為本地圖片 client.basicGeneral(image) # 如果有可選引數,定義引數變數 options = {} options["language_type"] = "CHN_ENG" options["detect_direction"] = "true" options["detect_language"] = "true" options["probability"] = "true" # 帶引數呼叫通用文字識別, 圖片引數為本地圖片 result = client.basicGeneral(image, options) # 如果圖片是url 呼叫示例如下 # result = apiOcr.webImage('http://www.xxxxxx.com/img.jpg') print('****以下為圖片中的文字內容****\n') for lis in result['words_result']: print(lis['words']) input('\n識別完畢,操作完畢後按回車鍵退出-----')
結果展示