Let's use Amazon Textract with Ruby

For those who want to use Amazon Textract, here is some code that can be executed.

There wasn't much Ruby sample code that used Amazon Textract in the world, so I wrote it. It's not useful code, but I think it can be used as a reference for those who want to use Amazon Textract in Ruby for the time being.

Notes

--Amazon Textract is a paid service. If you just touch it, I think it will fit in the free frame, but please be careful not to overdo it. --As of January 2, 2021, Amazon Textract cannot read Japanese.

Install the required gems

Create a Client class instance for Amazon Textract

We have designated Northern Virginia as the region because it is not yet available in the Tokyo region (ap-northeast-1). If this is omitted, the default region will be applied, and if the default is ap-northeast-1, an error will occur and it cannot be used.

client = Aws::Textract::Client.new(region: 'us-east-1')

See here for AWS credentials.

Read local file

Read the text by referring to the Amazon Textract Client Class API Reference.

The API reference also describes how to handle files on the S3 bucket, but since the information about bytes was thin, I wrote about bytes in this article.

detected = client.detect_document_text(document: { bytes: URI.open('sample.png').read })

reference

--API Reference for Amazon Textract Client Class -How to set AWS authentication information to refer to programmatically with AWS CLI

Recommended Posts

Let's use Amazon Textract with Ruby
Use Ruby with Google Colab
Let's make draw poker with ruby ~ Preparation ~
Let's make a smart home with Ruby!
Let's use jcmd
Let's make draw poker with ruby ~ test-unit preparation ~
Use Amazon ECR Credential Helper with Docker Desktop
Use ProGuard with Gradle
Use Puphpeteer with Docker
Use XVim2 with Xcode 12.0.1
Use CentOS with LXD
Getting Started with Ruby
Use ngrok with Docker
Use webmock with Rspec
Let's scrape with Java! !!
Let's make a LINE Bot with Ruby + Sinatra --Part 1
Use WebJars with Gradle
[Swift] Let's use extension
11th, Classification with Ruby
Use jlink with gradle
Evolve Eevee with Ruby
Use Coveralls with GitHub Actions in a Ruby repository
Use Lambda Layers with Java
Use GDAL with Python with Docker
Use Thymeleaf with Azure Functions
Let's experiment with Java inlining
Ruby version switching with rbenv
Solve Google problems with Ruby
I tried DI with Ruby
GraphQL Client starting with Ruby
Use pfx certificate with Okhttp3
How to use Ruby return
[Swift] Let's use Segmented Control
[RSpec] Let's use FactoryBot [Rails]
[Form_with] Let's unify form with form_with.
Let's operate Excel with Java! !!
Ruby: Send email with Starttls
Use Bulk API with RestHighLevelClient
Use C program from Ruby
Use Face API from Ruby
Format Ruby with VS Code
Integer check method with ruby
Use ruby variables in javascript.
Let's use Swift Firebase Firebase Auth
Use SDKMAN! With Git Bash
Use multiple databases with Rails 6.0
Rbenv command to use Ruby
Ruby: How to use cookies
Ruby Learning # 8 Working With String
Use Spring JDBC with Spring Boot
[Ruby] problem with if statement
Studying with CodeWar (ruby) ⑤ proc
Use SpatiaLite with Java / JDBC
Use log4j2 with YAML + Gradle
[Ruby] REPL-driven development with pry
Getting Started with Ruby Modules
[Docker] Use whenever with Docker + Rails
[ruby] Method call with argument
[Linux: Ruby] Use the driver with selenium without specifying the browser driver path