Python + 百度人脸api 简单的人脸检测

最近对人脸识别检测等模块很感兴趣,网上大概搜索查阅了一下相关的知识,用python简单的做了一个测试。

准备工具:

百度人脸识别api 的各种key,python

代码:

from aip import AipFace
import base64
import cv2
import math
import matplotlib.pyplot as plt


APP_ID = '*****'     # 百度人脸的appID,下面的参数同
API_KEY = '*****'
SECRET_KEY = '*****'

aipFace = AipFace(APP_ID, API_KEY, SECRET_KEY)

filePath = r'*****' # 待检测图片的文件地址

with open(filePath, 'rb') as fp:
    content = base64.b64encode(fp.read())
image = str(content, 'utf-8')

imageType = "BASE64"

options = {}
options["face_field"] = "age"
options["max_face_num"] = 1
options["face_type"] = "LIVE"

result = aipFace.detect(image, imageType, options)
print(result)


img = cv2.imread(filePath)
face_num = result['result']['face_num']

for num in range(0, int(face_num)):
    print(num)
    location = result['result']['face_list'][num - 1]['location']

    Theta = location['rotation'] / 60  
    A = (int(location['left']), int(location['top']))
    B = (int(location['left']) + int(location['width'] * math.cos(Theta)),
         int(location['top']) + int(location['width'] * math.sin(Theta)))
    AC_Len = math.sqrt(location['width'] ** 2 + location['height'] ** 2)
    AC_Theta = math.atan(location['height'] / location['width']) + location['rotation'] / 60  
    C = (int(location['left']) + int(AC_Len * math.cos(AC_Theta)), int(location['top']) + int(AC_Len * math.sin(AC_Theta)))
    D = (int(location['left']) - int(location['height'] * math.sin(Theta)),
         int(location['top']) + int(location['height'] * math.cos(Theta)))
    cv2.line(img, A, B, (0, 0, 255), 2)
    cv2.line(img, B, C, (0, 0, 255), 2)
    cv2.line(img, C, D, (0, 0, 255), 2)
    cv2.line(img, D, A, (0, 0, 255), 2)


cv2.imshow('img', img)
cv2.waitKey(0)

plt.imshow(img, 'gray')
plt.show()


运行结果:

{
    'error_code': 0,
    'error_msg': 'SUCCESS',   运行成功
    'log_id': 9475001159405,
    'timestamp': 1572925248,
    'cached': 0,
    'result': {
        'face_num': 1,     检测面部数量1
        'face_list': [{
            'face_token': '1ca1b913670e48a5dab9a53c4ee5b2d7',
            'location': {
                'left': 290.12,
                'top': 744.99,
                'width': 194,
                'height': 187,
                'rotation': 13
            },
            'face_probability': 1,
            'angle': {
                'yaw': 27.36,
                'pitch': 17.91,
                'roll': 5.31
            },
            'age': 22
        }]
    }
}

这些参数很直白,大家大概看一下就行。毕竟只是最简单的实验没有什么深度数据。

原图:

標題

代码运行之后:

 大概就是这样,学习的道路道阻且长,我们一起努力。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
识别结果大概是这样 {'result': {'face_num': 1, 'face_list': [{'quality': {'occlusion': {'right_eye': 0, 'left_cheek': 0.1459853947, 'right_cheek': 0.05144193396, 'left_eye': 0.465408802, 'mouth': 0.02919708006, 'chin_contour': 0.01420217194, 'nose': 0}, 'illumination': 116, 'blur': 7.266304692e-06, 'completeness': 1}, 'age': 22, 'face_token': 'dc6f8f9df5d977ea476e2d04acdf5063', 'race': {'type': 'white', 'probability': 0.6173604727}, 'glasses': {'type': 'common', 'probability': 0.9834988713}, 'gender': {'type': 'male', 'probability': 0.655915916}, 'face_probability': 0.9185044169, 'beauty': 51.21487427, 'angle': {'roll': -2.750922441, 'yaw': 28.97134399, 'pitch': 5.202290535}, 'location': {'height': 65, 'top': 112.0704803, 'width': 76, 'left': 76.20765686, 'rotation': -4}, 'face_type': {'type': 'human', 'probability': 0.9992217422}, 'face_shape': {'type': 'oval', 'probability': 0.4419156313}, 'expression': {'type': 'none', 'probability': 0.9999142885}}]}, 'error_msg': 'SUCCESS', 'timestamp': 1537413754, 'cached': 0, 'error_code': 0, 'log_id': 9465840013520} 年龄:22 颜值:51.21487427 表情-type(none:不笑;smile:微笑;laugh:大笑):none 表情-probability(表情置信度,范围【0~1】,0最小、1最大):0.9999142885 脸型-type(square: 正方形 triangle:三角形 oval: 椭圆 heart: 心形 round: 圆形):oval 脸型-probability(置信度,范围【0~1】,代表这是人脸形状判断正确的概率,0最小、1最大):0.4419156313 性别-type(male:男性 female:女性):male 性别-probability(性别置信度,范围【0~1】,0代表概率最小、1代表最大。):0.655915916 是否带眼镜-type(none:无眼镜,common:普通眼镜,sun:墨镜):common 是否带眼镜-probability(眼镜置信度,范围【0~1】,0代表概率最小、1代表最大。):0.9834988713 人种-type(yellow: 黄种人 white: 白种人 black:黑种人 arabs: 阿拉伯人):white 人种-probability(人种置信度,范围【0~1】,0代表概率最小、1代表最大。):0.6173604727 真实人脸/卡通人脸 -type(human: 真实人脸 cartoon: 卡通人脸):human 真实人脸/卡通人脸 -probability(人脸类型判断正确的置信度,范围【0~1】,0代表概率最小、1代表最大。):0.9992217422
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值