快速入门:使用计算机视觉 REST API 和 Python 生成缩略图Quickstart: Generate a thumbnail using the Computer Vision REST API and Python

本快速入门将使用计算机视觉 REST API 基于图像生成缩略图。In this quickstart, you'll generate a thumbnail from an image using the Computer Vision REST API. 使用获取缩略图方法,可以指定所需的高度和宽度,计算机视觉使用智能裁剪来智能识别感兴趣的区域并基于该区域生成裁剪坐标。With the Get Thumbnail method, you can specify the desired height and width, and Computer Vision uses smart cropping to intelligently identify the area of interest and generate cropping coordinates based on that region.

先决条件Prerequisites

  • Azure 订阅 - 创建试用订阅An Azure subscription - Create one for trial
  • 拥有 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 sample

若要创建和运行示例,请将以下代码复制到代码编辑器中。To create and run the sample, copy the following code into the code editor.

import os
import sys
import requests
# If you are using a Jupyter notebook, uncomment the following lines.
# %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.
subscription_key = os.environ['COMPUTER_VISION_SUBSCRIPTION_KEY']
endpoint = os.environ['COMPUTER_VISION_ENDPOINT']

thumbnail_url = endpoint + "vision/v3.0/generateThumbnail"

# Set image_url to the URL of an image that you want to analyze.
image_url = "https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg"

# Construct URL
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
params = {'width': '50', 'height': '50', 'smartCropping': 'true'}
data = {'url': image_url}
# Call API
response = requests.post(thumbnail_url, headers=headers, params=params, json=data)
response.raise_for_status()

# Open the image from bytes
thumbnail = Image.open(BytesIO(response.content))

# Verify the thumbnail size.
print("Thumbnail is {0}-by-{1}".format(*thumbnail.size))

# Save thumbnail to file
thumbnail.save('thumbnail.png')

# Display image
thumbnail.show()

# Optional. Display the thumbnail from Jupyter.
# plt.imshow(thumbnail)
# plt.axis("off")

接下来,请执行以下操作:Next, do the following:

  1. (可选)将 image_url 的值替换为自己的图像的 URL。(Optional) Replace the value of image_url with the URL of your own image.
  2. 将代码保存为以 .py 为扩展名的文件。Save the code as a file with an .py extension. 例如,get-thumbnail.pyFor example, get-thumbnail.py.
  3. 打开命令提示符窗口。Open a command prompt window.
  4. 在提示符处,使用 python 命令运行示例。At the prompt, use the python command to run the sample. 例如,python get-thumbnail.pyFor example, python get-thumbnail.py.

检查响应Examine the response

成功的响应将以二进制数据形式返回,这些数据表示缩略图的图像数据。A successful response is returned as binary data which represents the image data for the thumbnail. 该示例应显示此图像。The sample should display this image. 如果请求失败,则响应将显示在命令提示符窗口中,并且应包含错误代码。If the request fails, the response is displayed in the command prompt window and should contain an error code.

在 Jupyter 中运行(可选)Run in Jupyter (optional)

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

活页夹Binder

后续步骤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.