2 回答

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>

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)。但他的回答包含另一個很好的提示。他使用文本位置信息將文本逐行添加到新數組中。這可能非常有用,具體取決于您的目標如何顯示/存儲文本。
- 2 回答
- 0 關注
- 206 瀏覽
添加回答
舉報