欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > 百度智能云API调用

百度智能云API调用

2024/10/25 22:24:20 来源:https://blog.csdn.net/ylycrp/article/details/142443658  浏览:    关键词:百度智能云API调用

植物识别API 

import base64
import urllib
import requestsAPI_KEY = "你的图像识别API_KEY"
SECRET_KEY = "你的图像识别SECRET_KEY"def main():url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant?access_token=" + get_access_token()# image 可以通过 get_file_content_as_base64("C:\fakepath\苹果.png",True) 方法获取image=r"C:\Users\yly\Desktop\波罗蜜.png"base_image=get_file_content_as_base64(image)payload={'image':base_image}headers = {'Content-Type': 'application/x-www-form-urlencoded','Accept': 'application/json'}response = requests.request("POST", url, headers=headers, data=payload)print(response.text)def get_file_content_as_base64(path, urlencoded=False):"""获取文件base64编码:param path: 文件路径:param urlencoded: 是否对结果进行urlencoded :return: base64编码信息"""with open(path, "rb") as f:content = base64.b64encode(f.read()).decode("utf8")if urlencoded:content = urllib.parse.quote_plus(content)return contentdef get_access_token():"""使用 AK,SK 生成鉴权签名(Access Token):return: access_token,或是None(如果错误)"""url = "https://aip.baidubce.com/oauth/2.0/token"params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}return str(requests.post(url, params=params).json().get("access_token"))main()

 

人脸识别API 

import base64
import urllib
import requestsAPI_KEY = "你的人脸识别API_KEY"
SECRET_KEY = "你的人脸识别SECRET_KEY"def main():url = "https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=" + get_access_token()# image 可以通过 get_file_content_as_base64("C:\fakepath\微信截图_20240910210450.png",True) 方法获取headers = {'Content-Type': 'application/x-www-form-urlencoded','Accept': 'application/json'}image=r"C:\Users\yly\Desktop\liu.jpg"base_image=get_file_content_as_base64(image)payload = {"image":base_image,"image_type":"BASE64","face_field":'age,gender'}response = requests.request("POST", url, headers=headers, data=payload)print(response.text)def get_file_content_as_base64(path, urlencoded=False):"""获取文件base64编码:param path: 文件路径:param urlencoded: 是否对结果进行urlencoded :return: base64编码信息"""with open(path, "rb") as f:content = base64.b64encode(f.read()).decode("utf8")if urlencoded:content = urllib.parse.quote_plus(content)return contentdef get_access_token():"""使用 AK,SK 生成鉴权签名(Access Token):return: access_token,或是None(如果错误)"""url = "https://aip.baidubce.com/oauth/2.0/token"params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}return str(requests.post(url, params=params).json().get("access_token"))main()

 

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com