您的位置:首页 > 娱乐 > 八卦 > 英文网站推广服务_所有网站大全_百度怎么做广告_建立网站要多少钱一年

英文网站推广服务_所有网站大全_百度怎么做广告_建立网站要多少钱一年

2024/12/22 18:42:56 来源:https://blog.csdn.net/SUMPLUSS/article/details/142830517  浏览:    关键词:英文网站推广服务_所有网站大全_百度怎么做广告_建立网站要多少钱一年
英文网站推广服务_所有网站大全_百度怎么做广告_建立网站要多少钱一年

用python的tensorflow包写了个手写自动识别的py脚本
前提条件

pip install tensorflow pillow numpy matplotlib
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import numpy as np
import tkinter as tk
from PIL import Image, ImageOps, ImageDraw
from tkinter import ttk# 加载MNIST数据集
(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()
train_images, test_images = train_images / 255.0, test_images / 255.0# 构建卷积神经网络模型
model = models.Sequential([layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),layers.MaxPooling2D((2, 2)),layers.Conv2D(64, (3, 3), activation='relu'),layers.MaxPooling2D((2, 2)),layers.Conv2D(64, (3, 3), activation='relu'),layers.Flatten(),layers.Dense(64, activation='relu'),layers.Dense(10, activation='softmax')
])# 编译并训练模型
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
model.fit(train_images[..., np.newaxis], train_labels, epochs=5, validation_data=(test_images[..., np.newaxis], test_labels))# Tkinter UI 界面,手写输入并预测数字
class DigitRecognizerApp:def __init__(self, root):self.root = rootself.root.title("Handwritten Digit Recognition")# 创建画布用于手写,绑定窗口大小变化时调整画布大小self.canvas = tk.Canvas(self.root, bg='white')self.canvas.grid(row=0, column=0, pady=2, padx=2, sticky="nsew")self.canvas.bind("<B1-Motion>", self.paint)self.canvas.bind("<Configure>", self.resize_canvas)# 初始图像对象self.image = Image.new("L", (200, 200), 255)self.draw = ImageDraw.Draw(self.image)# 按钮:清除画布self.clear_button = tk.Button(self.root, text="Clear", command=self.clear_canvas)self.clear_button.grid(row=1, column=0, pady=2, sticky="ew")# 按钮:预测数字self.predict_button = tk.Button(self.root, text="Predict", command=self.predict_digit)self.predict_button.grid(row=2, column=0, pady=2, sticky="ew")# 结果显示区self.result_label = tk.Label(self.root, text="Prediction: None", font=('Helvetica', 16))self.result_label.grid(row=3, column=0, pady=2, sticky="ew")# 概率显示区 - 显示最高概率数字和所有概率self.prob_frame = tk.Frame(self.root)self.prob_frame.grid(row=4, column=0, pady=2, sticky="nsew")self.highest_prob_label = tk.Label(self.prob_frame, text="Highest Probability: None", font=('Helvetica', 12))self.highest_prob_label.pack(pady=2)self.prob_text = tk.Text(self.prob_frame, height=10, font=('Helvetica', 12))self.prob_text.pack(fill=tk.BOTH, expand=True)# 调整窗口布局self.root.grid_rowconfigure(0, weight=1)self.root.grid_columnconfigure(0, weight=1)self.root.grid_rowconfigure(4, weight=1)  # 使概率显示区域自适应def paint(self, event):# 在画布上绘制手写输入x, y = event.x, event.yr = 8  # 手写笔的半径self.canvas.create_oval(x-r, y-r, x+r, y+r, fill='black')self.draw.ellipse([x-r, y-r, x+r, y+r], fill='black')def resize_canvas(self, event):# 调整图像大小,保持用户手写的内容new_width, new_height = event.width, event.heightself.image = self.image.resize((new_width, new_height), Image.ANTIALIAS)self.draw = ImageDraw.Draw(self.image)def clear_canvas(self):# 清除画布self.canvas.delete("all")self.image = Image.new("L", (self.canvas.winfo_width(), self.canvas.winfo_height()), 255)self.draw = ImageDraw.Draw(self.image)self.result_label.config(text="Prediction: None")self.highest_prob_label.config(text="Highest Probability: None")self.prob_text.delete(1.0, tk.END)def predict_digit(self):# 将用户手写的图像处理为模型输入格式img = self.image.resize((28, 28))  # 将图像调整为28x28img = ImageOps.invert(img)  # 反转颜色,黑底白字img = np.array(img).reshape(1, 28, 28, 1) / 255.0  # 标准化# 使用模型进行预测predictions = model.predict(img)predicted_digit = np.argmax(predictions[0])  # 最高概率的数字probabilities = predictions[0]  # 每个数字的概率highest_prob = probabilities[predicted_digit]  # 获取最高概率# 更新UI显示结果self.result_label.config(text=f"Prediction: {predicted_digit}")self.highest_prob_label.config(text=f"Highest Probability: {predicted_digit} ({highest_prob:.4f})")# 显示所有数字的概率self.prob_text.delete(1.0, tk.END)for i in range(10):self.prob_text.insert(tk.END, f"Digit {i}: {probabilities[i]:.4f}\n")# 启动应用程序
if __name__ == "__main__":root = tk.Tk()app = DigitRecognizerApp(root)root.mainloop()

还有点缺陷就是不能ui界面不能根据画面的放大缩小自动适应

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com