快速入門:使用電腦視覺 REST API 和 Python 分析本機影像Quickstart: Analyze a local image using the Computer Vision REST API and Python

在本快速入門中,您將使用電腦視覺的 REST API,來分析本機儲存的影像以擷取視覺功能。In this quickstart, you analyze a locally stored image to extract visual features by using Computer Vision's REST API. 您可以使用分析影像方法,根據影像內容來擷取視覺功能。With the Analyze Image method, you can extract visual features based on image content.

您可以使用 MyBinder (英文) 上的 Jupyter Notebook,以逐步方式執行本快速入門。You can run this quickstart in a step-by step fashion using a Jupyter notebook on MyBinder. 若要啟動 Binder,請選取下列按鈕:To launch Binder, select the following button:

BinderBinder

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶If you don't have an Azure subscription, create a free account before you begin.

必要條件Prerequisites

  • 如果您想要在本機執行此範例,就必須安裝 PythonYou must have Python installed if you want to run the sample locally.
  • 您必須有電腦視覺的訂用帳戶金鑰。You must have a subscription key for Computer Vision. 您可以從試用認知服務取得免費試用的金鑰。You can get a free trial key from Try Cognitive Services. 或者,依照建立認知服務帳戶中的指示訂閱電腦視覺並取得金鑰。Or, follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your key. 然後,分別為名為 COMPUTER_VISION_SUBSCRIPTION_KEYCOMPUTER_VISION_ENDPOINT 的金鑰和服務端點字串建立環境變數Then, create environment variables for the key and service endpoint string, named COMPUTER_VISION_SUBSCRIPTION_KEY and COMPUTER_VISION_ENDPOINT, respectively.
  • 您必須已安裝下列 Python 套件。You must have the following Python packages installed. 您可以使用 pip 來安裝 Python 套件。You can use pip to install Python packages.

建立並執行範例Create and run the sample

若要建立並執行範例,請執行下列步驟:To create and run the sample, do the following steps:

  1. 將下列程式碼複製到文字編輯器。Copy the following code into a text editor.
  2. (選擇性) 將 image_path 的值取代為您要分析之不同影像的路徑和檔案名稱。Optionally, replace the value of image_path with the path and file name of a different image that you want to analyze.
  3. 將程式碼儲存為副檔名為 .py 的檔案。Save the code as a file with an .py extension. 例如: analyze-local-image.pyFor example, analyze-local-image.py.
  4. 開啟命令提示字元視窗。Open a command prompt window.
  5. 出現提示時,使用 python 命令執行範例。At the prompt, use the python command to run the sample. 例如: python analyze-local-image.pyFor example, python analyze-local-image.py.
import requests
# If you are using a Jupyter notebook, uncomment the following line.
# %matplotlib inline
import matplotlib.pyplot as plt
from PIL import Image
from io import BytesIO

# Add your Computer Vision subscription key and endpoint to your environment variables.
if 'COMPUTER_VISION_SUBSCRIPTION_KEY' in os.environ:
    subscription_key = os.environ['COMPUTER_VISION_SUBSCRIPTION_KEY']
else:
    print("\nSet the COMPUTER_VISION_SUBSCRIPTION_KEY environment variable.\n**Restart your shell or IDE for changes to take effect.**")
    sys.exit()

if 'COMPUTER_VISION_ENDPOINT' in os.environ:
    endpoint = os.environ['COMPUTER_VISION_ENDPOINT']

analyze_url = endpoint + "vision/v2.1/analyze"

# Set image_path to the local path of an image that you want to analyze.
image_path = "C:/Documents/ImageToAnalyze.jpg"

# Read the image into a byte array
image_data = open(image_path, "rb").read()
headers = {'Ocp-Apim-Subscription-Key': subscription_key,
           'Content-Type': 'application/octet-stream'}
params = {'visualFeatures': 'Categories,Description,Color'}
response = requests.post(
    analyze_url, headers=headers, params=params, data=image_data)
response.raise_for_status()

# The 'analysis' object contains various fields that describe the image. The most
# relevant caption for the image is obtained from the 'description' property.
analysis = response.json()
print(analysis)
image_caption = analysis["description"]["captions"][0]["text"].capitalize()

# Display the image and overlay it with the caption.
image = Image.open(BytesIO(image_data))
plt.imshow(image)
plt.axis("off")
_ = plt.title(image_caption, size="x-large", y=-0.1)

檢查回應Examine the response

成功的回應會以 JSON 的形式傳回。A successful response is returned in JSON. 範例網站會在命令提示字元視窗中剖析並顯示成功的回應,如下列範例所示:The sample webpage parses and displays a successful response in the command prompt window, similar to the following example:

{
  "categories": [
    {
      "name": "outdoor_",
      "score": 0.00390625,
      "detail": {
        "landmarks": []
      }
    },
    {
      "name": "outdoor_street",
      "score": 0.33984375,
      "detail": {
        "landmarks": []
      }
    }
  ],
  "description": {
    "tags": [
      "building",
      "outdoor",
      "street",
      "city",
      "people",
      "busy",
      "table",
      "walking",
      "traffic",
      "filled",
      "large",
      "many",
      "group",
      "night",
      "light",
      "crowded",
      "bunch",
      "standing",
      "man",
      "sign",
      "crowd",
      "umbrella",
      "riding",
      "tall",
      "woman",
      "bus"
    ],
    "captions": [
      {
        "text": "a group of people on a city street at night",
        "confidence": 0.9122243847383961
      }
    ]
  },
  "color": {
    "dominantColorForeground": "Brown",
    "dominantColorBackground": "Brown",
    "dominantColors": [
      "Brown"
    ],
    "accentColor": "B54316",
    "isBwImg": false
  },
  "requestId": "c11894eb-de3e-451b-9257-7c8b168073d1",
  "metadata": {
    "height": 600,
    "width": 450,
    "format": "Jpeg"
  }
}

後續步驟Next steps

探索使用「電腦視覺」在影像中執行光學字元辨識 (OCR)、建立智慧型裁剪縮圖以及偵測、分類、標記和描述視覺特徵 (包括臉部) 的 Python 應用程式。Explore a Python application that uses Computer Vision to perform optical character recognition (OCR); create smart-cropped thumbnails; plus detect, categorize, tag, and describe visual features, including faces, in an image. 若要快速地試驗電腦視覺 API,請嘗試 Open API 測試主控台To rapidly experiment with the Computer Vision API, try the Open API testing console.