Scan and block sensitive photos and content in Ruby

Today we will be looking at a very simple method to use our Image Recognition API to automatically filter out NSFW content. Let’s begin by add API client to your Gemfile:

gem 'cloudmersive-image-recognition-api-client', '~> 1.3.2'

Now we simply call nsfw_classify with the following code:

# load the gem
require 'cloudmersive-image-recognition-api-client'
# setup authorization
CloudmersiveImageRecognitionApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
api_instance = CloudmersiveImageRecognitionApiClient::NsfwApi.newimage_file ="/path/to/file") # File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.begin
#Not safe for work (NSFW) racy content classification
result = api_instance.nsfw_classify(image_file)
p result
rescue CloudmersiveImageRecognitionApiClient::ApiError => e
puts "Exception when calling NsfwApi->nsfw_classify: #{e}"

All done. Our image will be given a confidence rating between 0.0 and 1.0, with anything above 8.0 being a high likelihood of inappropriate content. We will also receive a success/failure for the operation and a classification enum to help you identify what type of image it is.

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store