Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
require "onnxruntime"
require "mini_magick"
img ="ranger.jpg")
img.crop "100x100+60+20", "-gravity", "center"
img.resize "64x64^", "-gravity", "center", "-extent", "64x64"
img.colorspace "Gray"
# all pixels are the same for grayscale, so just get one of them
pixels = img.get_pixels.flat_map { |r| }
pixels = OnnxRuntime::Utils.reshape(pixels, [1, 1, 64, 64])
model ="model.onnx")
result = model.predict("Input3" => pixels)
def softmax(x)
exp = { |v| Math.exp(v - x.max) } { |v| v / exp.sum }
probabilities = softmax(result["Plus692_Output_0"].first)
emotion_labels = [
"neutral", "happiness", "surprise", "sadness",
"anger", "disgust", "fear", "contempt"
pp { |_, v| -v }.to_h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment