python如何實(shí)現(xiàn)調(diào)用百度識(shí)圖api?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)自2013年起,先為襄州等服務(wù)建站,襄州等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為襄州企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。代碼模板
import cv2 import base64 import requests import numpy as np import traceback from retrying import retry token_list=[ { "ak":"xxxxxx", "sk":"xxxxxxxxxx" }, ] def get_token(ak,sk): url = "/tupian/20230522/question.html 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_token def baidu_api(image,token): """ 百度通用文字識(shí)別 :return: """ # 通用文本識(shí)別接口 url = "/tupian/20230522/general_basic" # 網(wǎng)絡(luò)圖片識(shí)別接口 # url = "/tupian/20230522/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 words def 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