百度ai 接口 人脸识别

api文档

http://ai.baidu.com/docs#/Face-Detect/top

能力介绍

接口能力

  • 人脸检测:检测图片中的人脸并标记出位置信息;
  • 人脸关键点:展示人脸的核心关键点信息,及72个关键点信息。
  • 人脸属性值:展示人脸属性信息,如年龄、性别等。
  • 人脸质量信息:返回人脸各部分的遮挡、光照、模糊、完整度、置信度等信息。

使用该api分析图片中任务的颜值和年龄

请求参数

参数 必选 类型 说明
image string base64编码后的图片数据,需urlencode,编码后的图片大小不超过2M
max_face_num uint32 最多处理人脸的数目,默认值为1,仅检测图片中面积最大的那个人脸
face_fields string 包括age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities信息,逗号分隔,默认只返回人脸框、概率和旋转角度

数据可以使用url或者base64格式

可以识别多个人脸

扫描二维码关注公众号,回复: 923359 查看本文章

我大仙女的颜值

{"error_code": 0, "error_msg": "SUCCESS", "log_id": 3065106228, "timestamp": 1526633826, "cached": 0,
 "result": {
     "face_num": 1,
     "face_list": [
         {"face_token": "65850302e75315946792b0ddb3553ef4",
          "location": {"left": 257.5062561, "top": 167.9508972, "width": 199,
                       "height": 184, "rotation": -4}, "face_probability": 1,
          "angle": {"yaw": -1.794678569, "pitch": 11.84180832, "roll": -4.842110157},
          "beauty": 81.2557373,
          "age": 22}]}}

对比包租婆的

{"error_code": 0, "error_msg": "SUCCESS", "log_id": 3077160341, "timestamp": 1526633947, "cached": 0,
 "result": {"face_num": 1,
            "face_list": [{"face_token": "17b342122e6e5d739ffbde29ff24ec6a",
                           "location": {"left": 163.8291473, "top": 96.57607269, "width": 115,
                                        "height": 115, "rotation": 5}, "face_probability": 0.9695456028,
                           "angle": {"yaw": -12.33260822, "pitch": -1.76208818, "roll": 3.611557245},
                           "beauty": 24.29637337, "age": 39}]}}

范冰冰

{"error_code": 0, "error_msg": "SUCCESS", "log_id": 3130713771, "timestamp": 1526634482, "cached": 0,
 "result": {"face_num": 1,
            "face_list": [{"face_token": "30b6a5174b362e4cc4bb5275f5620354",
                           "location": {"left": 143.0305786, "top": 215.0203247, "width": 246,
                                        "height": 229, "rotation": 6}, "face_probability": 1,
                           "angle": {"yaw": 8.355323792, "pitch": 0.03008880466, "roll": 8.090315819},
                           "beauty": 89.63048553, "age": 23}]}}

查看源图像

{"error_code": 0, "error_msg": "SUCCESS", "log_id": 3159305233, "timestamp": 1526634768, "cached": 0,
 "result": {"face_num": 1,
            "face_list": [{"face_token": "d518c7d0e041b4bf48e6d0d03e2b9e3e",
                           "location": {"left": 273.5806274, "top": 231.7171631, "width": 350,
                                        "height": 331, "rotation": 6}, "face_probability": 1,
                           "angle": {"yaw": -16.73430824, "pitch": 0.2803879082, "roll": 2.967574358},
                           "beauty": 88.6497345, "age": 23}]}}

不过这个算法的颜值计算还是很迷的。。。。不能轻信,感觉和角度啥的都有关系。。。

代码

import requests
import base64
import urllib3

# url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=24.f9ba9c5241b67688bb4adbed8bc91dec.2592000.1485570332.282335-8574074'
ak = 'ak'
sk = 'sk'
host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={ak}&client_secret={sk}'
ret = requests.get(host).json()
print(ret)
headers = {
    'Content-Type': 'application/json'
}

img_path = '刘亦菲.jpg'
with open(img_path, mode='rb') as f:
    img_base64 = bytes.decode(base64.b64encode(f.read()))


# 娜美
img_url = 'https://b-ssl.duitang.com/uploads/item/201410/14/20141014181857_UXTrH.jpeg'
data = {
    'image': img_url,
    'image_type': 'URL',
    'face_field': 'beauty,age'
}


data = {
    'image': img_base64,
    'image_type': 'BASE64',
    'face_field': 'beauty,age'
    # 'url': img_url
}
url = f"https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token={ret['access_token']}"
print(data)
print(url)

ans = requests.post(url, data=data, headers=headers).text

print(ans)

猜你喜欢

转载自my.oschina.net/ahaoboy/blog/1814951