天天看點

調用百度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做人臉檢測

繼續閱讀