15

I am reading documentation on vision API request schema. In image source, I only see option of using url of GCS image paths. Is it possible to use external image url like http://example.com/images/image01.jpg ?

pm0733464
  • 2,862
  • 14
  • 16
Ashish Jain
  • 329
  • 2
  • 5
  • 13

6 Answers6

13

Yes, this is working perfectly fine:

{
  "requests": [
    {
      "features": [
        {
          "type": "WEB_DETECTION"
        }
      ],
      "image": {
        "source": {
          "imageUri": "http://i3.kym-cdn.com/photos/images/facebook/000/242/592/1c8.jpg"
        }
      }
    }
  ]
}
JacopKane
  • 824
  • 11
  • 15
  • If that works, it really shouldn't (otherwise folks could use the Vision API to DOS image hosts)! The `imageUri` field should always start with `gs://` (pointing to Google Cloud Storage). – JJ Geewax Jul 03 '17 at 12:18
  • It was working fine for me when I was trialling with the API last month – JacopKane Jul 04 '17 at 14:58
  • 2
    @JJGeewax Google has a warning on the documentation page : Caution: When fetching images from HTTP/HTTPS URLs, Google cannot guarantee that the request will be completed. Your request may fail if the specified host denies the request (for example, due to request throttling or DOS prevention), or if Google throttles requests to the site for abuse prevention. You should not depend on externally-hosted images for production applications. https://cloud.google.com/vision/docs/ocr#vision_text_detection-cli – Deepak Thomas Jul 23 '19 at 10:21
  • 1
    In short, image.source.imageUri works, but subject to conditions above – Deepak Thomas Jul 23 '19 at 10:23
  • Thanks for the feedback guys. It's been quite some time since I didn't use the API, therefore please feel free to edit my answer if you want to update/improve. – JacopKane Jul 30 '19 at 23:06
4

Yes it can, but ONLY using google cloud storage urls. Try this:

{
  "requests": [
    {
      "image": {
        "source": {
          gcsImageUri: 'gs://something.com/image',
        },
      },
      "features": ...
      "imageContext": ...
    },
  ]
}
Belfordz
  • 630
  • 6
  • 13
  • 1
    Thanks. That's problem I am trying to work around, so that I don't have to upload every time to GS first. I guess, it's almost same effort, uploading to image service directly or uploading to GS first and then processing it. Just another extra API call to upload to GS before processing and delete from GS after processing. – Ashish Jain Mar 10 '16 at 21:13
  • depending on the language used, there could be some useful methods for fetching an image via http and gettings a base64 representation of the image, which you can pass to the google api. – Belfordz Mar 10 '16 at 23:19
2

Yes, you can do this with any image as long as it is smaller than 4Mb. It does not have to be on Google Cloud Storage.

Here is an example using the Golang client library:

// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

// [START vision_quickstart]
// Sample vision-quickstart uses the Google Cloud Vision API to label an image.
package main

import (
    "fmt"
    "log"

    // Imports the Google Cloud Vision API client package.
    vision "cloud.google.com/go/vision/apiv1"
    "golang.org/x/net/context"
)

func main() {
    ctx := context.Background()

    // Creates a client.
    client, err := vision.NewImageAnnotatorClient(ctx)
    if err != nil {
        log.Fatalf("Failed to create client: %v", err)
    }

    image := vision.NewImageFromURI("https://www.denaligrizzlybear.com/assets/images/scenic-denali.jpg")

    labels, err := client.DetectLabels(ctx, image, nil, 10)
    if err != nil {
        log.Fatalf("Failed to detect labels: %v", err)
    }

    fmt.Println("Labels:")
    for _, label := range labels {
        fmt.Println(label.Description)
    }
}

Here is the function on the Godoc:https://godoc.org/cloud.google.com/go/vision/apiv1#NewImageFromURI

The docs state:

NewImageFromURI returns an image that refers to an object in Google Cloud Storage (when the uri is of the form "gs://BUCKET/OBJECT") or at a public URL.

Joel
  • 1,585
  • 2
  • 10
  • 20
2

Answer for python users.

def detect_labels_uri(uri):
    """Detects labels 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.label_detection(image=image)
    labels = response.label_annotations
    print('Labels:')

    for label in labels:
        print(label.description)
# [END vision_label_detection_gcs]
Parker Shamblin
  • 111
  • 1
  • 10
  • That is not an answer, that's a slightly related code snippet without any reasonable explanation. Please answer OP's question. – bugmenot123 Feb 28 '20 at 07:49
0

Yes you can use external image URL and even can use BASE64 image content also.

You can find the same in official documentation also.

Documentation URL : https://cloud.google.com/vision/docs/request#json_request_format

I have checked it with all available options and it is working fine for me.

Moon
  • 4,014
  • 3
  • 30
  • 66
-4

Yes, Google Cloud Vision API DOES accept external URL images. I just used this image to get labels:

python label_sample.py -u "https://upload.wikimedia.org/wikipedia/commons/e/e6/Bleeding_finger.jpg"

Found label: red with 96.47 percent probability!!!!!!!!!!!
Found label: color with 95.46 percent probability!!!!!!!!!!!
Found label: pink with 92.15 percent probability!!!!!!!!!!!
Found label: finger with 91.06 percent probability!!!!!!!!!!!
Found label: hand with 90.45 percent probability!!!!!!!!!!!
Found label: nail with 73.23 percent probability!!!!!!!!!!!
Found label: lip with 73.09 percent probability!!!!!!!!!!!
Found label: jewellery with 68.84 percent probability!!!!!!!!!!!
Found label: produce with 68.39 percent probability!!!!!!!!!!!
Found label: macro photography with 67.86 percent probability!!!!!!!!!!!

You have to give it the url in proper format using urllib library and comment the part of opening an image like this:

url = url_file
opener = urllib.urlopen(url)

#    with open(photo_file, 'rb') as image:
image_content = base64.b64encode(opener.read())
mmla
  • 1,578
  • 19
  • 21
muglikar
  • 146
  • 2
  • 18
  • I don't understand the reason for a downvote to my answer. – muglikar Feb 26 '17 at 14:09
  • 2
    The question was "Can google vision API accept external image URL?". In other words: Can Google's API accept a URL from any external server (e.g imgur, flickr, s3) and return a response on that image. If the question was "How can I download any image resource on the internet and process it with Google Vision, then your answer would be valid. – mmla Apr 09 '17 at 11:31
  • You misinterpreted my answer as well as my solution to the problem. My solution does exactly what the question asked i.e. accept a URL from any external server and returns a response on that image. – muglikar May 26 '17 at 12:44
  • 1
    @muglikar Your example downloads the file locally, and then sends it in-line to the Vision API. The question was asking whether you could just send a URL only (not an image itself) to the Vision API for processing (which it can, but only for URLs starting with `gs://`). – JJ Geewax Jul 03 '17 at 12:16