Google Document AI c# mime 不支持的输入文件格式

我试图上传一个PDF文件到Google的Document AI服务进行处理。我使用的是Google的Google.Cloud.DocumentAI.V1库来处理”C#”。查看了GitHub和文档,但没有找到太多信息。PDF文件位于本地驱动器上。我将PDF文件转换成了字节数组,然后再转换成ByteString。接着我将请求的mime类型设置为”application/pdf”,但返回了一个错误:

状态(状态码=”InvalidArgument”, 详细信息=”不支持的输入文件格式。”, 调试异常=”Grpc.Core.Internal.CoreErrorDetailException: {“created”:”@1627582435.256000000″,”description”:”从对等端ipv4:142.250.72.170:443接收到的错误”,”file”:”……\src\core\lib\surface\call.cc”,”file_line”:1067,”grpc_message”:”不支持的输入文件格式。”,”grpc_status”:3}”)

代码:

try{    //生成文档    string pdfFilePath = "C:\\Users\\maponte\\Documents\\Projects\\SettonProjects\\OCRSTUFF\\DOC071621-0016.pdf";    var bytes = Encoding.UTF8.GetBytes(pdfFilePath);    ByteString content = ByteString.CopyFrom(bytes);    // 创建客户端    DocumentProcessorServiceClient documentProcessorServiceClient = await DocumentProcessorServiceClient.CreateAsync();    // 初始化请求参数    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;    // 发起请求    ProcessResponse response = await documentProcessorServiceClient.ProcessDocumentAsync(request);    Document docResponse = response.Document;    Console.WriteLine(docResponse.Text);   }catch(Exception ex){    Console.WriteLine(ex.Message);}

回答:

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

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

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注