LoginSignup
0
0

More than 3 years have passed since last update.

Amazon Rekognitionでローカルファイルの画像からテキストを検出してみた

Posted at

やったこと

  • Amazon Rekognitionでローカルファイルの画像からテキストを検出
  • rubyで叩いた

ここで書かないこと

  • rubyで叩くようにするまでの準備(アカウント作る〜SDKインストール)

ソースと画像を用意

画像

こんなのを用意

text.png
text.png

ソース

基本的に公式ドキュメントに叩き方の例はのっているのでそこを参考にした
(リージョンは~/.aws/configに設定)

get_text.rb

require 'aws-sdk-rekognition'

credentials = Aws::Credentials.new(
   'xxxxxxxxxxxxx',
   'xxxxxxxxxxxxxx'
)
client   = Aws::Rekognition::Client.new credentials: credentials
photo = 'text.png'
path = File.expand_path(photo)
file = File.read(path)

attrs = {
  image: {
    bytes: file
  },
}
response = client.detect_text attrs

response.text_detections.select! { |text| text.type == 'LINE' }
response.text_detections.each do |text|
  p text.detected_text
end

実行結果

$ ruby get_text.rb
"ABCDE"
"12345"
"EGHT]"
"6789"

取れたー

補足

text_detectionsには検出した行と単語を入れてくれるらしく、今回の画像だと行として"ABCDE"が入っているのと同時に単語として"ABCDE"が入っていた。
(selectでLINEだけにしているのはそのため)

なので英語の文章の画像を用意してみる
word.png

レスポンス表示を少し変えて

response = client.detect_text attrs

response.text_detections.each do |text|
  p "type:#{text.type} text:#{text.detected_text}"
end

実行してみるとこんな感じ

$ ruby get_text.rb
"type:LINE text:Hello World!"
"type:LINE text:This is a pen"
"type:WORD text:Hello"
"type:WORD text:World!"
"type:WORD text:This"
"type:WORD text:is"
"type:WORD text:a"
"type:WORD text:pen"

お手軽ですごい!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0