Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The function 'getRed' isn't defined. Try importing the library that defines 'getRed', correcting the name to the name of an existing function, or defining a function named #249

Open
itstabrez opened this issue Sep 10, 2024 · 7 comments

Comments

@itstabrez
Copy link

itstabrez commented Sep 10, 2024

 Float32List _imageToByteListFloat32(imglib.Image image) {
 var convertedBytes = Float32List(1 * 112 * 112 * 3);
 var buffer = Float32List.view(convertedBytes.buffer);
 int pixelIndex = 0;
for (var i = 0; i < 112; i++) {
  for (var j = 0; j < 112; j++) {
    var pixel = image.getPixel(j, i);
    buffer[pixelIndex++] = (imglib.getRed(pixel) - 128) / 128;
    buffer[pixelIndex++] = (imglib.getGreen(pixel) - 128) / 128;
    buffer[pixelIndex++] = (imglib.getBlue(pixel) - 128) / 128;
  }
}
return convertedBytes.buffer.asFloat32List();

}

-------------Above is the function to convert image to byte list Float32

Float32List imageAsList = _imageToByteListFloat32(img);

---------------- This is how i am calling the above function..

In the below version of image the code is working well
---image: 3.1.3
but after updating the image version to
---image: ^4.2.0

the imglib.getRed , imglib.getGreen , imglib.getBlue property is not working

how to solve this issue and get the same functionality as above code is serving thanks in advance ...

@itstabrez
Copy link
Author

  Float32List _imageToByteListFloat32(imglib.Image image) {
   var convertedBytes = Float32List(1 * 112 * 112 * 3);
   var buffer = Float32List.view(convertedBytes.buffer);
    int pixelIndex = 0;

for (var i = 0; i < 112; i++) {
  for (var j = 0; j < 112; j++) {
    int pixel = image.getPixel(j, i) as int;
    // Extract RGBA components from pixel value
    int r = (pixel >> 24) & 0xFF;
    int g = (pixel >> 16) & 0xFF;
    int b = (pixel >> 8) & 0xFF;

    // Normalize and store pixel values
    buffer[pixelIndex++] = (r - 128) / 128.0;
    buffer[pixelIndex++] = (g - 128) / 128.0;
    buffer[pixelIndex++] = (b - 128) / 128.0;
  }
}
return convertedBytes.buffer.asFloat32List();

}

solved by replacing the code as above

@PaulTR
Copy link
Collaborator

PaulTR commented Sep 10, 2024

Going to leave this open since it should be tracked to update in a PR. Thanks for bringing it up!

@PaulTR PaulTR reopened this Sep 10, 2024
@chris-netizen
Copy link

  Float32List _imageToByteListFloat32(imglib.Image image) {
   var convertedBytes = Float32List(1 * 112 * 112 * 3);
   var buffer = Float32List.view(convertedBytes.buffer);
    int pixelIndex = 0;

for (var i = 0; i < 112; i++) {
  for (var j = 0; j < 112; j++) {
    int pixel = image.getPixel(j, i) as int;
    // Extract RGBA components from pixel value
    int r = (pixel >> 24) & 0xFF;
    int g = (pixel >> 16) & 0xFF;
    int b = (pixel >> 8) & 0xFF;

    // Normalize and store pixel values
    buffer[pixelIndex++] = (r - 128) / 128.0;
    buffer[pixelIndex++] = (g - 128) / 128.0;
    buffer[pixelIndex++] = (b - 128) / 128.0;
  }
}
return convertedBytes.buffer.asFloat32List();

}

solved by replacing the code as above

I am getting the error "Error running model: type 'PixelUint8' is not a subtype of type 'int' in type cast" when I use this approach

@itstabrez
Copy link
Author

itstabrez commented Oct 30, 2024

  Float32List _imageToByteListFloat32(imglib.Image image) {
   var convertedBytes = Float32List(1 * 112 * 112 * 3);
   var buffer = Float32List.view(convertedBytes.buffer);
    int pixelIndex = 0;

for (var i = 0; i < 112; i++) {
  for (var j = 0; j < 112; j++) {
    int pixel = image.getPixel(j, i) as int;
    // Extract RGBA components from pixel value
    int r = (pixel >> 24) & 0xFF;
    int g = (pixel >> 16) & 0xFF;
    int b = (pixel >> 8) & 0xFF;

    // Normalize and store pixel values
    buffer[pixelIndex++] = (r - 128) / 128.0;
    buffer[pixelIndex++] = (g - 128) / 128.0;
    buffer[pixelIndex++] = (b - 128) / 128.0;
  }
}
return convertedBytes.buffer.asFloat32List();

}
solved by replacing the code as above

I am getting the error "Error running model: type 'PixelUint8' is not a subtype of type 'int' in type cast" when I use this approach

List<dynamic> imageToArray(img.Image inputImage) {
 img.Image resizedImage = img.copyResize(inputImage, width: WIDTH, height: HEIGHT);
List<double> flattenedList = resizedImage.data!.expand((channel) => [channel.r, channel.g, channel.b]).map((value) => value.toDouble()).toList();
     Float32List float32Array = Float32List.fromList(flattenedList);
     int channels = 3;
     int height = HEIGHT;
     int width = WIDTH;
     Float32List reshapedArray = Float32List(1 * height * width * channels);
     for (int c = 0; c < channels; c++) {
      for (int h = 0; h < height; h++) {
        for (int w = 0; w < width; w++) {
          int index = c * height * width + h * width + w;
          reshapedArray[index] =
              (float32Array[c * height * width + h * width + w] - 127.5) /127.5;
         }
       }
     }
     return reshapedArray.reshape([1, 168, 168, 3]);
   }

Try using this function. I also initialized the HEIGHT and WIDTH variables with 168, which my model supports—please adjust yours accordingly.

@chris-netizen
Copy link

  Float32List _imageToByteListFloat32(imglib.Image image) {
   var convertedBytes = Float32List(1 * 112 * 112 * 3);
   var buffer = Float32List.view(convertedBytes.buffer);
    int pixelIndex = 0;

for (var i = 0; i < 112; i++) {
  for (var j = 0; j < 112; j++) {
    int pixel = image.getPixel(j, i) as int;
    // Extract RGBA components from pixel value
    int r = (pixel >> 24) & 0xFF;
    int g = (pixel >> 16) & 0xFF;
    int b = (pixel >> 8) & 0xFF;

    // Normalize and store pixel values
    buffer[pixelIndex++] = (r - 128) / 128.0;
    buffer[pixelIndex++] = (g - 128) / 128.0;
    buffer[pixelIndex++] = (b - 128) / 128.0;
  }
}
return convertedBytes.buffer.asFloat32List();

}
solved by replacing the code as above

I am getting the error "Error running model: type 'PixelUint8' is not a subtype of type 'int' in type cast" when I use this approach

List<dynamic> imageToArray(img.Image inputImage) {
 img.Image resizedImage = img.copyResize(inputImage, width: WIDTH, height: HEIGHT);
List<double> flattenedList = resizedImage.data!.expand((channel) => [channel.r, channel.g, channel.b]).map((value) => value.toDouble()).toList();
     Float32List float32Array = Float32List.fromList(flattenedList);
     int channels = 3;
     int height = HEIGHT;
     int width = WIDTH;
     Float32List reshapedArray = Float32List(1 * height * width * channels);
     for (int c = 0; c < channels; c++) {
      for (int h = 0; h < height; h++) {
        for (int w = 0; w < width; w++) {
          int index = c * height * width + h * width + w;
          reshapedArray[index] =
              (float32Array[c * height * width + h * width + w] - 127.5) /127.5;
         }
       }
     }
     return reshapedArray.reshape([1, 168, 168, 3]);
   }

Try using this function. I also initialized the HEIGHT and WIDTH variables with 168, which my model supports—please adjust yours accordingly.

Thank you very much for this!
I was able to fix the issue and finalised what I am working on as well

@itstabrez
Copy link
Author

I am getting the error "Error running model: type 'PixelUint8' is not a subtype of type 'int' in type cast" when I use this approach

List<dynamic> imageToArray(img.Image inputImage) {
 img.Image resizedImage = img.copyResize(inputImage, width: WIDTH, height: HEIGHT);
List<double> flattenedList = resizedImage.data!.expand((channel) => [channel.r, channel.g, channel.b]).map((value) => value.toDouble()).toList();
     Float32List float32Array = Float32List.fromList(flattenedList);
     int channels = 3;
     int height = HEIGHT;
     int width = WIDTH;
     Float32List reshapedArray = Float32List(1 * height * width * channels);
     for (int c = 0; c < channels; c++) {
      for (int h = 0; h < height; h++) {
        for (int w = 0; w < width; w++) {
          int index = c * height * width + h * width + w;
          reshapedArray[index] =
              (float32Array[c * height * width + h * width + w] - 127.5) /127.5;
         }
       }
     }
     return reshapedArray.reshape([1, 168, 168, 3]);
   }

Try using this function. I also initialized the HEIGHT and WIDTH variables with 168, which my model supports—please adjust yours accordingly.

Thank you very much for this! I was able to fix the issue and finalised what I am working on as well

Happy to help you ! 👍

@suhailthakrani
Copy link

suhailthakrani commented Nov 15, 2024

Float32List _imageToByteListFloat32(imglib.Image image) {
 var convertedBytes = Float32List(1 * 112 * 112 * 3);
 var buffer = Float32List.view(convertedBytes.buffer);
 int pixelIndex = 0;
for (var i = 0; i < 112; i++) {
  for (var j = 0; j < 112; j++) {
    var pixel = image.getPixel(j, i);
    var red = pixel.r; ``
    var green = pixel.g;
    var blue = pixel.b;
    buffer[pixelIndex++] = red - 128) / 128;
    buffer[pixelIndex++] = green  - 128) / 128;
    buffer[pixelIndex++] = blue  - 128) / 128;
  }
}
return convertedBytes.buffer.asFloat32List();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants