{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2eb1eb15-6b75-4077-815b-eff228a752f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入必要的库\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "import onnxruntime as ort\n",
    "\n",
    "\n",
    "# 定义预处理函数，用于将图片转换为模型所需的输入格式\n",
    "def preprocess(image_path):\n",
    "    input_shape = (1, 1, 64, 64)    # 模型输入期望的形状，这里是 (N, C, H, W)，N=batch size, C=channels, H=height, W=width\n",
    "    img = Image.open(image_path).convert('L')    # 打开图像文件并将其转换为灰度图  1分\n",
    "    img = img.resize((64, 64), Image.Resampling.LANCZOS)    # 调整图像大小到模型输入所需的尺寸\n",
    "    img_data = np.array(img, dtype=np.float32)    # 将PIL图像对象转换为numpy数组，并确保数据类型是float32\n",
    "    # 调整数组的形状以匹配模型输入的形状\n",
    "    img_data = np.expand_dims(img_data, axis=0)  # 添加 batch 维度\n",
    "    img_data = np.expand_dims(img_data, axis=1)  # 添加 channel 维度\n",
    "    assert img_data.shape == input_shape, f\"Expected shape {input_shape}, but got {img_data.shape}\"    # 确保最终的形状与模型输入要求的形状一致\n",
    "    return img_data    # 返回预处理后的图像数据\n",
    "\n",
    "\n",
    "# 定义情感类别与数字标签的映射表 3分\n",
    "emotion_table = {____________}\n",
    "\n",
    "\n",
    "# 加载模型 3分\n",
    "ort_session = ____________    # 使用onnxruntime创建一个会话，用于加载并运行模型\n",
    "\n",
    "\n",
    "# 加载本地图片并进行预处理 3分\n",
    "input_data = ____________\n",
    "\n",
    "\n",
    "# 准备输入数据，确保其符合模型输入的要求\n",
    "ort_inputs = {ort_session.get_inputs()[0].name: input_data}    # ort_session.get_inputs()[0].name 是获取模型的第一个输入的名字\n",
    "\n",
    "\n",
    "# 运行模型，进行预测 3分\n",
    "ort_outs = ____________(None, ____________)\n",
    "\n",
    "\n",
    "# 解码模型输出，找到预测概率最高的情感类别 3分\n",
    "predicted_label = ____________(ort_outs[0])\n",
    "\n",
    "\n",
    "# 根据预测的标签找到对应的情感名称 3分\n",
    "predicted_emotion = ____________[predicted_label]\n",
    "\n",
    "\n",
    "# 输出预测的情感\n",
    "print(f\"Predicted emotion: {predicted_emotion}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d03e2102-7269-425a-9af8-70d891657c4d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
