tensorflow + pygame 手写数字识别的小游戏

news2024/10/24 21:25:59

起因, 目的:

很久之前,一个客户的作业,我帮忙写的。
今天删项目,觉得比较简洁,发出来给大家看看。

效果图:

在这里插入图片描述

1. 训练模型的代码
import sys
import tensorflow as tf

# Use MNIST handwriting dataset
mnist = tf.keras.datasets.mnist

# Prepare data for training
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
y_train = tf.keras.utils.to_categorical(y_train)
y_test = tf.keras.utils.to_categorical(y_test)
x_train = x_train.reshape(
    x_train.shape[0], x_train.shape[1], x_train.shape[2], 1
)
x_test = x_test.reshape(
    x_test.shape[0], x_test.shape[1], x_test.shape[2], 1
)

"""
model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation='softmax')
])

"""

# Create a convolutional neural network
model = tf.keras.models.Sequential([

    # 1.  Convolutional layer. Learn 32 filters using a 3x3 kernel, activation function is relu, input shape (28,28,1)
    tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),

    #2. Max-pooling layer, using 2x2 pool size
    tf.keras.layers.MaxPooling2D((2, 2)),

    #3.  Flatten units
    tf.keras.layers.Flatten(),

    #4. Add a hidden layer with dropout,
    tf.keras.layers.Dropout(0.2),

    #5. Add an output layer with output units for all 10 digits, activation function is softmax
    tf.keras.layers.Dense(10, activation='softmax')
    
])


# Train neural network
model.compile(
    optimizer="adam",
    loss="categorical_crossentropy",
    metrics=["accuracy"]
)
model.fit(x_train, y_train, epochs=10)

# Evaluate neural network performance
model.evaluate(x_test,  y_test, verbose=2)

# Save model to file
if len(sys.argv) == 2:
    filename = sys.argv[1]
    model.save(filename)
    print(f"Model saved to {filename}.")

"""
Run this code:  python handwriting.py model_1.pth

output:

1875/1875 [==============================] - 10s 5ms/step - loss: 0.0413 - accuracy: 0.9873
Epoch 8/10
1875/1875 [==============================] - 10s 5ms/step - loss: 0.0385 - accuracy: 0.9877
Epoch 9/10
1875/1875 [==============================] - 10s 5ms/step - loss: 0.0338 - accuracy: 0.9898
Epoch 10/10
1875/1875 [==============================] - 10s 5ms/step - loss: 0.0319 - accuracy: 0.9900
313/313 - 1s - loss: 0.0511 - accuracy: 0.9845 - 718ms/epoch - 2ms/step
Model saved to model_1.pth.
"""
2. 运行小游戏, 进行识别

从命令行运行:

python recognition.py model.h5


import numpy as np
import pygame
import sys
import tensorflow as tf
import time

"""
run this code:
python recognition.py  model_1.pth 
 
or,  
 
python recognition.py  model.h5 

output:
"""


print("len(sys.argv): ", len(sys.argv))

# Check command-line arguments
if len(sys.argv) != 2:
    print("Usage: python recognition.py model")
    sys.exit()


model = tf.keras.models.load_model(sys.argv[1])


# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# Start pygame
pygame.init()
size = width, height = 600, 400
screen = pygame.display.set_mode(size)

# Fonts
OPEN_SANS = "assets/fonts/OpenSans-Regular.ttf"
smallFont = pygame.font.Font(OPEN_SANS, 20)
largeFont = pygame.font.Font(OPEN_SANS, 40)

ROWS, COLS = 28, 28

OFFSET = 20
CELL_SIZE = 10

handwriting = [[0] * COLS for _ in range(ROWS)]
classification = None

while True:

    # Check if game quit
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    screen.fill(BLACK)

    # Check for mouse press
    click, _, _ = pygame.mouse.get_pressed()
    if click == 1:
        mouse = pygame.mouse.get_pos()
    else:
        mouse = None

    # Draw each grid cell
    cells = []
    for i in range(ROWS):
        row = []
        for j in range(COLS):
            rect = pygame.Rect(
                OFFSET + j * CELL_SIZE,
                OFFSET + i * CELL_SIZE,
                CELL_SIZE, CELL_SIZE
            )

            # If cell has been written on, darken cell
            if handwriting[i][j]:
                channel = 255 - (handwriting[i][j] * 255)
                pygame.draw.rect(screen, (channel, channel, channel), rect)

            # Draw blank cell
            else:
                pygame.draw.rect(screen, WHITE, rect)
            pygame.draw.rect(screen, BLACK, rect, 1)

            # If writing on this cell, fill in current cell and neighbors
            if mouse and rect.collidepoint(mouse):
                handwriting[i][j] = 250 / 255
                if i + 1 < ROWS:
                    handwriting[i + 1][j] = 220 / 255
                if j + 1 < COLS:
                    handwriting[i][j + 1] = 220 / 255
                if i + 1 < ROWS and j + 1 < COLS:
                    handwriting[i + 1][j + 1] = 190 / 255

    # Reset button
    resetButton = pygame.Rect(
        30, OFFSET + ROWS * CELL_SIZE + 30,
        100, 30
    )
    resetText = smallFont.render("Reset", True, BLACK)
    resetTextRect = resetText.get_rect()
    resetTextRect.center = resetButton.center
    pygame.draw.rect(screen, WHITE, resetButton)
    screen.blit(resetText, resetTextRect)

    # Classify button
    classifyButton = pygame.Rect(
        150, OFFSET + ROWS * CELL_SIZE + 30,
        100, 30
    )
    classifyText = smallFont.render("Classify", True, BLACK)
    classifyTextRect = classifyText.get_rect()
    classifyTextRect.center = classifyButton.center
    pygame.draw.rect(screen, WHITE, classifyButton)
    screen.blit(classifyText, classifyTextRect)

    # Reset drawing
    if mouse and resetButton.collidepoint(mouse):
        handwriting = [[0] * COLS for _ in range(ROWS)]
        classification = None

    # Generate classification
    if mouse and classifyButton.collidepoint(mouse):
        classification = model.predict(
            [np.array(handwriting).reshape(1, 28, 28, 1)]
        ).argmax()

    # Show classification if one exists
    if classification is not None:
        classificationText = largeFont.render(str(classification), True, WHITE)
        classificationRect = classificationText.get_rect()
        grid_size = OFFSET * 2 + CELL_SIZE * COLS
        classificationRect.center = (
            grid_size + ((width - grid_size) / 2),
            100
        )
        screen.blit(classificationText, classificationRect)

    pygame.display.flip()

完整项目,我已经上传了。 0积分下载。

完整项目链接
https://download.csdn.net/download/waterHBO/89881853

老哥留步,支持一下。

请求支持

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2211025.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C# 中循环的应用说明

一循环的概念说明 在C#编程中&#xff0c;循环结构是一种非常重要的控制流语句&#xff0c;它允许我们重复执行一段代码&#xff0c;直到满足某个特定条件为止。C#提供了几种不同类型的循环结构&#xff0c;包括for循环、while循环、do-while循环和foreach循环。 循环语句允许…

树莓派应用--AI项目实战篇来啦-7.OpenCV脸部和眼睛检测

1. 介绍 在深度学习时代&#xff0c;人脸识别一般是利用卷积神经网络进行监督式学习&#xff0c;也就是通过让算法&#xff08;神经网络&#xff09;自己去发现规律的方式&#xff0c;创造出有用的卷积核&#xff0c;然后利用其进行寻找图片和视频中的人脸&#xff0c;而在这之…

【Mac苹果电脑安装】DBeaverEE for Mac 数据库管理工具软件教程【保姆级教程】

Mac分享吧 文章目录 DBeaverEE 数据库管理工具 软件安装完成&#xff0c;打开效果图片Mac电脑 DBeaverEE 数据库管理工具 软件安装——v24.21️⃣&#xff1a;下载软件2️⃣&#xff1a;安装JDK&#xff0c;根据下图操作步骤提示完成安装3️⃣&#xff1a;安装DBeaverEE&#…

【ESP32】ESP-IDF开发 | LED PWM控制器+呼吸灯例程

1. 简介 LED PWM控制器&#xff0c;简称LEDC&#xff0c;主要用于控制LED的亮度和颜色&#xff0c;当然也可以当作普通的PWM进行使用。 LEDC有16路通道&#xff0c;8路高速通道和8路低速通道&#xff0c;16路通道都能够产生独立的数字波形来驱动LED设备。高速和低速通道分别有8…

C++:模拟stack、queue

目录 容器适配器 定义 特点 deque deque的优势与缺点 选择deque作为stack和queue的底层默认容器的原因 模拟实现stack 模拟实现queue 容器适配器 定义 在C标准模板库&#xff08;STL&#xff09;中&#xff0c;容器适配器&#xff08;Container Adapters&#xff09;是…

Harmony开发基础

背景介绍 鸿蒙系统的开发一来是为了打破国外垄断&#xff0c;实现操作系统的国产化&#xff0c;另一方面是针对目前市场上出现大量新的移动设备&#xff0c;手表&#xff0c;折叠屏等&#xff0c;移动端程序要适配不同设备&#xff0c;需要维护一套代码下的多个版本&#xff0…

U盘直接拔掉之后数据丢失怎么恢复 U盘数据丢失了怎么恢复

U盘作为一种存储设备&#xff0c;可以帮助人们存储很多资料文件&#xff0c;无论是办公文件&#xff0c;亦或是生活中的照片&#xff0c;所以在存储数据文件时&#xff0c;人们是比较依赖U盘。不过&#xff0c;U盘也存在很多的不确定性&#xff0c;比如数据容易丢失、或者损坏。…

NirCmd-sendkeysendkeypress

引入script [Script File] This command allows you to execute a sequence of commands stored inside the file specified in [Script File] parameter. Example: script "c:\temp\msg.ncl" Example for a script:infobox "Hello !" "This is the …

微知-如何查看服务器CPU当前运行主频?(cat /sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq)

关键命令 cat /sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq背景 首先lscpu可以查看到有多个cpu&#xff0c;里面也会显示cpu的频率&#xff0c;但是这里显示仅仅是规格&#xff0c;不是实际值。为了查看实际值&#xff0c;需要到/sys文件系统中查看&#xff0c;也…

大数据-167 ELK Elasticsearch 详细介绍 特点 分片 查询

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; 目前已经更新到了&#xff1a; Hadoop&#xff08;已更完&#xff09;HDFS&#xff08;已更完&#xff09;MapReduce&#xff08;已更完&am…

ARIMA 模型初体验 —— 预测股票数据

第 1 步&#xff0c;从 twelvedata 上获取苹果 11 号 15:30 到 16:00 的 OHLC、成交量 数据。 第 2 步&#xff0c;编写 Python 代码&#xff08;实际上可以用 R 语言&#xff0c;R 语言从语言的级别对分析预测提供了支持&#xff0c;而 Python 需要第三方库&#xff09;。 …

Yolov8 搭配 Frequency-aware Feature Fusion for Dense Image Prediction

个人觉得论文赞的地方 https://github.com/Linwei-Chen/FreqFusion https://www.arxiv.org/abs/2408.12879 因为我有个项目需要训练边界模糊的情况,但又需要目标能在模糊里凸显出来,就是看到这张图以后觉得很赞,边界变得清晰有特征是我想要的,所以尝试用了 使后感 哈哈…

【CSS3】css开篇基础(2)

1.❤️❤️前言~&#x1f973;&#x1f389;&#x1f389;&#x1f389; Hello, Hello~ 亲爱的朋友们&#x1f44b;&#x1f44b;&#xff0c;这里是E绵绵呀✍️✍️。 如果你喜欢这篇文章&#xff0c;请别吝啬你的点赞❤️❤️和收藏&#x1f4d6;&#x1f4d6;。如果你对我的…

数据结构-5.2.树的性质

一.树的常考性质&#xff1a; 性质1&#xff1a;结点数 总度数 1(结点的度&#xff1a;结点分支的数量) 一个分支中&#xff0c;如父结点B&#xff0c;两个子结点为E和F&#xff0c;结点B的度的值为2&#xff0c;等于子结点数量&#xff0c;加上这一个父结点(父结点只能有一…

【计算机网络 - 基础问题】每日 3 题(三十九)

✍个人博客&#xff1a;https://blog.csdn.net/Newin2020?typeblog &#x1f4e3;专栏地址&#xff1a;http://t.csdnimg.cn/fYaBd &#x1f4da;专栏简介&#xff1a;在这个专栏中&#xff0c;我将会分享 C 面试中常见的面试题给大家~ ❤️如果有收获的话&#xff0c;欢迎点赞…

大型语言模型(LLMs)关键技术指南

在AI这个超火的领域&#xff0c;打好基础真的超级重要&#xff0c;尤其是当你开始研究那些超大型的语言模型&#xff0c;也就是LLMs的时候。这个指南就是想帮新手们把那些听起来高大上的概念变得简单易懂&#xff0c;比如神经网络、自然语言处理&#xff08;NLP&#xff09;还有…

【ROS2实操一】话题通信与自定义消息

一、准备工作 1.请先创建工作空间 mkdir -p ws01_plumbing/src //创建工作空间 colcon build //在工作空间目录下编译 2.创建专门的接口功能包定义接口文件(需要注意的是&#xff0c;目前为止无法在Python功能包中定义接口文件)&#xff0c;终端下进…

线程基础学习

线程的实现 通过实现Runnable接口的方式&#xff0c;实现其中的run方法。继承Thread类&#xff0c;然后重写其中的run方法。通过线程池创建线程&#xff0c;默认采用DefaultThreadFactory。有返回值的callable&#xff0c;实现callable接口&#xff0c;实行call方法。 本质上…

wait和waitpid

在Linux中&#xff0c;wait 和 waitpid 是用于进程控制的系统调用&#xff0c;它们主要用来让父进程等待子进程的终止&#xff0c;并获取子进程的退出状态。下面详细介绍它们的用法和区别。 1. wait() 函数 wait() 会阻塞调用进程&#xff0c;直到一个子进程终止。它的典型用…

怎么ping网络ip地址通不通

怎么Ping网络IP地址通不通&#xff1f;要检查网络中的IP地址是否连通&#xff0c;可以使用‌Ping命令。Ping命令通过发送ICMP&#xff08;Internet Control Message Protocol&#xff0c;因特网控制消息协议&#xff09;Echo请求报文并等待回应&#xff0c;来判断目标主机是否可…