简体   繁体   English

从 TensorFlow Lite Model 的图像中获取 ByteBuffer

[英]Get ByteBuffer from Image for TensorFlow Lite Model

I am creating an android app to run on Google Glass Enterprise Edition 2 that does Real-time Face Recognition.我正在创建一个 android 应用程序以在执行实时人脸识别的 Google Glass Enterprise Edition 2 上运行。 I am using Camera X as my Camera API and TensorFlow Lite (TFLite) as my classification model.我使用相机 X 作为我的相机 API 和 TensorFlow Lite (TFLite) 作为我的分类 model。 However, the TFLite model input requires ByteBuffer which I am unable to convert into from the image retrieved from CameraX.但是,TFLite model 输入需要 ByteBuffer,我无法从从 CameraX 检索到的图像转换成它。

How do I get my Image from CameraX into ByteBuffer class for my TFLite Model?如何为我的 TFLite Model 将来自 CameraX 的图像获取到 ByteBuffer class 中?

Camera X Image Analysis: Reference Camera X 图像分析:参考

            val imageAnalysis = ImageAnalysis.Builder()
                    .setTargetResolution(Size(640, 360))
                    .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
                    .build()

            imageAnalysis.setAnalyzer(AsyncTask.THREAD_POOL_EXECUTOR, ImageAnalysis.Analyzer { imageProxy ->
                val rotationDegrees = imageProxy.imageInfo.rotationDegrees
                val mediaImage = imageProxy.image

                if (mediaImage != null) {
                    val image = InputImage.fromMediaImage(mediaImage, rotationDegrees)

                    /* Classify the Image using TensorFlow Lite Model */

                }

            })

TensorFlow Model Sample Code TensorFlow Model 示例代码

val model = FaceRecognitionModel.newInstance(context)

// Creates inputs for reference.
val inputFeature0 = TensorBuffer.createFixedSize(intArrayOf(1, 224, 224, 3), DataType.FLOAT32)
inputFeature0.loadBuffer(byteBuffer)

// Runs model inference and gets result.
val outputs = model.process(inputFeature0)
val outputFeature0 = outputs.outputFeature0AsTensorBuffer

// Releases model resources if no longer used.
model.close()

Try using the TensorImage class from the TensorFlow Lite Support Library .尝试使用TensorImage Lite Support Library中的 TensorImage class。

Roughly, you can follow these steps.大致而言,您可以按照以下步骤操作。

  1. Convert the Image object into Bitmap .Image object 转换为Bitmap There should be other Stackoverflow questions on how to do this (eg, this answer )应该有其他关于如何做到这一点的 Stackoverflow 问题(例如, 这个答案
  2. Create a TensorImage object from the Bitmap object using TensorImage.fromBitmap() factory.使用TensorImage.fromBitmap()工厂从Bitmap object 创建一个TensorImage object。
  3. Call getBuffer() method on the TensorImage object to get the underlying ByteBuffer .TensorImage object 上调用getBuffer()方法以获取底层ByteBuffer

You might also want to do some image pre-processing, in case the image from CameraX doesn't exactly match the format expected by the model.您可能还需要进行一些图像预处理,以防来自 CameraX 的图像与 model 预期的格式不完全匹配。 For this, you can explore the ImageProcessor utility .为此,您可以探索ImageProcessor实用程序

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

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