3.2.4 · 建议 20 分钟 · 满分 15
花朵智能识别系统交互流程设计
背景
花朵智能识别系统在现代城市绿化管理中起着越来越重要的作用,其利用先进的计算机视觉技术,如花朵检测与识别,实现了对花朵种类的实时监控与管理。本系统要求开发一个基于已训练模型的花朵检测与分类系统,能够准确识别出不同类别的花朵。 AI 模型说明:提供的模型 flower-detection.onnx 是使用 Pytorch 框架和基于深度卷积神经网络训练得到的,专门用于进行花朵识别。对应的标签文件为 labels.txt。该模型的使用交互流程为: 1) 加载模型 flower-detection.onnx 和加载类别标签 labels.txt; 2) 加载一张本地花朵图片 flower_test.png,并预处理图像; 3) 使用 flower-detection 模型对花朵图片进行识别; 4) 输出花朵的预测类型和识别的准确率。
工作任务
- 补全该模型的使用交互流程对应的 Python 代码(3.2.4.ipynb),实现本地测试图片 flower_test.png 的识别。
- 在上面的使用交互流程基础上,给出在花朵智能识别系统中使用 flower-detection.onnx 模型的一种人机交互的最优流程,将其保存为 docx 文件,命名为 3.2.4.docx。
素材预览
flower-detection.onnx
二进制文件,见素材包
labels.txt
tench goldfish great white shark ...
flower_test.png
二进制文件,见素材包
代码填空
import onnxruntime as ort
import numpy as np
import scipy.special
from PIL import Image
# 预处理图像
def preprocess_image(image, resize_size=256, crop_size=224, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]):
image = image.resize((resize_size, resize_size), Image.BILINEAR)
w, h = image.size
left = (w - crop_size) / 2
top = (h - crop_size) / 2
image = image.crop((left, top, left + crop_size, top + crop_size))
image = np.array(image).astype(np.float32)
image = image / 255.0
image = (image - mean) / std
image = np.transpose(image, (2, 0, 1))
image = image.reshape((1,) + image.shape)
return image
# 加载模型 2分
session =
# 加载类别标签 2分
with as f:
labels = [line.strip() for line in f.readlines()]
# 获取模型输入和输出的名称
input_name = session.get_inputs()[0].name
output_name = session.get_outputs()[0].name
# 加载图片 2分
image = ('RGB')
# 预处理图片 2分
processed_image =
# 确保输入数据是 float32 类型
processed_image = processed_image.astype(np.float32)
# 进行图片识别 2分
output = ([output_name], {input_name: processed_image})[0]
# 应用 softmax 函数获取识别分类后的准确率 2分
accuracy = (output, axis=-1)
# 获取预测的类别索引
predicted_idx =
# 获取预测的准确值(转换为百分比)
prob_percentage =
# 获取预测的类别标签
predicted_label =
# 输出预测结果,包含百分比形式的概率
print(f"Predicted class: {predicted_label}, Accuracy: {prob_percentage:.2f}%")