python如何調(diào)用百度識(shí)圖api
一.先去百度識(shí)別官網(wǎng)注冊(cè)開通服務(wù)且獲得ak和sk
鏈接:https://cloud.baidu.com/doc/Reference/s/9jwvz2egb
二.代碼模板
import cv2import base64import requestsimport numpy as npimport tracebackfrom retrying import retrytoken_list=[ { 'ak':'xxxxxx', 'sk':'xxxxxxxxxx' },]def get_token(ak,sk): url = 'https://aip.baidubce.com/oauth/2.0/token' params = { 'grant_type': 'client_credentials', 'client_id': ak, # AK 'client_secret': sk # SK } eaders={ 'Content-Type':'application/json; charset=UTF-8', } response = requests.get(url,params=params,headers=headers,timeout=8) res = response.json() access_token = res['access_token'] return access_tokendef baidu_api(image,token): ''' 百度通用文字識(shí)別 :return: ''' # 通用文本識(shí)別接口 url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic' # 網(wǎng)絡(luò)圖片識(shí)別接口 # url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/webimage' params = { 'access_token': token, } data = { 'image': base64.b64encode(image) #圖標(biāo)的bs64編碼 } response = requests.post(url, params=params, data=data) data_res = response.json() print(data_res) words = [i['words'] for i in data_res['words_result']] return wordsdef baidu_image_recognition(img_content): img2=img_content for i in range(len(token_list)): token = get_token(token_list[i]['ak'], token_list[i]['sk']) words = baidu_api(img2,token) return words
以上就是python如何調(diào)用百度識(shí)圖api的詳細(xì)內(nèi)容,更多關(guān)于python調(diào)用api的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼2. PHP橋接模式Bridge Pattern的優(yōu)點(diǎn)與實(shí)現(xiàn)過程3. asp.net core項(xiàng)目授權(quán)流程詳解4. html中的form不提交(排除)某些input 原創(chuàng)5. CSS3中Transition屬性詳解以及示例分享6. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼7. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式8. 開發(fā)效率翻倍的Web API使用技巧9. jsp文件下載功能實(shí)現(xiàn)代碼10. ASP常用日期格式化函數(shù) FormatDate()
