【发布时间】:2020-05-11 11:23:43
【问题描述】:
我想制作一个应用程序,使用移动相机捕获图像并将此图像传递给 tensorflow lite 以对图像中的内容进行分类。
【问题讨论】:
标签: tensorflow-lite tensorflow-android
我想制作一个应用程序,使用移动相机捕获图像并将此图像传递给 tensorflow lite 以对图像中的内容进行分类。
【问题讨论】:
标签: tensorflow-lite tensorflow-android
您可以使用TensorImage(来自 org.tensorflow.lite.support.image)。它具有 TensorBuffer、Bitmap、int [] 或 float [] 的构造函数。
因此,假设您有一个名为myImage 的位图图像,同时在myContext 上下文中运行,那么您可以使用以下代码在myModel.tflite 模型上运行tflite 解释器:
// create tflite options (currently empty) and load the tf model
Interpreter.Options tfliteOptions = (new Interpreter.Options());
tfliteModel = FileUtil.loadMappedFile(myContext, "myModel.tflite");
// create tflite interpreter
tfliteInterpreter = new Interpreter(tfliteModel, tfliteOptions);
// get model parameters (index tensor is 0 for a single image)
DataType myImageDataType = tfliteInterpreter.getInputTensor(0).dataType();
myTensorImage = new TensorImage(myImageDataType);
// load bitmap
myTensorImage.load(myImage);
// run inference
tfliteInterpreter.run(myTensorImage.getBuffer(), output);
如果你的图片输入是以字节形式给出的,你可以使用TensorBuffer的ByteBuffer,其余的都一样。
请注意,我没有指定解释器output。
您也可以使用TensorBuffer作为输出,它可以很容易地为您提供ByteBuffer、int []或float []数组。
希望这会有所帮助:)
【讨论】:
如果你有经验,你可以按照@somethingorange 提到的方法。
如果您是初学者并想在移动设备上开发图像分类应用程序,请遵循以下方法。例如,您正在尝试开发一个模型来分类给定图像是Cat 还是Dogs。
Cats 和 Dogs 的图片)model.tflite)label.txt
model.tflite 和label.txt 移动到Android Studio 中的assets 文件夹中。TFLite 团队在this tutorial 中提到了上述所有步骤,这是一个很好的起点。
希望这些步骤对初学者有所帮助。
【讨论】: