PyQt5编写显示图片代码,程序运行正常,但点击打开图片后直接闪退
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'classfier.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtGui
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QPushButton, QVBoxLayout, QFileDialog
import cv2
from keras.models import load_model
import numpy as np
import sys,os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
class Classfier(QWidget):
def __init__(self):
super(Classfier,self).__init__()
self.resize(800,600)
self.setWindowTitle("垃圾分类系统")
self.open = QPushButton(self)
self.open.setText("打开图片")
self.open.resize(790,50)
self.open.move(10, 10)
self.open.clicked.connect(self.open_image)
self.path = QLabel(self)
self.path.setText('图片路径')
self.path.move(10, 100)
self.display = QLabel(self)
self.display.setText("显示图片")
self.display.setFixedSize(300,300)
self.display.move(5,150)
self.display.setStyleSheet("QLabel{background:white;}"
"QLabel{color:rgb(300,300,300,120);font-size:20px;font-weight:bold;font-family:宋体;}"
)
self.click = QPushButton(self)
self.click.setText("点击预测分类")
self.click.resize(790,50)
self.click.move(5,490)
self.click.clicked.connect(self.classifer)
self.predicts = QLabel(self)
self.predicts.setText('预测类别')
self.predicts.setStyleSheet("font:16pt'楷体';border-width:2px;border-style: inset;border-color:gray")
self.predicts.resize(790,40)
self.predicts.move(5,550)
layout1 = QVBoxLayout()
layout1.addWidget(self.open)
layout1.addWidget(self.path)
layout1.addWidget(self.display)
layout2 = QVBoxLayout()
layout2.addWidget(self.click)
layout2.addWidget(self.predicts)
layout = QVBoxLayout()
layout.addLayout(layout1)
layout.addLayout(layout2)
self.setLayout(layout)
def open_image(self):
global imgName
imgName, imgType = QFileDialog.getOpenFileName(self, "打开图片","", "*.jpg;;*.png;;All Files(*)")
jpg = QtGui.QPixmap(imgName)
self.display.setScaledContents(True)
self.dispaly.setPixmap(jpg)
self.path.setText(str(imgName))
def classifer(self):
labels = {0: 'cardboard', 1: 'glass', 2: 'metal', 3: 'paper', 4: 'plastic', 5: 'trash'}
img = cv2.imread(str(imgName))
img = cv2.resize(img,(100, 100)) # 使尺寸大小一样
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = np.array(img) / 255
img = img.astype(np.float64)
img = img.reshape(-1, 100, 100, 1)
model = load_model('/result/Ynnex1.h5')
predict_y = model.predict(img)
pred_y = int(np.round(predict_y))
print(pred_y)
self.predicts.setText(labels[str(pred_y)])
if __name__ == "__main__":
app = QApplication(sys.argv)
my = Classfier()
my.show()
sys.exit(app.exec_())
请求各位帮忙解决一下,谢谢