View a markdown version of this page

ドキュメントページで項目を検索する - Amazon Textract

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

ドキュメントページで項目を検索する

Amazon Textract オペレーションは、ドキュメントページにある項目の場所とジオメトリを返します。DetectDocumentTextGetDocumentTextDetection は線と単語の場所とジオメトリを返し、AnalyzeDocumentGetDocumentAnalysis はキーと値のペア、テーブル、セル、選択要素の場所とジオメトリを返します。

ドキュメントページ上の項目の場所を確認するには、ブロックオブジェクトの Amazon Textract オペレーションによって返される境界ボックス (ジオメトリ) 情報を使用します。Geometry オブジェクトには、検出された項目の 2 種類の位置とジオメトリ情報が含まれます。

  • 左上の座標と項目の幅と高さを含む軸整列の BoundingBox オブジェクト。

  • 項目の概要を記述するポリゴンオブジェクト。各ポイントのドキュメントページの座標 X (水平軸) と Y (垂直軸) を含むポイントオブジェクトの配列として指定されます。

Block オブジェクトの JSON は次のようになります。BoundingBox および Polygonフィールドを書き留めます。

{ "Geometry": { "BoundingBox": { "Width": 0.053907789289951324, "Top": 0.08913730084896088, "Left": 0.11085548996925354, "Height": 0.013171200640499592 }, "Polygon": [ { "Y": 0.08985357731580734, "X": 0.11085548996925354 }, { "Y": 0.08913730084896088, "X": 0.16447919607162476 }, { "Y": 0.10159222036600113, "X": 0.16476328670978546 }, { "Y": 0.10230850428342819, "X": 0.11113958805799484 } ] }, "Text": "Name:", "TextType": "PRINTED", "BlockType": "WORD", "Confidence": 99.56285858154297, "Id": "c734fca6-c4c4-415c-b6c1-30f7510b72ee" },

ジオメトリ情報を使用して、検出された項目の周囲に境界ボックスを描画できます。BoundingBox および Polygon情報を使用して各単語の先頭と末尾の線と垂直線の周囲にボックスを描画する例については、「」を参照してくださいAmazon Textract を使用したドキュメントテキストの検出。出力例は次のようになります。

住所 123 Any Street、Anytown、米国、および生年月日 12-26-1980 の Jane Doe の個人情報を表示する名前、住所、および生年月日のフィールドを含むフォーム。

境界ボックス

境界ボックス (BoundingBox) には、次のプロパティがあります。

  • Height – ドキュメントページの高さ全体に対する境界ボックスの高さの比率。

  • 左 – ドキュメントページ全体の幅に対する境界ボックスの左上ポイントの X 座標。

  • 上部 – ドキュメントページの高さ全体に対する境界ボックスの左上ポイントの Y 座標。

  • 幅 – ドキュメントページの幅全体に対する境界ボックスの幅の比率。

各 BoundingBox プロパティには、0 から 1 の値があります。値は、イメージ全体の幅 ( Leftと に適用Width) または高さ ( Heightと に適用) の比率ですTop。たとえば、入力イメージが 700 x 200 ピクセルで、境界ボックスの左上座標が (350,50) ピクセルの場合、API は 0.5 (350/700) Leftの値と 0.25 (50/200) Topの値を返します。

次の図は、各 BoundingBox プロパティがカバーするドキュメントページの範囲を示しています。

ページの境界ボックスを定義する左、上、幅、高さのプロパティを示す図。

正しい場所とサイズで境界ボックスを表示するには、BoundingBox の値をドキュメントページの幅または高さ (必要な値に応じて) で乗算してピクセル値を取得します。境界ボックスを表示するには、ピクセル値を使用します。たとえば、幅 608 ピクセル x 高さ 588 ピクセルのドキュメントページと、分析されたテキストの次の境界ボックス値を使用します。

BoundingBox.Left: 0.3922065 BoundingBox.Top: 0.15567766 BoundingBox.Width: 0.284666 BoundingBox.Height: 0.2930403

ピクセル単位のテキスト境界ボックスの場所は、次のように計算されます。

Left coordinate = BoundingBox.Left (0.3922065) * document page width (608) = 238

Top coordinate = BoundingBox.Top (0.15567766) * document page height (588) = 91

Bounding box width = BoundingBox.Width (0.284666) * document page width (608) = 173

Bounding box height = BoundingBox.Height (0.2930403) * document page height (588) = 172

これらの値を使用して、分析されたテキストの周囲に境界ボックスを表示します。次の Java および Python の例は、境界ボックスを表示する方法を示しています。

Java
public void ShowBoundingBox(int imageHeight, int imageWidth, BoundingBox box, Graphics2D g2d) { float left = imageWidth * box.getLeft(); float top = imageHeight * box.getTop(); // Display bounding box. g2d.setColor(new Color(0, 212, 0)); g2d.drawRect(Math.round(left / scale), Math.round(top / scale), Math.round((imageWidth * box.getWidth()) / scale), Math.round((imageHeight * box.getHeight())) / scale); }
Python

この Python の例では、DetectDocumentText API オペレーションによってresponse返された を取り込みます。

def process_text_detection(response): # Get the text blocks blocks = response['Blocks'] width, height = image.size draw = ImageDraw.Draw(image) print('Detected Document Text') # Create image showing bounding box/polygon the detected lines/text for block in blocks: draw = ImageDraw.Draw(image) if block['BlockType'] == "LINE": box=block['Geometry']['BoundingBox'] left = width * box['Left'] top = height * box['Top'] draw.rectangle([left,top, left + (width * box['Width']), top +(height * box['Height'])],outline='black') # Display the image image.show() return len(blocks)

Polygon

によって返されるポリゴンAnalyzeDocumentは、ポイントオブジェクトの配列です。各 Point には、ドキュメントページの特定の場所の X 座標と Y 座標があります。BoundingBox 座標と同様に、ポリゴン座標はドキュメントの幅と高さに正規化され、0~1 です。

ポリゴン配列のポイントを使用して、Blockオブジェクトの周囲にきめ細かな境界ボックスを表示できます。ドキュメントページの各ポリゴンポイントの位置は、 で使用されているのと同じ手法を使用して計算しますBoundingBoxes。X 座標にドキュメントページの幅を乗算し、Y 座標にドキュメントページの高さを乗算します。

次の例は、ポリゴンの垂直線を表示する方法を示しています。

public void ShowPolygonVerticals(int imageHeight, int imageWidth, List <Point> points, Graphics2D g2d) { g2d.setColor(new Color(0, 212, 0)); Object[] parry = points.toArray(); g2d.setStroke(new BasicStroke(2)); g2d.drawLine(Math.round(((Point) parry[0]).getX() * imageWidth), Math.round(((Point) parry[0]).getY() * imageHeight), Math.round(((Point) parry[3]).getX() * imageWidth), Math.round(((Point) parry[3]).getY() * imageHeight)); g2d.setColor(new Color(255, 0, 0)); g2d.drawLine(Math.round(((Point) parry[1]).getX() * imageWidth), Math.round(((Point) parry[1]).getY() * imageHeight), Math.round(((Point) parry[2]).getX() * imageWidth), Math.round(((Point) parry[2]).getY() * imageHeight)); }

回転角度

Geometry オブジェクトの最後の部分は、テキストの回転角度です。回転角度は、テキストの回転度を表す 0~360 の数値です。