![](/img/trans.png)
[英]Creating a Bitmap ByteBuffer for quantized Tensorflow Lite Model
[英]Get ByteBuffer from Image for TensorFlow Lite Model
我正在创建一个 android 应用程序以在执行实时人脸识别的 Google Glass Enterprise Edition 2 上运行。 我使用相机 X 作为我的相机 API 和 TensorFlow Lite (TFLite) 作为我的分类 model。 但是,TFLite model 输入需要 ByteBuffer,我无法从从 CameraX 检索到的图像转换成它。
如何为我的 TFLite Model 将来自 CameraX 的图像获取到 ByteBuffer class 中?
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 示例代码
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()
尝试使用TensorImage
Lite Support Library中的 TensorImage class。
大致而言,您可以按照以下步骤操作。
Image
object 转换为Bitmap
。 应该有其他关于如何做到这一点的 Stackoverflow 问题(例如, 这个答案)TensorImage.fromBitmap()
工厂从Bitmap
object 创建一个TensorImage
object。TensorImage
object 上调用getBuffer()
方法以获取底层ByteBuffer
。 您可能还需要进行一些图像预处理,以防来自 CameraX 的图像与 model 预期的格式不完全匹配。 为此,您可以探索ImageProcessor
实用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.