百度智能云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()
相关推荐
我的offer在哪里1 小时前
百度新模型全景|2026 博客深度:文心 5.0+4.5+DuMate,从原生全模态到企业级落地
百度
fidgidf1 天前
新网站一直不被百度和谷歌收录怎么办?从内容到结构的完整优化思路
百度·seo优化·百度seo
fidgidf1 天前
页面被百度抓取但未收录是什么原因?网站收录异常情况分析
百度
陈思杰系统思考Jason1 天前
系统思考:结构性缺陷与企业波动
百度·微信·微信公众平台·新浪微博·微信开放平台
李子琪。2 天前
攀山的人
经验分享·笔记·百度·新浪微博
陈思杰系统思考Jason2 天前
动态博弈中的系统思考
百度·微信·微信公众平台·新浪微博·微信开放平台
羊群智妍2 天前
2026年免费GEO监测工具指南:SheepGeo解锁国内AI搜索优化
百度·微信·微信公众平台·facebook·新浪微博
陈思杰系统思考Jason4 天前
战略选择:挖坑与打井
百度·微信·微信公众平台·新浪微博·微信开放平台
百度安全4 天前
百度“龙虾”全家桶开张 安全虾正式上岗!
安全·百度
陈思杰系统思考Jason5 天前
系统思考:组织转型与碳元素启示
百度·微信·微信公众平台·新浪微博·微信开放平台