Php 使用googlecloudvisionapi的接收解析器

Php 使用googlecloudvisionapi的接收解析器,php,yii2,google-cloud-vision,Php,Yii2,Google Cloud Vision,TL;DR:我想使用Google Vision API从收据中提取项目、总价、付款类型、门店名称、交易日期 $imageAnnotator = new ImageAnnotatorClient(['credentials' => 'cloud-vision-keyfile.json']); $image = file_get_contents($image_path); $response = $imageAnnotator->textDetection($image); $tex

TL;DR:我想使用Google Vision API从收据中提取项目、总价、付款类型、门店名称、交易日期

$imageAnnotator = new ImageAnnotatorClient(['credentials' => 'cloud-vision-keyfile.json']);
$image = file_get_contents($image_path);

$response = $imageAnnotator->textDetection($image);
$texts = $response->getTextAnnotations();

foreach ($texts as $text) {
    // read through all the text scanned
    // some code to sanitize and extract $text->getDescription() ....
}

是一种有限访问功能

因此,我的目标是找到更好的方法来获取这些字段:

承租人姓名 目前我在标题上实现了模糊搜索

$outlet_block = explode(PHP_EOL, $texts[0]->getDescription(), 5);
$outlet_candidates = array_slice($outlet_block, 0, 4);
接收日期 我使用
date\u parse
获取一个看起来像日期的字符串,并检查该日期是否不超过一周

$parsed_date = date_parse($filtered_text);
if (
    $parsed_date['year'] && 
    $parsed_date['month'] &&
    $parsed_date['day']
)
{
    // ... and not old enough
    $date_candidate = implode('-', [
        $parsed_date['year'],
        $parsed_date['month'],
        $parsed_date['day']
    ]);
    $str_date_candidate = strtotime($date_candidate);
    if ($str_date_candidate >= strtotime($oldest_transaction_date) && $str_date_candidate <= strtotime('now'))
    {
        $date_candidates[] = date('Y-m-d', strtotime($date_candidate));
    }
$parsed\u date=date\u parse($filtered\u text);
如果(
$parsed_日期['year']和
$parsed_日期['month']&&
$parsed_date['day']
)
{
//…而且还不够大
$date_candidate=内爆('-'[
$parsed_日期['year'],
$parsed_date['month'],
$parsed_date['day']
]);
$str_date_candidate=strtime($date_candidate);
如果($str_date_candidate>=strtotime($oldest_transaction_date)&&$str_date_candidate_candidate getBoundingPoly()->getVertices();
//左上、右上、右下、左下
$y_min=min($vertices[0]->getY(),$vertices[1]->getY());
$y_max=max($vertices[2]->getY(),$vertices[3]->getY());
$y_min_总计[]=$y_min;
$y_max_总计[]=$y_max;
}
elseif($is\u total\u found)//在找到“total”文本后检查所有数字
{
$text\u number=$filtered\u text;
//修剪所有不必要的东西以获得数字
$text|u number=preg|u replace('/^(卢比|印尼盾|美元)|(\.00 | \,00)$/','',$text|u number);
$text_number=(int)filter_var(str_replace([',','.],'','','$text_number),filter_SANITIZE_number_int);
如果($text_number==0)
继续;
如果($text_numbergetBoundingPoly()->getVertices();
$y_min=min($vertices[0]->getY(),$vertices[1]->getY());
$y_max=max($vertices[2]->getY(),$vertices[3]->getY());
$y_中心=($y_最小值+y_最大值)/2;
foreach($y_min_总计为$key=>$y_min_总计)
{
//如果y_中心位于y_最小值总和和y_最大值总和内
如果($y_center>=$y_min_total&$y_center getBoundingPoly()->getVertices();
$x_center=($vertices[0]->getX()+$vertices[1]->getX()+$vertices[2]->getX()+$vertices[3]->getX())/4;
$y_center=($vertices[0]->getY()+$vertices[1]->getY()+$vertices[2]->getY()+$vertices[3]->getY())/4;
$x0=$顶点[0]->getX();
$y0=$texts[0]->getY();
返回(bool)(x0美元
我希望我能得到一些启示。谢谢


API是否有问题,或者您不知道如何解析结果?我正在寻找更好的解析/提取方法:门店名称、收货日期、项目/产品列表和付款类型(借记卡、信用卡、现金等)。目前我正在进行临时工作
if (in_array($filtered_text, $totals_arr))
{
    $is_total_found = true;
    $vertices = $text->getBoundingPoly()->getVertices();


    // top left, top right, bottom right, bottom left
    $y_min = min($vertices[0]->getY(), $vertices[1]->getY());
    $y_max = max($vertices[2]->getY(), $vertices[3]->getY());

    $y_min_totals[] = $y_min;
    $y_max_totals[] = $y_max;
}
elseif ($is_total_found) // check all number after the "total" text is found
{
    $text_number = $filtered_text;

    // trim everything unnecessary to get the number
    $text_number = preg_replace('/^(rp|idr|usd)|(\.00|\,00)$/', '', $text_number);
    $text_number = (int) filter_var(str_replace([',', '.'], '', $text_number), FILTER_SANITIZE_NUMBER_INT);

    if ($text_number == 0)
        continue;

    if ($text_number < max($price_candidates))
    {
        continue;
    }

    // check Y position
    $vertices = $text->getBoundingPoly()->getVertices();
    $y_min = min($vertices[0]->getY(), $vertices[1]->getY());
    $y_max = max($vertices[2]->getY(), $vertices[3]->getY());

    $y_center = ($y_min + $y_max) / 2;

    foreach ($y_min_totals as $key => $y_min_total)
    {
        // if y_center is inside the y_min_total and y_max_total
        if ($y_center >= $y_min_total && $y_center <= $y_max_totals[$key])
        {
            // we have a candidate
            $price_candidates[] = $text_number;
        }
    }
}
$vertices = $text->getBoundingPoly()->getVertices();
$x_center = ($vertices[0]->getX() + $vertices[1]->getX() + $vertices[2]->getX() + $vertices[3]->getX()) / 4;
$y_center = ($vertices[0]->getY() + $vertices[1]->getY() + $vertices[2]->getY() + $vertices[3]->getY()) / 4;

$x0 = $vertices[0]->getX();
$y0 = $vertices[0]->getY();

return (bool) ($x0 < $x_center && $y0 < $y_center);