示例#1
0
    def __init__(self):
        super(MainWindow,self).__init__()
    
        # 初始化参数
        self.result = [0, 0]

        # 初始化UI
        self.setupUi(self)
        self.center()

        # 初始化画板
        self.paintBoard = PaintBoard(self, Size = QSize(224, 224), Fill = QColor(0,0,0,255))
        self.paintBoard.setPenColor(QColor(255,255,255,255))
        self.dArea_Layout.addWidget(self.paintBoard)

        self.clearDataArea()
    def __init__(self):
        # 初始化继承的父类(Qmainwindow)
        super(MainWindow,self).__init__()
    
        # 初始化参数
        self.mode = MODE_MNIST
        self.result = [0, 0]

        # 初始化UI
        self.setupUi(self)
        self.center()
     
        # 初始化画板,可以用鼠标来输入数据的
        # 将画板变为gui画面的一部分。
        self.paintBoard = PaintBoard(self, Size = QSize(224, 224), Fill = QColor(0,0,0,0))
        self.paintBoard.setPenColor(QColor(0,0,0,0))
        self.dArea_Layout.addWidget(self.paintBoard) # gui界面中加入paintboard这个部分
        self.imgName = ''
        self.clearDataArea() # 清除数据区域
        png = QtGui.QPixmap('mnist-master\mnist-master\hj.jpg').scaled(160, 150)
        self.lbDataArea1.setPixmap(png)
        png1 = QtGui.QPixmap('mnist-master\mnist-master\hk.jpg').scaled(160, 115)
        self.lbDataArea2.setPixmap(png1)
示例#3
0
class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        # 初始化参数
        self.mode = MODE_MNIST
        self.result = [0, 0]

        # 初始化UI
        self.setupUi(self)
        self.center()

        # 初始化画板
        self.paintBoard = PaintBoard(self,
                                     Size=QSize(224, 224),
                                     Fill=QColor(0, 0, 0, 0))
        self.paintBoard.setPenColor(QColor(0, 0, 0, 0))
        self.dArea_Layout.addWidget(self.paintBoard)

        self.clearDataArea()

    # 窗口居中
    def center(self):
        # 获得窗口
        framePos = self.frameGeometry()
        # 获得屏幕中心点
        scPos = QDesktopWidget().availableGeometry().center()
        # 显示到屏幕中心
        framePos.moveCenter(scPos)
        self.move(framePos.topLeft())

    # 窗口关闭事件
    def closeEvent(self, event):
        reply = QMessageBox.question(self, 'Message', "Are you sure to quit?",
                                     QMessageBox.Yes | QMessageBox.No,
                                     QMessageBox.Yes)

        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

    # 清除数据待输入区
    def clearDataArea(self):
        self.paintBoard.Clear()
        self.lbDataArea.clear()
        self.lbResult.clear()
        self.lbCofidence.clear()
        self.result = [0, 0]

    """
    回调函数
    """

    # 模式下拉列表回调
    def cbBox_Mode_Callback(self, text):
        if text == '1:MINIST随机抽取':
            self.mode = MODE_MNIST
            self.clearDataArea()
            self.pbtGetMnist.setEnabled(True)

            self.paintBoard.setBoardFill(QColor(0, 0, 0, 0))
            self.paintBoard.setPenColor(QColor(0, 0, 0, 0))

        elif text == '2:鼠标手写输入':
            self.mode = MODE_WRITE
            self.clearDataArea()
            self.pbtGetMnist.setEnabled(False)

            # 更改背景
            self.paintBoard.setBoardFill(QColor(0, 0, 0, 255))
            self.paintBoard.setPenColor(QColor(255, 255, 255, 255))

    # 数据清除
    def pbtClear_Callback(self):
        self.clearDataArea()

    # 识别
    def pbtPredict_Callback(self):
        __img, img_array = [], [
        ]  # 将图像统一从qimage->pil image -> np.array [1, 1, 28, 28]

        # 获取qimage格式图像
        if self.mode == MODE_MNIST:
            __img = self.lbDataArea.pixmap()  # label内若无图像返回None
            if __img == None:  # 无图像则用纯黑代替
                # __img = QImage(224, 224, QImage.Format_Grayscale8)
                __img = Image.ImageQt(
                    Image.fromarray(np.uint8(np.zeros([224, 224]))))
            else:
                __img = __img.toImage()
        elif self.mode == MODE_WRITE:
            __img = self.paintBoard.getContentAsQImage()

        # 转换成pil image类型处理
        pil_img = Image.fromqimage(__img)
        pil_img = pil_img.resize((28, 28), Image.ANTIALIAS)

        # pil_img.save('test.png')

        img_array = np.array(pil_img.convert('L')).reshape(1, 1, 28,
                                                           28) / 255.0
        # img_array = np.where(img_array>0.5, 1, 0)

        # reshape成网络输入类型
        __result = network.predict(img_array)  # shape:[1, 10]

        # print (__result)

        # 将预测结果使用softmax输出
        __result = softmax(__result)

        self.result[0] = np.argmax(__result)  # 预测的数字
        self.result[1] = __result[0, self.result[0]]  # 置信度

        self.lbResult.setText("%d" % (self.result[0]))
        self.lbCofidence.setText("%.8f" % (self.result[1]))

    # 随机抽取
    def pbtGetMnist_Callback(self):
        self.clearDataArea()

        # 随机抽取一张测试集图片,放大后显示
        img = x_test[np.random.randint(0, 9999)]  # shape:[1,28,28]
        img = img.reshape(28, 28)  # shape:[28,28]

        img = img * 0xff  # 恢复灰度值大小
        pil_img = Image.fromarray(np.uint8(img))
        pil_img = pil_img.resize((224, 224))  # 图像放大显示

        # 将pil图像转换成qimage类型
        qimage = Image.ImageQt(pil_img)

        # 将qimage类型图像显示在label
        pix = QPixmap.fromImage(qimage)
        self.lbDataArea.setPixmap(pix)
class MainWindow(QMainWindow,Ui_MainWindow):
    def __init__(self):
        # 初始化继承的父类(Qmainwindow)
        super(MainWindow,self).__init__()
    
        # 初始化参数
        self.mode = MODE_MNIST
        self.result = [0, 0]

        # 初始化UI
        self.setupUi(self)
        self.center()
     
        # 初始化画板,可以用鼠标来输入数据的
        # 将画板变为gui画面的一部分。
        self.paintBoard = PaintBoard(self, Size = QSize(224, 224), Fill = QColor(0,0,0,0))
        self.paintBoard.setPenColor(QColor(0,0,0,0))
        self.dArea_Layout.addWidget(self.paintBoard) # gui界面中加入paintboard这个部分
        self.imgName = ''
        self.clearDataArea() # 清除数据区域
        png = QtGui.QPixmap('mnist-master\mnist-master\hj.jpg').scaled(160, 150)
        self.lbDataArea1.setPixmap(png)
        png1 = QtGui.QPixmap('mnist-master\mnist-master\hk.jpg').scaled(160, 115)
        self.lbDataArea2.setPixmap(png1)
        
    # 窗口居中,这次设置的是那个整体的窗口
    def center(self):
        # 获得窗口
        framePos = self.frameGeometry()
        # 获得屏幕中心点
        scPos = QDesktopWidget().availableGeometry().center()
        # 显示到屏幕中心,将窗口设置在屏幕中心
        framePos.moveCenter(scPos)
        self.move(framePos.topLeft())
    
    
    # 窗口关闭事件,是否关闭窗口,点击右上角关闭窗口
    def closeEvent(self, event):
        reply = QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QMessageBox.Yes | 
            QMessageBox.No, QMessageBox.Yes)

        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()   
    
    # 清除数据待输入区
    def clearDataArea(self):
        self.paintBoard.Clear()#清除画板
        self.lbDataArea.clear()
        self.lbResult.clear()   # 确认的数字
        self.lbCofidence.clear()    # softmax的值
        self.result = [0, 0]    # 一个用来存确认数字,一个用来存softmax值

    """
    回调函数
    """
    # 模式下拉列表回调,两种模式
    def cbBox_Mode_Callback(self, text):
        if text == '1:MINIST随机抽取':
            print(text)
            self.mode = MODE_MNIST
            self.clearDataArea()
            self.pbtGetMnist.setEnabled(True)
            self.pbtjiazai.setEnabled(False)
            self.paintBoard.setBoardFill(QColor(0,0,0,0))
            self.paintBoard.setPenColor(QColor(0,0,0,0))

        elif text == '2:鼠标手写输入':
            #print(text)
            self.mode = MODE_WRITE
            self.clearDataArea()
            self.pbtGetMnist.setEnabled(False)
            self.pbtjiazai.setEnabled(False)
            # 更改背景
            self.paintBoard.setBoardFill(QColor(0,0,0,255))
            self.paintBoard.setPenColor(QColor(255,255,255,255))

        elif text == '3:手写图片识别':
            #print('111')
            self.mode = MODE_PICTURE
            self.clearDataArea()
            self.pbtGetMnist.setEnabled(False)
            self.pbtjiazai.setEnabled(True)
            # 更改背景
            self.paintBoard.setBoardFill(QColor(0,0,0,0))
            self.paintBoard.setPenColor(QColor(0,0,0,0))



#    def get_picture(self):
        



    # 数据清除,点击数据清除区域
    def pbtClear_Callback(self):
        self.clearDataArea()
 

    # 识别
    def pbtPredict_Callback(self):
        __img, img_array =[],[]      # 将图像统一从qimage->pil image -> np.array [1, 1, 28, 28]
        
        # 获取qimage格式图像
        if self.mode == MODE_MNIST: #随机抽取
            __img = self.lbDataArea.pixmap()  # label内若无图像返回None
            if __img == None:   # 无图像则用纯黑代替
                # __img = QImage(224, 224, QImage.Format_Grayscale8)
                __img = ImageQt.ImageQt(Image.fromarray(np.uint8(np.zeros([224,224]))))
            else: __img = __img.toImage()
            pil_img = ImageQt.fromqimage(__img)
            pil_img = pil_img.resize((28, 28), Image.ANTIALIAS)
            img_array = np.array(pil_img.convert('L')).reshape(1,1,28, 28) / 255.0
            
        elif self.mode == MODE_WRITE:
            __img = self.paintBoard.getContentAsQImage()
            pil_img = ImageQt.fromqimage(__img)
            pil_img = pil_img.resize((28, 28), Image.ANTIALIAS)
            pil_img.save('test.png')
            img_array = np.array(pil_img.convert('L')).reshape(1,1,28, 28) / 255.0
            
        elif self.mode == MODE_PICTURE:
           
            pic = image_prepare(self.imgName)   # 得到28*28的数组
            img_array = pic.reshape(1,1,28, 28) / 255.0 # 转换成神经网络要求的输入格式
            '''
                关于这部分有一点需要说明:
                    开始通过image_prepare得到预处理的图片之后,最开始是变成一维数组,然后一直想着变成二维数组并进行灰度反转。
                    为什么会这样想呢,因为我开始在变成reshape(1,1,28, 28)格式时采用的是网上的另一种方法,以至于格式基本是没有变化的。
                    其实直接变成一维数组然后进行上面的转换也是一样可以的,这个部分的失误浪费了很长时间!以后debug时需要多注意。
            '''
        # reshape成网络输入类型 
        __result = network.predict(img_array)      # shape:[1, 10]

        # print (__result)

        # 将预测结果使用softmax输出,得到输出结果
        __result = softmax(__result)
       
        self.result[0] = np.argmax(__result)          # 预测的数字
        self.result[1] = __result[0, self.result[0]]     # 置信度

        # 结果显示在界面上
        self.lbResult.setText("%d" % (self.result[0]))
        self.lbCofidence.setText("%.8f" % (self.result[1]))


    # 随机抽取
    def pbtGetMnist_Callback(self):
        self.clearDataArea()
        
        # 随机抽取一张测试集图片,放大后显示,x_text为前面读取的数据集
        # 随机从10000个照片中选择一张。
        img = x_test[np.random.randint(0, 9999)]    # shape:[1,28,28] 
        img = img.reshape(28, 28)                   # shape:[28,28]  

        img = img * 0xff      # 恢复灰度值大小 
        pil_img = Image.fromarray(np.uint8(img))
        pil_img = pil_img.resize((224, 224))        # 图像放大显示,和绘图窗口大小一致

        # 将pil图像转换成qimage类型
        qimage = ImageQt.ImageQt(pil_img)
        
        # 将qimage类型图像显示在label
        pix = QPixmap.fromImage(qimage)
        self.lbDataArea.setPixmap(pix)

    def pbtjiazai_Callback(self):
        # 打开文件路径
        # 设置文件扩展名过滤,注意用双分号间隔
        self.imgName, imgType = QFileDialog.getOpenFileName(self, "打开图片", "", " *.png;;*.jpg;;*.jpeg;;*.bmp;;All Files (*)")
        print(self.imgName) # imgName就是图片的路径,为了之后在识别的时候也可以用,写成了self.imgName,self代表创建的实例本身
        png = QtGui.QPixmap(self.imgName).scaled(240, 240)
        # 显示图片并设置图片格式
        self.lbDataArea.setPixmap(png)
示例#5
0
class MainWindow(QMainWindow,Ui_MainWindow):
    def __init__(self):
        super(MainWindow,self).__init__()
    
        # 初始化参数
        self.result = [0, 0]

        # 初始化UI
        self.setupUi(self)
        self.center()

        # 初始化画板
        self.paintBoard = PaintBoard(self, Size = QSize(224, 224), Fill = QColor(0,0,0,255))
        self.paintBoard.setPenColor(QColor(255,255,255,255))
        self.dArea_Layout.addWidget(self.paintBoard)

        self.clearDataArea()

    # 窗口居中
    def center(self):
        # 获得窗口
        framePos = self.frameGeometry()
        # 获得屏幕中心点
        scPos = QDesktopWidget().availableGeometry().center()
        # 显示到屏幕中心
        framePos.moveCenter(scPos)
        self.move(framePos.topLeft())
    
    
    # 窗口关闭事件
    def closeEvent(self, event):
        reply = QMessageBox.question(self, 'Message',
            "你确定要退出吗?", QMessageBox.Yes | 
            QMessageBox.No, QMessageBox.Yes)

        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()   
    
    # 清除数据待输入区
    def clearDataArea(self):
        self.paintBoard.Clear()
        self.lbDataArea.clear()
        self.lbResult.clear()
        self.lbCofidence.clear()
        self.result = [0, 0]

    
        
        #mnist.train.images是一个55000 * 784维的矩阵, mnist.train.labels是一个55000 * 10维的矩阵

    
       


    # 数据清除
    def pbtClear_Callback(self):
        self.clearDataArea()
        Gui = MainWindow()
        Gui.show()
 

    # 识别
    def pbtPredict_Callback(self):
        img, img_array =[],[]      # 将图像统一从qimage->pil image -> np.array [1, 1, 28, 28]
        
        # 获取qimage格式图像
        
        img = self.paintBoard.getContentAsQImage()

        # 转换成pil image类型处理
        pil_img = ImageQt.fromqimage(img)
        pil_img = pil_img.resize((28, 28), Image.ANTIALIAS)
        
        pil_img.save('test.png')

        pil_img = pil_img.convert('L')
        tv = list(pil_img.getdata())
        tva = [(255-x)*1.0/255.0 for x in tv]

         #定义初始化权值函数
        def weight_variable(shape):
            initial=tf.truncated_normal(shape,stddev=0.1)
            return tf.Variable(initial)
        #定义初始化偏置函数
        def bias_variable(shape):
            initial=tf.constant(0.1,shape=shape)
            return tf.Variable(initial)
        #卷积层
        def conv2d(input,filter):
            return tf.nn.conv2d(input,filter,strides=[1,1,1,1],padding='SAME')
        #池化层
        def max_pool_2x2(value):
            return tf.nn.max_pool(value,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
        
        keep_prob=tf.placeholder(tf.float32) 
        #p_keep_conv = tf.placeholder(tf.float32) # 卷积层的dropout概率
        #p_keep_hidden = tf.placeholder(tf.float32)# 全连接层的dropout概率
        #输入层
        #定义两个placeholder
        x=tf.placeholder(tf.float32,[None,784]) #28*28
        y=tf.placeholder(tf.float32,[None,10])
        #改变x的格式转为4维的向量[batch,in_hight,in_width,in_channels]
        x_image=tf.reshape(x,[-1,28,28,1])
        w0 = weight_variable([625,10]) #FC 128 inputs,10 outputs
        w4 = weight_variable([128*4*4,625]) # FC 128 * 4 * 4 inputs, 625 outputs
        w1 = weight_variable([625,128]) # FC 625 inputs, 128 outputs (labels)
        #卷积、激励、池化操作
        #初始化第一个卷积层的权值和偏置
        W_conv1=weight_variable([3,3,1,32]) #3*3的采样窗口,32个卷积核从1个平面抽取特征
        b_conv1=bias_variable([32])
        #把x_image和权值向量进行卷积,再加上偏置值,然后应用于relu激活函数
        h_conv1=tf.nn.relu(conv2d(x_image,W_conv1))
        h_pool1=max_pool_2x2(h_conv1)  #进行max_pooling 池化层
        h_drop1=tf.nn.dropout(h_pool1,keep_prob) 
        #初始化第二个卷积层的权值和偏置
        W_conv2=weight_variable([3,3,32,64]) #3*3的采样窗口,64个卷积核从32个平面抽取特征
        b_conv2=bias_variable([64])
        #把第一个池化层结果和权值向量进行卷积,再加上偏置值,然后应用于relu激活函数
        h_conv2=tf.nn.relu(conv2d(h_pool1,W_conv2))
        h_pool2=max_pool_2x2(h_conv2)  #池化层
        h_drop2=tf.nn.dropout(h_pool2,keep_prob) 
        #初始化第三个卷积层的权值和偏置
        W_conv3=weight_variable([3,3,64,128]) #3*3的采样窗口,128个卷积核从64个平面抽取特征
        b_conv3=bias_variable([128])
        #把第二个池化层结果和权值向量进行卷积,再加上偏置值,然后应用于relu激活函数
        h_conv3=tf.nn.relu(conv2d(h_pool2,W_conv3))
        h_pool3=max_pool_2x2(h_conv3)
        h_drop3=tf.nn.dropout(h_pool3,keep_prob) 

        #合并所有的feature map
        h_feature = tf.reshape(h_drop3,[-1,w4.get_shape().as_list()[0]])
        h_f = tf.nn.dropout(h_feature,keep_prob)
        #全连接层
        h_fc1 = tf.nn.relu(tf.matmul(h_f,w4))
        h_fc2 = tf.nn.dropout(h_fc1,keep_prob)
        pyx = tf.matmul(h_fc2,w0)

        #交叉熵代价函数

        cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=pyx))
        #使用AdamOptimizer进行优化

        train_step = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cross_entropy)
        #结果放在bool集中
        correct_prediction=tf.equal(tf.argmax(pyx,1),tf.argmax(y,1))
        accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

        saver = tf.train.Saver() 
        #创建会话
        with tf.Session() as sess:
            
            sess.run(tf.global_variables_initializer()) #初始化变量
            saver.restore(sess,model_file)
            #测试
            prediction = tf.argmax(pyx,1)
            predint=prediction.eval(feed_dict={x: [tva],keep_prob: 1.0}, session=sess)
            print(predint[0])
            #acc=sess.run(accuracy,feed_dict={x:[tva],keep_prob:1.0})
        
        # img_array = np.where(img_array>0.5, 1, 0)
        # reshape成网络输入类型 
             # shape:[1, 10]
        # print (__result)

        # 将预测结果使用softmax输出
       
       
        self.result[0] = predint[0]          # 预测的数字
        #self.result[1] = __result[0, self.result[0]]     # 置信度

        self.lbResult.setText("%d" % (self.result[0]))
示例#6
0
class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        # 初始化参数
        self.mode = MODE_MNIST
        self.result = [0, 0]

        # 初始化UI
        self.setupUi(self)
        self.center()

        # 初始化画板
        self.paintBoard = PaintBoard(self,
                                     Size=QSize(224, 224),
                                     Fill=QColor(0, 0, 0, 0))
        self.paintBoard.setPenColor(QColor(0, 0, 0, 0))
        self.dArea_Layout.addWidget(self.paintBoard)

        self.clearDataArea()

    # 窗口居中
    def center(self):
        # 获得窗口
        framePos = self.frameGeometry()
        # 获得屏幕中心点
        scPos = QDesktopWidget().availableGeometry().center()
        # 显示到屏幕中心
        framePos.moveCenter(scPos)
        self.move(framePos.topLeft())

    # 窗口关闭事件
    def closeEvent(self, event):
        reply = QMessageBox.question(self, 'Message', "Are you sure to quit?",
                                     QMessageBox.Yes | QMessageBox.No,
                                     QMessageBox.Yes)

        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

    # 清除数据待输入区
    def clearDataArea(self):
        self.paintBoard.Clear()
        self.lbDataArea.clear()
        self.lbResult.clear()
        self.lbCofidence.clear()
        self.result = [0, 0]

    """
    回调函数
    """

    # 模式下拉列表回调
    def cbBox_Mode_Callback(self, text):
        if text == '1:MINIST随机抽取':
            self.mode = MODE_MNIST
            self.clearDataArea()
            self.pbtGetMnist.setEnabled(True)

            self.paintBoard.setBoardFill(QColor(0, 0, 0, 0))
            self.paintBoard.setPenColor(QColor(0, 0, 0, 0))

        elif text == '2:鼠标手写输入':
            self.mode = MODE_WRITE
            self.clearDataArea()
            self.pbtGetMnist.setEnabled(False)

            # 更改背景
            self.paintBoard.setBoardFill(QColor(0, 0, 0, 255))
            self.paintBoard.setPenColor(QColor(255, 255, 255, 255))

    # 数据清除
    def pbtClear_Callback(self):
        self.clearDataArea()

    # 识别
    def pbtPredict_Callback(self):
        __img, img_array = [], [
        ]  # 将图像统一从qimage->pil image -> np.array [1, 1, 28, 28]

        # 获取qimage格式图像
        if self.mode == MODE_MNIST:
            __img = self.lbDataArea.pixmap()  # label内若无图像返回None
            if __img == None:  # 无图像则用纯黑代替
                # __img = QImage(224, 224, QImage.Format_Grayscale8)
                __img = ImageQt.ImageQt(
                    Image.fromarray(np.uint8(np.zeros([224, 224]))))
            else:
                __img = __img.toImage()
        elif self.mode == MODE_WRITE:
            __img = self.paintBoard.getContentAsQImage()

        # 转换成pil image类型处理
        pil_img = ImageQt.fromqimage(__img)
        pil_img = pil_img.resize((28, 28), Image.ANTIALIAS)

        img_array = np.array(pil_img.convert('L')).reshape(1, 1, 28, 28)
        img = normalize(img_array)
        img = paddle.Tensor(img)
        __result = network(img)
        argmax__result = paddle.argmax(__result).numpy()

        self.result[0] = argmax__result[0]  # 置信度

        m = F.sigmoid(__result).numpy()

        self.result[1] = m[0][self.result[0]]

        self.lbResult.setText("%d" % (self.result[0]))
        self.lbCofidence.setText("%.8f" % (self.result[1]))

    # 随机抽取
    def pbtGetMnist_Callback(self):
        self.clearDataArea()

        # 随机抽取一张测试集图片,放大后显示
        path_origin = "data/test"
        files = list(
            filter(lambda x: x.endswith('.jpg'), os.listdir(path_origin)))
        random.shuffle(files)
        number = random.randint(0, len(files))
        img = Image.open(os.path.join(path_origin, files[number]))

        pil_img = Image.fromarray(np.uint8(img))
        pil_img = pil_img.resize((224, 224))  # 图像放大显示

        # 将pil图像转换成qimage类型
        qimage = ImageQt.ImageQt(pil_img)

        # 将qimage类型图像显示在label
        pix = QPixmap.fromImage(qimage)
        self.lbDataArea.setPixmap(pix)