{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae22e2bd-46eb-46cc-a776-c8ac56a833ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "import onnxruntime as ort\n",
    "import numpy as np\n",
    "import scipy.special\n",
    "from PIL import Image\n",
    "\n",
    "\n",
    "# 预处理图像\n",
    "def preprocess_image(image, resize_size=256, crop_size=224, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]):\n",
    "    image = image.resize((resize_size, resize_size), Image.BILINEAR)\n",
    "    w, h = image.size\n",
    "    left = (w - crop_size) / 2\n",
    "    top = (h - crop_size) / 2\n",
    "    image = image.crop((left, top, left + crop_size, top + crop_size))\n",
    "    image = np.array(image).astype(np.float32)\n",
    "    image = image / 255.0\n",
    "    image = (image - mean) / std\n",
    "    image = np.transpose(image, (2, 0, 1))\n",
    "    image = image.reshape((1,) + image.shape)\n",
    "    return image\n",
    "\n",
    "\n",
    "# 加载模型  2分\n",
    "session = _________________\n",
    "\n",
    "\n",
    "# 加载类别标签 2分\n",
    "with _________________ as f:\n",
    "    labels = [line.strip() for line in f.readlines()]\n",
    "\n",
    "\n",
    "# 获取模型输入和输出的名称\n",
    "input_name = session.get_inputs()[0].name\n",
    "output_name = session.get_outputs()[0].name\n",
    "\n",
    "\n",
    "# 加载图片  2分\n",
    "image = _________________('RGB')\n",
    "\n",
    "\n",
    "# 预处理图片  2分\n",
    "processed_image = _________________\n",
    "\n",
    "\n",
    "# 确保输入数据是 float32 类型\n",
    "processed_image = processed_image.astype(np.float32)\n",
    "\n",
    "\n",
    "# 进行图片识别  2分\n",
    "output = _________________([output_name], {input_name: processed_image})[0]\n",
    "\n",
    "\n",
    "# 应用 softmax 函数获取识别分类后的准确率  2分\n",
    "accuracy = _________________(output, axis=-1)\n",
    "\n",
    "\n",
    "# 获取预测的类别索引\n",
    "predicted_idx =  __________\n",
    "\n",
    "\n",
    "# 获取预测的准确值（转换为百分比）\n",
    "prob_percentage =  __________\n",
    "\n",
    "\n",
    "# 获取预测的类别标签\n",
    "predicted_label = __________\n",
    "\n",
    "\n",
    "# 输出预测结果，包含百分比形式的概率\n",
    "print(f\"Predicted class: {predicted_label}, Accuracy: {prob_percentage:.2f}%\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
