python调用百度人脸识别:来一次颜值评分

前言

在某公众号上偶然看到一篇文章 调用百度ai进行颜值打分给班级女生打分,还挺好玩的,遂也来试试给周围认识的女性打打分~

内容

打开百度ai人脸识别-点击立即使用-登录百度帐号
新建一个应用-获取key
在这里插入图片描述
来到帮助手册
找到换取token的网址,将其写出函数形式

def gettoken():
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=***********&client_secret=**********'
    response = requests.get(host)
    if response:
        return response.json()['access_token']

主体调用函数:

import requests

'''
人脸检测与属性分析
'''

request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"

params = "{\"image\":\"027d8308a2ec665acb1bdf63e513bcb9\",\"image_type\":\"FACE_TOKEN\",\"face_field\":\"faceshape,facetype\"}"
access_token = '[调用鉴权接口获取的token]'
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())

这是官方源代码,稍作修改让他可以显示我当前打分的照片成绩就够了,其他的参数暂时不需要
,照片的质量清晰度,人物角度都影响评分标准

import requests
import base64
import matplotlib.pyplot as plt # plt 用于显示图片
import matplotlib.image as mpimg # mpimg 用于读取图片
'''
换取token
'''
def gettoken():
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=***********&client_secret=**********'
    response = requests.get(host)
    if response:
        return response.json()['access_token']
'''
人脸检测与属性分析
'''
def getscore(url):
    f=open(url,'rb')#二进制读写,转换base64
    base64_data = base64.b64encode(f.read())
    request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
    params = {
        "image":base64_data,
        "image_type":"BASE64",
        "face_field":"beauty"
    }
    request_url = request_url + "?access_token=" + gettoken()
    headers = {'content-type': 'application/json'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        print ("经过AI大数据评分您的颜值分数为:",response.json()['result']['face_list'][0]['beauty'])
        print('百度人工智能技术支持')
        lena = mpimg.imread(url) 
        plt.imshow(lena) 
        plt.axis('off')
        plt.show()

通过matplotlib进行图片的显示,图片百度要求是base64
下面直接调用getscore(url)就可以了
url是放置图片的路径

试一试

想放几张周围认识的人的照片呢,一想还是算了,我怕被砍……
找个明星试试吧~
在这里插入图片描述
迪丽热巴,就是不一般~
再来一个:
在这里插入图片描述
……
在这里插入图片描述
我认为最后一个是最漂亮的…………
男性也可以:
在这里插入图片描述
还有一张照片评分,得分不高,不放这里了,我怕放出来我和百度第二天都没有了……

最后

偷偷的来为身边的人打一次分吧~
当然,分值仅供参考,切勿用于攀比,我们每个人都是独一无二的。

最最后

30岁之前的相貌是父母给的,30岁之后的相貌则是自己养的
腹有诗书气自华

发布了71 篇原创文章 · 获赞 204 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_44198436/article/details/105279427