[RAILS] I tried using the Google Cloud Vision API

What is Google Cloud Vision?

Google Cloud Vision is an image analysis service provided by Google. This time, I used it to read the text in the image.

Step 1 Register with Google Cloud Platform

Register for Google Cloud Platform from the link below. A credit card is required for registration. https://console.cloud.google.com/getting-started?hl=ja

Enable Cloud Vision API

Enter "Cloud Vision API" in the search window at the top of the screen. スクリーンショット 2020-10-02 17.34.47.png

Enable the Cloud Vision API on the screen after the transition and you're done. スクリーンショット 2020-10-02 17.35.14.png

Create a service account

What is a service account? Quoted from here → https://cloud.google.com/iam/docs/service-accounts?hl=ja

A service account is a special account used by an application or virtual machine (VM) instance, not a user. The application uses the service account to make authorized API calls.

Now let's create a service account. Click the service account from "IAM and Management". スクリーンショット 2020-10-02 17.57.26.png

Click "Create Service Account" on the screen after the transition. スクリーンショット 2020-10-02 18.00.30.png

Enter an appropriate service account name and click "Create" スクリーンショット 2020-10-02 18.02.16.png

Click "Continue" スクリーンショット 2020-10-02 18.08.46.png

Click "Finish" スクリーンショット 2020-10-02 19.44.21.png

Next is the creation of a private key for authentication. After performing the above operation, I think that the screen below is displayed, so click "Operation" to create a key. qVGSMdCDldLGqek1601635670_1601635725.png

A dialog will appear where you can select JSON and create the key. Place the key in any folder and specify the key path in the environment variable (GOOGLE_APPLICATION_CREDENTIALS) described later. ti0oDmp7fe6mIxY1601636143_1601636179.png

Install gem.

gem 'google-cloud-vision'

Set the private key path to an environment variable and you're ready to go.

export GOOGLE_APPLICATION_CREDENTIALS="/hoge/fuga.json"

Implementation

With reference to the official documentation, it is as follows. ・ Official document https://cloud.google.com/vision/docs/libraries?hl=ja#client-libraries-usage-ruby

The biggest difference from the official example is that it uses text_detection instead of label_detection. You can use label_detection to detect what is in the image. For example, if you take a picture of the inside of a station, things like ticket gates, railroad tracks, and ticket vending machines will be detected. This time I used text_detection because I wanted to detect the characters in the image.

@image = Image.new

require "google/cloud/vision"
image_annotator = Google::Cloud::Vision.image_annotator

#Specify the image to read
file_name = "~/hoge.jpg "

# file_Analyze with Cloud Vision with name as an argument
response = image_annotator.text_detection image: file_name

response.responses.each do |res|
  @image.OCR = res.text_annotations[0].description
end

Summary

In addition to this example, the Google Cloud Vision API can also be used for analyzing PDF files and detecting faces in images. You can load up to 1000 items a month for free, so let's try various things and incorporate them into your own app!

Recommended Posts

I tried using the Google Cloud Vision API
I tried the Google Cloud Vision API for the first time
I tried using the checkio API
I tried using the BigQuery Storage API
I tried "License OCR" with Google Vision API
I tried "Receipt OCR" with Google Vision API
I tried to extract characters from subtitles (OpenCV: Google Cloud Vision API)
When introducing the Google Cloud Vision API to rails, I followed the documentation.
I tried using the API of the salmon data project
I tried the Naro novel API 2
When I tried using Microsoft's Computer Vision API, I recognized the Galapagos sign "Stop"
I tried the Naruro novel API
Try to determine food photos using Google Cloud Vision API
[Python] I tried collecting data using the API of wikipedia
Let's publish the super resolution API using Google Cloud Platform
[For beginners] I tried using the Tensorflow Object Detection API
I tried to automatically collect erotic images from Twitter using GCP's Cloud Vision API
I tried using Twitter api and Line api
Speech transcription procedure using Google Cloud Speech API
Use Google Cloud Vision API from Python
I tried using UnityCloudBuild API from Python
I tried to touch the COTOHA API
Until you try the Google Cloud Vision API (harmful image detection)
I tried using the COTOHA API (there is code on GitHub)
I tried using docomo speech recognition API and Google Speech API in Java
I tried using parameterized
I tried using argparse
I tried using AWS Rekognition's Detect Labels API
I tried using scrapy for the first time
I checked the library for using the Gracenote API
I tried using mimesis
I tried using Remote API on GAE / J
I tried hitting the Qiita API from go
I tried using anytree
vprof --I tried using the profiler for Python
I tried using aiomysql
I tried using Summpy
I tried using PyCaret at the fastest speed
I tried using coturn
I tried using Pipenv
I tried to touch the API of ebay
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
How to use the Google Cloud Translation API
I tried using openpyxl
I tried to summarize various sentences using the automatic summarization API "summpy"
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using the Datetime module by Python
I tried using ngrok
I tried using face_recognition
I tried using Jupyter
I tried using PyCaret
I tried using Heapq
I tried using doctest
[Google Cloud Platform] Use Google Cloud API using API Client Library
I tried using the image filter of OpenCV
I tried using folium