2 回答

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊
下面的代碼對(duì)我有用。我有一個(gè) php 文件 test.php 和一個(gè)圖像文件 /images/UUIPXl.png。
為了獲取每一行文本,我迭代了來(lái)自 Google Vision 的文本注釋,并創(chuàng)建了一個(gè)行項(xiàng)目數(shù)組。每一個(gè)都有一個(gè) x 位置和一個(gè)文本值。
然后我按 x 位置對(duì)每一行進(jìn)行排序并連接以創(chuàng)建一行文本。
最后,一旦我們獲得最終所需的文本行,我們就停止。
我得到這樣的結(jié)果:
maybank2u.com
打開(kāi)賬單支付
狀態(tài):成功
參考編號(hào):2950211545
交易日期:2016年2月1日13:09:17
金額:RM100.00
來(lái)自賬戶 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>

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
如果您只想限制輸出及其每次應(yīng)該停止執(zhí)行的相同字符串,請(qǐng)執(zhí)行以下操作:
<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>
說(shuō)明:
如果$texts->info()['description']包含文本,F(xiàn)rom Account它將通過(guò) 結(jié)束 foreach 循環(huán)的執(zhí)行break。如果您需要檢查多個(gè)關(guān)鍵字,請(qǐng)閱讀此內(nèi)容。
另一種解決方案是在將圖像imagecrop()發(fā)送到 API 之前裁剪圖像。但是為此,您需要確保它永遠(yuǎn)不會(huì)改變文本的大小/位置。
PS 你確定每個(gè)人都應(yīng)該在你的截圖中看到那些私人數(shù)據(jù)嗎?
Update1
正如你所問(wèn)的。這將是相同的代碼,但使用控制結(jié)構(gòu)的替代語(yǔ)法:
<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>
也許這可以解決您的問(wèn)題,因?yàn)橥豁?yè)面包含此注釋:
不支持在同一控制塊中混合使用語(yǔ)法。
更新2
在你更新你的問(wèn)題之后,它現(xiàn)在更清楚了。輸出的每一行不包含一個(gè)元素。相反,它包含多行文本。因此,我的第一個(gè)代碼沒(méi)有回顯它From Account在第一個(gè)數(shù)組元素中找到的任何內(nèi)容。
因此,我們需要搜索字符串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 ?>以跳過(guò)以下所有數(shù)組元素:
<?php
if ($pos !== false) {
break;
}
?>
注意: @TerryLennox 用于preg_match查找From Account. 這和 using 沒(méi)有區(qū)別strpos(最喜歡避免 regex)。但他的回答包含另一個(gè)很好的提示。他使用文本位置信息將文本逐行添加到新數(shù)組中。這可能非常有用,具體取決于您的目標(biāo)如何顯示/存儲(chǔ)文本。
- 2 回答
- 0 關(guān)注
- 210 瀏覽
添加回答
舉報(bào)