Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require "onnxruntime"
require "mini_magick"
require "numo/narray"
img = MiniMagick::Image.open("images/32576677167_a066c5a7aa_z.jpg")
img.resize "448x448^", "-gravity", "center", "-extent", "448x448"
pixels = Numo::NArray[*img.get_pixels]
pixels = pixels.transpose(2, 0, 1)
pixels = pixels.expand_dims(0)
model = OnnxRuntime::Model.new("rain_princess.onnx")
result = model.predict(input1: pixels.to_a)
out_pixels = Numo::NArray[*result["output1"].first]
out_pixels = out_pixels.clip(0, 255)
out_pixels = out_pixels.transpose(1, 2, 0).cast_to(Numo::UInt8)
img = MiniMagick::Image.import_pixels(out_pixels.to_binary, img.width, img.height, 8, "rgb", "jpg")
img.write("output.jpg")
@kojix2

This comment has been minimized.

Copy link

@kojix2 kojix2 commented Sep 12, 2019

The following line is easy to understand,

blob = out_pixels.to_a.flatten.pack("C*")

But you can also write like this.

blog = out_pixels.to_string
blog = out_pixels.to_binary

That's the good part of Numo::UInt8.

@ankane

This comment has been minimized.

Copy link
Owner Author

@ankane ankane commented Sep 12, 2019

Awesome, much more readable - didn't know that existed. Will update the post and gist. Thanks @kojix2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment