Last active
September 12, 2019 01:09
-
-
Save ankane/33ffa59ea0f5add37edee04fa7aacd9e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
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
The following line is easy to understand,
But you can also write like this.
That's the good part of Numo::UInt8.