# -*- coding:utf-8 -*-
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PIL import Image
import tensorflow as tf
import matplotlib.pyplot as plt
import cv2
import mnist_inference
import mnist_train
import pathlib
class DigitalRecognition(QMainWindow):
def __init__(self):
super(DigitalRecognition, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("手写数字识别")
self.resize(800, 500)
_translate = QtCore.QCoreApplication.translate
self.button1 = QPushButton("选择图片", self)
self.button1.move(150, 350)
self.button1.resize(120,60)
self.button1.clicked.connect(self.select_image)
self.button2 = QPushButton("识别", self)
self.button2.move(550, 350)
self.button2.resize(120, 60)
self.button2.clicked.connect(self.recognition)
self.edit = QTextEdit(self)
self.edit.move(450, 100)
self.edit.resize(300, 160)
self.edit.setPlaceholderText(_translate("MainWindow", "结果显示"))
self.label = QLabel("待载入图片", self)
self.label.move(110, 60)
self.label.resize(250, 250)
self.label.setStyleSheet("QLabel{background:gray;}"
"QLabel{color:rgb(0,0,0,120);font-size:15px;font-weight:bold;font-family:宋体;}")
def select_image(self):
global fname
imgName, imgType = QFileDialog.getOpenFileName(self, "打开图片", "", "*.png;;*.jpg;;All Files(*)")
jpg = QtGui.QPixmap(imgName).scaled(self.label.width(), self.label.height())
self.label.setPixmap(jpg)
fname = imgName
def recognition(self):
global fname
def imageprepare():
"""
This function returns the pixel values.
The imput is a png file location.
"""
file_name = str(fname) # 导入自己的图片地址
im = Image.open(file_name)
plt.imshow(im)
img = cv2.imread(file_name, 1)
ret, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
im = Image.fromarray(cv2.cvtColor(th1, cv2.COLOR_BGR2RGB))
# in terminal 'mogrify -format png *.jpg' convert jpg to png
#灰度化
im = im.convert('L')
# 为图片重新指定尺寸
im = im.resize((28, 28), Image.ANTIALIAS)
im_list = list(im.getdata()) # get pixel values
# normalize pixels to 0 and 1. 0 is pure white, 1 is pure black.
#图像灰度反转
tva = [(255 - x) * 1.0 / 255.0 for x in im_list]
# print(tva)
return tva
# Define the model (same as when creating the model file)
# 图像处理:降噪、灰度化、修改尺寸以及灰度反转
pic = imageprepare()
def evaluate(pic):
with tf.Graph().as_default() as g:
x = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input')
y_ = tf.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input')
validate_feed = {x: [pic]}
y = mnist_inference.inference(x, None)
result = tf.argmax(y, 1)
variable_averages = tf.train.ExponentialMovingAverage(mnist_train.MOVING_AVERAGE_DECAY)
variables_to_restore = variable_averages.variables_to_restore()
saver = tf.train.Saver(variables_to_restore)
with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state(mnist_train.MODEL_SAVE_PATH)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
number = sess.run(result, feed_dict=validate_feed)
pic_name = pathlib.Path(fname).name#15元
print(pic_name, ' is :', number[0])
else:
print('No checkpoint file found')
return
self.edit.setText(pic_name+'识别的手写数字是>>>' + str(number[0]))
#识别图像
evaluate(pic) #15yuan
if __name__ == "__main__":
app = QApplication(sys.argv)
main = DigitalRecognition()
palette = QPalette()
pix5 = QPixmap("timg") # 背景图
pix = pix5.scaled(main.width(), main.height()) # 自适应窗口大小
palette.setBrush(QPalette.Background, QBrush(pix))
main.setPalette(palette)
main.show()
sys.exit(app.exec_())
没有合适的资源?快使用搜索试试~ 我知道了~
代码下载:Python+TensorFlow+PyQt实现手写体数字图片识别+GUI界面+画板数字识别
共79个文件
png:22个
index:10个
data-00000-of-00001:10个
需积分: 49 102 下载量 155 浏览量
2020-11-03
18:33:31
上传
评论 23
收藏 53.54MB ZIP 举报
温馨提示
博客:https://blog.csdn.net/weixin_45775701/article/details/109446515
资源详情
资源评论
资源推荐
收起资源包目录
Handwritten-Numeral-Recognition.zip (79个子文件)
Handwritten-Numeral-Recognition
minist_eval.py 2KB
app.py 2KB
MNIST_data
t10k-images-idx3-ubyte.gz 1.57MB
train-images.idx3-ubyte 44.86MB
t10k-images.idx3-ubyte 7.48MB
t10k-labels-idx1-ubyte.gz 4KB
train-images-idx3-ubyte.gz 9.45MB
t10k-labels.idx1-ubyte 10KB
train-labels.idx1-ubyte 59KB
train-labels-idx1-ubyte.gz 28KB
MNIST_model
mnist_model-29001.data-00000-of-00001 3.03MB
mnist_model-6001.data-00000-of-00001 3.03MB
mnist_model-7001.meta 62KB
mnist_model-28001.data-00000-of-00001 3.03MB
mnist_model-7001.data-00000-of-00001 3.03MB
mnist_model-8001.data-00000-of-00001 3.03MB
mnist_model-10001.meta 62KB
mnist_model-30000.data-00000-of-00001 3.03MB
mnist_model-10001.data-00000-of-00001 3.03MB
mnist_model-27001.data-00000-of-00001 3.03MB
mnist_model-8001.meta 62KB
mnist_model-26001.data-00000-of-00001 3.03MB
mnist_model-7001.index 470B
mnist_model-30000.meta 62KB
mnist_model-8001.index 470B
mnist_model-9001.meta 62KB
mnist_model-28001.index 470B
mnist_model-26001.index 470B
mnist_model-28001.meta 62KB
mnist_model-29001.meta 62KB
mnist_model-10001.index 470B
mnist_model-6001.index 470B
mnist_model-30000.index 470B
mnist_model-29001.index 470B
mnist_model-26001.meta 62KB
mnist_model-9001.index 470B
checkpoint 279B
mnist_model-27001.meta 62KB
mnist_model-6001.meta 62KB
mnist_model-9001.data-00000-of-00001 3.03MB
mnist_model-27001.index 470B
run1.py 5KB
导出.png 8KB
mnist_inference.py 1KB
__pycache__
mnist_train.cpython-37.pyc 2KB
mnist_train.cpython-36.pyc 2KB
now.cpython-36.pyc 2KB
mnist_inference.cpython-37.pyc 1003B
mnist_inference.cpython-36.pyc 1012B
as.png 10KB
.idea
workspace.xml 6KB
misc.xml 299B
modules.xml 314B
Handwritten-Numeral-Recognition.iml 398B
inspectionProfiles
profiles_settings.xml 174B
timg.jpeg 31KB
mnist_train.py 3KB
now.py 3KB
.DS_Store 14KB
picture
5.png 195KB
number1.png 65KB
number6.png 209KB
3.png 205KB
0.png 200KB
4.png 209KB
number5.png 149KB
9.png 201KB
number4.png 152KB
number3.png 69KB
7.png 289KB
number9.png 139KB
number8.png 122KB
number7.png 196KB
number0.png 101KB
number2.png 130KB
8.png 220KB
6.png 205KB
1.png 172KB
2.png 182KB
共 79 条
- 1
汪呀呀呀呀呀呀呀
- 粉丝: 107
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0