如何使用Google Vision Api检测块内的所有文本 [英] how can I detect all the text that inside a block with Google Vision Api

查看:113
本文介绍了如何使用Google Vision Api检测块内的所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用谷歌视觉api从图像中提取文本,它可以工作.但是我只想检测图像的一部分以获得某些文本.

I'm trying to extract text from an image with google vision api, it works. But I just want to detect part of the image to get certain text.

这是我使用的图片

我只想提取maybank2u.comFrom Account:的所有文本 我知道有一些教程可以通过使用块来实现此技巧,但是这些教程是不同的编程语言.

I just want to extract all the text from maybank2u.com until From Account: I know there are some tutorials to do this trick by using block but those tutorials are different programming languages.

我的代码:

<div class="row">
    <div class="col-12">
        <ol>
            <?php foreach ($text as $key => $texts): ?> 
                <li><h6> <?php echo ucfirst($texts->info()['description']) ?></h6><<br><br> 
                </li>
            <?php endforeach ?>
        </ol>
    </div>
</div>

此代码将从图像中获取所有文本

This code will getting all the text from image

输出:

推荐答案

以下代码对我有用.我有一个php文件,test.php和一个图像文件/images/UUIPXl.png.

The code below works for me. I have one php file, test.php and one image file /images/UUIPXl.png.

要获取每一行文本,请迭代Google Vision中的文本注释,并创建一个行项目数组.它们每个都有一个x位置和一个文本值.

To get each line of text, I iterate the text annotations from Google Vision, and create an array of row items. Each of these has an x position and a text value.

然后我按x位置对每一行进行排序,并连接以创建一行文本.

I then sort each row by x position and concatenate to create a line of text.

最后,我们在获得所需的最终文本行时就停止了操作.

Finally we stop once we get the final desired line of text.

我得到这样的结果:

  • maybank2u.com
  • 打开BillPayment
  • 状态:成功
  • 参考编号:2950211545
  • 交易日期:2016年2月1日13:09:17
  • 金额:RM100.00
  • 来自帐户564155051577 WCA
  • maybank2u.com
  • Open BillPayment
  • Status: Successful
  • Reference number: 2950211545
  • Transaction date: 01 Feb 2016 13:09:17
  • Amount: RM100.00
  • From Account 564155051577 WCA

php代码:

<?php 

    require 'vendor/autoload.php';
    use Google\Cloud\Vision\VisionClient;

    $config = ["keyFile" => json_decode(file_get_contents("./APIKey.json"), true) ];
    $vision = new VisionClient($config);

    $image = $vision->image(
        fopen('./images/UUIPXl.png', 'r'),
        ['TEXT_DETECTION']
    );

    $textAnnotations = $vision->annotate($image)->text();
    $rows = [];

    // Function used to sort our lines.
    function sortProc($a, $b)
    {
        if ($a["x"] === $b["x"]) {
            return 0;
        }
        return ($a["x"] < $b["x"]) ? -1 : 1;
    }

    // Remove first row (complete text).
    array_shift($textAnnotations);

    // We should calculate this, use a reasonable value to begin with.
    $lineHeight = 8;

    foreach ($textAnnotations as $text) {
        $key = round(((double)($text->info()["boundingPoly"]["vertices"][0]["y"]))/$lineHeight);
        $x = (int)$text->info()["boundingPoly"]["vertices"][0]["x"];
        $value = ["x" => $x, "text" => $text->description()];
        if (!isset($rows[$key])) {
            $rows[$key] = [];
        }
        $rows[$key][] = $value;
    }

    $text = [];
    foreach ($rows as $key => $value) {
        // Sort by x value.
        usort($value, "sortProc");

        // Concatenate each line
        $result = array_reduce($value, function($acc, $elem) {
            $acc .= " " . $elem["text"];
            return $acc;
        }, "");

        $text[] = $result;

        // Stop when we get here!
        if (preg_match("/from account/i", $result)) {
            break;
        }
    }

?>

<div class="row" style="padding: 20px;">
    <div class="col-12">
        <ul>
            <?php foreach ($text as $row): ?> 
                <li><h3> <?php echo ucfirst($row) ?></h3></li>
            <?php endforeach ?>
        </ul>
    </div>
</div>

这篇关于如何使用Google Vision Api检测块内的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆