最近做了個小專案,中間需要用到圖片文字識別,使用了百度的介面,這裡分享下程式碼相關程式碼。
注意:SECRET_KEY和 API_KEY 需要自己去免費申請,有相應的免費額度,具體可以參考官方文件:ai.baidu.com/ai-doc/OCR/dk3iqnq51
# -*- coding: utf-8 -*-
# @Author: bgods.cn
# @Create Date: 2020-07-22 17:08
# @File Name: api.py
# @Description: 圖片轉文字API
import base64
import requests
API_KEY = '*********' # 自行獲取
SECRET_KEY = '************' # 自行獲取
OCR_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic" # OCR介面
TOKEN_URL = 'https://aip.baidubce.com/oauth/2.0/token' # TOKEN獲取介面
def fetch_token():
# 獲取token
params = {'grant_type': 'client_credentials',
'client_id': API_KEY,
'client_secret': SECRET_KEY}
try:
f = requests.post(TOKEN_URL, params, timeout=5)
if f.status_code == 200:
result = f.json()
if 'access_token' in result.keys() and 'scope' in result.keys():
if not 'brain_all_scope' in result['scope'].split(' '):
return None, 'please ensure has check the ability'
return result['access_token'], ''
else:
return None, '請輸入正確的 API_KEY 和 SECRET_KEY'
else:
return None, '請求token失敗: code {}'.format(f.status_code)
except BaseException as err:
return None, '請求token失敗: {}'.format(err)
def read_file(image_path):
f = None
try:
f = open(image_path, 'rb') # 二進位制讀取圖片資訊
return f.read(), ''
except BaseException as e:
return None, '檔案({0})讀取失敗: {1}'.format(image_path, e)
finally:
if f:
f.close()
def pic2text(img_path):
def request_orc(img_base, token):
"""
呼叫百度OCR介面,圖片識別文字
:param img_base: 圖片的base64轉碼後的字元
:param token: fetch_token返回的token
:return: 返回一個識別後的文字字典
"""
try:
req = requests.post(
OCR_URL + "?access_token=" + token,
data={'image': img_base},
headers={'Content-Type': 'application/x-www-form-urlencoded'}
)
if req.status_code == 200:
result = req.json()
if 'words_result' in result.keys():
return req.json()["words_result"], ''
elif 'error_msg' in result.keys():
return None, '圖片識別失敗: {}'.format(req.json()["error_msg"])
else:
return None, '圖片識別失敗: code {}'.format(req.status_code)
except BaseException as err:
return None, '圖片識別失敗: {}'.format(err)
file_content, file_error = read_file(img_path)
if file_content:
token, token_err = fetch_token()
if token:
results, result_err = request_orc(base64.b64encode(file_content), token)
if result_err: # 列印失敗資訊
print(result_err)
for result in results: # 列印處理結果
print(result)
if __name__ == '__main__':
pic2text(img_path='圖片文字識別.jpg')
源圖片:
識別效果如下:
本作品採用《CC 協議》,轉載必須註明作者和本文連結