用于TFLite的Android Camera X图像分析仪图像格式

edqdpe6u  于 2023-01-11  发布在  Android
关注(0)|答案(2)|浏览(110)

我尝试使用CameraX API分析带有tflite模型的相机预览帧。
This documentation描述了如何使用ImageAnalyzer来处理传入帧。目前帧是以YUV格式传入的,我不知道如何将YUV图像数据传递给一个需要输入形状的tflite模型(BATCHxWIDTHxHEIGHTx3)。在旧的API中,您可以指定预览输出格式并将其更改为rgb,但此页面明确指出“CameraX以YUV_420_888格式生成图像”。
首先,我希望有人找到了一种方法来传递RGB到分析器,而不是YUV,其次,如果没有,有人能建议一种方法来传递YUV图像到TFLite解释器吗?传入的图像对象是ImageProxy类型,它有3个平面,Y,U和V。

e0bqpujr

e0bqpujr1#

AFAIK,ImageAnalysis用例仅提供YUV_420_888格式的图像(您可以看到它定义为here)。
官方的CameraX文档提供了一种将YUV图像转换为RGB位图的方法,它位于本节的底部。
有关显示如何将Media.Image对象从YUV_420_888格式转换为RGB Bitmap对象的示例代码,请参见YuvToRgbConverter.kt

cunj1qz1

cunj1qz12#

ImageAnalysis用例现在提供YUV_420_888以及RGBA_8888格式的图像,TFLite解释器支持这两种格式。

    • 用法:**
val imageAnalysis = ImageAnalysis.Builder()
            .setTargetAspectRatio(AspectRatio.RATIO_16_9)
            .setTargetRotation(viewFinder.display.rotation)
            .setBackpressureStrategy(ImageAnalysis.STRATEGY_BLOCK_PRODUCER)
            .setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_RGBA_8888)
            .build()

imageAnalysis.setAnalyzer(executor, ImageAnalysis.Analyzer { image ->
            if (!::bitmapBuffer.isInitialized) {
                // The image rotation and RGB image buffer are initialized only once
                // the analyzer has started running
                imageRotationDegrees = image.imageInfo.rotationDegrees
                bitmapBuffer = Bitmap.createBitmap(
                    image.width, image.height, Bitmap.Config.ARGB_8888)
            }

            // Copy out RGB bits to our shared buffer
            image.use { bitmapBuffer.copyPixelsFromBuffer(image.planes[0].buffer)  }
            image.close()

val imageProcessor =
        ImageProcessor.Builder()
            .add(Rot90Op(-frame.imageRotationDegrees / 90))
            .build()

    // Preprocess the image and convert it into a TensorImage for detection.
    val tensorImage = imageProcessor.process(TensorImage.fromBitmap(frame.bitmapBuffer))
    
val results = objectDetector?.detect(tensorImage)
}

查看官方示例应用了解更多详情:https://github.com/tensorflow/examples/blob/master/lite/examples/object_detection/android_play_services/app/src/main/java/org/tensorflow/lite/examples/objectdetection/fragments/CameraFragment.kt

相关问题