我正在使用谷歌文档 ai 中的表单解析器。
当我发送请求时:
curl -X POST -H "Authorization: Bearer "$(gcloud auth application-default print-access-token)
-H "Content-Type: application/json; charset=utf-8"
-d @request.json https://eu-documentai.googleapis.com/v1beta3/projects/<project id>
/locations/eu/processors/<processor id>:process
我得到一份没有实体的文件 具有这种结构:
{
"document": {
"uri": "",
"mimeType": "application/pdf",
"text": "Pascal Carrié\nincwo SAS\nx2289475d\nc/ santa isabel, 12, 4D\nN° TVA FR33494952401\n28012 Madrid\n16 rue de La Comète\nIban :\nES76 3023 0047 4866 6328 6612\n75007 Paris\nrib:\nBCOEESMM023\nFRANCE\nFactura nº 2020/02\nFecha\n11/2/20\nConcepto\nPrecio\nCuandidad\nIVA\nImporte\ndéveloppement backend\n4250\n1\n0%\n4,250.00 €\nCondicións de pago : A la recepción de la factura\nBase Imponible\n4,250.00 €\nTotal IVA\n0.00 €\nTOTAL\n4,250.00 €\nForma de pago\nContado\n",
"pages": [
{
"pageNumber": 1,
"dimension": {
"width": 2378,
"height": 1681,
"unit": "pixels"
},
"layout": {
"textAnchor": {
"textSegments": [
{
"endIndex": "431"
}
]
},
"boundingPoly": {
"vertices": [
{},
{ ...
完全没有分析。我做错了什么?
当我在演示中上传相同的文档时,它工作正常。
我不认为它与base64相关;我已经编码了我的文档并获得了文档中描述的字符串
您需要遍历 pages->formFields 对象。 在每个对象中,您会找到 fieldName 和 fieldValue,其中包括 textAnchor->textSegments startIndex 和 endIndex; 使用此信息,您可以对“文本”属性进行 substr。
php 中的基本示例:
$json = json_decode(file_get_contents('d:\tmp\response.json'));
$text = utf8_decode($json->text);
foreach($json->pages as $indx => $pag){
foreach($pag->formFields as $indx2 => $field){
$from = $field->fieldName->textAnchor->textSegments[0]->startIndex;
$to = $field->fieldName->textAnchor->textSegments[0]->endIndex - $from-1;
$name = substr($text, $from, $to);
$from = $field->fieldValue->textAnchor->textSegments[0]->startIndex;
$to = $field->fieldValue->textAnchor->textSegments[0]->endIndex - $from;
$value = substr($text, $from, $to);
$fields[] = [$name => $value];
}
}
print_r($fields);
这里是关于如何处理来自 Document AI Form Parser 的处理响应的更新文档。
您还可以查看这些演示如何在 Python 和 Node.js 中使用表单解析器的代码实验室。