今日总结-实现百度云api的调用

获取的json结果如下:

 

 

代码如下:

复制代码
# encoding:utf-8
import base64
import requests
def getToken():
    ak='B7E2OqVuDAyDs7OsuGPuKa4y'
    sk='idObOz6jqA2GdU49L2VG4VPVhgmiidvD'
    host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={ak}&client_secret={sk}'
    response = requests.get(host)
    return response.json().get("access_token")
def img_to_base64(file_path):
    with open(file_path,'rb') as f:
        base_64_data=base64.b64encode(f.read())
        s=base_64_data.decode()
        return s
def FaceDetect(token_,base_64_data):
    params={}
    request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
    params["image"]=base_64_data
    params["image_type"] = "BASE64"
    params["face_field"] = "age,beauty,landmark"
    access_token = token_
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/json'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        print(response.json()["result"]["face_list"])
        print(response.json()["result"]["face_list"][0]["age"])
        print(response.json()["result"]["face_list"][0]["beauty"])
        print(response.json()["result"]["face_list"][0]["landmark"])
if __name__=="__main__":
    base_64=img_to_base64("face2.jpg")
    token=getToken()
    FaceDetect(token,base_64)
复制代码

 

posted @   小彭先森  阅读(98)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示