计算机视觉 API Jupyter NotebookComputer Vision API Jupyter notebook

本指南介绍如何在 Python 中使用计算机视觉 API 以及如何使用常用库直观显示结果。This guide shows you how to use the Computer Vision API in Python and how to visualize your results using popular libraries. 你将使用 Jupyter 运行本教程。You will use Jupyter to run the tutorial. 若要了解如何开始使用交互式 Jupyter Notebook,请参阅 Jupyter 文档To learn how to get started with interactive Jupyter notebooks, refer to the Jupyter Documentation.

先决条件Prerequisites

在 Jupyter 中打开 NotebookOpen the notebook in Jupyter

  1. 转到认知视觉 Python GitHub 存储库。Go to the Cognitive Vision Python GitHub repo.
  2. 单击绿色按钮以克隆或下载存储库。Click on the green button to clone or download the repo.
  3. 打开命令提示符并导航到文件夹 Cognitive-Vision-Python\Jupyter Notebook 。Open a command prompt and navigate to the folder Cognitive-Vision-Python\Jupyter Notebook.
  4. 通过从命令提示符运行命令 pip install requests opencv-python numpy matplotlib,确保已安装所有必需的库。Ensure you have all the required libraries by running the command pip install requests opencv-python numpy matplotlib from the command prompt.
  5. 通过从命令提示符运行命令 jupyter notebook 启动 Jupyter。Start Jupyter by running the command jupyter notebook from the command prompt.
  6. 在 Jupyter 窗口中,单击“计算机视觉 API Example.ipynb”以打开教程笔记本 。In the Jupyter window, click on Computer Vision API Example.ipynb to open the tutorial notebook.

运行笔记本Run the notebook

若要使用此 Notebook,需要计算机视觉 API 的订阅密钥。To use this notebook, you will need a subscription key for the Computer Vision API. 访问 Azure 门户进行注册,主密钥或辅助密钥都将有效。Visit Azure portal to sign up Either the primary or the secondary key will work. 确保将密钥括在引号中以使其成为字符串。Be sure to enclose the key in quotes to make it a string.

还需要确保 _region 字段与你的订阅对应的区域匹配。You will also need to make sure the _region field matches the region that corresponds to your subscription.

# Variables
_region = 'chinanorth' #Here you enter the region of your subscription
_url = 'https://{}.api.cognitive.azure.cn/vision/v2.0/analyze'.format(_region)
_key = None #Here you have to paste your primary key
_maxNumRetries = 10

运行本教程时,你将能够从 URL 和本地存储中添加要分析的图像。When you run the tutorial, you will be able to add images to analyze, both from a URL and from local storage. 脚本将在 Notebook 中显示图像和分析信息。The script will display the images and analysis information in the notebook.