今回は画像の美人度を判定できる
APIを用いて顔の美人度の
点数を判定していきたいと思います。

解説動画はこちら 


今回使用したのが
Face++ というアプリのAPIです。

これは顔の美人度を判定して
返すことができるので
これを見ていきたいと思います。

なお利用にはAPIキーを取得する必要があるので
先に登録をしておかないと動かせません。

やってみたい方は
こちらのリンクからゲットしておいて下さい。

Face++の登録先

さてサンプルプログラムを見ていきましょう。

先にキーを文字列で指定しておきます。
api_key = "先にAPIキーとかをゲットしてね"
api_secret = "先にAPIキーとかをゲットしてね"

サンプルプログラムはこちら

import requests
import base64
from collections import defaultdict
from pathlib import Path
import pickle
import time

endpoint = 'https://api-us.faceplusplus.com'

face_dict1 = defaultdict(str)

# 画像のフォルダを指定する
src1 = sorted(Path('img/1/').glob('*.png'))

# APIをつついて結果を取得する
for i, file in enumerate(src1):
    img_file = base64.encodestring(open(file, 'rb').read())
    response = requests.post(
        endpoint + '/facepp/v3/detect',
        {
            'Content-Type': 'multipart/form-data',
            'api_key': api_key,
            'api_secret': api_secret,
            'image_base64': img_file,
            'return_attributes': 'gender,age,headpose,facequality,eyestatus,emotion,ethnicity,beauty,mouthstatus'
        }
    )
    time.sleep(1.9)
    try:
        result = response.json()
        face_dict1[file.name[:-4]] = result
        print(i,file.name[:-4],'OK')
    except:
        print(i,file)

apiキーの部分はご自身ので差し替えて下さい。

あらかじめ画像を用意しておく必要があるので
ノートブックの配下のフォルダに
pngファイルを格納して下さい。

キーなどが揃っていればAPIは動きますが
画像によっては失敗することもあります。

成功したものだけ結果に残します。


結果を見るには次のコードで行います。
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
%matplotlib inline

result_1 = {}
for k,v in face_dict1.items():
    result_1[k] = v['faces'][0]['attributes']['beauty']['female_score']

for k,v in sorted(result_1.items(),reverse=True,key=lambda x:x[1]):
    im = Image.open("img/1/{0}.png".format(k))
    img_resize = im.resize((500, int(500*(im.height/im.width))))
    plt.imshow(np.asarray(img_resize))
    plt.text(0.4, 0.4, v, size=30)
    plt.axis("off")
    plt.show()

さて、誰がどんな点数になったでしょうか
点数に関しては動画の方をご覧ください。

上位と下位で判定してみましたが
納得感のある点数が出るのでは
ないでしょうか?!?!?!

自分の顔画像とかも送ってみれば
客観的に判断できるかもしれませんね。

興味がある方は
使ってみて下さい。

今日はここまでです
それでは