JuliusForAndroidを動かしてみる
- 2020.10.05
- Android

作られた当時(2012年頃)はAndroidStudioがCMake、NDK-buildをサポートしていないのもあってその当時の環境構築方法を今試してみても中々うまくいかなかったです.(2012年以降に環境構築をまとめている方が見つかりませんでした.)
現在のバージョン(AndroidStudio2.2から)ではネイティブ(C/C++)コードが利用出来るため、そのやり方で起動と認証ができるようになったのでまとめます.
Android Studio 2.2 が CMake と ndk-build をサポート
ちなみに以前、ラズパイでJuliusを動かす方法についてまとめました.
JuliusForAndroidをダウンロード
以下からダウンロードします.
https://github.com/tech-sketch/JuliusForAndroid
Android端末
JuliusForAndroid>sdcardのjuliusフォルダをAndroid端末のルートフォルダ直下に配置します.

julius直下にconfファイル(辞書の読み込みなどの設定を記載)があります.
(今回は「demo-grammar-android.jconf」を使います.)
ちなみに中身はこんな(↓)感じです.使用する辞書とモデルはここに書かれています.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
-dfa grammar/demo.dfa -v grammar/demo.dict # -b 600 -b2 100 -penalty1 -7.0 -penalty2 0.0 #-1pass # #-input mic -demo #-input file # #-C ../../jconf/_AM_JNAS_tri -input rawfile -smpFreq 22050 -h model/phone_m/hmmdefs_ptm_gid.binhmm -hlist model/phone_m/logicalTri |
また、grammar直下が辞書ファイル(grammar-kitのfruitが登録されている)です.
中身を見るとなんとなくfruit の辞書だなってわかるかと思います.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
0 [•s–¾] h u m e i 0 [–¨Š¹] m i k a N 0 [ƒŠƒ“ƒS] r i N g o 0 [‚Ô‚Ç‚¤] b u d o: 1 [0] z e r o 1 [1] i ch i 1 [2] n i: 1 [3] s a N 1 [4] y o N 1 [5] g o: 1 [6] r o k u 1 [7] n a n a 1 [8] h a ch i 1 [9] ky u: 2 [ŒÂ] k o 3 [‚ª] g a 4 [<s>] silB 5 [</s>] silE |
ちなみに辞書の作成方法は少し前にまとめています.
そして最後にmodel直下は音響モデルが置いてあります.
Androidプロジェクト作成
AndroidStudioでプロジェクトを作成します.
ちなみに「JuliusForAndroid」のうち、使うファイルは以下になります.
・JuliusForAndroid/src/jp/co/tis/stc/juliusのJuliusActivity.java(要編集)
・JuliusForAndroid/libs/armeabiのlibjulius_arm.so
・JuliusForAndroid/res/layoutのactivity_julius.xml
・JuliusForAndroid/res/valuesのstrings.xml
・JuliusForAndroidのAndroidManifest.xml(要編集)
プロジェクトを作成したら以下の様に、「main」>「jniLibs」>「armeabi」と作成し、そこに「libjulius_arm.so」を置きます.

AndroidManifest.xmlから以下を削除します.
(新規作成してストレージとマイクへのアクセス権限を追加すれば良いだけですが)
1 2 3 |
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> |
JuliusActivity.javaは以下のようになります.
必要なファイルをSDカードではなく端末のストレージに置いているためpathを変更しています.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
package jp.co.tis.stc.julius; import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import android.annotation.SuppressLint; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.media.AudioFormat; import android.media.AudioRecord; import android.media.MediaRecorder; import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; public class JuliusActivity extends Activity { private static final String TAG = "Julius JulisuActivity"; private static final String INF = "info"; private static final String CONTINUOUS_JCONF = "/julius/fast-android.jconf"; private static final String GRAMMAR_JCONF = "/julius/demo-grammar-android.jconf"; private static final String WAVE_PATH = "/julius/voice.wav"; private static final int SAMPLING_RATE = 22050; private String path = Environment.getExternalStorageDirectory().getPath(); static { System.loadLibrary("julius_arm"); } private native boolean initJulius(String jconfpath); private native void recognize(String wavpath); private native void terminateJulius(); private boolean isInitialized = false; // initJulius()に成功したらtrue private AudioRecord audioRec = null; private int bufSize = 0; private String resultStr = ""; private RadioGroup radioGroup; private TextView resultText; private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_julius); resultText = (TextView) findViewById(R.id.result_text); bufSize = AudioRecord.getMinBufferSize(SAMPLING_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT) * 2; audioRec = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLING_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufSize); radioGroup = (RadioGroup) findViewById(R.id.radiogroup); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { new JuliusInitializer(JuliusActivity.this).execute(checkedId); } }); button = (Button) findViewById(R.id.speech_button); button.setEnabled(false); button.setOnClickListener(onClickListener); } @Override protected void onDestroy() { if (isInitialized) { Log.d(INF, "terminateJulius() 開始"); terminateJulius(); Log.d(INF, "terminateJulius() 終了"); isInitialized = false; } super.onDestroy(); } // Juliusの初期化を別スレッドで実行 @SuppressLint("StaticFieldLeak") private class JuliusInitializer extends AsyncTask<Integer, Void, Boolean> { private ProgressDialog progressDialog; Context context; public JuliusInitializer(Context context) { this.context = context; } // メインスレッドで実行 @Override protected void onPreExecute() { Log.d(TAG, "JuliusInitializer:onPreExecute"); progressDialog = new ProgressDialog(context); progressDialog.setMessage(JuliusActivity.this.getString(R.string.initializing_message)); progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.show(); } // メインスレッドとは別のスレッドで実行 @Override protected Boolean doInBackground(Integer... params) { if (isInitialized) { Log.d(INF, "terminateJulius() 開始"); terminateJulius(); Log.d(INF, "terminateJulius() 終了"); } String conf; int checkedId = params[0]; if (checkedId == R.id.continuous) { Log.d(TAG, "JuliusInitializer:doInBackground:conf is continuous"); conf = CONTINUOUS_JCONF; } else if (checkedId == R.id.grammer) { Log.d(TAG, "JuliusInitializer:doInBackground:conf is grammer"); conf = GRAMMAR_JCONF; } else { Log.d(TAG, "JuliusInitializer:doInBackground:invalid conf"); return false; } Log.d(INF, "initJulius() 開始"); Log.d(INF, "ストレージ:" + context.getFilesDir() + conf); if (initJulius(path + conf)) { Log.d(TAG, "JuliusInitializer:doInBackground:init julius success"); Log.d(INF, "initJulius() 終了"); return true; } else { Log.e(TAG, "JuliusInitializer:doInBackground:init julius error"); Log.d(INF, "initJulius() エラー終了"); return false; } } // doInBackgroundメソッドの実行後にメインスレッドで実行 @Override protected void onPostExecute(Boolean result) { Log.d(TAG, "JuliusInitializer:onPostExecute"); progressDialog.dismiss(); if (result) { isInitialized = true; button.setEnabled(true); } else { isInitialized = false; button.setEnabled(false); Toast.makeText(context, "initJulius Error", Toast.LENGTH_LONG).show(); } } } private final View.OnClickListener onClickListener = new View.OnClickListener() { private boolean isRecording = false; private Thread writeAudioToFileThread = null; @Override public void onClick(View v) { if (!isRecording) { Log.d(TAG, "start recording"); isRecording = true; writeAudioToFileThread = new Thread(writeAudioToFile); button.setText(R.string.recording); resultText.setText(JuliusActivity.this.getString(R.string.init_text));//ここに認識した音声が表示されます audioRec.startRecording(); writeAudioToFileThread.start(); } else { Log.d(TAG, "call recognize"); isRecording = false; // レコード中のループを抜ける try { writeAudioToFileThread.join(); } catch (InterruptedException e) { Log.e(TAG, e.toString()); } button.setText(R.string.recogninzing); button.setEnabled(false); new JuliusRecognizer(JuliusActivity.this).execute(Environment.getExternalStorageDirectory().getPath() + WAVE_PATH); // new JuliusRecognizer(JuliusActivity.this).execute(Environment.getExternalStorageDirectory() + WAVE_PATH); } } private final Runnable writeAudioToFile = new Runnable() { @Override public void run() { Log.d(INF, "ファイル書き込み 開始" + Environment.getExternalStorageDirectory().getPath() + WAVE_PATH); android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO); File recFile = new File(Environment.getExternalStorageDirectory().getPath() + WAVE_PATH); FileOutputStream fout = null; DataOutputStream dout = null; try { if (recFile.exists()) { recFile.delete(); } recFile.createNewFile(); fout = new FileOutputStream(recFile); dout = new DataOutputStream(fout); short buf[] = new short[bufSize]; int cnt = 0; long ls = 0L; long lrs = 0L; Log.d(TAG, "******* start"); while (isRecording) { audioRec.read(buf, 0, buf.length); for (short s : buf) { short rs = Short.reverseBytes(s); ls += Math.abs(s); lrs += Math.abs(rs); dout.writeShort(rs); if (++cnt >= 10000) { Log.d(TAG, "******* " + ls + " " + lrs); cnt = 0; ls = 0; lrs = 0; } } } audioRec.stop(); } catch (IOException e) { Log.e(TAG, e.toString()); } finally { try { dout.close(); fout.close(); } catch (IOException e) { Log.e(TAG, e.toString()); } } Log.d(INF, "ファイル書き込み 終了"); Log.d(TAG, "end recording"); } }; }; // Juliusを別スレッドで実行 private class JuliusRecognizer extends AsyncTask<String, Void, Void> { private ProgressDialog progressDialog; Context context; public JuliusRecognizer(Context context) { this.context = context; } @Override protected void onPreExecute() { Log.d(TAG, "JuliusRecognizer:onPreExecute"); progressDialog = new ProgressDialog(context); progressDialog.setMessage(JuliusActivity.this.getString(R.string.recognizing_message));//音声認識処理中 progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.show(); // プログレスダイアログを表示する } @Override protected Void doInBackground(String... params) { String wavepath = params[0]; Log.d(INF, "recognize() 開始"); recognize(wavepath); Log.d(INF, "recognize() 終了"); return null; } @Override protected void onPostExecute(Void result) { Log.d(TAG, "JuliusRecognizer:onPostExecute"); progressDialog.dismiss(); // プログレスダイアログを閉じる TextView resultView = (TextView) findViewById(R.id.result_text); resultView.setText(resultStr); button.setText(R.string.speech); button.setEnabled(true); } } //ここで認証結果を取得 public void callback(byte[] result) { Log.d(TAG, "callbacked"); StringBuilder bld = new StringBuilder(); for (byte b : result) { bld.append(String.format("%02x ", b)); } Log.d(TAG, "result:" + bld.toString()); try { resultStr = new String(result, "Shift_JIS");//dictation-kitの場合はUTF-8 } catch (UnsupportedEncodingException e) { Log.e(TAG, e.toString()); } Log.d(TAG, "callbacked " + resultStr); } } |
実行画面
「Speech」ボタンを押すと「録音モード」になります.
その状態で何か話した後に「Recording」ボタンを押下すると、一旦wavファイルとして保存したのちにその音声ファイルを使って認証を開始します.
試しに「りんごが1個」と言ってみました.

続いて「連続音声認識」を選択し、「こんにちは」と言ってみました.

juliusのバージョンが4.2.2ということもあるのか認識がとにかく悪いです.
悪すぎます…
終わりに
音響モデルを変え、Julius4.5のlibjulius,libsentを共有ライブラリ(.so)として作ることができれば改善するかもしれませんが、今度は共有ライブラリの作成に骨が折れそうです…
とりあえずはAndroidでJuliusが動かすことが今回の目的だったので、次は共有ライブラリの作成目指してぼちぼちとまた調べていこうかと思います.
-
前の記事
[Raspberry pi]Pythonで機械学習備忘録(3)〜実装 2020.10.02
-
次の記事
Raspberry Pi4にUbuntu 20.04(Mate)を入れようとしたけど結局UbuntuMateを入れた話 2020.10.19