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

在本快速入门中,你将通过计算机视觉的 REST API 使用域模型识别远程存储图像中的地标或者名人。In this quickstart, you use a domain model to identify landmarks or, optionally, celebrities in a remotely stored image by using Computer Vision's 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

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a Trial 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 follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your key.

创建并运行地标示例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. 必要时在代码中进行如下更改: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 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 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.0/"

landmark_analyze_url = vision_base_url + "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)

检查地标示例的响应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.0/"

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)

检查名人示例的响应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

浏览一款 Python 应用程序,该应用程序使用计算机视觉执行光学字符识别 (OCR)、创建智能裁剪缩略图,并对图像中的视觉特征(包括人脸)进行检测、分类、标记和描述。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.