百度智能云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()
相关推荐
摘星编程11 天前
中国MCP市场:腾讯、阿里、百度的本土化实践
百度·阿里巴巴·腾讯·中国mcp市场·本土化实践
市象12 天前
百度智能云给“数字人”发工牌
百度
白杨SEO营销14 天前
白杨SEO:百度搜索开放平台发布AI计划是什么?MCP网站红利来了?顺带说说其它
人工智能·百度
倔强的石头10614 天前
[源力觉醒 创作者计划]_文心4.5开源测评:国产大模型的技术突破与多维度能力解析
百度·开源·文心一言·文心大模型
数据知道14 天前
百度翻译详解:包括PaddleNLP、百度AI开放平台、接口逆向(包括完整代码)
人工智能·百度·语言模型·自然语言处理·机器翻译
下页、再停留17 天前
【PHP】接入百度AI开放平台人脸识别API,实现人脸对比
人工智能·百度·php
京东零售技术17 天前
京东零售在智能供应链领域的前沿探索与技术实践
人工智能·百度·零售
高hongyuan21 天前
搜索引擎高级搜索指令大全(Google、百度等浏览器通用)
搜索引擎·百度·google
趣味科技v22 天前
WAIC直击:百度慧播星发布新一代数字人技术NOVA
百度