Created
September 16, 2019 19:40
-
-
Save ankane/3bb4ddbf84edd7f05a24cd3697ccd9a7 to your computer and use it in GitHub Desktop.
This file contains 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
# 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