繁体   English   中英

如何在android studio中实现ML模型的代码并从中获取输出?

[英]How to implement ML model's code in android studio and get output from it?

我正在尝试通过使用 tfLite 模型来检测标志识别。 我已经导入了 tfLite 模型并传递了位图,但是当我运行应用程序时,什么也没有发生。 为了找出问题所在,我添加了 5 个吐司,但只有 1 个吐司成功运行。 我没有弄错,也不知道如何处理输出,至少在吐司中显示。 代码如下:

private void processImage() {
    imageConverter.run();
    rgbFrameBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);
    rgbFrameBitmap.setPixels(rgbBytes, 0, previewWidth, 0, 0, previewWidth, previewHeight);
    //Do your work here


    try {
        Model model = Model.newInstance(getApplicationContext());

        Toast.makeText(this, "Toast-1", Toast.LENGTH_SHORT).show();
        // Creates inputs for reference.
        TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 1, 1, 3}, DataType.UINT8);

        TensorImage tensorImage = new TensorImage(DataType.UINT8);
        tensorImage.load(rgbFrameBitmap);
        ByteBuffer byteBuffer = tensorImage.getBuffer();
        inputFeature0.loadBuffer(byteBuffer);

        Toast.makeText(this, "Toast-2", Toast.LENGTH_SHORT).show();
        // Runs model inference and gets result.
        Model.Outputs outputs = model.process(inputFeature0);
        TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();
        TensorBuffer outputFeature1 = outputs.getOutputFeature1AsTensorBuffer();
        TensorBuffer outputFeature2 = outputs.getOutputFeature2AsTensorBuffer();
        TensorBuffer outputFeature3 = outputs.getOutputFeature3AsTensorBuffer();
        TensorBuffer outputFeature4 = outputs.getOutputFeature4AsTensorBuffer();
        TensorBuffer outputFeature5 = outputs.getOutputFeature5AsTensorBuffer();
        TensorBuffer outputFeature6 = outputs.getOutputFeature6AsTensorBuffer();
        TensorBuffer outputFeature7 = outputs.getOutputFeature7AsTensorBuffer();

        
        Toast.makeText(this, outputFeature0.toString(), Toast.LENGTH_LONG).show();
        
        Toast.makeText(this, "Toast-3 Model Run Successfully", Toast.LENGTH_SHORT).show();
        // Releases model resources if no longer used.
        model.close();
    } catch (IOException e) {
        // TODO Handle the exception
        Toast.makeText(this, "Toast-4 Scan Failed,Again Trying..", Toast.LENGTH_SHORT).show();
    }

    Toast.makeText(this, "Toast-5 Again startinng", Toast.LENGTH_SHORT).show();
    postInferenceCallback.run();
}

请检查此线程上的答案How to pass image to tflite model in android 他在代码中使用了 tensorflow-lite 包中的ImageProcessor对象。 我想这对你会有帮助。 如果它不起作用或您有任何问题,请回复。 祝你好运。

暂无
暂无

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

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