【问题标题】:Camerax Analyzer ImageProxy - Image Already ClosedCamerax Analyzer ImageProxy - 图像已关闭
【发布时间】:2020-09-30 10:02:32
【问题描述】:

我正在使用 CameraX (Beta) 并使用带有执行程序的 Analyzer 来分析图像并在其上绘制边界/叠加层。根据文档,我需要关闭图像代理才能继续将图像放入分析功能。当我添加 imageProxy.close() 调用时,它失败并出现 Image Already closed 错误。我在这里想念什么? 分析器代码:

       private val successListener = OnSuccessListener<List<DetectedObject>> { papers ->
        isAnalyzing.set(false)

        val  rectPoints= mutableListOf<Rect>()
        Log.d(TAG," overlayRef Info: ${overlayRef.get()}")

        for (paper in papers) {
            val bounds = paper.boundingBox
            val paperId = paper.trackingId
            rectPoints+=bounds


            Log.d(TAG, "Successful Paper Analysis - Bounds of the paper: $bounds")
            Log.d(TAG," Labels found on the paper: ${paper.labels}")

        }
        Log.d(TAG, "Invoking pointsRectListener for : $rectPoints")
        pointsRectListener?.invoke(rectPoints)

    }
    private val failureListener = OnFailureListener { e ->
        isAnalyzing.set(false)
        Log.e(TAG, "Paper analysis failure.", e)
    }

    @SuppressLint("UnsafeExperimentalUsageError")
    override fun analyze(imageProxy: ImageProxy) {
        val mediaImage = imageProxy?.image ?: return
        Log.d(TAG,"entered analysis..analysis in progress?$isAnalyzing.get()")


        if (!isAnalyzing.get()){
            isAnalyzing.set(true)
            Log.d(TAG,"No other analysis in progress..so starting analysis now")
            val currentTimestamp = System.currentTimeMillis()
            if (currentTimestamp - lastAnalyzedTimestamp >= TimeUnit.SECONDS.toMillis(1)) {
                currentTimestamp - lastAnalyzedTimestamp
                    analysisSizeListener?.invoke(Size(imageProxy.width, imageProxy.height))

                    val image = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)

                    objectDetector.process(image)
                            .addOnSuccessListener(successListener)
                            .addOnFailureListener(failureListener)

            }

        }
        imageProxy.close()


    }

我在其中实例化并绑定到生命周期的代码

        paperAnalyzer=ImageAnalysis.Builder()
                .setTargetAspectRatio(screenAspectRatio)
                .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
                .setTargetRotation(rotation)
                .build()
                .also {
                    it.setAnalyzer(cameraExecutor, PaperAnalyzer( WeakReference(overlayView)).apply {
                        pointsRectListener = { rectPoints ->
                            overlayView.rectPoints = rectPoints
                        }
                        analysisSizeListener = {
                            updateOverlayTransform(overlayView, it)
                        }
                    }


                      )

                }

        cameraProvider.unbindAll()

        try {
            camera = cameraProvider.bindToLifecycle(
                    this, cameraSelector, preview, imageCapture, paperAnalyzer)

            // Attach the viewfinder's surface provider to preview use case
            preview?.setSurfaceProvider(viewFinder.createSurfaceProvider())
        } catch (exc: Exception) {
            Log.e(TAG, "Use case binding failed", exc)
        }

【问题讨论】:

    标签: kotlin firebase-mlkit android-camerax


    【解决方案1】:

    您尝试完成的逻辑看起来像这样。

    fun analyze(imageProxy) {
        if (isAnalyzing) {
            imageProxy.close() // 1
        } else {
            val image = imageInput.fromMediaImage(...)
            objectDetector.process(image)
                        .addOnSuccessListener(OnSuccessListener { result ->
                            // Do something with the result
                            imageProxy.close() // 2
                            isAnalyzing.set(false)
                        })
                        .addOnFailureListener(OnFailureListener { exception ->
                            // Do something with the exception
                            imageProxy.close() // 3
                            isAnalyzing.set(false)
                        })
        }
    }
    

    我可能错了,但看起来您正在执行 1,而不是 2 和 3。您能否更新您的代码以遵循此模式,看看您是否仍然遇到问题。

    【讨论】:

    • 这确实解决了图像关闭的问题,我现在也得到了连续的图像流 - 谢谢。看起来这样做内联与创建​​侦听器方法和调用是有区别的。我的观察是,如果你不内联,ImageProxy 会自动关闭。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多