python call Baidu face recognition: to score a face value

Foreword

On a number of the public the chance to see an article calling Baidu ai be the value of Yen Rate class girls scoring, kinda fun, then you have to try to recognize women around the fight scoring -

content

Open Baidu ai face recognition - Click for immediate use - Baidu account login
to create a new application - Get the key
Here Insert Picture Description
to the help manual
to find the URL in exchange for token will write its functional form

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']

Call the main function:

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())

This is the official source code, with some slight modifications so that he can show my photos the current scoring and score enough, no need other parameters
, the quality of clarity, character angle photos are impact score

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()

Display picture by matplotlib, picture Baidu requirement is base64
following direct call getscore(url)it
url is the path to place pictures

Try

Want to put around a few people you know photographs of it, think about or forget, I'm afraid to cut ......
find a star try it ~
Here Insert Picture Description
Dilly Reba, is not generally ~
encore:
Here Insert Picture Description
......
Here Insert Picture Description
I think the last one is the most beautiful ............
men can also:
Here Insert Picture Description
there is a picture score, the score is not high, we hold here, I'm afraid I put out the next day and Baidu are gone ......

At last

Secretly to play once a minute for the people around it -
of course, scores are for reference only, not for the competition, each of us is unique.

Finally, the most

Appearance before the age of 30 are parents, and looks after his own 30 years of age is raised
abdominal poetry and gas from China

Published 71 original articles · won praise 204 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_44198436/article/details/105279427