Search code examples
c#asp.netgoogle-cloud-platformgoogle-cloud-vision

PDF/TIFF Document Text Detection gcsDestinationBucketName


I'm working on Pdf to text file conversion using google cloud vision API.

I got an initial code help through there side, image to text conversion working fine with JSON key which I got through registration and activation,

here is a code which I got for pdf to text conversion

private static object DetectDocument(string gcsSourceUri,
string gcsDestinationBucketName, string gcsDestinationPrefixName)
{
var client = ImageAnnotatorClient.Create();

var asyncRequest = new AsyncAnnotateFileRequest
{
    InputConfig = new InputConfig
    {
        GcsSource = new GcsSource
        {
            Uri = gcsSourceUri
        },
        // Supported mime_types are: 'application/pdf' and 'image/tiff'
        MimeType = "application/pdf"
    },
    OutputConfig = new OutputConfig
    {
        // How many pages should be grouped into each json output file.
        BatchSize = 2,
        GcsDestination = new GcsDestination
        {
            Uri = $"gs://{gcsDestinationBucketName}/{gcsDestinationPrefixName}"
        }
    }
};

asyncRequest.Features.Add(new Feature
{
    Type = Feature.Types.Type.DocumentTextDetection
});

List<AsyncAnnotateFileRequest> requests =
    new List<AsyncAnnotateFileRequest>();
requests.Add(asyncRequest);

var operation = client.AsyncBatchAnnotateFiles(requests);

Console.WriteLine("Waiting for the operation to finish");

operation.PollUntilCompleted();

// Once the rquest has completed and the output has been
// written to GCS, we can list all the output files.
var storageClient = StorageClient.Create();

// List objects with the given prefix.
var blobList = storageClient.ListObjects(gcsDestinationBucketName,
    gcsDestinationPrefixName);
Console.WriteLine("Output files:");
foreach (var blob in blobList)
{
    Console.WriteLine(blob.Name);
}

// Process the first output file from GCS.
// Select the first JSON file from the objects in the list.
var output = blobList.Where(x => x.Name.Contains(".json")).First();

var jsonString = "";
using (var stream = new MemoryStream())
{
    storageClient.DownloadObject(output, stream);
    jsonString = System.Text.Encoding.UTF8.GetString(stream.ToArray());
}

var response = JsonParser.Default
            .Parse<AnnotateFileResponse>(jsonString);

// The actual response for the first page of the input file.
var firstPageResponses = response.Responses[0];
var annotation = firstPageResponses.FullTextAnnotation;

// Here we print the full text from the first page.
// The response contains more information:
// annotation/pages/blocks/paragraphs/words/symbols
// including confidence scores and bounding boxes
Console.WriteLine($"Full text: \n {annotation.Text}");

return 0;
}

this function required 3 parameters string gcsSourceUri, string gcsDestinationBucketName, string gcsDestinationPrefixName

I don't understand which value should I set for those 3 params. I never worked on third party API before so it's a little bit confusing for me


Solution

  • Suppose you own a GCS bucket named 'giri_bucket' and you put a pdf at the root of the bucket 'test.pdf'. If you wanted to write the results of the operation to the same bucket you could set the arguments to be

    • gcsSourceUri: 'gs://giri_bucket/test.pdf'
    • gcsDestinationBucketName: 'giri_bucket'
    • gcsDestinationPrefixName: 'async_test'

    When the operation completes, there will be 1 or more output files in your GCS bucket at giri_bucket/async_test.

    If you want, you could even write your output to a different bucket. You just need to make sure your gcsDestinationBucketName + gcsDestinationPrefixName is unique.

    You can read more about the request format in the docs: AsyncAnnotateFileRequest