利用百度AI的人脸检测接口python sdk来识进行图片的人脸检测
laoyan 2018-01-04 14:46:02
分 享


使用简介

百度AI平台开放了很多接口,其中今天要介绍的是人脸检测接口,这里主要讲解使用python的sdk来进行调取接口进行图片中人脸的检测,默认百度人脸检测开放2 QPS限制,更多需要付费。

python sdk安装

pip install baidu-aip


调用实例

# -*- coding: utf-8 -*-
from aip import AipFace
import cv2
APP_ID = '1062xxxxxx'
API_KEY = 'Z3Vjb20pLdgsxxxxxxxxx'
SECRET_KEY = 'QCQmfjkBjfRevABSxxxxxxxx'
client = AipFace(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()
image = get_file_content('faceapitest.png')
""" 调用人脸检测 """
result=client.detect(image);
# 解析位置信息
location=result['result'][0]['location']
left_top=(location['left'],location['top'])
right_bottom=(left_top[0]+location['width'],left_top[1]+location['height'])
myimage='faceapitest.png'
img=cv2.imread(myimage)
cv2.rectangle(img,left_top,right_bottom,(0,0,255),2)
cv2.imshow('Image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()


以上demo把  faceapitest.png图片方在一个目录中,执行demo文件   python  baidufaceapi.py 出现结果如下:


利用百度ai的人脸检测接口python sdk来识进行人脸检测.png



注意:本实例使用了python模块三方模块,需要事先安装,安装方法: pip install  opencv-python


人脸检测返回json示例

{
    "result_num": 1,
    "result": [
        {
            "location": {
                "left": 117,
                "top": 131,
                "width": 172,
                "height": 170
            },
            "face_probability": 1,
            "rotation_angle": 2,
            "yaw": -0.34859421849251,
            "pitch": 2.3033397197723,
            "roll": 1.9135693311691,
            "landmark": [
                {
                    "x": 161.74819946289,
                    "y": 163.30244445801
                },
                ...
            ],
            "landmark72": [
                {
                    "x": 115.86531066895,
                    "y": 170.0546875
                },
                ...
            ],
            "age": 29.298097610474,
            "beauty": 55.128883361816,
            "expression": 1,
            "expression_probablity": 0.5543018579483,
            "gender": "male",
            "gender_probability": 0.99979132413864,
            "glasses": 0,
            "glasses_probability": 0.99999964237213,
            "race": "yellow",
            "race_probability": 0.99999976158142,
            "qualities": {
                "occlusion": {
                    "left_eye": 0,
                    "right_eye": 0,
                    "nose": 0,
                    "mouth": 0,
                    "left_cheek": 0.0064102564938366,
                    "right_cheek": 0.0057411273010075,
                    "chin": 0
                },
                "blur": 1.1886881756684e-10,
                "illumination": 141,
                "completeness": 1,
                "type": {
                    "human": 0.99935841560364,
                    "cartoon": 0.00064159056637436
                }
            }
        }
    ],
    "log_id": 2493878179101621
}


参考地址

https://cloud.baidu.com/doc/FACE/Face-Python-SDK.html#.E4.BA.BA.E8.84.B8.E6.AF.94.E5.AF.B9


我要小额支持下

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

取消
吐槽一下

游客

所有吐槽
  • 暂时还没有吐槽,赶紧来吐槽!