You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
IoT-For-Beginners/translations/zh/4-manufacturing/lessons/2-check-fruit-from-device/single-board-computer-class...

4.7 KiB

分类图像 - 虚拟 IoT 硬件和 Raspberry Pi

在本节课程中,您将把摄像头捕获的图像发送到 Custom Vision 服务进行分类。

将图像发送到 Custom Vision

Custom Vision 服务提供了一个 Python SDK您可以用它来分类图像。

任务 - 将图像发送到 Custom Vision

  1. 在 VS Code 中打开 fruit-quality-detector 文件夹。如果您使用的是虚拟 IoT 设备,请确保虚拟环境在终端中运行。

  2. 用于将图像发送到 Custom Vision 的 Python SDK 可作为一个 Pip 包安装。使用以下命令安装:

    pip3 install azure-cognitiveservices-vision-customvision
    
  3. app.py 文件的顶部添加以下导入语句:

    from msrest.authentication import ApiKeyCredentials
    from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
    

    这将引入 Custom Vision 库中的一些模块,一个用于使用预测密钥进行身份验证,另一个提供可以调用 Custom Vision 的预测客户端类。

  4. 在文件末尾添加以下代码:

    prediction_url = '<prediction_url>'
    prediction_key = '<prediction key>'
    

    <prediction_url> 替换为您在本课程前面从 Prediction URL 对话框中复制的 URL。将 <prediction key> 替换为您从同一对话框中复制的预测密钥。

  5. Prediction URL 对话框提供的预测 URL 设计用于直接调用 REST 端点。Python SDK 在不同的地方使用 URL 的不同部分。添加以下代码将此 URL 分解为所需的部分:

    parts = prediction_url.split('/')
    endpoint = 'https://' + parts[2]
    project_id = parts[6]
    iteration_name = parts[9]
    

    这会拆分 URL提取出 https://<location>.api.cognitive.microsoft.com 的端点、项目 ID 和已发布迭代的名称。

  6. 使用以下代码创建一个预测器对象来执行预测:

    prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
    predictor = CustomVisionPredictionClient(endpoint, prediction_credentials)
    

    prediction_credentials 包装了预测密钥。这些随后用于创建一个指向端点的预测客户端对象。

  7. 使用以下代码将图像发送到 Custom Vision

    image.seek(0)
    results = predictor.classify_image(project_id, iteration_name, image)
    

    这会将图像回滚到起始位置,然后将其发送到预测客户端。

  8. 最后,使用以下代码显示结果:

    for prediction in results.predictions:
        print(f'{prediction.tag_name}:\t{prediction.probability * 100:.2f}%')
    

    这会循环显示所有返回的预测结果,并在终端中显示它们。返回的概率是从 0 到 1 的浮点数,其中 0 表示与标签匹配的概率为 0%1 表示与标签匹配的概率为 100%。

    💁 图像分类器会返回所有使用过的标签的百分比。每个标签都会有一个图像与该标签匹配的概率。

  9. 运行您的代码,将摄像头对准一些水果,或者使用适当的图像集,或者如果使用虚拟 IoT 硬件,则让水果在您的网络摄像头中可见。您将在控制台中看到输出:

    (.venv) ➜  fruit-quality-detector python app.py
    ripe:   56.84%
    unripe: 43.16%
    

    您将能够看到拍摄的图像,以及这些值在 Custom Vision 的 Predictions 选项卡中显示。

    Custom Vision 中的一根香蕉,预测成熟度为 56.8%,未成熟度为 43.1%

💁 您可以在 code-classify/picode-classify/virtual-iot-device 文件夹中找到此代码。

😀 您的水果质量分类器程序成功了!

免责声明
本文档使用AI翻译服务Co-op Translator进行翻译。尽管我们努力确保翻译的准确性,但请注意,自动翻译可能包含错误或不准确之处。应以原文档的原始语言版本为权威来源。对于关键信息,建议使用专业人工翻译。我们对因使用本翻译而引起的任何误解或误读不承担责任。