调用百度AipFace做人脸检测

需要注意的几个问题:

1.需要在百度人脸识别云平台注册账号并登陆:https://login.bce.baidu.com/?account=

然后创建人脸检测应用,则会生成相应的APP_ID API_KEY SECRET_KEY,开发时要用到

调用百度AipFace做人脸检测

# 定义常量
APP_ID = '*****'
API_KEY = '*****'
SECRET_KEY = '************'

 2.读图像数据主要用到的两种格式,示例如下

image = "取决于image_type参数,传入BASE64字符串或URL字符串或FACE_TOKEN字符串" 

# image = "http://n1.itc.cn/img8/wb/recom/2017/04/19/149256623627782055.JPEG"
# imageType = "URL"

filepath = "D:\\Project\\FaceReg\\test.jpg"
with open(filepath, "rb") as fp:
    base64_data = base64.b64encode(fp.read())
image = str(base64_data, 'utf-8')
imageType = "BASE64"

3.可以定义人脸检测的最大检测人脸数,默认为1,最大为10

# 定义参数变量

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

4.画人脸检测框,cv2.rectangle()只能画标准的矩形框,不能画倾斜的,故使用cvline()一条条画,但一定要找准4个顶点。经多次尝试,可以画出很好的检测框。但里面有个参数解释不通,还需要后续继续搞明白。

下面贴下所有源码:

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

# 定义常量
APP_ID = '*****'
API_KEY = '*******'
SECRET_KEY = '*******************************'

#初始化AipFace对象
aipFace = AipFace(APP_ID, API_KEY, SECRET_KEY)

#读取图片

# image = "http://n1.itc.cn/img8/wb/recom/2017/04/19/149256623627782055.JPEG"
# imageType = "URL"

filepath = "D:\\Project\\FaceReg\\test.jpg"
with open(filepath, "rb") as fp:
    base64_data = base64.b64encode(fp.read())
image = str(base64_data, 'utf-8')
imageType = "BASE64"

# 定义参数变量

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

# 调用人脸属性检测接口
result = aipFace.detect(image, imageType, options)

print(result)
print(type(result))

# 读取原图
# cap = cv2.VideoCapture(image)
# ret, img = cap.read()
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']
    # print(location)
    # print(location['face_list'][0])

    Theta = location['rotation'] / 60 ### 注意:为啥是60度,自己多次测试的结果,必须得弄清楚rotation啥意思,相对于哪里的旋转角度
    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)

    # left_top = (int(location['left']),int(location['top']))
    # right_bottom = (left_top[0]+int(location['width']),left_top[1]+int(location['height']))
    # cv2.rectangle(img,left_top, right_bottom, (0,0,255),2)

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

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

最后给出程序运行结果:

调用百度AipFace做人脸检测