简体   繁体   中英

Why are these log statements not printing?

I'm building an object detection application (in Kotlin, for Android). The application uses CameraX to build a camera preview and Google ML to provide machine learning expertise. Just for reference; I used this CameraX documentation and this this Google ML Kit documentation.

I'm currently attempting to print Log.d("TAG", "onSuccess" + it.size) to my IDE console in order to determine if .addonSuccessListener is actually running. If it does, it should print something along the lines of onSuccess1 . However, this isn't the case. Infact, it isn't even printing the Log statement from the .addOnFailureListener either, which really confuses me as I'm not even entirely sure the objectDetector code is even running. What really puzzles me is that I have relatively completed the same project in Java and have not faced this issue.

I did have someone point out that within my YourImageAnalyzer.kt class, if mediaImage is null, then I won't see anything logging. However, upon my own debugging (this is actually my very first time debugging), I was unable to find out if my first sentence of this paragraph is true or not. I suppose this issue may provide a lead into how I'll resolve this issue, and also learn how to properly debug.

Here is my YourImageAnalyzer.kt class, and I will also add the code for my MainActivity.kt class below as well.

YourImageAnalyzer.kt

private class YourImageAnalyzer : ImageAnalysis.Analyzer {

    override fun analyze(imageProxy: ImageProxy) {
        val mediaImage = imageProxy.image
        if (mediaImage != null) {
            val image =
                    InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)

            val localModel = LocalModel.Builder()
                    .setAssetFilePath("mobilenet_v1_0.75_192_quantized_1_metadata_1.tflite")
                    .build()

            val customObjectDetectorOptions =
                    CustomObjectDetectorOptions.Builder(localModel)
                            .setDetectorMode(CustomObjectDetectorOptions.STREAM_MODE)
                            .enableClassification()
                            .setClassificationConfidenceThreshold(0.5f)
                            .setMaxPerObjectLabelCount(3)
                            .build()


            val objectDetector =
                    ObjectDetection.getClient(customObjectDetectorOptions)

            objectDetector           //Here is where the issue stems, with the following listeners
                    .process(image)
                    .addOnSuccessListener {
                        Log.i("TAG", "onSuccess" + it.size)
                        for (detectedObjects in it)
                        {
                            val boundingBox = detectedObjects.boundingBox
                            val trackingId = detectedObjects.trackingId
                            for (label in detectedObjects.labels) {
                                val text = label.text
                                val index = label.index
                                val confidence = label.confidence
                            }
                        }
                    }
                    .addOnFailureListener { e -> Log.e("TAG", e.getLocalizedMessage()) }
                    .addOnCompleteListener { it -> imageProxy.close() }
        }
    }
}

MainActivity.kt

class MainActivity : AppCompatActivity() {
    
    private lateinit var cameraProviderFuture: ListenableFuture<ProcessCameraProvider>
    override fun onCreate(savedInstanceState: Bundle?) {
        cameraProviderFuture = ProcessCameraProvider.getInstance(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        cameraProviderFuture.addListener(Runnable {
            val cameraProvider = cameraProviderFuture.get()
            bindPreview(cameraProvider)
        }, ContextCompat.getMainExecutor(this))

    }

    fun bindPreview(cameraProvider: ProcessCameraProvider) {
        val previewView = findViewById<PreviewView>(R.id.previewView)
        var preview : Preview = Preview.Builder()
                .build()

        var cameraSelector : CameraSelector = CameraSelector.Builder()
                .requireLensFacing(CameraSelector.LENS_FACING_BACK)
                .build()

        preview.setSurfaceProvider(previewView.surfaceProvider)

        var camera = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview)
    }

}

You are not binding your ImageAnalysis use case. Something in the line of:

val imageAnalysis = ImageAnalysis.Builder()
    .setTargetResolution(Size(1280, 720))
    .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
    .setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_RGBA_8888)
    .build()

and then;

imageAnalysis.setAnalyzer(executor, YourImageAnalyzer())

cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, imageAnalysis, preview)

Also a suggestion as a bonus: You should get your LocalModel.Builder() out of analyze as this is called each time an image arrives. You do not need to execute this code piece each time as it will make your analysis slower. So move this code:

val localModel = LocalModel.Builder()
                    .setAssetFilePath("mobilenet_v1_0.75_192_quantized_1_metadata_1.tflite")
                    .build()

to just below of the class private class YourImageAnalyzer : ImageAnalysis.Analyzer { .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM