百度智能云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()
相关推荐
奥顺互联V1 天前
百度SEO关键词布局从堆砌到场景化的转型指南
搜索引擎·百度
程序员的世界你不懂2 天前
Mysql配置文件My.cnf(my.ini)配置参数说明
数据库·mysql·百度·新浪微博
北京自在科技5 天前
苹果Siri升级遇阻,国行iPhone或将引入阿里、百度AI自救
百度·ios·ai·iphone·阿里
树莓集团6 天前
海南自贸港的数字先锋:树莓集团的战略布局解析
大数据·人工智能·物联网·百度·创业创新
树莓集团7 天前
树莓集团南京新项目:百度百科更新背后的战略意图
人工智能·科技·物联网·百度·创业创新
广拓科技9 天前
GPT-4.5震撼登场,AI世界再掀波澜!
人工智能·百度
一只积极向上的小咸鱼9 天前
deepseek、腾讯元宝deepseek R1、百度deepseekR1关系
百度
聚名网9 天前
百度权重有什么用?如何查看百度权重?
百度·百度权重
陈思杰系统思考Jason9 天前
心智模式与企业瓶颈突破
百度·微信·微信公众平台·新浪微博·微信开放平台
树莓集团11 天前
树莓集团百度百科新动态:南京又添一产业园项目
人工智能·科技·物联网·百度