2017年11月2日木曜日

音声感情プログラム(Empath APIを利用)

音声から感情を読み取るAPIを試して見ました。
(1) APIの準備
https://webempath.net/lp-jpn/
からアカウントを作成しておきます。契約はフリープランにしておきました(月300回まで)。
適当な名前をつけてAPI Keyを発行しておきます。

(2) 情報取得の確認 (PHPで実施)
Empath WebAPI 仕様書 (version 1)  のサンプルコードを使って確認して見ました。
POSTMANでやってみると図のようになります。
これをPHPコードでエクスポートして、コントローラに設定しました。
public function emotion($id = null) {
if (!$this->Audio->exists($id)) {
throw new NotFoundException(__('Invalid audio'));
}
$options = array('conditions' => array('Audio.' . $this->Audio->primaryKey => $id));
$this->set('audio', $this->Audio->find('first', $options));
$audio = $this->Audio->find('first', $options);
$curl = curl_init();
$wav = '@/home/ms000/www/oc/app/webroot/img/wav/'.$audio['Audio']['path'];
$postfields = array(
"wav" => $wav
);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.webempath.net/v2/analyzeWav?apikey=naisyo",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: multipart/form-data",
"postman-token: 669b5410-eec8-4fa2-1aa3-a"
),
CURLOPT_FOLLOWLOCATION => TRUE
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$res = json_decode($response);
print_r($res);
if($res->error == 0 ){
$data = array(
'id' => $id,
'calm' => $res->calm,
'anger' => $res->anger,
'joy' => $res->joy,
'sorrow' => $res->sorrow,
'energy' => $res->energy,
);
$this->Audio->save($data);
}
}
view raw emotion hosted with ❤ by GitHub


テーブルは次を利用しています。
CREATE TABLE IF NOT EXISTS `audios` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`path` varchar(255) DEFAULT NULL,
`calm` int(11) DEFAULT NULL,
`anger` int(11) DEFAULT NULL,
`joy` int(11) DEFAULT NULL,
`sorrow` int(11) DEFAULT NULL,
`energy` int(11) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
view raw audios.sql hosted with ❤ by GitHub


(3) 動作確認
実際に音声をアップロードしてやってみると、こんな感じでした。


実際はAndroidアプリで録音し、そこで評価。評価したものをアップロードしてテーブルに格納するのが良さそうです。

0 件のコメント:

コメントを投稿