百度智能云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()
相关推荐
陈思杰系统思考Jason12 小时前
系统思考:飞轮效应与系统结构
百度·微信·微信公众平台·新浪微博·微信开放平台
智能相对论15 小时前
CES深度观察|AI硬件消费时代的“幕后英雄”走向台前
人工智能·百度
陈思杰系统思考Jason1 天前
系统思考:复盘第一性原理
百度·微信·微信公众平台·新浪微博·微信开放平台
BitaHub20242 天前
文献分享 | 百度提出AI搜索新范式:以多智能体协作重构复杂信息检索流程
人工智能·百度·ai搜索
极新3 天前
智面玄赏联合创始人李男:人工智能赋能招聘行业——从效率革新到平台经济重构|2025极新AIGC峰会演讲实录
人工智能·百度
IT学习资源每日免费分享3 天前
Python精选视频教程资料大全
百度·微信·课程设计·oneapi·微信开放平台
陈思杰系统思考Jason4 天前
战略共识与系统思考
百度·微信·微信公众平台·新浪微博·微信开放平台
LINGYI0004 天前
百度百科:什么是新消费品牌电商代运营公司?
百度·产品运营
芝士大爆发5 天前
初级会计信息照片采集全流程(附上传失败解决方法)
经验分享·百度·微信·微信公众平台·新浪微博
周杰伦fans6 天前
百度脑图怎么了?将彻底关停!!
百度