百度智能云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()
相关推荐
LaughingZhu7 小时前
Product Hunt 每日热榜 | 2026-09-19
人工智能·深度学习·神经网络·搜索引擎·百度
LaughingZhu1 天前
Product Hunt 每日热榜 | 2026-09-20
人工智能·深度学习·神经网络·搜索引擎·百度
Ztt6666666662 天前
一抹霞红停在圆肩壶上,留白让颜色更醒目
经验分享·笔记·其他·百度·微信公众平台
Rocky Ding*4 天前
【三年面试五年模拟】2026-09-10 百度多模态大模型一面:9道技术问答与2道手撕题详解
论文阅读·人工智能·深度学习·机器学习·百度·aigc·ai-native
Ztt6666666665 天前
竹节密封罐披上紫釉,硬朗筋骨也有柔和一面
经验分享·笔记·其他·百度·微信公众平台
SXkehuirongsheng5 天前
APP定制开发怎么判断服务商的技术实力
百度·微信·app·网站建设·文心一言·微信公众平台
LaughingZhu5 天前
Product Hunt 每日热榜 | 2026-09-17
人工智能·深度学习·神经网络·搜索引擎·百度
LaughingZhu6 天前
Product Hunt 每日热榜 | 2026-09-15
人工智能·深度学习·神经网络·搜索引擎·百度
白杨SEO营销7 天前
白杨SEO:百家号APP改名,百度站长平台上线AI流量分析、百度搜索和文心接入智能体形式广告三个新变化
百度
回眸&啤酒鸭7 天前
【回眸】AI新鲜事——百度 AI 搜索实战应用场景与落地
人工智能·百度