一个简单的基于百度api人脸检测工具

百度的人脸人脸检测API能够获得一幅图的属性信息,包括人脸关键点,框,年龄性别,颜值,清晰度,质量信息等,如下是一个简单的封装,能够直接获取人脸信息,有效期三十天,自己可以申请appId,app_key,有QPS=2的最大限制,不过简单的测试基本够用了,详细如下:

# -*- coding:utf-8 -*-
from aip import AipFace
import random
import base64
class baidui(object):
    def __init__(self):
        self.path = None
        self.APP_ID = None
        self.API_KEY = None
        self.SECRET_KEY = None
        self.filePath = None

    def getAPPID(self,ranNum):
        APP_ID = None
        API_KEY = None
        SECRET_KEY = None
        if ranNum ==0:
            APP_ID = '14353376'
            API_KEY ='ZwozYGhyUvMIQR4GcGXwdH2Y'
            SECRET_KEY = 'gX24HW4UrE8QcwH9ryCinn28F4QFGw4n'
        if ranNum==1:
            APP_ID = '14583156'
            API_KEY = '11y1QeeNaA1Lgh4SzaZ0h59G'
            SECRET_KEY = 'C47u4XKxy4eS3vjU98p7ujrByN6Iaevp'
        if ranNum==2:
            APP_ID = '14583166'
            API_KEY = 'uwN8ozLaLVwmQH7biGzonUAs'
            SECRET_KEY = 'cof4NFgtHtLTxj4RXppzWw0QLGM1IKeZ'
        if ranNum==3:
            APP_ID = '14583175'
            API_KEY = 'EUPOCeWQvUGEuCGojSTuwdBz'
            SECRET_KEY = 'SUvooBBBRvOyeOjsQ6EmUuf75malj9Rb'
        if ranNum==4:
            APP_ID = '14601956'
            API_KEY = 'tRC8SjSsW2X8bQBzP6dYVkhI'
            SECRET_KEY = 'R5T0A15hqRKHKvm6y6DUBhGifMidQlgI'
        if ranNum==5:
            APP_ID = '14602023'
            API_KEY = 'DTc1ZOVrPiuI5gZIZ6lsxmPl'
            SECRET_KEY = 'UUaS5LYfyS7K1yKDG5CTrvF3KevfiTRy'
        return APP_ID,API_KEY,SECRET_KEY
    def get_file_content(self,filePath):
        with open(filePath, 'rb') as fp:
            return fp.read()

    def get_img_attr(self,filePath):
        ranNum =random.randint(0,5)
        APP_ID, API_KEY, SECRET_KEY = self.getAPPID(ranNum)
        client = AipFace(APP_ID, API_KEY, SECRET_KEY)
        image = base64.b64encode(self.get_file_content(filePath))
        imageType = "BASE64"
        options = {}
        options["face_field"] = "quality"
        options["max_face_num"] = 3
        options["face_type"] = "LIVE"
        # options["image"] = image
        Infos = client.detect(image, imageType, options)
        return Infos

你可能感兴趣的:(人脸相关)