您目前查看的是 Apigee Edge 說明文件。
前往 Apigee X 說明文件。 info
版本:2.0.2
使用機器學習模型探索圖片中的內容和文字。
這項內容提供設定及使用這項擴充功能的參考資料。如要透過 API Proxy 使用這項擴充功能,請務必先完成下列步驟:
為服務帳戶啟用 Cloud Vision API。
如果使用 Cloud Storage 做為圖片來源,您也需要按照 Google Cloud Storage 擴充功能參考資料所述,授予這項擴充功能 Cloud Storage 的存取權。
如果服務帳戶具有 Cloud Vision 的權限 (以及 Cloud Storage 的權限,如果您使用這項服務),請使用 Google Cloud 控制台為服務帳戶產生金鑰。
關於 Cloud Vision
Google Cloud Vision API 會使用機器學習模型分析圖片。您可以訓練模型供 API 使用,或使用內建模型。
Cloud Vision 會使用內建模型,將圖片分類為「摩天大樓」、「帆船」、「獅子」或「艾菲爾鐵塔」等類別。可偵測圖片中的物件、臉孔、標誌和地標,以及圖片中的文字。
範例
下列範例說明如何使用 ExtensionCallout 政策,設定 Cloud Vision 擴充功能動作的支援功能。
偵測標籤
在下列範例中,擴充功能的 detectLabels 動作會取得 image_uri 中的圖片,並傳送至 Cloud Vision API 進行分析。API 會檢查圖片,判斷哪些標籤適用於圖片內容。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ConnectorCallout async="false" continueOnError="true" enabled="true" name="Cloud-Vision-Extension">
<DisplayName>Cloud Vision Extension</DisplayName>
<Connector>cloud-vision-extension-example</Connector>
<Action>detectLabels</Action>
<Input><![CDATA[
{
"image_uri" : "gs://cloud-vision-example/empire-state-building.jpg"
}
]]></Input>
<Output>vision.labels.retrieved</Output>
</ConnectorCallout>
下列「指派訊息」政策會使用儲存擴充功能回應的變數值,指派回應酬載。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Get-Image-Labels">
<DisplayName>Get Image Labels</DisplayName>
<AssignTo type="response" createNew="false"/>
<Set>
<Payload contentType="application/json">{vision.labels.retrieved}</Payload>
</Set>
</AssignMessage>
假設您提供一張市區圖片,其中有一棟非常高的建築物,可能會收到類似以下的回覆:
{
"labels": [
{
"locations": [
],
"properties": [
],
"mid": "/m/0j_s4",
"locale": "",
"description": "metropolitan area",
"score": 0.9868549704551697,
"confidence": 0,
"topicality": 0.9868549704551697,
"boundingPoly": null
},
{
"locations": [
],
"properties": [
],
"mid": "/m/079cl",
"locale": "",
"description": "skyscraper",
"score": 0.966157853603363,
"confidence": 0,
"topicality": 0.966157853603363,
"boundingPoly": null
}
]
}
偵測文字
在下列範例中,擴充功能 detectText 動作會取得 image_uri 中的圖片,並傳送至 Cloud Vision API 進行分析。API 會檢查圖片,並辨識圖片中的文字。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ConnectorCallout async="false" continueOnError="true" enabled="true" name="Cloud-Vision-Text">
<DisplayName>Cloud Vision Text</DisplayName>
<Connector>cloud-vision-extension-example</Connector>
<Action>detectText</Action>
<Input><![CDATA[
{
"image_uri" : "gs://cloud-vision-example/parking-signs1.jpg"
}
]]></Input>
<Output>vision.text.retrieved</Output>
</ConnectorCallout>
下列「指派訊息」政策會使用儲存擴充功能回應的變數值,指派回應酬載。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Get-Image-Text">
<DisplayName>Get Image Text</DisplayName>
<AssignTo type="response" createNew="false"/>
<Set>
<Payload contentType="application/json">{vision.text.retrieved}</Payload>
</Set>
</AssignMessage>
如果圖片包含停車場的標誌,可能會收到類似以下的回覆:
{
"text": [
{
"locations": [
],
"properties": [
],
"mid": "",
"locale": "en",
"description": "RESERVED\nVISITORPARKING\nPARKING\nONLY>\n$150 FINE\n",
"score": 0,
"confidence": 0,
"topicality": 0,
"boundingPoly": {
"vertices": [
{
"x": 64,
"y": 56
},
{
"x": 378,
"y": 56
},
{
"x": 378,
"y": 218
},
{
"x": 64,
"y": 218
}
]
}
},
{
"locations": [
],
"properties": [
],
"mid": "",
"locale": "",
"description": "RESERVED",
"score": 0,
"confidence": 0,
"topicality": 0,
"boundingPoly": {
"vertices": [
{
"x": 243,
"y": 56
},
{
"x": 378,
"y": 56
},
{
"x": 378,
"y": 84
},
{
"x": 243,
"y": 84
}
]
}
}
]
}
動作
detectLabels
偵測並擷取指定圖片中實體的相關資訊。偵測到的實體涵蓋廣泛的類別群組。舉例來說,你可以使用這項動作辨識物件、地點、活動、動物物種和產品等。
此外,請務必參閱 Cloud Vision API 說明文件。
要求參數
| 參數 | 說明 | 類型 | 預設 | 必填 |
|---|---|---|---|---|
| image_uri | 圖片來源。來源可以是網際網路或 Google Cloud Storage (格式:gs://bucketname/filename)。如果來源是 Google Cloud Storage,圖片檔案必須設為公開。 |
字串 | 無。 | 是。 |
語法
<Input><![CDATA[{
"image_uri" : "uri-of-image-to-analyze"
}
]]></Input>
範例
在下列範例中,擴充功能的 detectLabels 動作會將指定圖片傳送至 Vision API 進行分析。
<Input><![CDATA[
{
"image_uri" : "gs://cloud-vision-example/empire-state-building.jpg"
}
]]></Input>
回應
包含 labels 標籤陣列的物件,代表圖片中偵測到的實體。詳情請參閱「偵測標籤」。
detectText
偵測並擷取指定圖片中的文字。
要求參數
| 參數 | 說明 | 類型 | 預設 | 必填 |
|---|---|---|---|---|
| image_uri | 圖片來源。來源可以是網際網路或 Google Cloud Storage (格式:gs://bucketname/filename)。如果來源是 Google Cloud Storage,圖片檔案必須設為公開。 |
字串 | 無。 | 是。 |
語法
<Input><![CDATA[
{
"image_uri" : "uri-of-image-to-analyze"
}
]]></Input>
範例
在下列範例中,擴充功能的 detectText 動作會將指定圖片傳送至 Vision API 進行分析。
<Input><![CDATA[
{
"image_uri" : "gs://cloud-vision-example/parking-signs1.jpg"
}
]]></Input>
回應
包含偵測到文字的 text 陣列物件。詳情請參閱「偵測標籤」。
設定參考資料
設定及部署這個擴充功能,以便在 API Proxy 中使用時,請參考下列資訊。如要瞭解如何使用 Apigee 控制台設定擴充功能,請參閱「新增及設定擴充功能」。
常見的擴充功能屬性
每個擴充功能都有下列屬性。
| 屬性 | 說明 | 預設 | 必要 |
|---|---|---|---|
name |
您要為這項擴充功能設定的名稱。 | 無 | 相容 |
packageName |
Apigee Edge 指定的擴充功能套件名稱。 | 無 | 相容 |
version |
擴充功能的擴充功能套件版本號碼。 | 無 | 相容 |
configuration |
您要新增的擴充功能專屬的設定值。請參閱「這個擴充功能套件的屬性」一文 | 無 | 相容 |