

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

# 文檔頁面上的項目位置
<a name="text-location"></a>

Amazon Textract 操作會返回文檔頁面上找到的商品的位置和幾何形狀。[DetectDocumentText](API_DetectDocumentText.md)和[GetDocumentTextDetection](API_GetDocumentTextDetection.md)返回線和單詞的位置和幾何，而[AnalyzeDocument](API_AnalyzeDocument.md)和[GetDocumentAnalysis](API_GetDocumentAnalysis.md)返回鍵/值組、表格、單元格和選擇元素的位置和幾何。

若要確定項目在文件頁面上的位置，請使用週框 ([Geometry](API_Geometry.md)）由 Amazon Textract 操作返回的信息[Block](API_Block.md)物件。所以此`Geometry`物件包含兩類檢測到的項目的位置和幾何資訊：
+ 軸對齊[BoundingBox](API_BoundingBox.md)物件，該物件包含左上方座標以及項目的寬度和高度。
+ 描述項目輪廓的多邊形對象，指定為[Point](API_Point.md)對象包含`X`(水平軸) 和`Y`（垂直軸）每個點的文檔頁面座標。

JSON`Block`物件看起來類似以下的內容。請注意`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 檢測文檔文本](detecting-document-text.md)。範例輸出類似如下。

![](http://docs.aws.amazon.com/zh_tw/textract/latest/dg/images/janedoe.png)


## Bounding Box (週框方塊)
<a name="bounding-box"></a>

邊框 (`BoundingBox`）具有以下屬性：
+ 高度 — 週框方塊的高度，以整體文檔頁面高度的比例表示。
+ 左邊 — 左上方方塊的 X 座標，以整體文檔頁面寬度的比例表示。
+ 上方 — 左上方方方塊的 Y 座標，以整體文檔頁面高度的比例表示。
+ 寬度 — 週框方塊的寬度，以整體文件頁面寬度的比例表示。

每個 BoundingBox 屬性都有一個介於 0 和 1 的值。值為整體影像寬度的比例（適用於`Left`和`Width`）或高度（適用於`Height`和`Top`。例如，如果輸入影像為 700 x 200 像素，而周框方塊的左上方座標為 (350,50) 像素，則 API 會傳回`Left`值為 0.5 (350/700) 和一個`Top`值為 0.25 (50/200).

下圖顯示每個 BoundingBox 屬性涵蓋的文檔頁面範圍。

![](http://docs.aws.amazon.com/zh_tw/textract/latest/dg/images/bounding-box.png)


若要顯示正確位置和大小的定框方塊，您必須將 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 示例採用了`response`返回的[DetectDocumentText](API_DetectDocumentText.md)API 操作。

```
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)
```

------

## 多邊形
<a name="polygon"></a>

返回的面`AnalyzeDocument`是[Point](API_Point.md)物件。每個`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));

    }
```