3_lx 2022-05-19 13:09 采纳率: 75%
浏览 58
已结题

pyqt点击预测分类无法显示分类,直接闪退

pyqt垃圾分类多分类问题显示分类结果,在点击完预测分类后无法显示分类,直接退出

# -*- 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.display.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_path = 'results/Ynnex1.h5'
        try:
            model_path = os.path.realpath(__file__).replace('main.py', model_path)
        except NameError:
            model_path = './' + model_path
        model = load_model(model_path)
        predict_y = model.predict(img)
        pred_y = int(np.round(predict_y))
        print(pred_y)
        self.predicts.setText(labels[pred_y])


if __name__ == "__main__":
    app = QApplication(sys.argv)
    my = Classfier()
    my.show()
    sys.exit(app.exec_())

img

请求各位解决一下问题,谢谢

  • 写回答

1条回答 默认 最新

  • 关注

    你主要调查下这个函数

    img


    可以进行单步调试下,应该就是有个值为空,所以导致程序奔溃

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 7月28日
  • 已采纳回答 7月20日
  • 创建了问题 5月19日