Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# https://github.com/onnx/models/tree/master/vision/body_analysis/emotion_ferplus
require "onnxruntime"
require "mini_magick"
img = MiniMagick::Image.open("ranger.jpg")
img.crop "100x100+60+20", "-gravity", "center"
img.resize "64x64^", "-gravity", "center", "-extent", "64x64"
img.colorspace "Gray"
img.write("resized.jpg")
# all pixels are the same for grayscale, so just get one of them
pixels = img.get_pixels.flat_map { |r| r.map(&:first) }
pixels = OnnxRuntime::Utils.reshape(pixels, [1, 1, 64, 64])
model = OnnxRuntime::Model.new("model.onnx")
result = model.predict("Input3" => pixels)
def softmax(x)
exp = x.map { |v| Math.exp(v - x.max) }
exp.map { |v| v / exp.sum }
end
probabilities = softmax(result["Plus692_Output_0"].first)
emotion_labels = [
"neutral", "happiness", "surprise", "sadness",
"anger", "disgust", "fear", "contempt"
]
pp emotion_labels.zip(probabilities).sort_by { |_, v| -v }.to_h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.