注意事項
Foundry Tools 中的 Azure Vision 中的影像分析 4.0 服務已被棄用,將於 2028 年 9 月 25 日停用,之後對該服務的呼叫將無法使用。 我們建議您切換到 遷移指南中列出的可用替代方案之一。
本文說明如何利用 Image Analysis REST API 或用戶端函式庫來設定基本的影像標籤腳本。 Analyze Image 服務提供 AI 演算法,用於處理影像並回傳有關其視覺特徵的資訊。 請依照以下步驟安裝套件到你的應用程式,並試用範例程式碼。
使用 C# 的影像分析用戶端函式庫來分析圖片內容標籤。 此快速入門定義了一個方法, AnalyzeImageUrl利用用戶端物件分析遠端影像並列印結果。
參考文件 | 函式庫原始碼 | Package (NuGet) | Samples
提示
你也可以分析當地影像。 請參閱 ComputerVisionClient 的方法,例如 AnalyzeImageInStreamAsync。 或者,請參閱 GitHub 上的範例程式碼,了解涉及本地影像的情境。
提示
Analyze Image API 除了產生影像標籤外,還能執行許多不同的操作。 請參閱 影像分析操作指南 ,裡面展示所有可用功能的範例。
先決條件
- 一個 Azure 訂閱。 你可以 免費創建一個。
Visual Studio IDE 或目前版本的.NET Core 。- 當你擁有 Azure 訂閱後,在 Azure 入口網站建立一個 電腦視覺資源,以取得你的金鑰和端點。 部署完成後,選擇 前往資源。
- 你需要從你建立的資源中取得金鑰和端點,才能將你的應用程式連接到 Foundry Tools 中的 Azure Vision。
- 你可以先用免費價格等級
F0()試用服務,之後再升級到付費等級以進行生產。
建立環境變數
在這個例子中,將你的憑證寫入執行該應用程式的本地機器上的環境變數。
前往 Azure 入口網站。 如果您在 必要條件 區段創建的資源成功部署,請在 下一步驟 中選擇 前往資源。 你可以在 Face 資源的「金鑰與端點」頁面的資源管理中找到你的金鑰和端點。 你的資源金鑰和你的 Azure 訂閱 ID 不一樣。
要設定你的鍵和端點的環境變數,請打開主控台視窗,並依照作業系統和開發環境的指示操作。
- 若要設定
VISION_KEY環境變數,請將<your_key>取代為您資源的其中一個金鑰。 - 要設定
VISION_ENDPOINT環境變數,請將<your_endpoint>替換為你的資源端點。
重要
我們建議使用 Microsoft Entra ID 搭配 Azure 資源的 受控身份 驗證,以避免將憑證儲存在雲端執行的應用程式中。
使用 API 金鑰時要小心。 不要直接把 API 金鑰放進程式碼裡,也絕對不要公開發佈。 如果使用 API 金鑰,請將它們安全存放在 Azure Key Vault,定期更換金鑰,並透過基於角色的存取控制和網路存取限制來限制對 Azure Key Vault 的存取。 欲了解更多如何在應用程式中安全使用 API 金鑰的資訊,請參見 API 金鑰搭配 Azure Key Vault。
如需了解更多有關 AI 服務安全性的資訊,請參閱 驗證對 Azure AI 服務的請求。
setx VISION_KEY <your_key>
setx VISION_ENDPOINT <your_endpoint>
加入環境變數後,可能需要重新啟動所有會讀取環境變數的執行程式,包括主控台視窗。
分析影像
建立一個新的 C# 應用程式。
使用 Visual Studio,建立一個新的 .NET Core 應用程式。
安裝用戶端函式庫
建立新專案後,以滑鼠右鍵按一下 [方案總管] 中的專案解決方案,然後選取 [管理 NuGet 套件],以安裝用戶端程式庫。 在開啟套件管理器時,選擇 Browse,勾選 Include prerelease,並搜尋
Microsoft.Azure.CognitiveServices.Vision.ComputerVision。 選擇版本7.0.0,然後 安裝。從專案目錄中,在你偏好的編輯器或整合開發環境中開啟 Program.cs 檔案。 貼上以下程式碼:
using System; using System.Collections.Generic; using Microsoft.Azure.CognitiveServices.Vision.ComputerVision; using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models; using System.Threading.Tasks; using System.IO; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Threading; using System.Linq; namespace ComputerVisionQuickstart { class Program { // Add your Computer Vision key and endpoint static string key = Environment.GetEnvironmentVariable("VISION_KEY"); static string endpoint = Environment.GetEnvironmentVariable("VISION_ENDPOINT"); // URL image used for analyzing an image (image of puppy) private const string ANALYZE_URL_IMAGE = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/landmark.jpg"; static void Main(string[] args) { Console.WriteLine("Azure Cognitive Services Computer Vision - .NET quickstart example"); Console.WriteLine(); // Create a client ComputerVisionClient client = Authenticate(endpoint, key); // Analyze an image to get features and other properties. AnalyzeImageUrl(client, ANALYZE_URL_IMAGE).Wait(); } /* * AUTHENTICATE * Creates a Computer Vision client used by each example. */ public static ComputerVisionClient Authenticate(string endpoint, string key) { ComputerVisionClient client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(key)) { Endpoint = endpoint }; return client; } public static async Task AnalyzeImageUrl(ComputerVisionClient client, string imageUrl) { Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("ANALYZE IMAGE - URL"); Console.WriteLine(); // Creating a list that defines the features to be extracted from the image. List<VisualFeatureTypes?> features = new List<VisualFeatureTypes?>() { VisualFeatureTypes.Tags }; Console.WriteLine($"Analyzing the image {Path.GetFileName(imageUrl)}..."); Console.WriteLine(); // Analyze the URL image ImageAnalysis results = await client.AnalyzeImageAsync(imageUrl, visualFeatures: features); // Image tags and their confidence score Console.WriteLine("Tags:"); foreach (var tag in results.Tags) { Console.WriteLine($"{tag.Name} {tag.Confidence}"); } Console.WriteLine(); } } }重要
我們建議使用 Microsoft Entra ID 搭配 Azure 資源的 受控身份 驗證,以避免將憑證儲存在雲端執行的應用程式中。
使用 API 金鑰時要小心。 不要直接把 API 金鑰放進程式碼裡,也絕對不要公開發佈。 如果使用 API 金鑰,請將它們安全存放在 Azure Key Vault,定期更換金鑰,並透過基於角色的存取控制和網路存取限制來限制對 Azure Key Vault 的存取。 欲了解更多如何在應用程式中安全使用 API 金鑰的資訊,請參見 API 金鑰搭配 Azure Key Vault。
如需了解更多有關 AI 服務安全性的資訊,請參閱 驗證對 Azure AI 服務的請求。
執行應用程式
請點擊 IDE 視窗頂端的 除錯 按鈕來執行應用程式。
產出
你的操作輸出應該像以下範例。
----------------------------------------------------------
ANALYZE IMAGE - URL
Analyzing the image sample16.png...
Tags:
grass 0.9957543611526489
dog 0.9939157962799072
mammal 0.9928356409072876
animal 0.9918001890182495
dog breed 0.9890419244766235
pet 0.974603533744812
outdoor 0.969241738319397
companion dog 0.906731367111206
small greek domestic dog 0.8965123891830444
golden retriever 0.8877675533294678
labrador retriever 0.8746421337127686
puppy 0.872604250907898
ancient dog breeds 0.8508287668228149
field 0.8017748594284058
retriever 0.6837497353553772
brown 0.6581960916519165
清理資源
如果你想清理並移除 Foundry Tools 的訂閱,可以刪除該資源或資源群組。 刪除資源群組也會刪除與之相關的其他資源。
相關內容
在這個快速入門中,你學會了如何安裝影像分析客戶端函式庫並進行基本的影像分析呼叫。 接著,了解更多關於影像分析API的功能。
使用 Python 的影像分析客戶端函式庫來分析遠端影像的內容標籤。
提示
你也可以分析當地影像。 請參閱 ComputerVisionClientOperationsMixin 方法,例如 analyze_image_in_stream。 或者,請參閱 GitHub 上的
提示
Analyze Image API 除了產生影像標籤外,還能執行許多不同的操作。 請參閱 影像分析操作指南 ,裡面展示所有可用功能的範例。
參考文件 | 函式庫原始碼 | 套件(PiPy) | Samples
先決條件
- 一個 Azure 訂閱。 你可以 免費創建一個。
-
Python 3.x。
- 你的Python安裝應該包含pip。 你可以透過命令列執行
pip --version來確認你有沒有安裝 PIP。 安裝最新版本的 Python 來取得 PIP。
- 你的Python安裝應該包含pip。 你可以透過命令列執行
- 當你擁有 Azure 訂閱後,在 Azure 入口網站建立一個 電腦視覺資源,以取得你的金鑰和端點。 部署完成後,選擇 前往資源。
- 你需要從你建立的資源中取得金鑰和端點,才能將你的應用程式連接到 Foundry Tools 中的 Azure Vision。
- 你可以先用免費價格等級
F0()試用服務,之後再升級到付費等級以進行生產。
建立環境變數
在這個例子中,將你的憑證寫入執行該應用程式的本地機器上的環境變數。
前往 Azure 入口網站。 如果您在 必要條件 區段創建的資源成功部署,請在 下一步驟 中選擇 前往資源。 你可以在 Face 資源的「金鑰與端點」頁面的資源管理中找到你的金鑰和端點。 你的資源金鑰和你的 Azure 訂閱 ID 不一樣。
要設定你的鍵和端點的環境變數,請打開主控台視窗,並依照作業系統和開發環境的指示操作。
- 若要設定
VISION_KEY環境變數,請將<your_key>取代為您資源的其中一個金鑰。 - 要設定
VISION_ENDPOINT環境變數,請將<your_endpoint>替換為你的資源端點。
重要
我們建議使用 Microsoft Entra ID 搭配 Azure 資源的 受控身份 驗證,以避免將憑證儲存在雲端執行的應用程式中。
使用 API 金鑰時要小心。 不要直接把 API 金鑰放進程式碼裡,也絕對不要公開發佈。 如果使用 API 金鑰,請將它們安全存放在 Azure Key Vault,定期更換金鑰,並透過基於角色的存取控制和網路存取限制來限制對 Azure Key Vault 的存取。 欲了解更多如何在應用程式中安全使用 API 金鑰的資訊,請參見 API 金鑰搭配 Azure Key Vault。
如需了解更多有關 AI 服務安全性的資訊,請參閱 驗證對 Azure AI 服務的請求。
setx VISION_KEY <your_key>
setx VISION_ENDPOINT <your_endpoint>
加入環境變數後,可能需要重新啟動所有會讀取環境變數的執行程式,包括主控台視窗。
分析影像
安裝客戶端函式庫。
你可以用以下方式安裝客戶端函式庫:
pip install --upgrade azure-cognitiveservices-vision-computervision另外也安裝 Pillow 函式庫。
pip install pillow建立一個新的 Python 應用程式。
建立一個新的 Python 檔案。 你可以把它命名 為 quickstart-file.py,例如。
在文字編輯器或 IDE 中開啟 quickstart-file.py ,然後貼上以下程式碼。
from azure.cognitiveservices.vision.computervision import ComputerVisionClient from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes from msrest.authentication import CognitiveServicesCredentials from array import array import os from PIL import Image import sys import time ''' Authenticate Authenticates your credentials and creates a client. ''' subscription_key = os.environ["VISION_KEY"] endpoint = os.environ["VISION_ENDPOINT"] computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key)) ''' END - Authenticate ''' ''' Quickstart variables These variables are shared by several examples ''' # Images used for the examples: Describe an image, Categorize an image, Tag an image, # Detect faces, Detect adult or racy content, Detect the color scheme, # Detect domain-specific content, Detect image types, Detect objects images_folder = os.path.join (os.path.dirname(os.path.abspath(__file__)), "images") remote_image_url = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/landmark.jpg" ''' END - Quickstart variables ''' ''' Tag an Image - remote This example returns a tag (key word) for each thing in the image. ''' print("===== Tag an image - remote =====") # Call API with remote image tags_result_remote = computervision_client.tag_image(remote_image_url ) # Print results with confidence score print("Tags in the remote image: ") if (len(tags_result_remote.tags) == 0): print("No tags detected.") else: for tag in tags_result_remote.tags: print("'{}' with confidence {:.2f}%".format(tag.name, tag.confidence * 100)) print() ''' END - Tag an Image - remote ''' print("End of Computer Vision quickstart.")使用
python指令在快速啟動檔案中執行應用程式。python quickstart-file.py
產出
操作的輸出應該如下範例。
===== Tag an image - remote =====
Tags in the remote image:
'outdoor' with confidence 99.00%
'building' with confidence 98.81%
'sky' with confidence 98.21%
'stadium' with confidence 98.17%
'ancient rome' with confidence 96.16%
'ruins' with confidence 95.04%
'amphitheatre' with confidence 93.99%
'ancient roman architecture' with confidence 92.65%
'historic site' with confidence 89.55%
'ancient history' with confidence 89.54%
'history' with confidence 86.72%
'archaeological site' with confidence 84.41%
'travel' with confidence 65.85%
'large' with confidence 61.02%
'city' with confidence 56.57%
End of Azure Vision quickstart.
清理資源
如果你想清理並移除 Foundry Tools 的訂閱,可以刪除該資源或資源群組。 刪除資源群組也會刪除與之相關的其他資源。
下一步
在這個快速入門中,你學會了如何安裝影像分析客戶端函式庫並進行基本的影像分析呼叫。 接著,進一步了解 Analyze Image API 的功能。
使用 Java 的影像分析客戶端函式庫,分析遠端影像中的標籤、文字描述、臉部、成人內容等。
提示
你也可以分析當地影像。 請參閱 ComputerVision 的方法,例如 AnalyzeImage。 或者,請參閱 GitHub 上的
提示
Analyze Image API 除了產生影像標籤外,還能執行許多不同的操作。 請參閱 影像分析操作指南 ,裡面展示所有可用功能的範例。
參考文件 | 程式庫原始碼 |Artifact (Maven) | 範例
先決條件
- 一個 Azure 訂閱。 你可以 免費創建一個。
- 目前版本的 Java 開發套件(JDK)。
- Gradle 建置工具,或其他相依管理工具。
- 當你擁有 Azure 訂閱後,在 Azure 入口網站建立一個 電腦視覺資源,以取得你的金鑰和端點。 部署完成後,選擇 前往資源。
- 你需要從你建立的資源中取得金鑰和端點,才能將你的應用程式連接到 Foundry Tools 中的 Azure Vision。
- 你可以先用免費價格等級
F0()試用服務,之後再升級到付費等級以進行生產。
建立環境變數
在這個例子中,將你的憑證寫入執行該應用程式的本地機器上的環境變數。
前往 Azure 入口網站。 如果您在 必要條件 區段創建的資源成功部署,請在 下一步驟 中選擇 前往資源。 你可以在 Face 資源的「金鑰與端點」頁面的資源管理中找到你的金鑰和端點。 你的資源金鑰和你的 Azure 訂閱 ID 不一樣。
要設定你的鍵和端點的環境變數,請打開主控台視窗,並依照作業系統和開發環境的指示操作。
- 若要設定
VISION_KEY環境變數,請將<your_key>取代為您資源的其中一個金鑰。 - 要設定
VISION_ENDPOINT環境變數,請將<your_endpoint>替換為你的資源端點。
重要
我們建議使用 Microsoft Entra ID 搭配 Azure 資源的 受控身份 驗證,以避免將憑證儲存在雲端執行的應用程式中。
使用 API 金鑰時要小心。 不要直接把 API 金鑰放進程式碼裡,也絕對不要公開發佈。 如果使用 API 金鑰,請將它們安全存放在 Azure Key Vault,定期更換金鑰,並透過基於角色的存取控制和網路存取限制來限制對 Azure Key Vault 的存取。 欲了解更多如何在應用程式中安全使用 API 金鑰的資訊,請參見 API 金鑰搭配 Azure Key Vault。
如需了解更多有關 AI 服務安全性的資訊,請參閱 驗證對 Azure AI 服務的請求。
setx VISION_KEY <your_key>
setx VISION_ENDPOINT <your_endpoint>
加入環境變數後,可能需要重新啟動所有會讀取環境變數的執行程式,包括主控台視窗。
分析影像
建立一個新的 Gradle 專案。
在主控台視窗(例如 cmd、PowerShell 或 Bash)中,建立一個新的應用程式目錄,然後導覽到裡面。
mkdir myapp && cd myapp從你的工作目錄執行這個
gradle init指令。 此指令會建立 Gradle 的重要建置檔案,包括 build.gradle.kts,這些檔案在執行時用於建立與配置您的應用程式。gradle init --type basic當被要求選擇 DSL 時,請選擇 Kotlin。
安裝客戶端函式庫。
這份快速入門指南使用 Gradle 相依管理工具。 你可以在 Maven 中央倉庫找到客戶端函式庫及其他相依管理者的資訊。
找到 build.gradle.kts ,並用你偏好的 IDE 或文字編輯器開啟。 接著複製並貼上以下的建置設定到檔案中。 此配置定義專案為一個Java應用程式,其入口點為類別
ImageAnalysisQuickstart。 它會匯入 Azure Vision 函式庫。plugins { java application } application { mainClass.set("ImageAnalysisQuickstart") } repositories { mavenCentral() } dependencies { implementation(group = "com.microsoft.azure.cognitiveservices", name = "azure-cognitiveservices-computervision", version = "1.0.9-beta") }建立一個 Java 檔案。
從你的工作目錄中,執行以下指令建立專案來源資料夾:
mkdir -p src/main/java進入新資料夾,建立一個名為 ImageAnalysisQuickstart.java 的檔案。
在你偏好的編輯器或 IDE 中開啟 ImageAnalysisQuickstart.java ,並貼上以下程式碼。
import com.microsoft.azure.cognitiveservices.vision.computervision.*; import com.microsoft.azure.cognitiveservices.vision.computervision.implementation.ComputerVisionImpl; import com.microsoft.azure.cognitiveservices.vision.computervision.models.*; import java.io.*; import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.UUID; public class ImageAnalysisQuickstart { // Use environment variables static String key = System.getenv("VISION_KEY"); static String endpoint = System.getenv("VISION_ENDPOINT"); public static void main(String[] args) { System.out.println("\nAzure Cognitive Services Computer Vision - Java Quickstart Sample"); // Create an authenticated Computer Vision client. ComputerVisionClient compVisClient = Authenticate(key, endpoint); // Analyze local and remote images AnalyzeRemoteImage(compVisClient); } public static ComputerVisionClient Authenticate(String key, String endpoint){ return ComputerVisionManager.authenticate(key).withEndpoint(endpoint); } public static void AnalyzeRemoteImage(ComputerVisionClient compVisClient) { /* * Analyze an image from a URL: * * Set a string variable equal to the path of a remote image. */ String pathToRemoteImage = "https://github.com/Azure-Samples/cognitive-services-sample-data-files/raw/master/ComputerVision/Images/faces.jpg"; // This list defines the features to be extracted from the image. List<VisualFeatureTypes> featuresToExtractFromRemoteImage = new ArrayList<>(); featuresToExtractFromRemoteImage.add(VisualFeatureTypes.TAGS); System.out.println("\n\nAnalyzing an image from a URL ..."); try { // Call the Computer Vision service and tell it to analyze the loaded image. ImageAnalysis analysis = compVisClient.computerVision().analyzeImage().withUrl(pathToRemoteImage) .withVisualFeatures(featuresToExtractFromRemoteImage).execute(); // Display image tags and confidence values. System.out.println("\nTags: "); for (ImageTag tag : analysis.tags()) { System.out.printf("\'%s\' with confidence %f\n", tag.name(), tag.confidence()); } } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } // END - Analyze an image from a URL. }回到專案根目錄,然後用以下方式建立應用程式:
gradle build請用以下指令執行:
gradle run
產出
操作的輸出應該如下範例。
Azure Vision - Java Quickstart Sample
Analyzing an image from a URL ...
Tags:
'person' with confidence 0.998895
'human face' with confidence 0.997437
'smile' with confidence 0.991973
'outdoor' with confidence 0.985962
'happy' with confidence 0.969785
'clothing' with confidence 0.961570
'friendship' with confidence 0.946441
'tree' with confidence 0.917331
'female person' with confidence 0.890976
'girl' with confidence 0.888741
'social group' with confidence 0.872044
'posing' with confidence 0.865493
'adolescent' with confidence 0.857371
'love' with confidence 0.852553
'laugh' with confidence 0.850097
'people' with confidence 0.849922
'lady' with confidence 0.844540
'woman' with confidence 0.818172
'group' with confidence 0.792975
'wedding' with confidence 0.615252
'dress' with confidence 0.517169
清理資源
如果你想清理並移除 Foundry Tools 的訂閱,可以刪除該資源或資源群組。 刪除資源群組也會刪除與之相關的其他資源。
下一步
在這個快速入門中,你學會了如何安裝影像分析客戶端函式庫並進行基本的影像分析呼叫。 接著,進一步了解 Analyze Image API 的功能。
使用 JavaScript 的影像分析客戶端函式庫來分析遠端影像中的內容標籤。
提示
你也可以分析當地影像。 請參閱 ComputerVisionClient 的方法,例如 describeImageInStream。 或者,請參閱 GitHub 上的
提示
Analyze Image API 除了產生影像標籤外,還能執行許多不同的操作。 請參閱 影像分析操作指南 ,裡面展示所有可用功能的範例。
先決條件
- 一個 Azure 訂閱。 你可以 免費創建一個。
- 目前的 Node.js 版本。
- 當你擁有 Azure 訂閱後,在 Azure 入口網站建立一個 電腦視覺資源,以取得你的金鑰和端點。 部署完成後,選擇 前往資源。
- 你需要從你建立的資源中取得金鑰和端點,才能將你的應用程式連接到 Foundry Tools 中的 Azure Vision。
- 你可以先用免費價格等級
F0()試用服務,之後再升級到付費等級以進行生產。
建立環境變數
在這個例子中,將你的憑證寫入執行該應用程式的本地機器上的環境變數。
前往 Azure 入口網站。 如果您在 必要條件 區段創建的資源成功部署,請在 下一步驟 中選擇 前往資源。 你可以在 Face 資源的「金鑰與端點」頁面的資源管理中找到你的金鑰和端點。 你的資源金鑰和你的 Azure 訂閱 ID 不一樣。
要設定你的鍵和端點的環境變數,請打開主控台視窗,並依照作業系統和開發環境的指示操作。
- 若要設定
VISION_KEY環境變數,請將<your_key>取代為您資源的其中一個金鑰。 - 要設定
VISION_ENDPOINT環境變數,請將<your_endpoint>替換為你的資源端點。
重要
我們建議使用 Microsoft Entra ID 搭配 Azure 資源的 受控身份 驗證,以避免將憑證儲存在雲端執行的應用程式中。
使用 API 金鑰時要小心。 不要直接把 API 金鑰放進程式碼裡,也絕對不要公開發佈。 如果使用 API 金鑰,請將它們安全存放在 Azure Key Vault,定期更換金鑰,並透過基於角色的存取控制和網路存取限制來限制對 Azure Key Vault 的存取。 欲了解更多如何在應用程式中安全使用 API 金鑰的資訊,請參見 API 金鑰搭配 Azure Key Vault。
如需了解更多有關 AI 服務安全性的資訊,請參閱 驗證對 Azure AI 服務的請求。
setx VISION_KEY <your_key>
setx VISION_ENDPOINT <your_endpoint>
加入環境變數後,可能需要重新啟動所有會讀取環境變數的執行程式,包括主控台視窗。
分析影像
建立新的 Node.js 應用程式
在主控台視窗(例如 cmd、PowerShell 或 Bash)中,建立一個新的應用程式目錄,然後導覽到裡面。
mkdir myapp && cd myapp執行
npm init指令建立一個帶有 package.json 檔案的節點應用程式。npm init安裝用戶端函式庫
安裝
ms-rest-azure和@azure/cognitiveservices-computervisionnpm 套件:npm install @azure/cognitiveservices-computervision另外安裝非同步模組:
npm install async你的應用程式
package.json檔案會更新依賴關係。建立一個新檔案, index.js。
在文字編輯器中開啟 index.js ,並貼上以下程式碼。
'use strict'; const async = require('async'); const fs = require('fs'); const https = require('https'); const path = require("path"); const createReadStream = require('fs').createReadStream const sleep = require('util').promisify(setTimeout); const ComputerVisionClient = require('@azure/cognitiveservices-computervision').ComputerVisionClient; const ApiKeyCredentials = require('@azure/ms-rest-js').ApiKeyCredentials; /** * AUTHENTICATE * This single client is used for all examples. */ const key = process.env.VISION_KEY; const endpoint = process.env.VISION_ENDPOINT; const computerVisionClient = new ComputerVisionClient( new ApiKeyCredentials({ inHeader: { 'Ocp-Apim-Subscription-Key': key } }), endpoint); /** * END - Authenticate */ function computerVision() { async.series([ async function () { /** * DETECT TAGS * Detects tags for an image, which returns: * all objects in image and confidence score. */ console.log('-------------------------------------------------'); console.log('DETECT TAGS'); console.log(); // Image of different kind of dog. const tagsURL = 'https://github.com/Azure-Samples/cognitive-services-sample-data-files/blob/master/ComputerVision/Images/house.jpg'; // Analyze URL image console.log('Analyzing tags in image...', tagsURL.split('/').pop()); const tags = (await computerVisionClient.analyzeImage(tagsURL, { visualFeatures: ['Tags'] })).tags; console.log(`Tags: ${formatTags(tags)}`); // Format tags for display function formatTags(tags) { return tags.map(tag => (`${tag.name} (${tag.confidence.toFixed(2)})`)).join(', '); } /** * END - Detect Tags */ console.log(); console.log('-------------------------------------------------'); console.log('End of quickstart.'); }, function () { return new Promise((resolve) => { resolve(); }) } ], (err) => { throw (err); }); } computerVision();用快速啟動檔上的指令執行應用程式
node。node index.js
產出
操作的輸出應該如下範例。
-------------------------------------------------
DETECT TAGS
Analyzing tags in image... sample16.png
Tags: grass (1.00), dog (0.99), mammal (0.99), animal (0.99), dog breed (0.99), pet (0.97), outdoor (0.97), companion dog (0.91), small greek domestic dog (0.90), golden retriever (0.89), labrador retriever (0.87), puppy (0.87), ancient dog breeds (0.85), field (0.80), retriever (0.68), brown (0.66)
-------------------------------------------------
End of quickstart.
清理資源
如果你想清理並移除 Foundry Tools 的訂閱,可以刪除該資源或資源群組。 刪除資源群組也會刪除與之相關的其他資源。
下一步
在這個快速入門中,你學會了如何安裝影像分析客戶端函式庫並進行基本的影像分析呼叫。 接著,進一步了解 Analyze Image API 的功能。
使用 Image Analysis REST API 來分析圖片中的標籤。
提示
Analyze Image API 除了產生影像標籤外,還能執行許多不同的操作。 請參閱 影像分析操作指南 ,裡面展示所有可用功能的範例。
註
這個快速入門是使用 cURL 指令來呼叫 REST API。 你也可以用程式語言呼叫 REST API。 請參考GitHub範例,範例包括 C#、Python、Java 以及 JavaScript。
先決條件
- 一個 Azure 訂閱。 你可以 免費創建一個。
- 當你擁有 Azure 訂閱後,在 Azure 入口網站建立一個 電腦視覺資源,以取得你的金鑰和端點。 部署完成後,選擇 前往資源。
- 你需要從你建立的資源中取得金鑰和端點,才能將你的應用程式連接到 Foundry Tools 中的 Azure Vision。
- 你可以先用免費價格等級
F0()試用服務,之後再升級到付費等級以進行生產。
- 已安裝 cURL。
分析一張影像
要分析影像中的各種視覺特徵,請執行以下步驟:
將以下指令複製到文字編輯器中。
curl.exe -H "Ocp-Apim-Subscription-Key: <yourKey>" -H "Content-Type: application/json" "https://westcentralus.api.cognitive.microsoft.com/vision/v3.2/analyze?visualFeatures=Tags" -d "{'url':'https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png'}"在需要的地方,請對指令做以下修改:
- 將
<yourKey>的值替換為您電腦視覺資源的金鑰。 - 將請求 URL
westcentralus.api.cognitive.microsoft.com的第一部分()替換成你自己的端點 URL。註
2019 年 7 月 1 日後新增的資源將使用自訂子網域名稱。 欲了解更多資訊及完整區域端點清單,請參閱 Foundry Tools 的自訂子網域名稱。
- 可選擇性地將請求主體
https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png中的圖片網址()改為要分析的其他圖片的網址。
- 將
打開命令提示字元視窗。
把你從文字編輯器編輯好的
curl指令貼到命令提示字元視窗,然後執行該指令。
檢視回應
成功回應會以 JSON 格式回傳。 範例應用程式解析並在命令提示字元視窗中顯示成功的回應,類似以下範例:
{
"tags":[
{
"name":"text",
"confidence":0.9992657899856567
},
{
"name":"post-it note",
"confidence":0.9879657626152039
},
{
"name":"handwriting",
"confidence":0.9730165004730225
},
{
"name":"rectangle",
"confidence":0.8658561706542969
},
{
"name":"paper product",
"confidence":0.8561884760856628
},
{
"name":"purple",
"confidence":0.5961999297142029
}
],
"requestId":"2788adfc-8cfb-43a5-8fd6-b3a9ced35db2",
"metadata":{
"height":945,
"width":1000,
"format":"Jpeg"
},
"modelVersion":"2021-05-01"
}
下一步
在這個快速入門中,你學會了如何使用 REST API 進行基本的影像分析呼叫。 接著,進一步了解 Analyze Image API 的功能。