imageToTensor function
Implementation
Tensor imageToTensor(String path, int targetSize) {
// 1. Load and Resize
final bytes = File(path).readAsBytesSync();
final rawImage = img.decodeImage(bytes);
if (rawImage == null) throw "Could not decode image";
final resized = img.copyResize(
rawImage,
width: targetSize,
height: targetSize,
);
// 2. Convert to Float32 List (Normalized 0.0 to 1.0)
final floatData = Float32List(targetSize * targetSize * 3);
int i = 0;
for (var pixel in resized) {
floatData[i++] = pixel.r / 255.0;
floatData[i++] = pixel.g / 255.0;
floatData[i++] = pixel.b / 255.0;
}
// 3. Upload to GPU
// Shape: [1, pixels] or [numPatches, pixels] depending on your ViT setup
return Tensor.fromList([1, floatData.length], floatData);
}