先进入这个界面:https://console.bce.baidu.com/ai/#/ai/face/overview/index
创建好应用后 点击图片右下角的免费获取资源(把能获取的都获取来)
点击管理应用 然后查看左边的技术文档
在API文档中找到想要的技术
根据提示 获得需要的access_token 这里的AK和SK在上面的第二张图中 从resopnse.json()中选择["access_token"]并返回
def getAccess(AK="CZ1HCPYGjXvlRVEGKR5XCIWT",SK="xT1CfbqzUa9xFAittlnS8ibc4tL4nTFn"):
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'.format(AK,SK)
response = requests.get(host)
if response:
print(response.json()["access_token"])
return (response.json()["access_token"])
将access_token替换 并根据所需要的内容 替换params中的参数
代码:AIFaceDetect.py
'''
人脸检测与属性分析
'''
def AI_facedetect(Path="./face_img/face1.jpg"):
# Path="./face1.jpg"
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
f = open(Path, 'rb')
#base64编码后的图片
img = base64.b64encode(f.read())
params = {"image":img,"image_type":"BASE64","face_field":"faceshape,age,beauty,emotion,gender"}
access_token = getAccess("CZ1HCPYGjXvlRVEGKR5XCIWT","xT1CfbqzUa9xFAittlnS8ibc4tL4nTFn")
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/json'}
response = requests.post(request_url, data=params, headers=headers)
if response:
print(response.json()['result'],end="\n")
print((response.json()['result']['face_list'][0]['location']),\
(response.json()['result']['face_list'][0]['face_shape']['type']),\
(response.json()['result']['face_list'][0]['beauty']), \
(response.json()['result']['face_list'][0]['emotion']['type']), \
(response.json()['result']['face_list'][0]['gender']['type']), \
(response.json()['result']['face_list'][0]['age']), \
(response.json()['result']['face_num'])
)
return(response.json()['result']['face_list'][0]['location']),\
(response.json()['result']['face_list'][0]['face_shape']['type']),\
(response.json()['result']['face_list'][0]['beauty']), \
(response.json()['result']['face_list'][0]['emotion']['type']), \
(response.json()['result']['face_list'][0]['gender']['type']), \
(response.json()['result']['face_list'][0]['age']), \
(response.json()['result']['face_num'])
AI_facedetect()
输出的结果:
{'face_num': 1, 'face_list': [{'face_token': 'b4051e5145fa8e0b86c608805a64d80b', 'location': {'left': 423.64, 'top': 669.23, 'width': 245, 'height': 239, 'rotation': -5}, 'face_probability': 1, 'angle': {'yaw': -15.34, 'pitch': 18.32, 'roll': -1.93}, 'face_shape': {'type': 'heart', 'probability': 0.73}, 'age': 23, 'beauty': 66.05, 'emotion': {'type': 'neutral', 'probability': 1}, 'gender': {'type': 'female', 'probability': 1}}]}
{'left': 423.64, 'top': 669.23, 'width': 245, 'height': 239, 'rotation': -5} heart 66.05 neutral female 23 1
将其封装成函数后 便可以在界面中进行调用 响应按钮
槽函数:face_detect.py
def facedet(self): #人脸检测
result_face_path="./face_img/result_face1.jpg"
self.location,self.face_shape,self.beauty,self.emotion,self.gender,self.age,self.facenum=AI_facedetect(self.imgPath)
img2 = cv2.imread(self.imgPath)
#绘制矩形
# 矩形需要得到左上角和右下角的坐标 即第二和第三个参数
print("location:",self.location)
print((int(self.location['left']),int(self.location['top'])), (int(self.location['left']+self.location['width']), int(self.location["top"]+self.location["height"])))
cv2.rectangle(img2, (int(self.location['left']),int(self.location['top'])),
(int(self.location['left']+self.location['width']), int(self.location["top"]+self.location["height"]))
, color=(0, 0, 255),thickness=6) # 给对角线上的点pointlist[左上,右上,右下,坐下]
# cv2.imshow("res",img2)
# cv2.waitKey()
cv2.imwrite(result_face_path, img2)
self.graylabPic.setPixmap(QPixmap(result_face_path).scaled(self.graylabPic.size()))
self.lineEditL.setText("人脸:"+str(self.facenum))
self.lineEditM.setText("性别:"+str(self.gender))
self.lineEditR.setText("年龄:"+str(self.age))
self.lineEditDL.setText("脸型:"+str(self.face_shape)) #注意这里要先强制类型转化成string
self.lineEditDM.setText("表情:"+str(self.emotion))
self.lineEditDR.setText("魅力值:"+str(self.beauty))