LoginSignup
5
1

More than 5 years have passed since last update.

Pepperに推定年齢を喋らせるアプリ

Posted at

概要

PepperをPythonだけで開発できる(PythonSDK)と、IBM社のwatsonの"visual_recognition"を連携させて、性別と顔年齢を推定するアプリケーションです。

PythonSDK

PythonSDKについて詳しく知りたい方は、以下のページを参照してください。

  1. https://qiita.com/kyohara/items/ae0dd0f57413caa90674 (PythonSDKの導入方法)
  2. https://qiita.com/Atelier-Akihabara/items/3289e60985586b8da709 (Python SDKによるsay hello)
  3. http://naoqi.memo.wiki/d/Python%20SDK (PythonSDKリファレンス)

ソースコード詳細

face_age.py
#coding:utf8
from naoqi import ALProxy#peperのライブラリを使うためにimport
import sys
import paramiko#ssh通信をするためのライブラリ
import scp#scp通信するためのライブラリ
import time
import json
from os.path import join, dirname
from os import environ
from watson_developer_cloud import VisualRecognitionV3#watsonのAPIを使うための


def face_judge(api_key,img_path):
    visual_recognition = VisualRecognitionV3(
    '2016-05-20',
    api_key=api_key)

    with open(img_path, 'rb') as images_file:
        classes = visual_recognition.detect_faces(
            images_file,
            parameters=json.dumps({
                'threshold': 0.4
            }))
    return classes

def main():
    api_key = 'your api key'#ワトソンのAPIキーを取得して、ここに入力してください。
    pepperIP = "your pepper ip adress"#pepperのIPアドレスを入力してください。
    local_path = ""#作業するディレクトリまでのpathを指定
    tts = ALProxy("ALTextToSpeech",pepperIP, 9559);
    tts.say("写真撮影しちゃうよー")
    time.sleep(1)
    tts.say("さん")
    time.sleep(0.3)
    tts.say("にー")
    time.sleep(0.3)
    tts.say("いち")
    time.sleep(0.3)
    tts.say("かしゃ")
    photoCapture = ALProxy("ALPhotoCapture",pepperIP,9559)
    recordFolder = "/home/nao/recordings/cameras/"#pepperは写真をとったときに、このディレクトリに保存される。
    fileName = "test.jpg"
    photoCapture.takePicture(recordFolder, fileName)
    with paramiko.SSHClient() as ssh:
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname= pepperIP, port=22, username='nao', password='nao')
        with scp.SCPClient(ssh.get_transport()) as SCP:
       # scpに対してgetするだけでファイルが取得できる
            SCP.get(recordFolder+fileName,local_path+fileName)
    tts.say("今あなたのことを解析中だよ。ちょっとまってね")
    classes = face_judge(api_key,fileName)

    min_age = classes['images'][0]['faces'][0]['age']['min']#年齢の最大値を取得
    max_age = classes['images'][0]['faces'][0]['age']['max']#年齢の最小値を取得
    gender = classes['images'][0]['faces'][0]['gender']['gender']#性別を取得

    if gender =='FEMALE':
        tts.say("あなたは女性ですね。あなたの年齢を当てますよーー,"+str(min_age)+"さいから"+str(max_age)+"さいくらいの年齢ですね。")
    elif gender == 'MALE':
        tts.say("あなたは男性ですね。あなたの年齢を当てますよーー,"+str(min_age)+"さいから"+str(max_age)+"さいくらいの年齢ですね。")

if __name__ == "__main__":
    main()

まず、pepperの写真を撮る機能を使うと、pepperの頭の中のOS(naokiOS)上の、/home/nao/recordings/cameras/test.jpgという名前で保存されます。この画像をscpを用いて、ローカル環境(自分のパソコン)に落とし込みます。その後、watsonの"visual_recognition"を用いて、画像から年齢、性別を判別します。判別結果をペッパーに喋らせます。

結果

私(22)は、メガネをつけない場合は18歳から24歳と診断されましたが、メガネをつけると35歳から44歳と判断されました。悲しいです。ただ、Pepperと連携することで、データの収集などできるかも??

by アトリエスタッフ

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1