900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Python调用百度人脸识别API接口检测颜值

Python调用百度人脸识别API接口检测颜值

时间:2020-09-29 20:37:01

相关推荐

Python调用百度人脸识别API接口检测颜值

我们今天使用Python发送网络请求到百度人脸识别API接口。

我们需要requests模块,它需要安装,代码是

pip3 install requests

注意:不好意思各位,我的配额没了!如需使用请在getToken里边更换为自己的key

首先我们先来介绍一下API是什么:

API就是操作系统留给应用程序的一个调用接口,应用程序通过调用操作系统的 API 而使操作系统去执行应用程序的命令。

介绍完了,我们开始写代码:

#导入所需模块from tkinter import Tkfrom tkinter.messagebox import showinfo,showerrorfrom tkinter.filedialog import askopenfilenamefrom requests import post,getfrom base64 import b64encode#声明变量totalList = []#获取百度人脸识别接口def getToken():host = '/oauth/2.0/token?grant_type=client_credentials' \'&client_id=T1KUrVlob1vUuLpQ0sOYrfoB&client_secret=obx6I60FomQIHqRwlx3mp1GXTGKOkHPu'response = get(host)content = response.json()content = content['access_token']return content#获取人脸识别数据def getData(img):global beauty,path,levelrequestUrl = '/rest/2.0/face/v3/detect'token = getToken()params = {'access_token': token}f = open(img, 'rb')temp = f.read()image = b64encode(temp)data = {'image':image,'image_type':'BASE64','face_field':'beauty'}response = post(requestUrl, params=params, data=data)print('响应结果:', response)content = response.json()print('解析结果:', content)#可以将颜值加上20beauty = content['result']['face_list'][0]['beauty']if beauty <= 50 and beauty > 0:level = '一般般吧!'elif beauty <= 60:level = '有些漂亮了!'elif beauty <= 70:level = '挺漂亮!'elif beauty <= 80:level = '好漂亮!'elif beauty > 90:level = '漂亮的爆表了!'tempDict = {'image': img,'beauty':beauty,'level':level}path = tempDict['image']totalList.append(tempDict)#创建页面 window = Tk()window.withdraw()image = askopenfilename(title = '请选择一张图片!',initialdir = 'C:/Windows')try:if not image != 0:showerror(title = '错误',message = '请上传一张图片!')else:getData(img = image)showinfo(title = '通知',message = '您的颜值是:{0}。\n颜值等级:{1}\n您上传的图片路径是:{2}。'.format(beauty,level,path))except FileNotFoundError:showerror(title = '错误',message = '请上传一张图片!')window.mainloop()

实现效果:

我上传的是一张钟汉良的照片,如果换个人的话,数值可能就变了,注意,一定要上传一张清晰的人脸照片,否则会报错。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。