[GO] [Rails] How to detect radical images by analyzing images using Cloud Vision API

Target

If you edit the user's profile image with refile Image analysis of the uploaded image with Cloud Vision API In the case of a radical image, it cannot be saved in the DB.

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

Premise

The following has been implemented.

-Login function implementationImplementation of posting function

Enable Cloud Vision API

1. Activate the Cloud Vision API by following the same procedure as "Enable Geocoding API" in the following article.

How to calculate latitude / longitude using Geolocation API

2. Go to the home screen and click ʻIAM and Manage`

スクリーンショット 2020-06-08 9.47.58.png

3. Click Service Account

スクリーンショット 2020-06-08 9.48.31.png

4. Click Add Service Account

スクリーンショット 2020-06-08 9.48.56.png

5. Enter the service account name (appropriate) and click Create

スクリーンショット 2020-06-08 9.49.18.png

6. Click Continue

スクリーンショット 2020-06-08 9.49.50.png

7. Click Create Key

スクリーンショット 2020-06-08 9.55.48.png

8. Set the key type to JSON and click Create

スクリーンショット 2020-06-08 9.50.13.png

9. Confirm that the key has been created and click Close

スクリーンショット 2020-06-08 9.55.29.png

10. Click Done

スクリーンショット 2020-06-08 9.55.55.png

11. Move the key directly under the application

スクリーンショット 2020-06-08 10.10.52.png

About the evaluation of Cloud Vision API

Evaluation category

Category Contents
adult Whether it is an adult image
spoof Whether it's an internet meme
medical Whether it's an internet meme
violence Whether it is a violent image
racy Whether it's a racy image

Evaluation value

Evaluation value Degree
UNKNOWN Cannot be analyzed
VERY_UNLIKELY Very low
UNLIKELY Low
POSSIBLE potential
LIKELY high
VERY_LIKELY Very expensive

Implementation

1. Introduce Gem

Regardless of the evaluation category, the evaluation value is LIKELY or VERY_LIKELY If it is included, we will implement it so that it will not be saved in the DB.

Gemfile


gem 'google-cloud-vision'

Terminal


$ bundle

2. Create and edit the Cloud Vision API configuration file

Terminal


$ touch lib/vision.rb

lib/vision.rb


require 'base64'
require 'json'
require 'net/https'
module Vision
  class << self
    def image_analysis(profile_image)
      image_annotator = Google::Cloud::Vision::ImageAnnotator.new(
          version: :v1,
          credentials: JSON.parse(File.open('Key file name') do |f| f.read end)
      )

      #Request parameter creation
      image = profile_image #Image you want to analyze(argument)
      requests_content = { image: { content: image }, features: [{ type: :SAFE_SEARCH_DETECTION }] }
      requests =   [requests_content]

      #Send images to Cloud Vision API
      response = image_annotator.batch_annotate_images(requests)
      result = response.responses[0].safe_search_annotation.to_h

      #The analysis result is "LIKELY" or "VERY"_Returns false if "LIKELY" is included, true otherwise
      if result.values.include?(:LIKELY) || result.values.include?(:VERY_LIKELY)
        return false
      else
        return true
      end
    end
  end
end

3. Edit controller

users_controller.rb


def update
  #When the image is edited
  if params[:user][:profile_image].present?
    #parameter(image)As a "tempfile" and assign it to a variable
    profile_image = File.open(params[:user][:profile_image].tempfile)
    #Image analysis with Cloud Vision API and assign the analysis result to a variable
    result = Vision.image_analysis(profile_image)
  else
    #Substitute "true" if the image has not been edited
    result = true
  end
  #Conditional branching depending on the analysis result
  if result == true
    @user.update(user_params)
    redirect_to user_user_path(@user)
  elsif result == false
    flash[:notice] = 'Image is inappropriate'
    render 'edit'
  end
end

Recommended Posts

[Rails] How to detect radical images by analyzing images using Cloud Vision API
How to use GCP's Cloud Vision API
Detect Japanese characters from images using Google's Cloud Vision API in Python
[Rails] How to get location information using Geolocation API
Get data labels by linking with Google Cloud Vision API when previewing images with Rails
I tried to automatically collect erotic images from Twitter using GCP's Cloud Vision API
Try to determine food photos using Google Cloud Vision API
When introducing the Google Cloud Vision API to rails, I followed the documentation.
How to plot multiple fits images side by side in galactic coordinates using python
I tried using the Google Cloud Vision API
How to use the Google Cloud Translation API
How to get article data using Qiita API
How to analyze with Google Colaboratory using Kaggle API
Cloud Functions to resize images using OpenCV with Cloud Storage triggers
How to reset password via API using Django rest framework
How to get followers and followers from python using the Mastodon API
django + nginx How to make images viewed only by logged-in users
How to not run some tests by default using pytest's mark