百度智能云API调用

植物识别API

复制代码
import base64
import urllib
import requests

API_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 content

def 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 requests

API_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 content

def 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()
相关推荐
陈思杰系统思考Jason2 小时前
亲子烘焙公益时光
百度·微信·微信公众平台·新浪微博·微信开放平台
交通上的硅基思维21 小时前
人工智能安全:风险、机制与治理框架研究
人工智能·安全·百度
人工智能AI技术1 天前
手搓一个AI搜索引擎:基于百度DeepSearch框架的实战开发笔记
人工智能·百度
陈思杰系统思考Jason2 天前
系统思考与敏捷的区别
百度·微信·微信公众平台·新浪微博·微信开放平台
zhangfeng11333 天前
千帆平台目前不支持用户自助上传自定义词表(vocabulary)进行训练 Post-pretrain 阶段 SFT都不支持
人工智能·百度
wei_shuo3 天前
「源力觉醒 创作者计划」_百度开源文心 4.5 系列开源大模型:ERNIE-4.5-VL-28B-A3B-Paddle vs DeepSeek-R1 多维度测评分析
百度·开源·文心大模型·gitcode
数据猿3 天前
我参与了百度文心App AI群聊内测,提前感受到了社交AI的革命
人工智能·百度
大力财经3 天前
性能超越DeepSeek-OCR2,百度发布并开源新一代SOTA OCR模型
百度
小朱笼包3 天前
小程序实现对接百度AI大模型,通过websocket连接进行百度实时语音识别,将返回的文字调用AI大模型API获得返回的消息内容进行文字转语音朗诵并操作
人工智能·websocket·百度·小程序·语音识别
陈思杰系统思考Jason3 天前
系统思考:以客户为中心
百度·微信·微信公众平台·新浪微博·微信开放平台