When introducing the Google Cloud Vision API to rails, I followed the documentation.

environment

・ Rails 6.0.3.3 ・ Ruby 2.7.1

Reference material

Google Cloud Vision API documentation 1、https://cloud.google.com/vision/docs/libraries?hl=ja

Installation procedure

1, gem installation

Described in gemfile

source "https://rubygems.org"
gem "google-cloud-vision"

After that,

bundle installl

* Officially, the method of "gem install google-cloud-vision" is recommended, but it didn't work with my app. </ font> Will come out later

require "google/cloud/vision"

An error occurred and I got stuck. (I ate a lot of time because of this)

2, authentication settings

In order to use the Google Cloud Vision API, you need to create a dedicated json file and load it into the target rails app. ・ Create json file ▶ ︎ Do not get stuck with the procedure according to the document. The completed file looks like this

{
  "type": "",
  "project_id": "",
  "private_key_id": "private_key_Contents of id",
  "private_key": "-----BEGIN PRIVATE KEY-----Contents\=\n-----END PRIVATE KEY-----\n",
  "client_email": "",
  "client_id": "",
  "auth_uri": "",
  "token_uri": "",
  "auth_provider_x509_cert_url": "",
  "client_x509_cert_url": ""
}

・ Load into rails app ▶ ︎.Enter in bash_profile and pass the path

export GOOGLE_APPLICATION_CREDENTIALS="$PATH:Write the path/file name.json"

3, actually use it.

routes.rb


get  "contents/index"  => "contents#index"

contents_controller.rb


class ContentsController < ApplicationController
  def index
  end
end

index.erb


<%=
#Load the gem you just installed
require "google/cloud/vision"

#Instantiation
image_annotator = Google::Cloud::Vision.image_annotator

#Enter the image path (local or online image is fine)
file_name = "./resources/cat.jpg "

#Return value returned after recognizing the image
response = image_annotator.label_detection image: file_name
response.responses.each do |res|
  puts "Labels:"
  res.label_annotations.each do |label|
    puts label.description
  end
end
%>

4, contents / index When redialing ...

スクリーンショット 2020-09-25 13.25.09.png

If you see a return value like this, you're successful! (Since I captured the image of a cat as a trial, "Cat" appears. Lol)

From here, you can get the information you want and format it so that you can use it for good! Thank you for reading !!

Recommended Posts

When introducing the Google Cloud Vision API to rails, I followed the documentation.
I tried the Google Cloud Vision API for the first time
I tried to extract characters from subtitles (OpenCV: Google Cloud Vision API)
How to use the Google Cloud Translation API
Get data labels by linking with Google Cloud Vision API when previewing images with Rails
Try to determine food photos using Google Cloud Vision API
Introducing Google Map API with rails
Until you try the Google Cloud Vision API (harmful image detection)
Use Google Cloud Vision API from Python
I tried to touch the COTOHA API
How to use GCP's Cloud Vision API
[Rails] How to detect radical images by analyzing images using Cloud Vision API
I tried "License OCR" with Google Vision API
When I tried using Microsoft's Computer Vision API, I recognized the Galapagos sign "Stop"
I tried to touch the API of ebay
I tried "Receipt OCR" with Google Vision API
I tried to automatically collect erotic images from Twitter using GCP's Cloud Vision API
I checked the Python package pre-installed in Google Cloud Dataflow
I wanted to skip certain extensions when building Sphinx documentation
Let's publish the super resolution API using Google Cloud Platform
I tried to get various information from the codeforces API
I touched the Qiita API
The record I was addicted to when putting MeCab on Heroku
[Linux] I want to know the date when the user logged in
I created a Python library to call the LINE WORKS API
What I was addicted to when introducing ALE to Vim for Python
When you want to keep the Sphinx documentation theme as usual
I want to run the Python GUI when starting Raspberry Pi
What to do when you get "I can't see the site !!!!"
Regularly upload files to Google Drive using the Google Drive API in Python
I compared while reading the documentation to use Jinja2 with Django
Python beginners hit the unofficial API of Google Play Music to play music
How to fix the shit heavy when reading Google Cloud Storage images from Django deployed on GAE