Detect web annotations in a Cloud Storage file

Perform web annotation detection on a file stored in Cloud Storage.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample


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.

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

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

	image := vision.NewImageFromURI(file)
	web, err := client.DetectWeb(ctx, image, nil)
	if err != nil {
		return err

	fmt.Fprintln(w, "Web properties:")
	if len(web.FullMatchingImages) != 0 {
		fmt.Fprintln(w, "\tFull image matches:")
		for _, full := range web.FullMatchingImages {
			fmt.Fprintf(w, "\t\t%s\n", full.Url)
	if len(web.PagesWithMatchingImages) != 0 {
		fmt.Fprintln(w, "\tPages with this image:")
		for _, page := range web.PagesWithMatchingImages {
			fmt.Fprintf(w, "\t\t%s\n", page.Url)
	if len(web.WebEntities) != 0 {
		fmt.Fprintln(w, "\tEntities:")
		fmt.Fprintln(w, "\t\tEntity\t\tScore\tDescription")
		for _, entity := range web.WebEntities {
			fmt.Fprintf(w, "\t\t%-14s\t%-2.4f\t%s\n", entity.EntityId, entity.Score, entity.Description)
	if len(web.BestGuessLabels) != 0 {
		fmt.Fprintln(w, "\tBest guess labels:")
		for _, label := range web.BestGuessLabels {
			fmt.Fprintf(w, "\t\t%s\n", label.Label)

	return nil


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.

import java.util.ArrayList;
import java.util.List;

public class DetectWebDetectionsGcs {

  public static void detectWebDetectionsGcs() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg";

  // Detects whether the remote image on Google Cloud Storage has features you would want to
  // moderate.
  public static void detectWebDetectionsGcs(String gcsPath) throws 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(Feature.Type.WEB_DETECTION).build();
    AnnotateImageRequest request =

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
      BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
      List<AnnotateImageResponse> responses = response.getResponsesList();

      for (AnnotateImageResponse res : responses) {
        if (res.hasError()) {
          System.out.format("Error: %s%n", res.getError().getMessage());

        // Search the web for usages of the image. You could use these signals later
        // for user input moderation or linking external references.
        // For a full list of available annotations, see
        WebDetection annotation = res.getWebDetection();
        for (WebDetection.WebEntity entity : annotation.getWebEntitiesList()) {
              entity.getDescription() + " : " + entity.getEntityId() + " : " + entity.getScore());
        for (WebDetection.WebLabel label : annotation.getBestGuessLabelsList()) {
          System.out.format("%nBest guess label: %s", label.getLabel());
        System.out.println("%nPages with matching images: Score%n==");
        for (WebDetection.WebPage page : annotation.getPagesWithMatchingImagesList()) {
          System.out.println(page.getUrl() + " : " + page.getScore());
        System.out.println("%nPages with partially matching images: Score%n==");
        for (WebDetection.WebImage image : annotation.getPartialMatchingImagesList()) {
          System.out.println(image.getUrl() + " : " + image.getScore());
        System.out.println("%nPages with fully matching images: Score%n==");
        for (WebDetection.WebImage image : annotation.getFullMatchingImagesList()) {
          System.out.println(image.getUrl() + " : " + image.getScore());
        System.out.println("%nPages with visually similar images: Score%n==");
        for (WebDetection.WebImage image : annotation.getVisuallySimilarImagesList()) {
          System.out.println(image.getUrl() + " : " + image.getScore());


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';

// Detect similar images on the web to a remote file
const [result] = await client.webDetection(`gs://${bucketName}/${fileName}`);
const webDetection = result.webDetection;
if (webDetection.fullMatchingImages.length) {
    `Full matches found: ${webDetection.fullMatchingImages.length}`
  webDetection.fullMatchingImages.forEach(image => {
    console.log(`  URL: ${image.url}`);
    console.log(`  Score: ${image.score}`);

if (webDetection.partialMatchingImages.length) {
    `Partial matches found: ${webDetection.partialMatchingImages.length}`
  webDetection.partialMatchingImages.forEach(image => {
    console.log(`  URL: ${image.url}`);
    console.log(`  Score: ${image.score}`);

if (webDetection.webEntities.length) {
  console.log(`Web entities found: ${webDetection.webEntities.length}`);
  webDetection.webEntities.forEach(webEntity => {
    console.log(`  Description: ${webEntity.description}`);
    console.log(`  Score: ${webEntity.score}`);

if (webDetection.bestGuessLabels.length) {
    `Best guess labels found: ${webDetection.bestGuessLabels.length}`
  webDetection.bestGuessLabels.forEach(label => {
    console.log(`  Label: ${label.label}`);


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_web_uri(uri):
    """Detects web annotations in the file located in Google Cloud Storage."""
    from import vision
    client = vision.ImageAnnotatorClient()
    image = vision.Image()
    image.source.image_uri = uri

    response = client.web_detection(image=image)
    annotations = response.web_detection

    if annotations.best_guess_labels:
        for label in annotations.best_guess_labels:
            print('\nBest guess label: {}'.format(label.label))

    if annotations.pages_with_matching_images:
        print('\n{} Pages with matching images found:'.format(

        for page in annotations.pages_with_matching_images:
            print('\n\tPage url   : {}'.format(page.url))

            if page.full_matching_images:
                print('\t{} Full Matches found: '.format(

                for image in page.full_matching_images:
                    print('\t\tImage url  : {}'.format(image.url))

            if page.partial_matching_images:
                print('\t{} Partial Matches found: '.format(

                for image in page.partial_matching_images:
                    print('\t\tImage url  : {}'.format(image.url))

    if annotations.web_entities:
        print('\n{} Web entities found: '.format(

        for entity in annotations.web_entities:
            print('\n\tScore      : {}'.format(entity.score))
            print(u'\tDescription: {}'.format(entity.description))

    if annotations.visually_similar_images:
        print('\n{} visually similar images found:\n'.format(

        for image in annotations.visually_similar_images:
            print('\tImage url    : {}'.format(image.url))

    if response.error.message:
        raise Exception(
            '{}\nFor more info on error messages, check: '

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser