Detecting Safe Search Properties

Safe Search Detection detects explicit content such as adult content or violent content within an image. This feature uses five categories ("adult", "spoof", "medical", "violence", and "racy") and returns the likelihood that each is present in a given image. See the SafeSearchAnnotation page for details on these fields.

Running Safe Search Detection on a local image

Protocol

Refer to the images:annotate API endpoint for complete details.

To perform Safe Search Detection, make a POST request and provide the appropriate request body:

POST https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY
{
  "requests": [
    {
      "image": {
        "content": "/9j/7QBEUGhvdG9zaG9...base64-encoded-image-content...fXNWzvDEeYxxxzj/Coa6Bax//Z"
      },
      "features": [
        {
          "type": "SAFE_SEARCH_DETECTION"
        }
      ]
    }
  ]
}

See the AnnotateImageRequest reference documentation for more information on configuring the request body.

If the request is successful, the server returns a 200 OK HTTP status code and the response in JSON format:

{
  "responses": [
    {
      "safeSearchAnnotation": {
        "adult": "VERY_UNLIKELY",
        "spoof": "VERY_LIKELY",
        "medical": "UNLIKELY",
        "violence": "UNLIKELY",
        "racy": "VERY_UNLIKELY",
      }
    }
  ]
}

C#

Before trying this sample, follow the C# setup instructions in the Vision Quickstart Using Client Libraries . For more information, see the Vision C# API reference documentation .

// Load an image from a local file.
var image = Image.FromFile(filePath);
var client = ImageAnnotatorClient.Create();
var response = client.DetectSafeSearch(image);
Console.WriteLine("Adult: {0}", response.Adult.ToString());
Console.WriteLine("Spoof: {0}", response.Spoof.ToString());
Console.WriteLine("Medical: {0}", response.Medical.ToString());
Console.WriteLine("Violence: {0}", response.Violence.ToString());
Console.WriteLine("Racy: {0}", response.Racy.ToString());

Go

Before trying this sample, follow the Go setup instructions in the Vision Quickstart Using Client Libraries . For more information, see the Vision Go API reference documentation .

// detectSafeSearch gets image properties from the Vision API for an image at the given file path.
func detectSafeSearch(w io.Writer, file string) error {
	ctx := context.Background()

	client, err := vision.NewImageAnnotatorClient(ctx)
	if err != nil {
		return err
	}

	f, err := os.Open(file)
	if err != nil {
		return err
	}
	defer f.Close()

	image, err := vision.NewImageFromReader(f)
	if err != nil {
		return err
	}
	props, err := client.DetectSafeSearch(ctx, image, nil)
	if err != nil {
		return err
	}

	fmt.Fprintln(w, "Safe Search properties:")
	fmt.Fprintln(w, "Adult:", props.Adult)
	fmt.Fprintln(w, "Medical:", props.Medical)
	fmt.Fprintln(w, "Racy:", props.Racy)
	fmt.Fprintln(w, "Spoofed:", props.Spoof)
	fmt.Fprintln(w, "Violence:", props.Violence)

	return nil
}

Java

Before trying this sample, follow the Java setup instructions in the Vision Quickstart Using Client Libraries . For more information, see the Vision Java API reference documentation .

public static void detectSafeSearch(String filePath, PrintStream out) throws Exception,
    IOException {
  List<AnnotateImageRequest> requests = new ArrayList<>();

  ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));

  Image img = Image.newBuilder().setContent(imgBytes).build();
  Feature feat = Feature.newBuilder().setType(Type.SAFE_SEARCH_DETECTION).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
  requests.add(request);

  try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();

    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        out.printf("Error: %s\n", res.getError().getMessage());
        return;
      }

      // For full list of available annotations, see http://g.co/cloud/vision/docs
      SafeSearchAnnotation annotation = res.getSafeSearchAnnotation();
      out.printf(
          "adult: %s\nmedical: %s\nspoofed: %s\nviolence: %s\nracy: %s\n",
          annotation.getAdult(),
          annotation.getMedical(),
          annotation.getSpoof(),
          annotation.getViolence(),
          annotation.getRacy());
    }
  }
}

Node.js

Before trying this sample, follow the Node.js setup instructions in the Vision Quickstart Using Client Libraries . For more information, see the Vision Node.js API reference documentation .

const vision = require('@google-cloud/vision');

// Creates a client
const client = new vision.ImageAnnotatorClient();

/**
 * TODO(developer): Uncomment the following line before running the sample.
 */
// const fileName = 'Local image file, e.g. /path/to/image.png';

// Performs safe search detection on the local file
const [result] = await client.safeSearchDetection(fileName);
const detections = result.safeSearchAnnotation;
console.log('Safe search:');
console.log(`Adult: ${detections.adult}`);
console.log(`Medical: ${detections.medical}`);
console.log(`Spoof: ${detections.spoof}`);
console.log(`Violence: ${detections.violence}`);
console.log(`Racy: ${detections.racy}`);

PHP

Before trying this sample, follow the PHP setup instructions in the Vision Quickstart Using Client Libraries . For more information, see the Vision PHP API reference documentation .

namespace Google\Cloud\Samples\Vision;

use Google\Cloud\Vision\V1\ImageAnnotatorClient;

// $path = 'path/to/your/image.jpg'

function detect_safe_search($path)
{
    $imageAnnotator = new ImageAnnotatorClient();

    # annotate the image
    $image = file_get_contents($path);
    $response = $imageAnnotator->safeSearchDetection($image);
    $safe = $response->getSafeSearchAnnotation();

    $adult = $safe->getAdult();
    $medical = $safe->getMedical();
    $spoof = $safe->getSpoof();
    $violence = $safe->getViolence();
    $racy = $safe->getRacy();

    # names of likelihood from google.cloud.vision.enums
    $likelihoodName = ['UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY',
    'POSSIBLE','LIKELY', 'VERY_LIKELY'];

    printf("Adult: %s" . PHP_EOL, $likelihoodName[$adult]);
    printf("Medical: %s" . PHP_EOL, $likelihoodName[$medical]);
    printf("Spoof: %s" . PHP_EOL, $likelihoodName[$spoof]);
    printf("Violence: %s" . PHP_EOL, $likelihoodName[$violence]);
    printf("Racy: %s" . PHP_EOL, $likelihoodName[$racy]);

    $imageAnnotator->close();
}

Python

Before trying this sample, follow the Python setup instructions in the Vision Quickstart Using Client Libraries . For more information, see the Vision Python API reference documentation .

def detect_safe_search(path):
    """Detects unsafe features in the file."""
    from google.cloud import vision
    client = vision.ImageAnnotatorClient()

    with io.open(path, 'rb') as image_file:
        content = image_file.read()

    image = vision.types.Image(content=content)

    response = client.safe_search_detection(image=image)
    safe = response.safe_search_annotation

    # Names of likelihood from google.cloud.vision.enums
    likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
                       'LIKELY', 'VERY_LIKELY')
    print('Safe search:')

    print('adult: {}'.format(likelihood_name[safe.adult]))
    print('medical: {}'.format(likelihood_name[safe.medical]))
    print('spoofed: {}'.format(likelihood_name[safe.spoof]))
    print('violence: {}'.format(likelihood_name[safe.violence]))
    print('racy: {}'.format(likelihood_name[safe.racy]))

Ruby

Before trying this sample, follow the Ruby setup instructions in the Vision Quickstart Using Client Libraries . For more information, see the Vision Ruby API reference documentation .

# image_path = "Path to local image file, eg. './image.png'"

require "google/cloud/vision"

image_annotator = Google::Cloud::Vision::ImageAnnotator.new

response = image_annotator.safe_search_detection image: image_path

response.responses.each do |res|
  safe_search = res.safe_search_annotation

  puts "Adult:    #{safe_search.adult}"
  puts "Spoof:    #{safe_search.spoof}"
  puts "Medical:  #{safe_search.medical}"
  puts "Violence: #{safe_search.violence}"
  puts "Racy:     #{safe_search.racy}"
end

Running Safe Search Detection on a Remote Image

For your convenience, the Vision can perform Safe Search Detection directly on an image file located in Google Cloud Storage or on the Web without the need to send the contents of the image file in the body of your request.

Protocol

Refer to the images:annotate API endpoint for complete details.

To perform Safe Search Detection, make a POST request and provide the appropriate request body:

POST https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY
{
  "requests": [
    {
      "image": {
        "source": {
          "gcsImageUri": "gs://YOUR_BUCKET_NAME/YOUR_FILE_NAME"
        }
      },
      "features": [
        {
          "type": "SAFE_SEARCH_DETECTION"
        }
      ]
    }
  ]
}

See the AnnotateImageRequest reference documentation for more information on configuring the request body.

If the request is successful, the server returns a 200 OK HTTP status code and the response in JSON format:

{
  "responses": [
    {
      "safeSearchAnnotation": {
        "adult": "VERY_UNLIKELY",
        "spoof": "VERY_LIKELY",
        "medical": "UNLIKELY",
        "violence": "UNLIKELY",
        "racy": "VERY_UNLIKELY",
      }
    }
  ]
}

C#

Before trying this sample, follow the C# setup instructions in the Vision Quickstart Using Client Libraries . For more information, see the Vision C# API reference documentation .

// Specify a Google Cloud Storage uri for the image
// or a publicly accessible HTTP or HTTPS uri.
var image = Image.FromUri(uri);
var client = ImageAnnotatorClient.Create();
var response = client.DetectSafeSearch(image);
Console.WriteLine("Adult: {0}", response.Adult.ToString());
Console.WriteLine("Spoof: {0}", response.Spoof.ToString());
Console.WriteLine("Medical: {0}", response.Medical.ToString());
Console.WriteLine("Violence: {0}", response.Violence.ToString());
Console.WriteLine("Racy: {0}", response.Racy.ToString());

Go

Before trying this sample, follow the Go setup instructions in the Vision Quickstart Using Client Libraries . For more information, see the Vision Go API reference documentation .

// detectSafeSearch gets image properties from the Vision API for an image at the given file path.
func detectSafeSearchURI(w io.Writer, file string) error {
	ctx := context.Background()

	client, err := vision.NewImageAnnotatorClient(ctx)
	if err != nil {
		return err
	}

	image := vision.NewImageFromURI(file)
	props, err := client.DetectSafeSearch(ctx, image, nil)
	if err != nil {
		return err
	}

	fmt.Fprintln(w, "Safe Search properties:")
	fmt.Fprintln(w, "Adult:", props.Adult)
	fmt.Fprintln(w, "Medical:", props.Medical)
	fmt.Fprintln(w, "Racy:", props.Racy)
	fmt.Fprintln(w, "Spoofed:", props.Spoof)
	fmt.Fprintln(w, "Violence:", props.Violence)

	return nil
}

Java

Before trying this sample, follow the Java setup instructions in the Vision Quickstart Using Client Libraries . For more information, see the Vision Java API reference documentation .

public static void detectSafeSearchGcs(String gcsPath, PrintStream out) throws Exception,
    IOException {
  List<AnnotateImageRequest> requests = new ArrayList<>();

  ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
  Image img = Image.newBuilder().setSource(imgSource).build();
  Feature feat = Feature.newBuilder().setType(Type.SAFE_SEARCH_DETECTION).build();
  AnnotateImageRequest request =
      AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
  requests.add(request);

  try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
    BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
    List<AnnotateImageResponse> responses = response.getResponsesList();

    for (AnnotateImageResponse res : responses) {
      if (res.hasError()) {
        out.printf("Error: %s\n", res.getError().getMessage());
        return;
      }

      // For full list of available annotations, see http://g.co/cloud/vision/docs
      SafeSearchAnnotation annotation = res.getSafeSearchAnnotation();
      out.printf(
          "adult: %s\nmedical: %s\nspoofed: %s\nviolence: %s\nracy: %s\n",
          annotation.getAdult(),
          annotation.getMedical(),
          annotation.getSpoof(),
          annotation.getViolence(),
          annotation.getRacy());
    }
  }
}

Node.js

Before trying this sample, follow the Node.js setup instructions in the Vision Quickstart Using Client Libraries . For more information, see the Vision Node.js API reference documentation .

// Imports the Google Cloud client libraries
const vision = require('@google-cloud/vision');

// Creates a client
const client = new vision.ImageAnnotatorClient();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const bucketName = 'Bucket where the file resides, e.g. my-bucket';
// const fileName = 'Path to file within bucket, e.g. path/to/image.png';

// Performs safe search property detection on the remote file
const [result] = await client.safeSearchDetection(
  `gs://${bucketName}/${fileName}`
);
const detections = result.safeSearchAnnotation;
console.log(`Adult: ${detections.adult}`);
console.log(`Spoof: ${detections.spoof}`);
console.log(`Medical: ${detections.medical}`);
console.log(`Violence: ${detections.violence}`);

PHP

Before trying this sample, follow the PHP setup instructions in the Vision Quickstart Using Client Libraries . For more information, see the Vision PHP API reference documentation .

namespace Google\Cloud\Samples\Vision;

use Google\Cloud\Vision\V1\ImageAnnotatorClient;

// $path = 'gs://path/to/your/image.jpg'

function detect_safe_search_gcs($path)
{
    $imageAnnotator = new ImageAnnotatorClient();

    # annotate the image
    $response = $imageAnnotator->safeSearchDetection($path);
    $safe = $response->getSafeSearchAnnotation();

    if ($safe) {
        $adult = $safe->getAdult();
        $medical = $safe->getMedical();
        $spoof = $safe->getSpoof();
        $violence = $safe->getViolence();
        $racy = $safe->getRacy();

        # names of likelihood from google.cloud.vision.enums
        $likelihoodName = ['UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY',
        'POSSIBLE','LIKELY', 'VERY_LIKELY'];

        printf("Adult: %s" . PHP_EOL, $likelihoodName[$adult]);
        printf("Medical: %s" . PHP_EOL, $likelihoodName[$medical]);
        printf("Spoof: %s" . PHP_EOL, $likelihoodName[$spoof]);
        printf("Violence: %s" . PHP_EOL, $likelihoodName[$violence]);
        printf("Racy: %s" . PHP_EOL, $likelihoodName[$racy]);
    } else {
        print('No Results.' . PHP_EOL);
    }

    $imageAnnotator->close();
}

Python

Before trying this sample, follow the Python setup instructions in the Vision Quickstart Using Client Libraries . For more information, see the Vision Python API reference documentation .

def detect_safe_search_uri(uri):
    """Detects unsafe features in the file located in Google Cloud Storage or
    on the Web."""
    from google.cloud import vision
    client = vision.ImageAnnotatorClient()
    image = vision.types.Image()
    image.source.image_uri = uri

    response = client.safe_search_detection(image=image)
    safe = response.safe_search_annotation

    # Names of likelihood from google.cloud.vision.enums
    likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
                       'LIKELY', 'VERY_LIKELY')
    print('Safe search:')

    print('adult: {}'.format(likelihood_name[safe.adult]))
    print('medical: {}'.format(likelihood_name[safe.medical]))
    print('spoofed: {}'.format(likelihood_name[safe.spoof]))
    print('violence: {}'.format(likelihood_name[safe.violence]))
    print('racy: {}'.format(likelihood_name[safe.racy]))

Ruby

Before trying this sample, follow the Ruby setup instructions in the Vision Quickstart Using Client Libraries . For more information, see the Vision Ruby API reference documentation .

# image_path = "Google Cloud Storage URI, eg. 'gs://my-bucket/image.png'"

require "google/cloud/vision"

image_annotator = Google::Cloud::Vision::ImageAnnotator.new

response = image_annotator.safe_search_detection image: image_path

response.responses.each do |res|
  safe_search = res.safe_search_annotation

  puts "Adult:    #{safe_search.adult}"
  puts "Spoof:    #{safe_search.spoof}"
  puts "Medical:  #{safe_search.medical}"
  puts "Violence: #{safe_search.violence}"
  puts "Racy:     #{safe_search.racy}"
end

Try it

Try Safe Search detection below. You can use the image specified already (gs://bucket-name-123/demo-image.jpg) by clicking Execute, or you can specify your own image in its place.

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Vision API Documentation
Need help? Visit our support page.