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

本快速入门将使用计算机视觉 REST API 基于图像生成缩略图。In this quickstart, you generate a thumbnail from an image using the Computer Vision REST API. 可以指定所需的高度和宽度,可以与输入图像的纵横比不同。You specify the desired height and width, which can differ in aspect ration from the input image. 计算机视觉使用智能裁剪来智能识别感兴趣的区域并围绕该区域生成裁剪坐标。Computer Vision uses smart cropping to intelligently identify the area of interest and generate cropping coordinates around that region.

先决条件Prerequisites

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

创建并运行示例命令Create and run the sample command

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

  1. 将以下命令复制到文本编辑器中。Copy the following command into a text editor.

  2. 必要时在命令中进行如下更改:Make the following changes in the command where needed:

    1. <subscriptionKey> 的值替换为你的订阅密钥。Replace the value of <subscriptionKey> with your subscription key.
    2. <thumbnailFile> 的值替换为要保存缩略图的文件的路径和名称。Replace the value of <thumbnailFile> with the path and name of the file in which to save the thumbnail.
    3. 将请求 URL 的第一部分 (westcentralus) 替换为你自己的终结点 URL 中的文本。Replace the first part of the request URL (westcentralus) with the text in your own endpoint URL.

      备注

      2019 年 7 月 1 日之后创建的新资源将使用自定义子域名。New resources created after July 1, 2019, will use custom subdomain names. 获取详细信息和区域终结点的完整列表。For more information and a complete list of regional endpoints.

    4. (可选)将请求正文 (https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Shorkie_Poo_Puppy.jpg/1280px-Shorkie_Poo_Puppy.jpg\) 中的图像 URL 更改为要从中生成缩略图的其他图像的 URL。Optionally, change the image URL in the request body (https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Shorkie_Poo_Puppy.jpg/1280px-Shorkie_Poo_Puppy.jpg\) to the URL of a different image from which to generate a thumbnail.
  3. 打开命令提示符窗口。Open a command prompt window.

  4. 将文本编辑器中的命令粘贴到命令提示符窗口。Paste the command from the text editor into the command prompt window.

  5. 按 Enter 运行此程序。Press enter to run the program.

    curl -H "Ocp-Apim-Subscription-Key: <subscriptionKey>" -o <thumbnailFile> -H "Content-Type: application/json" "https://api.cognitive.azure.cn/vision/v3.0/generateThumbnail?width=100&height=100&smartCropping=true" -d "{\"url\":\"https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Shorkie_Poo_Puppy.jpg/1280px-Shorkie_Poo_Puppy.jpg\"}"
    

检查响应Examine the response

成功的响应会将缩略图写入 <thumbnailFile> 中指定的文件。A successful response writes the thumbnail image to the file specified in <thumbnailFile>. 如果请求失败,则响应包含错误代码和消息,以帮助确定出错的地方。If the request fails, the response contains an error code and a message to help determine what went wrong. 如果请求似乎是成功的,但创建的缩略图不是有效的图像文件,则可能是因为订阅密钥无效。If the request seems to succeed but the created thumbnail is not a valid image file, it might be that your subscription key is not valid.

后续步骤Next steps

探索计算机视觉 API,了解如何分析图像、检测名人和地标、创建缩略图,并提取印刷体文本和手写文本。Explore the Computer Vision API to how to analyze an image, detect celebrities and landmarks, create a thumbnail, and extract printed and handwritten text. 要快速体验计算机视觉 API,请尝试使用 Open API 测试控制台To rapidly experiment with the Computer Vision API, try the Open API testing console.