我一直在使用Firebase ML Kit在我的应用程序中做一些图像标记。一切正常,但是今天更新了gradle依赖关系后,我不能再使用诸如FirebaseVisionLabelDetectorOptions
和FirebaseVisionCloudLabel
之类的类。它们以红色突出显示,当我将鼠标悬停时,会看到以下消息:
未解决的参考:FirebaseVisionLabelDetectorOptions
这是我的代码:
class ImageActivity : AppCompatActivity() {
// ... omited code for brevity
private fun labelImages(image: FirebaseVisionImage) {
val detector = FirebaseVision.getInstance()
.visionLabelDetector
val result = detector.detectInImage(image)
.addOnSuccessListener { labels ->
for (label in labels) {
val text = label.label
val entityId = label.entityId
val confidence = label.confidence
}
}
}
private fun labelImagesCloud(image: FirebaseVisionImage) {
val options = FirebaseVisionCloudDetectorOptions.Builder()
.setModelType(FirebaseVisionCloudDetectorOptions.LATEST_MODEL)
.setMaxResults(30)
.build()
val detector = FirebaseVision.getInstance()
.visionCloudLabelDetector
val result = detector.detectInImage(image)
.addOnSuccessListener(
object : OnSuccessListener<List<FirebaseVisionCloudLabel>> {
override fun onSuccess(labels: List<FirebaseVisionCloudLabel>) {
for (label in labels) {
val text = label.label
val entityId = label.entityId
val confidence = label.confidence
}
}
})
}
}
这是我对build.gradle文件的依赖:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.firebase:firebase-core:16.0.6'
implementation 'com.google.firebase:firebase-ml-vision:19.0.0'
implementation 'com.google.firebase:firebase-ml-vision-image-label-model:17.0.2'
}
我想念什么吗?
答案 0 :(得分:2)
Android Studio找不到这些类的原因是因为它们已在ML Kit SDK的19.0.0版本中重命名,正如您在更新后的Documentation上看到的那样。
以下是更改列表:
课程
FirebaseVisionLabelDetectorOptions
-> FirebaseVisionOnDeviceImageLabelerOptions
FirebaseVisionCloudDetectorOptions
-> FirebaseVisionCloudImageLabelerOptions
FirebaseVisionLabelDetector
-> FirebaseVisionImageLabeler
FirebaseVisionCloudLabelDetector
-> FirebaseVisionImageLabeler
FirebaseVisionLabel
-> FirebaseVisionImageLabel
FirebaseVisionCloudLabel
-> FirebaseVisionImageLabel
方法
getVisionCloudLabelDetector()
-> getCloudImageLabeler()
getVisionLabelDetector()
-> getOnDeviceImageLabeler()
getLabel()
-> getText()
detectInImage()
-> processImage()
所以基本上您的代码将变为:
class ImageActivity : AppCompatActivity() {
// ... omited code for brevity
private fun labelImages(image: FirebaseVisionImage) {
val options = FirebaseVisionOnDeviceImageLabelerOptions.Builder()
.setConfidenceThreshold(0.8f)
.build()
val detector = FirebaseVision.getInstance()
.onDeviceImageLabeler
val result = detector.processImage(image)
.addOnSuccessListener { labels ->
for (label in labels) {
val text = label.text
val entityId = label.entityId
val confidence = label.confidence
}
}
}
private fun labelImagesCloud(image: FirebaseVisionImage) {
val options = FirebaseVisionCloudImageLabelerOptions.Builder()
.setModelType(FirebaseVisionCloudImageLabelerOptions.LATEST_MODEL)
.setMaxResults(30)
.build()
val detector = FirebaseVision.getInstance()
.cloudImageLabeler
val result = detector.processImage(image)
.addOnSuccessListener(
object : OnSuccessListener<List<FirebaseVisionImageLabel>> {
override fun onSuccess(labels: List<FirebaseVisionImageLabel>) {
for (label in labels) {
val text = label.text
val entityId = label.entityId
val confidence = label.confidence
}
}
})
}
}