Я пытаюсь протестировать Firebase ML kit Text recognition
, так что согласно официальному сайту
Я добавил classpath 'com.google.gms:google-services:4.3.2'
в приложение gradle и добавил следующие зависимости в модуле gradle:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-ml-vision:24.0.0'
implementation 'com.google.firebase:firebase-core:17.2.1'
...
}
также я добавил эти метаданные в файл манифеста:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name = "android.intent.action.MAIN" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.firebase.ml.vision.DEPENDENCIES"
android:value="text" />
</application>
В основной операции по назначению я запускаю приложение камеры, а после захвата изображения в onActivityResult
собираю данные о намерениях и преобразую в Растровое изображение я вызвал мой textRecognisation(photo)
метод:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == TEXT_RECO_REQ_CODE && resultCode == Activity.RESULT_OK) {
var photo:Bitmap = data?.extras?.get("data") as Bitmap
textRecognisation(photo)
}else if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "Operation cancelled by user", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this, "Failed to capture image", Toast.LENGTH_LONG).show()
}
}
Это textRecognisation
метод:
private fun textRecognisation(photo: Bitmap) {
val image = FirebaseVisionImage.fromBitmap(photo)
val detector = FirebaseVision.getInstance()
.onDeviceTextRecognizer
val result = detector.processImage(image)
.addOnSuccessListener { result ->
val resultText = result.text
}
.addOnFailureListener { e ->
Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()
}
}
Но я получил исключение в addOnFailureListener
обратном вызове:
com.google.firebase.ml.common.FirebaseMLException: ожидание загрузки модели распознавания текста. Пожалуйста, подождите.
Что происходит и что я могу сделать?
Заранее спасибо!