【问题标题】:Google Document AI c# mime Unsupported input file formatGoogle Document AI c# mime 不支持的输入文件格式
【发布时间】:2021-07-29 18:30:31
【问题描述】:

我正在尝试将要处理的 pdf 上传到 google 的 Document AI 服务。使用谷歌的“C#”使用 Google.Cloud.DocumentAI.V1。看了github和docs,信息不多。 PDF 位于本地驱动器上。我将pdf转换为字节数组,然后将其转换为Bystring。然后将请求 mime 设置为“application/pdf”,但它返回的错误是:

Status(StatusCode="InvalidArgument", Detail="Unsupported input file format.", DebugException="Grpc.Core.Internal.CoreErrorDetailException: {"created":"@1627582435.256000000","description":"错误来自peer ipv4:142.250.72.170:443","file":"......\src\core\lib\surface\call.cc","file_line":1067,"grpc_message":"不支持的输入文件格式.","grpc_status":3}")

代码:

try
{
    //Generate a document
    string pdfFilePath = "C:\\Users\\maponte\\Documents\\Projects\\SettonProjects\\OCRSTUFF\\DOC071621-0016.pdf";
    var bytes = Encoding.UTF8.GetBytes(pdfFilePath);


    ByteString content = ByteString.CopyFrom(bytes);

    // Create client
    DocumentProcessorServiceClient documentProcessorServiceClient = await DocumentProcessorServiceClient.CreateAsync();
    // Initialize request argument(s)
    ProcessRequest request = new ProcessRequest
    {
        ProcessorName = ProcessorName.FromProjectLocationProcessor("*****", "mycountry", "***"),
        SkipHumanReview = false,
        InlineDocument = new Document(),
        RawDocument = new RawDocument(),
    };
    
    request.RawDocument.MimeType = "application/pdf";
    request.RawDocument.Content = content;

    // Make the request
    ProcessResponse response = await documentProcessorServiceClient.ProcessDocumentAsync(request);

    Document docResponse = response.Document;

    Console.WriteLine(docResponse.Text);
   
}
catch(Exception ex)
{
    Console.WriteLine(ex.Message);
}

【问题讨论】:

    标签: c# artificial-intelligence cloud-document-ai


    【解决方案1】:

    这是问题所在(或至少是一个问题)——您实际上并没有加载文件:

    string pdfFilePath = "C:\\Users\\maponte\\Documents\\Projects\\SettonProjects\\OCRSTUFF\\DOC071621-0016.pdf";
    var bytes = Encoding.UTF8.GetBytes(pdfFilePath);
    
    ByteString content = ByteString.CopyFrom(bytes);
    

    你想要:

    string pdfFilePath = "path-as-before";
    var bytes = File.ReadAllBytes(pdfFilePath);
    ByteString content = ByteString.CopyFrom(bytes);
    

    不过,我还要注意,InlineDocumentRawDocument 是彼此的替代品 - 指定其中一个会删除另一个。您的请求创建最好写成:

    ProcessRequest request = new ProcessRequest
    {
        ProcessorName = ProcessorName.FromProjectLocationProcessor("*****", "mycountry", "***"),
        SkipHumanReview = false,
        RawDocument = new RawDocument
        {
            MimeType = "application/pdf",
            Content = content
        }
    };
    

    【讨论】:

      猜你喜欢
      • 2022-08-05
      • 2021-10-16
      • 1970-01-01
      • 2012-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多