无法从 10368 字节的 TensorFlowLite 张量 (StatefulPartitionedCall:0) 复制到 324 字节的 Java 缓冲区

发布于 2025-01-20 03:42:05 字数 1730 浏览 4 评论 0原文

我正在尝试在 android studio 上制作一个手写文本识别 android 应用程序。我将模型转换为 TensorFlow Lite,然后将该文件上传到我的应用程序中。下面我分享相同的代码...代码中有很多日志语句。检查直到它被执行的地方。

    public void classifyImage(Bitmap image){
        try {

            Ocr mdl = Ocr.newInstance(getApplicationContext());
            TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 128, 32, 1}, DataType.FLOAT32);
            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4*128*32*1);

            int[] intValues = new int[128 * imageSize];
            image.getPixels(intValues, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
            Log.d("TAG Model", " get pixels ");
            int pixel = 0;

            for (int i=0; i<4096; i++){
                int val = intValues[pixel++];
                byteBuffer.putFloat(val);
            }
            Log.d("TAG Model", " bytebuffer putfloat com ");

            inputFeature0.loadBuffer(byteBuffer);

            Ocr.Outputs outputs = mdl.process(inputFeature0);
            Log.d("TAG Model", " Model process ");
            TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();
            Log.d("TAG Model", " Model output");

            float[] confidences = outputFeature0.getFloatArray();
            mdl.close();
        } catch (Exception e) {
            // TODO Handle the exception
            Log.d("TAG Model", "classifyImage: "+e.getMessage());
        }
    }

在运行我的应用程序时,我收到错误:D/TAG模型:classifyImage:无法从 10368 字节的 TensorFlowLite 张量 (StatefulPartitionedCall:0) 复制到 324 字节的 Java 缓冲区。

错误的屏幕截图

您能帮助我们吗这是因为我对这个错误感到震惊,无法进一步进行。

I'm trying to make a handwritten text recognition android application on android studio. I converted my model into tensorflow lite and then uploaded that file in my application. Below Im sharing the code for the same... There are many log statements in the code. to check till where it is getting executed.

    public void classifyImage(Bitmap image){
        try {

            Ocr mdl = Ocr.newInstance(getApplicationContext());
            TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 128, 32, 1}, DataType.FLOAT32);
            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4*128*32*1);

            int[] intValues = new int[128 * imageSize];
            image.getPixels(intValues, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
            Log.d("TAG Model", " get pixels ");
            int pixel = 0;

            for (int i=0; i<4096; i++){
                int val = intValues[pixel++];
                byteBuffer.putFloat(val);
            }
            Log.d("TAG Model", " bytebuffer putfloat com ");

            inputFeature0.loadBuffer(byteBuffer);

            Ocr.Outputs outputs = mdl.process(inputFeature0);
            Log.d("TAG Model", " Model process ");
            TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();
            Log.d("TAG Model", " Model output");

            float[] confidences = outputFeature0.getFloatArray();
            mdl.close();
        } catch (Exception e) {
            // TODO Handle the exception
            Log.d("TAG Model", "classifyImage: "+e.getMessage());
        }
    }

On running my application Im getting an error : D/TAG Model: classifyImage: Cannot copy from a TensorFlowLite tensor (StatefulPartitionedCall:0) with 10368 bytes to a Java Buffer with 324 bytes.

screenshot of the error

Can you please help we with this, as i'm struck on this error and cannot proceed further.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文