1 Star 0 Fork 248

刘亚翔/OPTIMAL_KNN_MNIST_QUESTION_2

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
optimal_knn_webapp.py 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
刘亚翔 提交于 6个月前 . 1
import os
import joblib
import gradio as gr
import numpy as np
from PIL import Image
# 指定模型文件的路径
model_filename = r'D:/下载/optimal_knn_mnist_question/best_knn_model.pkl'
# 检查模型文件是否存在
if not os.path.isfile(model_filename):
print(f"Error: {model_filename} not found.")
# 如果模型文件不存在,退出程序
exit()
else:
# 加载模型
best_knn_model = joblib.load(model_filename)
# 定义预测函数,这个函数将用于Gradio接口进行预测
def predict_digit(drawing):
if not drawing:
return "Please draw a digit."
# 将输入的图像转换为模型需要的格式
image_array = np.array(drawing, dtype=np.float32) # 直接使用drawing
image_array = image_array.reshape(28, 28) # 调整数组形状
image_array = (image_array > 0).astype(int) # 将图像转换为二值图像
prediction = best_knn_model.predict(image_array.reshape(1, -1))
return prediction[0]
# 创建Gradio接口,这个接口将用于用户输入和显示预测结果
iface = gr.Interface(
fn=predict_digit,
inputs=gr.Sketchpad(label="Draw a Digit", type="numpy"),
outputs="label",
title="Digit Prediction with KNN",
description="Draw a digit in the box below to get a prediction."
)
# 启动Gradio接口,用户可以通过这个接口进行交互
iface.launch()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/liu-yaxiang/optimal_knn_mnist_question_2.git
git@gitee.com:liu-yaxiang/optimal_knn_mnist_question_2.git
liu-yaxiang
optimal_knn_mnist_question_2
OPTIMAL_KNN_MNIST_QUESTION_2
main

搜索帮助