Skip to content

Instantly share code, notes, and snippets.

@cmar
Last active May 4, 2018 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmar/2595028100753a557917707cca6d6628 to your computer and use it in GitHub Desktop.
Save cmar/2595028100753a557917707cca6d6628 to your computer and use it in GitHub Desktop.
Use AWS Rekognition to Translate binary code from screenshot
#!/usr/bin/env ruby
# You will need to setup your aws keys in your ENV
# AWS_SECRET_ACCESS_KEY
# AWS_ACCESS_KEY_ID
# and also enable the user to have access to Rekognition:detect_text in IAM
require 'aws-sdk'
# Screenshot from email https://s3.amazonaws.com/spdev/inkovate-binary-2018.jpg
# use AWS Rekognition to detect text on screenshot
client = Aws::Rekognition::Client.new
resp = client.detect_text(
image: {
s3_object: {
bucket: "spdev",
name: "inkovate-binary-2018.jpg"
},
}
)
# parse results to build array like
# ["01101000", "01110100", "01110100", "01110000", "01110011", "00111010", "00101111", ...
words = resp.text_detections.sort_by(&:id).select do |detection|
detection.type == "WORD" &&
detection.detected_text =~ /^[01 ]/
end.map &:detected_text
# translate "01101000" to a base(2) integer and then character
translated = words.map {|binary_char| binary_char.to_i(2).chr }
p translated.join('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment