我有客户的要求,我们需要在 cameraView 中检测我们的产品框。为此,我正在尝试使用 firebase ML Kit 对象检测。现在它可以检测门、沙发等一般物体。我希望能够检测到我的产品。有没有办法做到这一点?下面是我目前的实现
class DetectActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detect)
cameraView.setLifecycleOwner(this)
cameraView.addFrameProcessor {
extractDataFromFrame(it) { result ->
tvDetectedItem.text = result
}
}
}
private fun extractDataFromFrame(frame: Frame, callback: (String) -> Unit) {
val options = FirebaseVisionObjectDetectorOptions.Builder()
.setDetectorMode(FirebaseVisionObjectDetectorOptions.STREAM_MODE)
.enableMultipleObjects()
.enableClassification() // Optional
.build()
val objectDetector = FirebaseVision.getInstance().getOnDeviceObjectDetector(options)
objectDetector.processImage(getVisionImageFromFrame(frame))
.addOnSuccessListener {
var result = ""
it.forEach { item ->
result += "${item.entityId}\n" //TODO : Get the knowledge graph result for this entity
Log.e("TAG",item.classificationCategory.toString())
}
callback(result)
}
.addOnFailureListener {
callback("Unable to detect an object")
}
.addOnCompleteListener {
}
}
private fun getVisionImageFromFrame(frame : Frame) : FirebaseVisionImage{
//ByteArray for the captured frame
val data = frame.data
//Metadata that gives more information on the image that is to be converted to FirebaseVisionImage
val imageMetaData = FirebaseVisionImageMetadata.Builder()
.setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
.setRotation(FirebaseVisionImageMetadata.ROTATION_90)
.setHeight(frame.size.height)
.setWidth(frame.size.width)
.build()
val image = FirebaseVisionImage.fromByteArray(data, imageMetaData)
return image
}
}
我也希望它完全脱机。为此,有设备 ML 套件实现。但是我该如何使用它呢?对不起,我的英语不好。