ビジネスパーソン・ガジェット置場 empty lot for business

営業や仕事、それに伴う生活を便利に楽にするツール、ガジェットを作ります。既にあるツールも自分用にカスタマイズ。

見た目チェッカー【python + Face api】

AzureのFace apiを使用した見た目年齢のチェックガジェットです。機械学習で導き出した年齢と実年齢を比較するだけの単純なものになります。

例えばこんな状況で

今回のガジェットはこういう声に対応
「近頃だんだん年齢が。。他の人には何歳くらいに見えているんだろう。。もし年齢が歳に見えるのであればエイジングを始めないと!」

このガジェットでできることはこちら

・AzureのFace APIを使用した画像分析ででた年齢と実年齢を比較します。
・比較後コメントを簡単に載せます。
・一人の写真だけでなく複数人で写っている写真にも対応しています

注意点はこちら

ナヨン大好きです!!

 

評価(自己)

役立ち度  ★★★★(良し)
効率化   ★★★(平均)
ミス防止  ★★★(平均)
楽しさ   ★★★★(良し)
操作性   ★★★★(良し)

実行環境

使用環境    streamlit Face api
使用言語    python
使用ライブラリ    json、requests、PIL 、io

 

コード

import json
import requests
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import io
import streamlit as st


with open('secret.json', 'r') as f:
    secret = json.load(f)
    
subscription_key = secret['API_KEY']
face_api_url = 'https://agechecker20220319.cognitiveservices.azure.com/face/v1.0/detect'

st.title('見た目年齢チェッカー')
st.write('チェックする写真をアップロードしてください')

uploaded_file = st.file_uploader('Choose an image...', type='jpg')
        
if uploaded_file is not None:
    img = Image.open(uploaded_file)
    with io.BytesIO() as output:
        img.save(output, format='JPEG')
        binary_img = output.getvalue()

    headers = {
        'Content-Type': 'application/octet-stream',
        'Ocp-Apim-Subscription-Key': subscription_key
    }
    params = {
        'returnFaceId': 'true',
        'returnFaceAttributes': "age, gender, headPose, smile, facialHair,glasses, emotion, hair, makeup, occlusion, accessories, blur, exposure, noise"

    }
    real = st.slider('スライダーを実年齢に合わせてください',min_value=10, max_value=70, value=15,)
    st.write(f'実年齢は{real}歳ですね?')
    
    if st.button('チェック開始'):
        ages = []
        res = requests.post(face_api_url, params=params,
                           headers=headers, data=binary_img)

        results = res.json()
        face_num = len(results)

        text_color = (255, 255, 255)
        rect_color = (255, 0, 0)
        textsize = 40
        font = ImageFont.truetype("mplus-1c-bold.ttf", size=textsize)
        line_width = 3
        for result in results:
            # print(result)
            age = int(result['faceAttributes']['age'])
            rect = result['faceRectangle']
            draw = ImageDraw.Draw(img)
            draw.rectangle([(rect['left'], rect['top']), (rect['left']+rect['width'], rect['top']+rect['height'])], fill=None, outline='green', width=line_width)
            text = f'{age}歳'
            txw, txh = draw.textsize(text, font=font)
            txpos = (rect['left'], rect['top']-textsize-5)
            draw.rectangle([txpos, (rect['left']+txw, rect['top'])], outline=rect_color, fill=rect_color, width=line_width)
            draw.text(txpos, text, font=font, fill=text_color)
            ages.append(age)


        st.image(img, caption='uploaded image.', use_column_width=True)
        
        
        st.write('#### 見た目年齢は')
        for looks in ages:
            st.write(f'##### {looks}歳です')
        
            if face_num == 1:
                if looks < real:
                    st.write('##### 見た目若い!!素敵!!')
                elif looks == real:
                    st.write('##### 見たままです')
                else:
                    st.write('##### 頑張りましょう!')