brendan-duncan / image

Dart Image Library for opening, manipulating, and saving various different image file formats.
MIT License
1.17k stars 265 forks source link

#621 latest image lib not work in face recognition #637

Closed ronak-cipl closed 5 months ago

ronak-cipl commented 5 months ago

issue continue #621

not work,

I am using face recognition with tensorflowlite model

working file in image: ^3.3.0 version but not work in latest version image: ^4.1.7

brendan-duncan commented 5 months ago

Your bug report is not detailed enough for me to help you. I offered multiple suggestions around the difference between 3. and 4. versions of the library. I don't have anything to offer beyond that, unless you provide more details.

ronak-cipl commented 5 months ago

// this function is work on old lib image: ^3.3.0 version // but not work on new lib image: ^4.1.7 imglib.Image _convertCameraImage(CameraImage image, CameraLensDirection dir) { int width = image.width; int height = image.height; var img = imglib.Image(width: width, height: height); // Create Image buffer final int uvyButtonStride = image.planes[1].bytesPerRow; final int uvPixelStride = image.planes[1].bytesPerPixel!; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { final int uvIndex = uvPixelStride (x / 2).floor() + uvyButtonStride (y / 2).floor(); final int index = y width + x; final yp = image.planes[0].bytes[index]; final up = image.planes[1].bytes[uvIndex]; final vp = image.planes[2].bytes[uvIndex]; // Calculate pixel color int r = (yp + vp 1436 / 1024 - 179).round().clamp(0, 255); int g = (yp - up 46549 / 131072 + 44 - vp 93604 / 131072 + 91) .round() .clamp(0, 255); int b = (yp + up * 1814 / 1024 - 227).round().clamp(0, 255); // color: 0x FF FF FF FF // A B G R img.data[index] = 0xFF000000 | (b << 16) | (g << 8) | r; } } var img1 = (dir == CameraLensDirection.front) ? imglib.copyRotate(img, angle: -90) : imglib.copyRotate(img, angle: 90); return img1; }

// this function is work on old lib image: ^3.3.0 version // but not work on new lib image: ^4.1.7 Float32List imageToByteListFloat32( imglib.Image image, int inputSize, double mean, double std) { var convertedBytes = Float32List(1 inputSize inputSize * 3); var buffer = Float32List.view(convertedBytes.buffer); int pixelIndex = 0; for (var i = 0; i < inputSize; i++) { for (var j = 0; j < inputSize; j++) { var pixel = image.getPixel(j, i); buffer[pixelIndex++] = (imglib.getRed(pixel) - mean) / std; buffer[pixelIndex++] = (imglib.getGreen(pixel) - mean) / std; buffer[pixelIndex++] = (imglib.getBlue(pixel) - mean) / std; } } return convertedBytes.buffer.asFloat32List(); }

ronak-cipl commented 5 months ago

621 apply this solution but not working

plz help and support

brendan-duncan commented 5 months ago
// this function is work on old lib image: ^3.3.0 version
// but not work on new lib image: ^4.1.7
imglib.Image _convertCameraImage(CameraImage image, CameraLensDirection dir) {
  int width = image.width;
  int height = image.height;
  final img = imglib.Image(width: width, height: height); // Create Image buffer
  final int uvyButtonStride = image.planes[1].bytesPerRow;
  final int uvPixelStride = image.planes[1].bytesPerPixel!;
  // Use a pixel iterator to iterate over each pixel in the image
  for (final p in img) {
    final int uvIndex = uvPixelStride * (x / 2).floor() + uvyButtonStride * (y / 2).floor();
    // Pixel has x and y properties.
    final int index = p.y * width + p.x;
    final yp = image.planes[0].bytes[index];
    final up = image.planes[1].bytes[uvIndex];
    final vp = image.planes[2].bytes[uvIndex];
    // Calculate pixel color
    // Pixel has setters for each channel
    p.r = (yp + vp * 1436 / 1024 - 179).round().clamp(0, 255);
    p.g = (yp - up * 46549 / 131072 + 44 - vp * 93604 / 131072 + 91).round().clamp(0, 255);
    p.b = (yp + up * 1814 / 1024 - 227).round().clamp(0, 255);
  }

  var img1 = (dir == CameraLensDirection.front)
    ? imglib.copyRotate(img, angle: -90)
    : imglib.copyRotate(img, angle: 90);
  return img1;
}

// this function is work on old lib image: ^3.3.0 version
// but not work on new lib image: ^4.1.7
Float32List imageToByteListFloat32(imglib.Image image, int inputSize, double mean, double std) {
  var convertedBytes = Float32List(1 * inputSize * inputSize * 3);
  var buffer = Float32List.view(convertedBytes.buffer);
  int pixelIndex = 0;
  for (var i = 0; i < inputSize; i++) {
    for (var j = 0; j < inputSize; j++) {
      final pixel = image.getPixel(j, i); // returns a Pixel object
      buffer[pixelIndex++] = (pixel.r - mean) / std; // Pixel object has a getter for each channel
      buffer[pixelIndex++] = (pixel.g - mean) / std;
      buffer[pixelIndex++] = (pixel.b - mean) / std;
    }
  }
  return convertedBytes.buffer.asFloat32List();
}
ronak-cipl commented 5 months ago

this code is not work

dixitdashstack commented 5 months ago

Yes, I have the same problem. apply your solution, but it is not working.

brendan-duncan commented 5 months ago

I'm sorry, I can't write your program for you. This is the information I have to offer you. I can't run your program, perhaps there's a typo or something, but that's the gist of it and the rest is on you.

ronak-cipl commented 5 months ago

yes, but there is a big change in your lib, so how to handle it.

brendan-duncan commented 5 months ago

Good luck with that. The question has been answered.