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. ASP的Global.asa文件技巧用法2. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)3. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法4. ASP中常用的22個(gè)FSO文件操作函數(shù)整理5. SharePoint Server 2019新特性介紹6. 告別AJAX實(shí)現(xiàn)無(wú)刷新提交表單7. Vue+elementUI下拉框自定義顏色選擇器方式8. PHP函數(shù)原理理解詳談9. XML入門的常見問題(四)10. 使用css實(shí)現(xiàn)全兼容tooltip提示框
