欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【百度人脸识别】》人脸检测接口调用

程序员文章站 2022-07-12 18:40:15
...

相关文档

https://cloud.baidu.com/doc/FACE/s/yk37c1u4t

接口详细相关参数请参考开发文档

代码:

import base64
import json

import requests
from flask import Flask, jsonify
from constants import API_Key, Secret_Key

app = Flask(__name__)


@app.route('/face_detection')
def detection():
    Access_Token = None
    # 获取调用接口所需的AccessToken
    host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={API_Key}&client_secret={Secret_Key}'
    response = requests.get(host)
    if response:
        # print(response.json())
        Access_Token = response.json().get('access_token')
        print(Access_Token)
    # 进行人脸检测调用接口
    with open("903.jpg", "rb") as f:
        # b64encode:编码,b64decode: 解码
        base64_903 = base64.b64encode(f.read())
    headers = {'content-type': 'application/json'}
    payload = json.dumps(
        {
            'image': str(base64_903, 'utf-8'),
            'image_type': 'BASE64',
         }
    )
    URL = 'https://aip.baidubce.com/rest/2.0/face/v3/detect' + "?access_token=" + Access_Token
    resp = requests.post(URL, data=payload, headers=headers)
    if resp:
        print(resp.json())
        return jsonify(resp.json())
    return jsonify('出错了!!!')
if __name__ == '__main__':
    app.run()

返回结果:

【百度人脸识别】》人脸检测接口调用

 

人脸对比接口调用代码相关链接:

https://blog.csdn.net/h18208975507/article/details/109000613

相关标签: python