pdf/tiff文档文本检测gcsdestinationbucketname



我正在使用PDF使用Google Cloud Vision API进行文本文件转换。

我在那里获得了初始代码帮助,图像到文本转换与JSON键正常工作,我通过注册和激活获得了

这是我为PDF提供的代码来短信转换

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

此功能需要3个参数字符串GCSSOURCEURI,字符串gcsdestinationbucketname,字符串gcsdestinationprefixname

我不明白我应该为这3个参数设置哪个值。我以前从未在第三方API上工作过,所以我有点困惑

假设您拥有一个名为'giri_bucket'的gcs桶,然后将pdf放在桶'test.pdf'的根部。如果您想将操作结果写入同一存储桶,则可以将参数设置为

  • gcssourceuri:'gs://giri_bucket/test.pdf'
  • gcsdestinationbucketname:'giri_bucket'
  • gcsdestinationprefixname:'async_test'

当操作完成后,giri_bucket/async_test中的GCS存储桶中将有1个或更多输出文件。

如果愿意,甚至可以将输出写入其他存储桶。您只需要确保您的gcsdestinationbucketname gcsdestinationprefixname是唯一的。

您可以在文档中阅读有关请求格式的更多信息:asyncannotatefilerequest

相关内容

  • 没有找到相关文章

最新更新