快速入门:通过计算机视觉中的 REST API 和 Python 使用域模型Quickstart: Use a domain model using the REST API and Python in Computer Vision

本快速入门将通过计算机视觉 REST API 使用域模型识别远程存储图像中的地标或(可选)名人。In this quickstart, you'll use a domain model to identify landmarks or, optionally, celebrities in a remotely stored image using the Computer Vision REST API. 使用识别域特定内容方法,可以应用一个特定于域的模型来识别图像中的内容。With the Recognize Domain Specific Content method, you can apply a domain-specific model to recognize content within an image.

可以在 MyBinder 上使用 Jupyter 笔记本以分步方式运行此快速入门。You can run this quickstart in a step-by step fashion using a Jupyter notebook on MyBinder. 要启动活页夹,请选择以下按钮:To launch Binder, select the following button:

活页夹Binder

先决条件Prerequisites

  • Azure 订阅 - 创建试用订阅An Azure subscription - Create one for trial
  • PythonPython
  • 拥有 Azure 订阅后,在 Azure 门户中创建计算机视觉资源 ,获取密钥和终结点。Once you have your Azure subscription, create a Computer Vision resource in the Azure portal to get your key and endpoint. 部署后,单击“转到资源”。After it deploys, click Go to resource.
    • 需要从创建的资源获取密钥和终结点,以便将应用程序连接到计算机视觉服务。You will need the key and endpoint from the resource you create to connect your application to the Computer Vision service. 你稍后会在快速入门中将密钥和终结点粘贴到下方的代码中。You'll paste your key and endpoint into the code below later in the quickstart.
    • 可以使用免费定价层 (F0) 试用该服务,然后再升级到付费层进行生产。You can use the free pricing tier (F0) to try the service, and upgrade later to a paid tier for production.
  • 为密钥和终结点 URL 创建环境变量,分别将其命名为 COMPUTER_VISION_SUBSCRIPTION_KEYCOMPUTER_VISION_ENDPOINTCreate environment variables for the key and endpoint URL, named COMPUTER_VISION_SUBSCRIPTION_KEY and COMPUTER_VISION_ENDPOINT, respectively.

创建并运行地标示例Create and run the landmarks sample

要创建和运行地标示例,请执行以下步骤:To create and run the landmark sample, do the following steps:

  1. 将以下代码复制到文本编辑器中。Copy the following code into a text editor.
  2. (可选)将 image_url 的值替换为要在其中检测地标的其他图像的 URL。Optionally, replace the value of image_url with the URL of a different image in which you want to detect landmarks.
  3. 将代码保存为以 .py 为扩展名的文件。Save the code as a file with an .py extension. 例如,get-landmarks.pyFor example, get-landmarks.py.
  4. 打开命令提示符窗口。Open a command prompt window.
  5. 在提示符处,使用 python 命令运行示例。At the prompt, use the python command to run the sample. 例如,python get-landmarks.pyFor example, python get-landmarks.py.
import os
import sys
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']

landmark_analyze_url = endpoint + "vision/v3.0/models/landmarks/analyze"

# Set image_url to the URL of an image that you want to analyze.
image_url = "https://upload.wikimedia.org/wikipedia/commons/f/f6/" + \
    "Bunker_Hill_Monument_2005.jpg"

headers = {'Ocp-Apim-Subscription-Key': subscription_key}
params = {'model': 'landmarks'}
data = {'url': image_url}
response = requests.post(
    landmark_analyze_url, headers=headers, params=params, json=data)
response.raise_for_status()

# The 'analysis' object contains various fields that describe the image. The
# most relevant landmark for the image is obtained from the 'result' property.
analysis = response.json()
assert analysis["result"]["landmarks"] is not []
print(analysis)
landmark_name = analysis["result"]["landmarks"][0]["name"].capitalize()

# Display the image and overlay it with the landmark name.
image = Image.open(BytesIO(requests.get(image_url).content))
plt.imshow(image)
plt.axis("off")
_ = plt.title(landmark_name, size="x-large", y=-0.1)
plt.show()

检查地标示例的响应Examine the response for the landmarks sample

成功的响应以 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:

{
  "result": {
    "landmarks": [
      {
        "name": "Bunker Hill Monument",
        "confidence": 0.9768505096435547
      }
    ]
  },
  "requestId": "659a10cd-44bb-44db-9147-a295b853b2b8",
  "metadata": {
    "height": 1600,
    "width": 1200,
    "format": "Jpeg"
  }
}

创建并运行名人示例Create and run the celebrities sample

要创建和运行地标示例,请执行以下步骤:To create and run the landmark sample, do the following steps:

  1. 将以下代码复制到文本编辑器中。Copy the following code into a text editor.
  2. 必要时在代码中进行如下更改:Make the following changes in code where needed:
    1. subscription_key 的值替换为你的订阅密钥。Replace the value of subscription_key with your subscription key.
    2. 如有必要,请将 vision_base_url 的值替换为获取的订阅密钥所在的 Azure 区域中的计算机视觉资源的终结点 URL。Replace the value of vision_base_url with the endpoint URL for the Computer Vision resource in the Azure region where you obtained your subscription keys, if necessary.
    3. (可选)将 image_url 的值替换为要在其中检测名人的其他图像的 URL。Optionally, replace the value of image_url with the URL of a different image in which you want to detect celebrities.
  3. 将代码保存为以 .py 为扩展名的文件。Save the code as a file with an .py extension. 例如,get-celebrities.pyFor example, get-celebrities.py.
  4. 打开命令提示符窗口。Open a command prompt window.
  5. 在提示符处,使用 python 命令运行示例。At the prompt, use the python command to run the sample. 例如,python get-celebrities.pyFor example, python get-celebrities.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

# Replace <Subscription Key> with your valid subscription key.
subscription_key = "<Subscription Key>"
assert subscription_key

vision_base_url = "https://api.cognitive.azure.cn/vision/v2.1/"

celebrity_analyze_url = vision_base_url + "models/celebrities/analyze"

# Set image_url to the URL of an image that you want to analyze.
image_url = "https://upload.wikimedia.org/wikipedia/commons/d/d9/" + \
    "Bill_gates_portrait.jpg"

headers = {'Ocp-Apim-Subscription-Key': subscription_key}
params = {'model': 'celebrities'}
data = {'url': image_url}
response = requests.post(
    celebrity_analyze_url, headers=headers, params=params, json=data)
response.raise_for_status()

# The 'analysis' object contains various fields that describe the image. The
# most relevant celebrity for the image is obtained from the 'result' property.
analysis = response.json()
assert analysis["result"]["celebrities"] is not []
print(analysis)
celebrity_name = analysis["result"]["celebrities"][0]["name"].capitalize()

# Display the image and overlay it with the celebrity name.
image = Image.open(BytesIO(requests.get(image_url).content))
plt.imshow(image)
plt.axis("off")
_ = plt.title(celebrity_name, size="x-large", y=-0.1)
plt.show()

检查名人示例的响应Examine the response for the celebrities sample

成功的响应以 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:

{
  "result": {
    "celebrities": [
      {
        "faceRectangle": {
          "top": 123,
          "left": 156,
          "width": 187,
          "height": 187
        },
        "name": "Bill Gates",
        "confidence": 0.9993845224380493
      }
    ]
  },
  "requestId": "f14ec1d0-62d4-4296-9ceb-6b5776dc2020",
  "metadata": {
    "height": 521,
    "width": 550,
    "format": "Jpeg"
  }
}

清理资源Clean up resources

不再需要这两个示例该文件时,请将其删除。When no longer needed, delete the files for both samples.

后续步骤Next steps

接下来,了解一款使用计算机视觉执行光学字符识别 (OCR) 功能的 Python 应用程序;创建智能裁剪缩略图;对图像中的视觉特征进行检测、分类、标记和说明。Next, explore a Python application that uses Computer Vision to perform optical character recognition (OCR); create smart-cropped thumbnails; and detect, categorize, tag, and describe visual features in images.