简体   繁体   English

Android TFLite 的 Camera X ImageAnalyzer 图像格式

[英]Android Camera X ImageAnalyzer Image Format for TFLite

I am attempting to analyze camera preview frames with a tflite model, using the CameraX api.我正在尝试使用 CameraX api 分析带有 tflite model 的相机预览帧。

This documentation describes using the ImageAnalyzer to process incoming frames.本文档描述了使用 ImageAnalyzer 处理传入的帧。 Currently the frames are incoming as YUV, and I'm not sure how to pass YUV image data to a tflite model thats expecting an input of the shape (BATCHxWIDTHxHEIGHTx3).目前,帧作为 YUV 传入,我不确定如何将 YUV 图像数据传递给 tflite model,它期望输入形状 (BATCHxWIDTHxHEIGHTx3)。 In the old APIs you could specify preview output formats and change it to rgb, however this page specifically says "CameraX produces images in YUV_420_888 format."在旧的 API 中,您可以指定预览 output 格式并将其更改为 rgb,但是此页面特别指出“CameraX 以 YUV_420_888 格式生成图像。”

First I'm hoping someone has found a way to pass RGB to the Analyzer rather than YUV, and secondly if not, could someone suggest a way of passing a YUV image to a TFLite interpreter?首先,我希望有人找到了一种将 RGB 传递给分析器而不是 YUV 的方法,其次,如果没有,有人可以建议一种将 YUV 图像传递给 TFLite 解释器的方法吗? The incoming image object is of the type ImageProxy and it has 3 planes, Y, U, and V.输入图像 object 是 ImageProxy 类型,它有 3 个平面,Y、U 和 V。

AFAIK, the ImageAnalysis use case only provides images in the YUV_420_888 format (You can see it defined here ). AFAIK, ImageAnalysis用例仅提供 YUV_420_888 格式的图像(您可以在此处看到它的定义)。

The official CameraX documentation provides a way to convert YUV images to RGB bitmaps, it's at the bottom of this section . CameraX 官方文档提供了一种将 YUV 图像转换为 RGB 位图的方法,它位于本节的底部。

For sample code that shows how to convert a Media.Image object from YUV_420_888 format to an RGB Bitmap object, see YuvToRgbConverter.kt .有关显示如何将 Media.Image object 从 YUV_420_888 格式转换为 RGB Bitmap object 的示例代码,请参阅YuvToRgbConverter

For anyone having this problem now.对于现在有这个问题的任何人。 ImageAnalysis use cases now provides images in YUV_420_888 as well as RGBA_8888 which is supported in TFLite interpreter. ImageAnalysis 用例现在提供 YUV_420_888 和 TFLite 解释器支持的RGBA_8888格式的图像。

Usage:用法:

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)
}

Check official sample app for more details: 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查看官方示例应用程序了解更多详情: https://github.com/tensorflow/examples/blob/master/lite/examples/object_detection/android_play_services/app/src/main/java/org/tensorflow/lite/examples/objectdetection/片段/CameraFragment.kt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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