You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Android Studio中使用TFLite处理大尺寸图片时遇越界错误

Fixing IndexOutOfBoundsException with TFLite Image Classification for Large Bitmaps

Hey there, let's work through this index out of bounds error you're facing when using TensorFlow for Poets 2's TFLite model with images larger than 224px. I’ve debugged similar issues before, so here’s the breakdown and step-by-step fix:

Why This Happens

The classifyFrame function (and its underlying preprocessing code) is built to expect a 224x224 input bitmap—that’s the fixed input size the TFLite model was trained on. When you pass a larger bitmap, the code tries to read pixel data into a pre-allocated array sized for 224x224 pixels. If the bitmap’s width/height exceeds 224, it’ll try to write beyond the array’s bounds, triggering the IndexOutOfBoundsException.

Step-by-Step Solution

1. Resize and Crop Your Bitmap to 224x224 First

You need to convert your camera-captured bitmap to the exact size the model expects. To avoid stretching the image (which would hurt classification accuracy), resize the bitmap so its shortest edge is 224px, then crop the center 224x224 area. Here’s the code to do that:

// Call this before passing your bitmap to classifyFrame
Bitmap preparedBitmap = prepareBitmapForModel(originalCameraBitmap, 224);

private Bitmap prepareBitmapForModel(Bitmap originalBitmap, int targetSize) {
    // Ensure we're working with ARGB_8888 format (matches most preprocessing logic)
    if (originalBitmap.getConfig() != Bitmap.Config.ARGB_8888) {
        originalBitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, false);
    }

    int width = originalBitmap.getWidth();
    int height = originalBitmap.getHeight();

    // Calculate scale to make the shortest edge equal to targetSize
    float scaleFactor = (float) targetSize / Math.min(width, height);
    int scaledWidth = Math.round(width * scaleFactor);
    int scaledHeight = Math.round(height * scaleFactor);

    // Scale the bitmap
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, scaledWidth, scaledHeight, true);

    // Calculate coordinates to crop the center 224x224 area
    int cropX = (scaledWidth - targetSize) / 2;
    int cropY = (scaledHeight - targetSize) / 2;

    // Crop and return the final 224x224 bitmap
    return Bitmap.createBitmap(scaledBitmap, cropX, cropY, targetSize, targetSize);
}

2. Verify Preprocessing Logic in classifyFrame

Double-check that your classifyFrame code is using the 224x224 bitmap correctly. For example, if you have code that reads pixels into a fixed-size array, it should now work because the input bitmap matches the array size:

// Example preprocessing snippet (ensure this uses the prepared 224x224 bitmap)
int[] intValues = new int[224 * 224];
float[] floatValues = new float[224 * 224 * 3];

// Now safe to call, since preparedBitmap is exactly 224x224
preparedBitmap.getPixels(intValues, 0, preparedBitmap.getWidth(), 0, 0, preparedBitmap.getWidth(), preparedBitmap.getHeight());

// Rest of your preprocessing (converting int pixels to float values for the model)
for (int i = 0; i < intValues.length; ++i) {
    final int val = intValues[i];
    floatValues[i * 3 + 0] = ((val >> 16) & 0xFF) / 255.0f;
    floatValues[i * 3 + 1] = ((val >> 8) & 0xFF) / 255.0f;
    floatValues[i * 3 + 2] = (val & 0xFF) / 255.0f;
}

3. Test with Different Image Sizes

After implementing the resize/crop step, test with images larger than 224px (like your camera shots) and smaller ones too—this should eliminate the index error while keeping classification accuracy consistent.

Key Takeaway

The TFLite model from TensorFlow for Poets 2 is hardcoded to accept 224x224 inputs. Skipping the resizing step breaks the preprocessing array bounds, which is exactly what’s causing your error. By standardizing the input size first, you’ll fix the exception and ensure the model gets the format it expects.

内容的提问来源于stack exchange,提问作者Eulices Nicot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:33:53