
PepperでWatson API「Visual Recognition」を使ってみよう
こんにちは。
AI coordinatorの清水秀樹です。
こんにちは。
AI coordinatorの清水秀樹です。
Pepperに搭載してあるカメラから、今映し出している画像がなんの画像なのか判別できるPepperを作ってみたので紹介します。
これをもっと応用できれば、色々なことに使えそうですね。
準備するもの
コレグラフを使用します。
Watson APIは「Visual Recognition」を使用しますので、Bluemix上で「Visual Recognition」アプリを作成しておく必要があります。
Bluemixの登録がまだの方は以下の記事を参考にしてください。
IBM Bluemix登録方法 30日間は無料で使用できるぞ
Bluemixの登録が済んでいる方は、カタログメニューから「Visual Recognition」を選択してアプリを作成してください。

プロジェクトファイルの説明

仕組みは簡単です。
まず、「Take Picture」ボックスで写真を撮ってから、写真を保存しているパスをWatosn API「Visual Recognition」を組み込んでいる次の「Python Script」ボックスに繋いでいきます。
その分析結果を「Say Text(2)」でしゃべらせています。
ボックス名が色々適当なのはご容赦ください。
「Take Picture」ボックスは写真のパスを出力するようにカスタマイズしています。
以下ソースコードの最後のロジック「self.onStopped」に「r」を指定しています。
この「r」には1行上のパスを出力するように設計しています。
import time
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self, False)
self.resolutionMap = {
'160 x 120': 0,
'320 x 240': 1,
'640 x 480': 2,
'1280 x 960': 3
}
self.cameraMap = {
'Top': 0,
'Bottom': 1
}
self.recordFolder = "/home/nao/recordings/cameras/"
def onLoad(self):
self.bIsRunning = False
try:
self.photoCapture = ALProxy( "ALPhotoCapture" )
except Exception as e:
self.photoCapture = None
self.logger.error(e)
def onUnload(self):
pass
def onInput_onStart(self):
if( self.bIsRunning ):
return
self.bIsRunning = True
resolution = self.resolutionMap[self.getParameter("Resolution")]
cameraID = self.cameraMap[self.getParameter("Camera")]
fileName = self.getParameter("File Name")
if self.photoCapture:
self.photoCapture.setResolution(resolution)
self.photoCapture.setCameraID(cameraID)
self.photoCapture.setPictureFormat("jpg")
self.photoCapture.takePicture( self.recordFolder, fileName )
self.bIsRunning = False
r = "/home/nao/recordings/cameras/image.jpg"
self.onStopped(r)
Watson API「Visual Recognition」を組み込んでいる「Python Script」のソースは以下の通りです。
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
def onLoad(self):
#put initialization code here
self.pathModified = False
self.appFolder = self.behaviorAbsolutePath().replace(self.behaviorRelativePath(), "")
self.folderName = os.path.join(self.appFolder, "lib")
if self.folderName not in sys.path:
sys.path.append(self.folderName)
self.pathModified = True
def onUnload(self):
#put clean-up code here
if self.pathModified and self.folderName and self.folderName in sys.path:
sys.path.remove(self.folderName)
self.folderName = None
def onInput_onStart(self, p):
#self.onStopped() #activate the output of the box
import json
from os.path import join, dirname
import requests
#Visual Recognitionへの投入
#url = 'https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify'
url = 'https://access.alchemyapi.com/visual-recognition/api/v3/classify'
payload = {'api_key': 'サービス資格情報から取得する', 'version':'2016-05-20'}
#with open(join(dirname(__file__), p), 'rb') as images:
images = open(p)
r = requests.post(url, images, params = payload,)
res = json.loads(r.text)
final = res['images'][0]['classifiers'][0]['classes'][0]['class']
self.onStopped(final.encode('utf-8'))
def onInput_onStop(self):
self.onUnload() #it is recommended to reuse the clean-up as the box is stopped
self.onStopped() #activate the output of the box
プロジェクトファイルのダウンロードはこちらから → Visual Recognition.zip
実機で動作確認済みです。
是非お試しあれ。
ただ、URLに
‘https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify’
を指定すると何故かエラーになるので、エラー内容に沿って以下のアドレスを指定したらうまく動きました。
‘https://access.alchemyapi.com/visual-recognition/api/v3/classify’
原因が良く分かりませんが、知っている方がいればぜひご教授ください。
よろしくお願い致します。
それではまた。
LEAVE A REPLY