LoginSignup
8
7

More than 5 years have passed since last update.

ChoregrapheからBluemixのLanguage Translationを呼び出してみる

Last updated at Posted at 2016-02-29

目的

ChoregrapheもPythonもBluemixもQiitaも超初心者が、ChoregrapheからBluemixのWatson APIを呼び出す。
その勢いのままQiitaに投稿してみる。(勢いのため、途中の画像は無い。。。)

参考にしたのはこちらの記事。
ChoregrapheからWatsonを使用してみる
この文章中の引用は、こちらから引用してます。

環境

  • Choregrapheはインストール済み、起動してペッパーくんを表示できた
  • Python2.7インストール済み、動かしたことはない
  • Bluemix無料トライアル登録済み、ログインだけしてみた

Bluemix

Bluemixにログイン後、ダッシュボードからスペースの作成を行います。その後、サービス&APIメニューより使用するサービスを登録します。

今回は、Language Translationを選択。
ダッシュボードに作成されたサービスを選択、サービス資格情報にCredentialsが表示される。
これはあとで使うので覚えておく。

Choregraphe

Choregrapheのroot上にPython scriptボックスを配置し、onInput_onStart内に以下を記述します。

user="取得したusername"
pswd="取得したpassword"
target = "ar"
string = "Good morning, Pepper."    

###Set the url###
url="https://gateway.watsonplatform.net/language-translation/api/v2/translate?source=en&target="+str(target)+str("&text=")+str(string)
###Send the request###
r = requests.get(url ,auth =(user,pswd))

### print the result###
NewText= r.text
final= NewText.encode('utf-8')
self.tts.say(str(final))

user,pswdには、Bluemixで表示したCredentialsからコピペする。

保存して実行すると失敗した。
ログビュー確認する。
どうやらPythonのrequestsというライブラリが足りないようだ。

requestsとは???ライブラリ入れるのってどうやるの???状態。
requestsのインストール方法を調べる。
http://qiita.com/sqrtxx/items/49beaa3795925e7de666

pip install requests

というコマンドでインストールできるらしい。

ターミナル開いてコマンドうってみるが、pipが入っていない。
pipとは、Pythonのライブラリ管理用便利ツールらしい。
pipのインストール方法を調べる。
https://pip.pypa.io/en/stable/installing/
ここから、get-pip.pyをダウンロードし、適当な場所に置いてインストールできるらしい。

置いた場所にcdし、インストールコマンド実行。

$ python get-pip.py
Exception:
Traceback (most recent call last):
  File "/var/folders/qc/0nm54h514xzcpcjjx5n__qvc0000gn/T/tmp1DyCOX/pip.zip/pip/basecommand.py", line 209, in main
    status = self.run(options, args)
  File "/var/folders/qc/0nm54h514xzcpcjjx5n__qvc0000gn/T/tmp1DyCOX/pip.zip/pip/commands/install.py", line 317, in run
    prefix=options.prefix_path,
  File "/var/folders/qc/0nm54h514xzcpcjjx5n__qvc0000gn/T/tmp1DyCOX/pip.zip/pip/req/req_set.py", line 731, in install
    **kwargs
  File "/var/folders/qc/0nm54h514xzcpcjjx5n__qvc0000gn/T/tmp1DyCOX/pip.zip/pip/req/req_install.py", line 841, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/var/folders/qc/0nm54h514xzcpcjjx5n__qvc0000gn/T/tmp1DyCOX/pip.zip/pip/req/req_install.py", line 1040, in move_wheel_files
    isolated=self.isolated,
  File "/var/folders/qc/0nm54h514xzcpcjjx5n__qvc0000gn/T/tmp1DyCOX/pip.zip/pip/wheel.py", line 343, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/var/folders/qc/0nm54h514xzcpcjjx5n__qvc0000gn/T/tmp1DyCOX/pip.zip/pip/wheel.py", line 314, in clobber
    ensure_dir(destdir)
  File "/var/folders/qc/0nm54h514xzcpcjjx5n__qvc0000gn/T/tmp1DyCOX/pip.zip/pip/utils/__init__.py", line 82, in ensure_dir
    os.makedirs(path)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/pip'

しかしまた失敗。
mkdirのPermission deniedらしいので、sudoつけて実行してみる。

$ sudo python get-pip.py
Password:
The directory '/Users/xxxx/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/xxxx/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pip
  Downloading pip-8.0.3-py2.py3-none-any.whl (1.2MB)
    100% |████████████████████████████████| 1.2MB 410kB/s 
Collecting wheel
  Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
    100% |████████████████████████████████| 69kB 3.4MB/s 
Installing collected packages: pip, wheel
Successfully installed pip-8.0.3 wheel-0.29.0

pipインストール成功。

ここまできてやっとrequestsのインストールコマンドを実行。

$ sudo pip install requests
The directory '/Users/xxxx/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/xxxx/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting requests
  Downloading requests-2.9.1-py2.py3-none-any.whl (501kB)
    100% |████████████████████████████████| 503kB 947kB/s 
Installing collected packages: requests
Successfully installed requests-2.9.1

requestsライブラリをインストールできた。

あとは、ChoregrapheのpythonScriptに

import requests

というインポート文を書けば良いらしい。
どこに書けばいいか分からなかったが、とりあえず一番上に書いて実行。
エラー発生。

    [ERROR] behavior.box :_safeCallOfUserMethod:125 _Behavior__lastUploadedChoregrapheBehaviorbehavior_1140665607444400:/Python Script_1: Traceback (most recent call last):
      File "/Applications/Aldebaran/Choregraphe Suite 2.4/Choregraphe.app/Contents/Resources/lib/albehavior.py", line 115, in _safeCallOfUserMethod
        func()
      File "<string>", line 29, in onInput_onStart
      File "/Applications/Aldebaran/Choregraphe Suite 2.4/Choregraphe.app/Contents/Resources/lib/ialbehavior.py", line 166, in <lambda>
        __getattr__ = lambda self, name: _swig_getattr(self, behavior, name)
      File "/Applications/Aldebaran/Choregraphe Suite 2.4/Choregraphe.app/Contents/Resources/lib/ialbehavior.py", line 74, in _swig_getattr
        return _swig_getattr_nondynamic(self, class_type, name, 0)
      File "/Applications/Aldebaran/Choregraphe Suite 2.4/Choregraphe.app/Contents/Resources/lib/ialbehavior.py", line 69, in _swig_getattr_nondynamic
        return object.__getattr__(self, name)
    AttributeError: type object 'object' has no attribute '__getattr__'

onInput_onStartに書いた
self.tts.say(str(final))
のself.ttsがインスタンス化(?)されていないらしい。

initに以下を追加

self.tts = ALProxy('ALTextToSpeech')

実行。。。できた!

超初心者でも、Pepperくんをアラビア語でしゃべらせることができました。
しかし、Pepperくんに頭の上に表示されている吹き出しはすぐに消えてしまう。
喋った内容はダイアログビューに表示されるので、そちらで確認できるみたいですね。

まとめ

ChoregrapheからBluemix APIを呼ぶことができた。
今後は、他のBluemix APIも呼び出して動きを確認しつつ、ChoregrapheとBluemixを連携してどんなものができるのか試してみたい。

8
7
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
8
7