新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
python如何实现调用百度识图api?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
创新互联自2013年起,先为襄州等服务建站,襄州等地企业,进行企业商务咨询服务。为襄州企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。代码模板
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): """ 百度通用文字识别 :return: """ # 通用文本识别接口 url = "/tupian/20230522/general_basic" # 网络图片识别接口 # url = "/tupian/20230522/webimage" params = { "access_token": token, } data = { "image": base64.b64encode(image) #图标的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