LoginSignup
48
27

More than 5 years have passed since last update.

マツコ・デラックスとの類似度を返信してくれるマツコbotを開発した

Last updated at Posted at 2019-02-01

はじめに

今回はLINEmessagingAPIとMicrosoftFaceAPIを組み合わせてマツコ・デラックスさんとどれだけ似ているかを返信してくれるマツコbotを開発しました.以下のようにマツコが返信してくれます.しっかりマツコに対して高い数値を出しています.
IMG_0413.PNG

開発環境

  • macOS Mojave 10.14.2
  • python 3.6

マツコ・デラックスとは

知らない人はいないと思いますが一応説明しておきます.マツコ・デラックスさんは日本のコメンテーターで現在たくさんの番組に出演されており,毒舌キャラで大人気です.詳しく知りたい方はWikipediaをご覧ください.

実装

今回は以下のように実装していきます.すでにbotを作成している前提で進めていきます.
図1.png

Herokuの設定

こちらはすでにたくさんの方が詳しく説明しているのでそちらを見た方がわかりやすかと思います.こちらの記事を参考にさせていただきました.ターミナルで以下のように設定していきます.

$ heroku login
$ heroku create matsuko-bot
$ heroku config:set YOUR_CHANNEL_SECRET="Channel Secretの欄の文字列" --app matsuko-bot
$ heroku config:set YOUR_CHANNEL_ACCESS_TOKEN="アクセストークンの欄の文字列" --app matsuko-bot

設定ファイルの作成

それぞれ設定ファイルを作成します.

runtime.txt
python-3.6.6
requirements.txt
Flask==0.12.2
line-bot-sdk==1.5.0
requests==2.18.4
gunicorn
Procfile
web: python main.py
main.py
#一部のみ

@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
    line_bot_api.reply_message(
        event.reply_token,
        TextSendMessage(text='顔画像を送信しなさい'))


@handler.add(MessageEvent, message=ImageMessage)
def handle_image(event):
    message_id = event.message.id
    message_content = line_bot_api.get_message_content(message_id)
    image = BytesIO(message_content.content)

    r_1 = requests.post(url_detect, headers = headers_detect, params = params, data = open('matsuko.jpg','rb'))
    r_2 = requests.post(url_detect , headers = headers_detect, params = params, data = image)
    faceId = r_1.json()[0]['faceId']

    try:
        faceIds = r_2.json()[0]['faceId']
        body['faceId'] = faceId
        body['faceIds'] = [faceIds]
        r_find_similar = requests.post(url_similar, headers = headers_similar, data = json.dumps(body)).json()[0]['confidence']
        line_bot_api.reply_message(event.reply_token,TextSendMessage(text='私との類似度は{}'.format(r_find_similar)))

    except:
        line_bot_api.reply_message(event.reply_token,TextSendMessage(text='顔が検出できなかったですよ'))

matsuko.jpg(こちらの顔画像と似ているかを比較します)
matsuko.jpg

ファイル構造
$ tree
.
├── Procfile
├── main.py
├── matsuko.jpg
├── requirements.txt
└── runtime.txt

Herokuにデプロイ

$ git init
$ git add .
$ git commit -m "new commit"
$ git push heroku master

git pushできない場合は以下のコマンドを実行してください.

$ git remote add heroku https://git.heroku.com/アプリケーション名.git

デバックするためにログを確認

$ heroku logs --tail -a <アプリケーション名>

webhookの設定

LINEbotの設定画面で以下のように設定します
Webhook送信:利用する
Webhook URL:https://<アプリケーション名>.herokuapp.com/callback

まとめ

今回はLINEとMicrosoftのAPIを組み合わせて顔認識のbotを作ってみました.web関連の知識は全くなかったのですが案外簡単にできました(Herokuの設定で少し苦戦した).今後は自分でAPIサーバーを構築して便利なLINEbotを作っていきたいです.今回の実装はGitHubに載せておきます.

48
27
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
48
27