View a markdown version of this page

在文件頁面上尋找項目 - Amazon Textract

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

在文件頁面上尋找項目

Amazon Textract 操作會傳回文件頁面上項目的位置和幾何。DetectDocumentTextGetDocumentTextDetection 會傳回線條和單字的位置和幾何,而 AnalyzeDocumentGetDocumentAnalysis 則會傳回鍵值對、資料表、儲存格和選取元素的位置和幾何。

若要判斷項目在文件頁面上的位置,請使用區塊物件中 Amazon Textract 操作傳回的週框方塊 (幾何) 資訊。Geometry 物件包含兩種偵測到項目的位置和幾何資訊:

  • 軸對齊的 BoundingBox 物件,其中包含左上角座標以及項目的寬度和高度。

  • 描述項目輪廓的多邊形物件,指定為包含每個X(水平軸) 和 Y(垂直軸) 文件頁面座標的點物件陣列。

Block 物件的 JSON 看起來類似以下內容。請注意 BoundingBoxPolygon 欄位。

{ "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" },

您可以使用幾何資訊,在偵測到的項目周圍繪製週框方塊。如需使用 BoundingBoxPolygon資訊在每個單字開頭和結尾的線條和垂直線條周圍繪製方塊的範例,請參閱 使用 Amazon Textract 偵測文件文字。範例輸出類似於以下內容。

包含名稱、地址和出生日期欄位的表單,其中顯示地址為 123 Any Street, Anytown, USA 且出生日期為 12-26-1980 的 Jane Doe 的個人資訊。

Bounding Box (邊界框)

週框方塊 (BoundingBox) 具有下列屬性:

  • 高度:週框方塊的高度與整體文件頁面高度的比例。

  • 左 – 邊界框左上角的 X 座標,以整體文件頁面寬度的比例表示。

  • 頂端 – 邊界框左上角的 Y 座標,以整體文件頁面高度的比率表示。

  • 寬度:週框方塊的寬度與整體文件頁面寬度的比例。

每個 BoundingBox 屬性都有一個介於 0 和 1 的值。值是整體影像寬度 (適用於 LeftWidth) 或高度 (適用於 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)

多邊形

傳回的多邊形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)); }

旋轉角度

幾何物件的最後一部分是文字的旋轉角度。旋轉角度是介於 0 到 360 之間的數字,代表文字旋轉的程度。