Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Unexpected output image on inference

$
0
0

I'm trying to upscale image in flutter. But i'm getting unexpected result.I'm totally newbie on working with inferencing model.Orginal Image:Orginal Image

Output Image:Output Image

LOG: I/flutter ( 592): Image converted to normalized Float32ListI/flutter ( 592): Image normalized successfully.I/flutter ( 592): Input tensor created successfully.I/flutter ( 592): Height 2880 Width 1640

Orginal Image: Height 720 Width 210

Here is the code i tried:

  Future<void> inference() async {    if (selectedImage == null) {      debugPrint('No image selected');      return;    }    var preprocessedImage =    NormalizeImage.imageToNormalizedFloat32List(selectedImage!);    final shape = [1, 3, selectedImage!.height, selectedImage!.width];    debugPrint('Image normalized successfully.');    final inputOrt =    OrtValueTensor.createTensorWithDataList(preprocessedImage, shape);    final inputs = {'input': inputOrt};    debugPrint('Input tensor created successfully.');    final runOptions = OrtRunOptions();    final outputs = await ortSession.runAsync(runOptions, inputs);    inputOrt.release();    runOptions.release();    outputs?.forEach((element) {      final outputValue = element?.value;      if (outputValue is List<List<List<List<double>>>>) {        img.Image generatedImage = generateImageFromOutput(outputValue);        List<int> pngBytes = img.encodePng(generatedImage);        img.Image decodedImage = img.decodeImage(Uint8List.fromList(pngBytes))!;        showDialog(          context: context,          builder: (BuildContext context) {            return Dialog(              child: SizedBox(                width: 200,                height: 200,                child: Image.memory(Uint8List.fromList(img.encodePng(decodedImage))),              ),            );          },        );      } else {        debugPrint("Output is of unknown type");      }      element?.release();    });  }  img.Image generateImageFromOutput(List<List<List<List<double>>>> output) {    final width = output[0][0].length;    final height = output[0][0][0].length;    debugPrint("Height $height Width $width");    Float32List float32Data = flattenList(output);    var imgData = NormalizeImage.denormalizedFloat32ListToImage(float32Data, width, height);    return imgData;  }  Float32List flattenList(List<List<List<List<double>>>> nestedList) {    List<double> flattened = [];    for (var fourDimList in nestedList) {      for (var threeDimList in fourDimList) {        for (var twoDimList in threeDimList) {          for (var value in twoDimList) {            flattened.add(value);          }        }      }    }    return Float32List.fromList(flattened);  }

Normalize Class

class NormalizeImage {  static Float32List imageToNormalizedFloat32List(Image image) {    final int height = image.height;    final int width = image.width;    Float32List float32Image = Float32List(3 * height * width);    for (int i = 0; i < height; i++) {      for (int j = 0; j < width; j++) {        final int pixelIndex = (i * width + j) * 3;        final int pixel = image.getPixel(j, i);        float32Image[pixelIndex] = getRed(pixel) / 255.0;        float32Image[pixelIndex + 1] = getGreen(pixel) / 255.0;        float32Image[pixelIndex + 2] = getBlue(pixel) / 255.0;      }    }    print("Image converted to normalized Float32List");    return float32Image;  }  static Image denormalizedFloat32ListToImage(Float32List float32Image, int width, int height) {    final imgData = Image(width, height);    for (int i = 0; i < height; i++) {      for (int j = 0; j < width; j++) {        final int pixelIndex = (i * width + j) * 3;        final int r = (float32Image[pixelIndex] * 255).toInt().clamp(0, 255);        final int g = (float32Image[pixelIndex + 1] * 255).toInt().clamp(0, 255);        final int b = (float32Image[pixelIndex + 2] * 255).toInt().clamp(0, 255);        final int color = getColor(r, g, b);        imgData.setPixel(j, i, color);      }    }    return imgData;  }}

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>