欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python实现带GUI界面的手写数字识别

程序员文章站 2022-03-04 15:05:42
目录1.效果图2.数据集3.关于模型4.关于gui设计5.缺点6.遗留问题1.效果图有点low,轻喷点击选择图片会优先从当前目录查找2.数据集这部分我是对mnist数据集进行处理保存对应代码:impo...

1.效果图

有点low,轻喷

Python实现带GUI界面的手写数字识别

Python实现带GUI界面的手写数字识别

点击选择图片会优先从当前目录查找

Python实现带GUI界面的手写数字识别

2.数据集

这部分我是对mnist数据集进行处理保存

对应代码:

import tensorflow as tf
import matplotlib.pyplot as plt
import cv2
from pil import image
import numpy as np
from scipy import misc
(x_train_all,y_train_all),(x_test,y_test) = tf.keras.datasets.mnist.load_data()
x_valid,x_train = x_train_all[:5000],x_train_all[5000:]
y_valid,y_train = y_train_all[:5000],y_train_all[5000:]
print(x_valid.shape,y_valid.shape)
print(x_train.shape,y_train.shape)
print(x_test.shape,y_test.shape)
#读取单张图片
def show_single_img(img_arr,len=100,path='/users/zhangcaihui/desktop/case/jpg/'):
    for i in range(len):#我这种写法会进行覆盖,只能保存10张照片,想保存更多的数据自己看着改
        new_im = image.fromarray(img_arr[i])  # 调用image库,数组归一化
        #new_im.show()
        #plt.imshow(img_arr)  # 显示新图片
        label=y_train[i]
        new_im.save(path+str(label)+'.jpg')  # 保存图片到本地

#显示多张图片
def show_imgs(n_rows,n_cols,x_data,y_data):
    assert len(x_data) == len(y_data)
    assert n_rows * n_cols < len(x_data)
    plt.figure(figsize=(n_cols*1.4,n_rows*1.6))
    for row in range(n_rows):
        for col in range(n_cols):
            index = n_cols * row + col
            plt.subplot(n_rows,n_cols,index+1)
            plt.imshow(x_data[index],cmap="binary",interpolation="nearest")
            plt.axis("off")
    plt.show()
#show_imgs(2,2,x_train,y_train)
show_single_img(x_train)

3.关于模型

我保存了了之前训练好的模型,用来加载预测

关于tensorflow下训练神经网络模型:手把手教你,mnist手写数字识别

训练好的模型model.save(path)即可

4.关于gui设计

1)排版

#ui_openimage.py
# -*- coding: utf-8 -*-
# from pyqt5 import qtcore, qtgui, qtwidgets
# from pyqt5.qtcore import qt
import sys,time
from pyqt5 import qtgui, qtcore, qtwidgets
from pyqt5.qtwidgets import *
from pyqt5.qtcore import *
from pyqt5.qtgui import *

class ui_form(object):
    def setupui(self, form):
        form.setobjectname("form")
        form.resize(1144, 750)
        self.label_1 = qtwidgets.qlabel(form)
        self.label_1.setgeometry(qtcore.qrect(170, 130, 351, 251))
        self.label_1.setobjectname("label_1")
        self.label_2 = qtwidgets.qlabel(form)
        self.label_2.setgeometry(qtcore.qrect(680, 140, 351, 251))
        self.label_2.setobjectname("label_2")
        self.btn_image = qtwidgets.qpushbutton(form)
        self.btn_image.setgeometry(qtcore.qrect(270, 560, 93, 28))
        self.btn_image.setobjectname("btn_image")
        self.btn_recognition = qtwidgets.qpushbutton(form)
        self.btn_recognition.setgeometry(qtcore.qrect(680,560,93,28))
        self.btn_recognition.setobjectname("bnt_recognition")
        #显示时间按钮
        self.bnt_timeshow = qtwidgets.qpushbutton(form)
        self.bnt_timeshow.setgeometry(qtcore.qrect(900,0,200,50))
        self.bnt_timeshow.setobjectname("bnt_timeshow")
        self.retranslateui(form)
        self.btn_image.clicked.connect(self.slot_open_image)
        self.btn_recognition.clicked.connect(self.slot_output_digital)
        self.bnt_timeshow.clicked.connect(self.buttonclicked)
        self.center()
        qtcore.qmetaobject.connectslotsbyname(form)

    def retranslateui(self, form): #设置文本填充label、button
        _translate = qtcore.qcoreapplication.translate
        form.setwindowtitle(_translate("form", "数字识别系统"))
        self.label_1.settext(_translate("form", "点击下方按钮"))
        self.label_1.setstylesheet('font:50px;')
        self.label_2.settext(_translate("form", "0~9"))
        self.label_2.setstylesheet('font:50px;')
        self.btn_image.settext(_translate("form", "选择图片"))
        self.btn_recognition.settext(_translate("from","识别结果"))
        self.bnt_timeshow.settext(_translate("form","当前时间"))

    # 状态条显示时间模块
    def buttonclicked(self):  # 动态显示时间
        timer = qtimer(self)
        timer.timeout.connect(self.showtime)
        timer.start()
    def showtime(self):
        datetime = qdatetime.currentdatetime()
        time_now = time.strftime('%y-%m-%d %h:%m:%s', time.localtime())
        #self.statusbar().showmessage(time_now)
        #self.bnt_timeshow.setfont(qtgui.qfont().setpointsize(100))
        self.bnt_timeshow.settext(time_now)

    def center(self):#窗口放置*
        screen = qdesktopwidget().screengeometry()
        size = self.geometry()
        self.move((screen.width() - size.width()) / 2,
                    (screen.height() - size.height()) / 2)


    def keypressevent(self, e):
        if e.key() == qt.key_escape:
            self.close()



2)直接运行这个文件(调用1)

#ui_main.py
import random

from pyqt5.qtwidgets import qfiledialog
from pyqt5.qtgui import qpixmap
from ui_openimage import ui_form
import sys
from pyqt5 import qtwidgets, qtgui
from pyqt5.qtwidgets import qmainwindow, qtextedit, qaction, qapplication
import os,sys
from pyqt5.qtcore import qt

import tensorflow
from tensorflow.keras.models import load_model
from tensorflow.keras.datasets import mnist
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras.utils import to_categorical
import tensorflow.keras.preprocessing.image as image
import matplotlib.pyplot as plt
import numpy as np
import cv2
import warnings
warnings.filterwarnings("ignore")
class window(qtwidgets.qmainwindow,ui_form):
    def __init__(self):
        super(window, self).__init__()
        self.cwd = os.getcwd()
        self.setupui(self)
        self.labels = self.label_1
        self.img=none
    def slot_open_image(self):
        file, filetype = qfiledialog.getopenfilename(self, '打开多个图片', self.cwd, "*.jpg, *.png, *.jpg, *.jpeg, all files(*)")
        jpg = qtgui.qpixmap(file).scaled(self.labels.width(), self.labels.height())
        self.labels.setpixmap(jpg)
        self.img=file

    def slot_output_digital(self):
    	'''path为之前保存的模型路径'''
        path='/users/zhangcaihui/pycharmprojects/py38_tf/dl_book_keras/save_the_model.h5'
        model= load_model(path)
        #防止不上传数字照片而直接点击识别
        if self.img==none:
            self.label_2.settext('请上传照片!')
            return
        img = image.load_img(self.img, target_size=(28, 28))
        img = img.convert('l')#转灰度图像
        x = image.img_to_array(img)
        #x = abs(255 - x)
        x = np.expand_dims(x, axis=0)
        print(x.shape)
        x = x / 255.0
        prediction = model.predict(x)
        print(prediction)
        output = np.argmax(prediction, axis=1)
        print("手写数字识别为:" + str(output[0]))
        self.label_2.settext(str(output[0]))

if __name__ == "__main__":
  app = qtwidgets.qapplication(sys.argv)
  my = window()
  my.show()
  sys.exit(app.exec_())

5.缺点

界面low

只能识别单个数字

其实可以将多数字图片进行裁剪分割,这就涉及到制作数据集了

6.遗留问题

我自己手写的数据照片处理成28281送入网络预测,识别结果紊乱。

反思:自己写的数据是rgb,且一张几kb,图片预处理后,按28*28读入失真太严重了,谁有好的方法可以联系我!!!

其他的水果识别系统,手势识别系统啊,改改直接套!

到此这篇关于python实现带gui界面的手写数字识别的文章就介绍到这了,更多相关python手写数字识别内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!