百度智能云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()
相关推荐
1296004521 天前
神经网络压缩
搜索引擎·百度
TGC达成共识3 天前
菌菇食用攻略:从营养解析到安全指南,解锁科学食菌
大数据·人工智能·其他·安全·百度·生活·新浪微博
百度安全5 天前
BCS 2025|百度副总裁陈洋:智能体在安全领域的应用实践
安全·百度
MILI元宇宙5 天前
纳米AI搜索与百度AI搜、豆包的核心差异解析
人工智能·百度
劲速云算力7 天前
科技创新驱动人工智能,计算中心建设加速产业腾飞
百度
产业家8 天前
2025年,百度智能云打响AI落地升维战
人工智能·百度
陈思杰系统思考Jason11 天前
系统思考:短期利益与长期系统影响
百度·微信·微信公众平台·新浪微博·微信开放平台
蜘蛛池.中国12 天前
百度蜘蛛池的作用是什么?技术@baidutopseo
百度
华院计算14 天前
华院计算出席信创论坛,分享AI教育创新实践并与燧原科技共同推出教育一体机
人工智能·科技·百度
广州正荣17 天前
百度量子蜘蛛3.0横空出世,搜索引擎迎来“量子跃迁“级革命
深度学习·百度·区块链