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)
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]))