4

I am currently following this example on the Vision API docs:found here


import com.google.cloud.vision.v1.*;
import com.google.cloud.vision.v1.Feature.Type;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class VisionApiTest {
    public static void main(String... args) throws Exception {

        PrintStream stream = new PrintStream(new File("src/test.txt"));

        detectTextGcs("https://www.w3.org/TR/SVGTiny12/examples/textArea01.png", stream);
    }

    public static void detectTextGcs(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.TEXT_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
                for (EntityAnnotation annotation : res.getTextAnnotationsList()) {
                    out.printf("Text: %s\n", annotation.getDescription());
                    out.printf("Position : %s\n", annotation.getBoundingPoly());
                }
            }
        }
    }
}

After passing in the gcsPath String into the detectTextGcs method in the example, I am given the error: "Error: Invalid GCS path specified: https://www.w3.org/TR/SVGTiny12/examples/textArea01.png"

I am expecting for the PrintStream object to write to the file the text that is held within the picture which will be "Tomorrow, and\ntomorrow, and\ntomorrow; blah blah blah...". After trying the API on Vision API doc page mentioned above, it works fine, but not within IntelliJ.

Any help is greatly appreciated. Thank you. (Also forgive me if this isn't a well worded question, it's my first time posting)

Niko
  • 61
  • 1
  • 4

2 Answers2

2

I actually figured out the problem. The problem lies within the in line 3 of the detectGcsText() method.

 ImageSource imgSource = imageSource.newBuilder().setGcsImageUri(gcsPath).build();

If you would like to use a regular HTTP URI, you must use setImageUri(path) instead of setGcsImageUri(gcsPath).

Thank you for everyone's help!

Niko
  • 61
  • 1
  • 4
1

Google Cloud Storage (GCS) is a storage system where you can persistently save data as blob storage. In GCS, we have the concept of buckets which are "named" containers of data and objects which are named instances of data. To specify a Blob, we Google has invented the notion of a GCS URL of the form:

gs://[BUCKET_NAME]/[OBJECT_NAME]

In your story, you have specified an HTTP URL where a GCS Url was expected. You must not specify an HTTP URL where a GCS URL is required.

Kolban
  • 13,794
  • 3
  • 38
  • 60
  • Ok, that makes perfect sense. The only reason I am somewhat is confused is when I test the Vision API (via the API docs in the link mentioned at the beginning of the post) using the HTTP URL, it still returns the expected response which is the text that is within the image. What is the reason for this? – Niko Aug 20 '19 at 17:35
  • Often GCP API's are inconsistent unfortunately, and sometimes the developers will make the web UI convert from HTTP to GCS for you before making the request. When calling the API's yourself this won't happen. – Jaboy Aug 20 '19 at 18:48