百度云----示例代码

 需要安装的模块:

pip install requests

pip install urllib 

import requests
import json
import base64
from urllib import parse
API_KEY = "0FTR66MExxxxfhxpk2e"
SECRET_KEY = "1mD3rYYxxxxxxxxLDtteonQR0IG"


def main():
    url = "https://aip.baidubce.com/rest/2.0/face/v3/match?access_token=" + get_access_token()
    # image 可以通过 get_file_content_as_base64("C:\fakepath\竺新景.png",False) 方法获取
    payload = json.dumps([
        {
            "image": get_file_content_as_base64('./111.png',False),
            "image_type": "BASE64"
        },
        {
            "image": get_file_content_as_base64('./222.png',False),
            "image_type": "BASE64"
        }
    ])
    headers = {
        'Content-Type': '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 = 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"))


if __name__ == '__main__':
    main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值