diff --git a/app/build.gradle b/app/build.gradle index 08dbc25..faa7db6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,7 +8,7 @@ android { defaultConfig { applicationId "it.awt.mycroft.deepspeech" - minSdkVersion 26 + minSdkVersion 24 targetSdkVersion 29 versionCode 1 versionName "1.0" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 910df78..7bab31d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,8 +4,6 @@ - - { return suspendCoroutine { cont -> @@ -54,16 +50,15 @@ class SpeechRecognitionUseCase { voiceListener?.onError(errorType, error) } } - val modelPath = - ctx.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)?.absoluteFile?.toPath() - ?.resolve(MODEL_PATH)?.toString(); + + val modelPath = File(ctx.filesDir, MODEL_PATH).absolutePath val builder: SpeechServiceSettings.Builder = SpeechServiceSettings.Builder() .withLanguage(LANGUAGE) .withStoreSamples(true) .withStoreTranscriptions(true) .withProductTag("product-tag") .withUseDeepSpeech(true) // If using DeepSpeech - .withModelPath(modelPath!!) // If using DeepSpeech + .withModelPath(modelPath) // If using DeepSpeech if (ModelUtils.isReady(modelPath)) { // The model is already downloaded and unzipped @@ -79,32 +74,32 @@ class SpeechRecognitionUseCase { private suspend fun copyModel(ctx: Context) { return withContext(Dispatchers.IO) { - val modelPath = ctx.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)?.absoluteFile?.toPath()?.resolve(MODEL_PATH) - Files.createDirectories(modelPath) - ctx.assets.open("model/0.8.2/output_graph.tflite").use { - val path = modelPath?.resolve("output_graph.tflite") - Log.i(TAG, "copying file to: "+path?.toString()) - Files.copy(it, path, StandardCopyOption.REPLACE_EXISTING); - } - ctx.assets.open("model/0.8.2/alphabet.txt").use { - val path = modelPath?.resolve("alphabet.txt") - Log.i(TAG, "copying file to: "+path?.toString()) - Files.createDirectories(path?.parent) - Files.copy(it, path, StandardCopyOption.REPLACE_EXISTING); - } - ctx.assets.open("model/0.8.2/scorer").use { - val path = modelPath?.resolve("scorer") - Log.i(TAG, "copying file to: "+path?.toString()) - Files.createDirectories(path?.parent) - Files.copy(it, path, StandardCopyOption.REPLACE_EXISTING); - } - ctx.assets.open("model/0.8.2/info.json").use { - val path = modelPath?.resolve("info.json") - Log.i(TAG, "copying file to: "+path?.toString()) - Files.createDirectories(path?.parent) - Files.copy(it, path, StandardCopyOption.REPLACE_EXISTING); + val modelPath = File(ctx.filesDir, MODEL_PATH) + modelPath.mkdirs() + + copy(ctx, modelPath, "output_graph.tflite") + copy(ctx, modelPath, "alphabet.txt") + copy(ctx, modelPath, "scorer") + copy(ctx, modelPath, "info.json") + } + } + + @Throws(IOException::class) + private fun copy(ctx: Context, modelPath: File, filename: String) { + ctx.assets.open("model/0.8.2/$filename").use { it -> + FileOutputStream(File(modelPath, filename)).use { output -> + val buffer = ByteArray(1024) + var length: Int + while (it.read(buffer).also { length = it } > 0) { + output.write(buffer, 0, length) + } } } + } + companion object { + private const val TAG = "SpeechRecognitionUseCase" + private const val MODEL_PATH = "deepspeech/models/it/" + private const val LANGUAGE = "it-it" } }