サイト作成の勉強中です。
一通りHTMLやCSS、IS、PHPを勉強したので業務でレシートをExcelに転記しているのでスマホで撮影したレシートを読み込み、DBに保存するプログラムを作成しようと思いました。
PHPでVisionAPIを使用したサンブルだとうまく読み込めず・・・レシート左の品名と単価が別れて表示されます。
そこでいろいろ検索したところ、以下の記事に辿り着きました。
https://qiita.com/shoku-pan/items/bf5645894803769edc72
これをPHPに書き換えたら、うまく行として読み込めると思った次第です。
作ったPHPは以下のとおりです
PHP
1<?PHP 2// APIキー 3$api_key = "私のAPIキー" ; 4 5// リクエスト用のJSONを作成 6$json = json_encode( array( 7 "requests" => array( 8 array( 9 "image" => array( 10 "content" => str_replace("data:image/png;base64,", "", $_POST['image']), 11 ) , 12 "features" => array( 13 array( 14 "type" => "TEXT_DETECTION", 15 "maxResults" => 10, 16 ) , 17 ) , 18 "imageContext" => array( 19 "languageHints" => array("ja"), 20 ), 21 ) , 22 ) , 23) ) ; 24 25// リクエストを実行 26$curl = curl_init(); 27curl_setopt($curl, CURLOPT_URL, "https://vision.googleapis.com/v1/images:annotate?key=" . $api_key); 28curl_setopt($curl, CURLOPT_HEADER, true); 29curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); 30curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 31curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 32curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 33if(isset($referer) && !empty($referer)) curl_setopt($curl, CURLOPT_REFERER, $referer) ; 34curl_setopt($curl, CURLOPT_TIMEOUT, 15); 35curl_setopt($curl, CURLOPT_POSTFIELDS, $json); 36$res1 = curl_exec($curl); 37$res2 = curl_getinfo($curl); 38curl_close($curl); 39 40// 取得したデータ 41$json = substr($res1, $res2["header_size"]); 42$array_json=json_decode($json, true); 43 44$document = $array_json["responses"]["0"]["fullTextAnnotation"]; 45$bounds = array(); 46foreach ($document["pages"] as $pages){ 47 foreach ($pages["blocks"] as $block) { 48 foreach ($block["paragraphs"] as $paragraph) { 49 foreach ($paragraph["words"] as $words) { 50 foreach ($words["symbols"] as $symbol) { 51 $x = $symbol["boundingBox"]["vertices"]["0"]["x"]; 52 $y = $symbol["boundingBox"]["vertices"]["0"]["y"]; 53 $text = $symbol["text"]; 54 array_push($bounds, array("x" => $x, "y" => $y, "text" => $text)); 55 } 56 } 57 } 58 } 59} 60$ys = array_column($bounds, "y"); 61array_multisort($ys, SORT_ASC, $bounds); 62$old_y = -1; 63$line = array(); 64$lines = array(); 65$threshold = 1; 66foreach($bounds as $bound) { 67 $x = $bound["x"]; 68 $y = $bound["y"]; 69 if ($old_y == -1) { 70 $old_y = $y; 71 } 72 elseif ((($old_y - $threshold) <= $y) && ($y <= ($old_y + $threshold))) { 73 $old_y = $y; 74 } 75 else { 76 $old_y = -1; 77 $xs = array_column($line, "x"); 78 array_multisort($xs, SORT_ASC, $line); 79 array_push($lines, $line); 80 $line = array(); 81 } 82 array_push($line, $bound); 83} 84$xs = array_column($line, "x"); 85array_multisort($xs, SORT_ASC, $line); 86array_push($lines, $line); 87 88foreach($lines as $line) { 89 $texts = ""; 90 foreach($line as $i) { 91 $texts = $texts . $i["text"]; 92 } 93 print_r($texts . "<br />"); 94}
このようにOCRされます。
ま
いば
すと
けっ
TEL
045野庭町店
-840101063
-62FAX045840-
領収証-
営業時間:あさ9時~よる11時
年中無休
レジ01042019/6/28(金)17:05
EX7345:002153742
どん兵衛釜たま風うどん276
<2個X単138)
カルピスカルピスウォー184
<2個X単92)
雪メグなめらかプリン98
とろりクリームプリン128
糖質みかんゼリー98
メイトーなめらかプリン88
QTTA明太チーズ味128
(ボーナスポイント30P)
小計¥1,000
外税8%対象額
¥1,000
外税8%
¥80
合計
¥1,080
現金
¥1,100
ID:0164
お釣り
¥20
少しでもズレがあると違う行になっているように見えます。
私としては、パイソンの
python
1elif old_y-threshold <= y <= old_y+threshold:
PHP
1elseif ((($old_y - $threshold) <= $y) && ($y <= ($old_y + $threshold))) {
ここが違うのかなぁと思うのですが、わかる方いらっしゃいますでしょうか?
アドバイスのほどよろしくお願いいたします。

回答1件
あなたの回答
tips
プレビュー