如何解决谷歌文档ai不解析表单
我使用 Google 文档 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; 有了这些信息,您就可以减去“text”属性。
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,$to);
$fields[] = [$name => $value];
}
}
print_r($fields);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。