为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 Google Vision Api 检测块内的所有文本

如何使用 Google Vision Api 检测块内的所有文本

PHP
慕村225694 2022-01-02 16:26:16
我正在尝试使用 google vision api 从图像中提取文本,它有效。但我只想检测图像的一部分以获得某些文本。这是我使用的图像我只想提取所有文本,maybank2u.com直到From Account: 我知道有一些教程可以通过使用块来完成这个技巧,但这些教程是不同的编程语言。我的代码:<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>此代码将从图像中获取所有文本输出:
查看完整描述

2 回答

?
大话西游666

TA贡献1817条经验 获得超14个赞

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

为了获取每一行文本,我迭代了来自 Google Vision 的文本注释,并创建了一个行项目数组。每一个都有一个 x 位置和一个文本值。

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

最后,一旦我们获得最终所需的文本行,我们就停止。

我得到这样的结果:

  • maybank2u.com

  • 打开账单支付

  • 状态:成功

  • 参考编号:2950211545

  • 交易日期:2016年2月1日13:09:17

  • 金额:RM100.00

  • 来自账户 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>


查看完整回答
反对 回复 2022-01-02
?
倚天杖

TA贡献1828条经验 获得超3个赞

如果您只想限制输出及其每次应该停止执行的相同字符串,请执行以下操作:


<div class="row">

    <div class="col-12">

        <ol>

            <?php foreach ($text as $key => $texts): ?> 

                <?php if (strpos($texts->info()['description'], 'From Account') !== false) break; ?>

                <li><h6> <?php echo ucfirst($texts->info()['description']) ?></h6><<br><br> 

                </li>

            <?php endforeach ?>

        </ol>

    </div>

</div>

说明:

如果$texts->info()['description']包含文本,From Account它将通过 结束 foreach 循环的执行break。如果您需要检查多个关键字,请阅读此内容。


另一种解决方案是在将图像imagecrop()发送到 API 之前裁剪图像。但是为此,您需要确保它永远不会改变文本的大小/位置。


PS 你确定每个人都应该在你的截图中看到那些私人数据吗?


Update1

正如你所问的。这将是相同的代码,但使用控制结构的替代语法:


<div class="row">

    <div class="col-12">

        <ol>

            <?php foreach ($text as $key => $texts): ?> 

                <?php if (strpos($texts->info()['description'], 'From Account') !== false): ?>

                <?php break; ?>

                <?php endif; ?>

                <li><h6> <?php echo ucfirst($texts->info()['description']) ?></h6><<br><br> 

                </li>

            <?php endforeach ?>

        </ol>

    </div>

</div>

也许这可以解决您的问题,因为同一页面包含此注释:


不支持在同一控制块中混合使用语法。


更新2


在你更新你的问题之后,它现在更清楚了。输出的每一行不包含一个元素。相反,它包含多行文本。因此,我的第一个代码没有回显它From Account在第一个数组元素中找到的任何内容。


因此,我们需要搜索字符串From Account 并剪切文本行:


<div class="row">

    <div class="col-12">

        <ol>

            <?php foreach ($text as $key => $texts): ?> 

                <?php

                $text = $texts->info()['description'];

                // search for string

                $pos = strpos($texts->info()['description'], 'From Account');

                if ($pos !== false) {

                    // if the string was found cut the text

                    $text = substr($text, 0, $pos);

                }

                ?>

                <li><h6> <?php echo $text ?></h6><<br><br> 

                </li>

            <?php endforeach ?>

        </ol>

    </div>

</div>

您可以选择在此之前添加它<?php endforeach ?>以跳过以下所有数组元素:


                <?php

                if ($pos !== false) {

                    break;

                }

                ?>

注意: @TerryLennox 用于preg_match查找From Account. 这和 using 没有区别strpos(最喜欢避免 regex)。但他的回答包含另一个很好的提示。他使用文本位置信息将文本逐行添加到新数组中。这可能非常有用,具体取决于您的目标如何显示/存储文本。


查看完整回答
反对 回复 2022-01-02
  • 2 回答
  • 0 关注
  • 187 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号