PyQt 多线程多进程研究
2024-01-09 05:48:22
PyQT系统界面卡顿的问题是因为检测图片的耗时太长了,大概一张图片四十多秒。如果用多线程来解决耗时几秒的任务,是可以解决闪退的问题的,并进一步解决卡顿问题。但是不能解决像我们这种超长耗时的任务的,只能利用多线程来避免。我尝试用多进程来解决,还是没办法解决这种问题。
完成的多进程操作,但是仍然未响应
test2.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test2.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.
'''
这个文件用于显示PyQt,无需输入正确的账号和密码就可以登录进去
选择图片的时候只能选择28*28像素的图片,也就是TestDigitImgs的第一张图片
'''
'''PyQt'''
import cv2
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QDesktopServices
from docx2pdf import convert
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
from infer_config import acne_infer_config
import math
from datetime import datetime
from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QApplication, QFileDialog
from PyQt5.Qt import Qt, QIcon
from PyQt5 import QtCore, QtGui, QtWidgets
'''调用类'''
from Docx import GenerateDocx
import os
import multiprocessing
import cv2
import numpy as np
from infer_config import acne_infer_config
from mmdeploy_runtime import Detector, Classifier
import utils
INFER_WINDOW_SIZE = [1024, 1024]
INFER_WINDOW_STRIDES = (960, 960)
OVERLAP_NMS_THRESHOLD = 0.5
BBOX_DISPLAY_CONFIDENCE = 0.5
CATEGORY_COLOR = {
'papule':(140,240,255),
'nevus':(162,247,223),
'nodule':(152,192,250),
'open_comedo':(86,143,128),
'closed_comedo':(60,57,166),
'atrophic_scar':(103,49,132),
'hypertrophic_scar':(249,233,160),
'melasma':(164,93,222),
'pustule':(235,177,161),
'other':(233,222,252)
}
CATEGORY_INDEX = {0:'papule',
1:'nevus',
2:'nodule',
3:'open_comedo',
4:'closed_comedo',
5:'atrophic_scar',
6:'hypertrophic_scar',
7:'melasma',
8:'pustule',
9:'other'}
CLASSIFICATION_DRUG = {
1:"无需治疗",
2:"BPO或弱效维A酸类外用",
3:"BPO联合维A酸类外用",
4:"BPO联合维A酸,考虑口服抗生素",
5:"BPO联合维A酸,口服抗生素",
6:"BPO联合维A酸,口服抗生素,并开始考虑异维A酸",
7:"BPO联合维A酸,口服抗生素,推荐异维A酸",
8:"异维A酸"
}
QApplication.processEvents()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1410, 902)
MainWindow.setWindowIcon(QIcon('./icon/main.ico'))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.groupBox_main = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_main.setTitle("")
self.groupBox_main.setObjectName("groupBox_main")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.groupBox_main)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout_largest = QtWidgets.QHBoxLayout()
self.horizontalLayout_largest.setObjectName("horizontalLayout_largest")
self.groupBox_left = QtWidgets.QGroupBox(self.groupBox_main)
self.groupBox_left.setMinimumSize(QtCore.QSize(320, 0))
self.groupBox_left.setMaximumSize(QtCore.QSize(320, 16777215))
self.groupBox_left.setTitle("")
self.groupBox_left.setObjectName("groupBox_left")
self.verticalLayout_8 = QtWidgets.QVBoxLayout(self.groupBox_left)
self.verticalLayout_8.setObjectName("verticalLayout_8")
self.groupBox_menu = QtWidgets.QGroupBox(self.groupBox_left)
self.groupBox_menu.setMaximumSize(QtCore.QSize(16777215, 40))
self.groupBox_menu.setTitle("")
self.groupBox_menu.setObjectName("groupBox_menu")
self.horizontalLayout_6 = QtWidgets.QHBoxLayout(self.groupBox_menu)
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
self.label_menu = QtWidgets.QLabel(self.groupBox_menu)
self.label_menu.setStyleSheet("font-size: 22px;")
self.label_menu.setObjectName("label_menu")
self.horizontalLayout_6.addWidget(self.label_menu)
spacerItem = QtWidgets.QSpacerItem(205, 17, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_6.addItem(spacerItem)
self.verticalLayout_8.addWidget(self.groupBox_menu)
self.groupBox_startAction = QtWidgets.QGroupBox(self.groupBox_left)
self.groupBox_startAction.setMaximumSize(QtCore.QSize(16777215, 130))
self.groupBox_startAction.setTitle("")
self.groupBox_startAction.setObjectName("groupBox_startAction")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.groupBox_startAction)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.horizontalLayout_imgAndDir = QtWidgets.QHBoxLayout()
self.horizontalLayout_imgAndDir.setObjectName("horizontalLayout_imgAndDir")
self.pushButton_selectImg = QtWidgets.QPushButton(self.groupBox_startAction)
self.pushButton_selectImg.setMinimumSize(QtCore.QSize(0, 40))
self.pushButton_selectImg.setStyleSheet("font-size: 18px;")
self.pushButton_selectImg.setObjectName("pushButton_selectImg")
self.horizontalLayout_imgAndDir.addWidget(self.pushButton_selectImg)
self.pushButton_selectDir = QtWidgets.QPushButton(self.groupBox_startAction)
self.pushButton_selectDir.setMinimumSize(QtCore.QSize(0, 40))
self.pushButton_selectDir.setStyleSheet("font-size: 18px;")
self.pushButton_selectDir.setObjectName("pushButton_selectDir")
self.horizontalLayout_imgAndDir.addWidget(self.pushButton_selectDir)
self.verticalLayout_4.addLayout(self.horizontalLayout_imgAndDir)
self.pushButton_startAction = QtWidgets.QPushButton(self.groupBox_startAction)
self.pushButton_startAction.setMinimumSize(QtCore.QSize(50, 40))
self.pushButton_startAction.setStyleSheet("font-size: 18px;")
self.pushButton_startAction.setObjectName("pushButton_startAction")
self.verticalLayout_4.addWidget(self.pushButton_startAction)
self.verticalLayout_8.addWidget(self.groupBox_startAction)
self.groupBox = QtWidgets.QGroupBox(self.groupBox_left)
self.groupBox.setTitle("")
self.groupBox.setObjectName("groupBox")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.groupBox)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton_save = QtWidgets.QPushButton(self.groupBox)
self.pushButton_save.setMinimumSize(QtCore.QSize(0, 40))
self.pushButton_save.setStyleSheet("font-size: 18px;")
self.pushButton_save.setObjectName("pushButton_save")
self.horizontalLayout.addWidget(self.pushButton_save)
self.lineEdit_savePath = QtWidgets.QLineEdit(self.groupBox)
self.lineEdit_savePath.setMinimumSize(QtCore.QSize(0, 40))
self.lineEdit_savePath.setObjectName("lineEdit_savePath")
self.horizontalLayout.addWidget(self.lineEdit_savePath)
self.horizontalLayout_3.addLayout(self.horizontalLayout)
self.verticalLayout_8.addWidget(self.groupBox)
self.groupBox_showDir = QtWidgets.QGroupBox(self.groupBox_left)
self.groupBox_showDir.setMaximumSize(QtCore.QSize(16777215, 55))
self.groupBox_showDir.setTitle("")
self.groupBox_showDir.setObjectName("groupBox_showDir")
self.horizontalLayout_10 = QtWidgets.QHBoxLayout(self.groupBox_showDir)
self.horizontalLayout_10.setObjectName("horizontalLayout_10")
self.pushButton_showPDF = QtWidgets.QPushButton(self.groupBox_showDir)
self.pushButton_showPDF.setMinimumSize(QtCore.QSize(0, 40))
self.pushButton_showPDF.setStyleSheet("font-size: 18px;")
self.pushButton_showPDF.setObjectName("pushButton_showPDF")
self.horizontalLayout_10.addWidget(self.pushButton_showPDF)
self.verticalLayout_8.addWidget(self.groupBox_showDir)
self.verticalLayout_result = QtWidgets.QVBoxLayout()
self.verticalLayout_result.setObjectName("verticalLayout_result")
self.groupBox_resultTitle = QtWidgets.QGroupBox(self.groupBox_left)
self.groupBox_resultTitle.setMinimumSize(QtCore.QSize(0, 50))
self.groupBox_resultTitle.setMaximumSize(QtCore.QSize(16777215, 50))
self.groupBox_resultTitle.setTitle("")
self.groupBox_resultTitle.setObjectName("groupBox_resultTitle")
self.horizontalLayout_8 = QtWidgets.QHBoxLayout(self.groupBox_resultTitle)
self.horizontalLayout_8.setObjectName("horizontalLayout_8")
self.label_resultTitle = QtWidgets.QLabel(self.groupBox_resultTitle)
self.label_resultTitle.setStyleSheet("font-size: 22px;")
self.label_resultTitle.setObjectName("label_resultTitle")
self.horizontalLayout_8.addWidget(self.label_resultTitle)
spacerItem1 = QtWidgets.QSpacerItem(181, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_8.addItem(spacerItem1)
self.verticalLayout_result.addWidget(self.groupBox_resultTitle)
self.groupBox_resultContent = QtWidgets.QGroupBox(self.groupBox_left)
self.groupBox_resultContent.setMinimumSize(QtCore.QSize(0, 50))
self.groupBox_resultContent.setTitle("")
self.groupBox_resultContent.setObjectName("groupBox_resultContent")
self.horizontalLayout_9 = QtWidgets.QHBoxLayout(self.groupBox_resultContent)
self.horizontalLayout_9.setObjectName("horizontalLayout_9")
self.listWidget = QtWidgets.QListWidget(self.groupBox_resultContent)
self.listWidget.setLayoutDirection(QtCore.Qt.LeftToRight)
self.listWidget.setStyleSheet("font-size: 14px;")
self.listWidget.setIconSize(QtCore.QSize(7, 7))
self.listWidget.setObjectName("listWidget")
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("./icon/closed_comedo.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("./icon/opend_comedo.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon1)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("./icon/papule.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon2)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap("./icon/pustule.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon3)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon4 = QtGui.QIcon()
icon4.addPixmap(QtGui.QPixmap("./icon/nodule.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon4)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon5 = QtGui.QIcon()
icon5.addPixmap(QtGui.QPixmap("./icon/atrophic_scar.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon5)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon6 = QtGui.QIcon()
icon6.addPixmap(QtGui.QPixmap("./icon/hypertrophic_scar.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon6)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon7 = QtGui.QIcon()
icon7.addPixmap(QtGui.QPixmap("./icon/melasma.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon7)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon8 = QtGui.QIcon()
icon8.addPixmap(QtGui.QPixmap("./icon/nevus.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon8)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon9 = QtGui.QIcon()
icon9.addPixmap(QtGui.QPixmap("./icon/other.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon9)
self.listWidget.addItem(item)
self.horizontalLayout_9.addWidget(self.listWidget)
self.verticalLayout_result.addWidget(self.groupBox_resultContent)
self.verticalLayout_8.addLayout(self.verticalLayout_result)
self.verticalLayout_advice = QtWidgets.QVBoxLayout()
self.verticalLayout_advice.setObjectName("verticalLayout_advice")
self.groupBox_10 = QtWidgets.QGroupBox(self.groupBox_left)
self.groupBox_10.setMinimumSize(QtCore.QSize(0, 50))
self.groupBox_10.setMaximumSize(QtCore.QSize(16777215, 50))
self.groupBox_10.setTitle("")
self.groupBox_10.setObjectName("groupBox_10")
self.horizontalLayout_12 = QtWidgets.QHBoxLayout(self.groupBox_10)
self.horizontalLayout_12.setObjectName("horizontalLayout_12")
self.label_adviceTitle = QtWidgets.QLabel(self.groupBox_10)
self.label_adviceTitle.setStyleSheet("font-size: 22px;")
self.label_adviceTitle.setObjectName("label_adviceTitle")
self.horizontalLayout_12.addWidget(self.label_adviceTitle)
spacerItem2 = QtWidgets.QSpacerItem(137, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_12.addItem(spacerItem2)
self.verticalLayout_advice.addWidget(self.groupBox_10)
self.groupBox_12 = QtWidgets.QGroupBox(self.groupBox_left)
self.groupBox_12.setMinimumSize(QtCore.QSize(0, 50))
self.groupBox_12.setMaximumSize(QtCore.QSize(16777215, 120))
self.groupBox_12.setTitle("")
self.groupBox_12.setObjectName("groupBox_12")
self.horizontalLayout_13 = QtWidgets.QHBoxLayout(self.groupBox_12)
self.horizontalLayout_13.setObjectName("horizontalLayout_13")
self.label_adviceResult = QtWidgets.QLabel(self.groupBox_12)
self.label_adviceResult.setMinimumSize(QtCore.QSize(0, 50))
self.label_adviceResult.setMaximumSize(QtCore.QSize(16777215, 120))
self.label_adviceResult.setStyleSheet("font-size: 16px;")
self.label_adviceResult.setAlignment(QtCore.Qt.AlignCenter)
self.label_adviceResult.setObjectName("label_adviceResult")
self.horizontalLayout_13.addWidget(self.label_adviceResult)
self.verticalLayout_advice.addWidget(self.groupBox_12)
self.verticalLayout_8.addLayout(self.verticalLayout_advice)
self.horizontalLayout_largest.addWidget(self.groupBox_left)
self.groupBox_right = QtWidgets.QGroupBox(self.groupBox_main)
self.groupBox_right.setTitle("")
self.groupBox_right.setObjectName("groupBox_right")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.groupBox_right)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.groupBox_selectImg = QtWidgets.QGroupBox(self.groupBox_right)
self.groupBox_selectImg.setMaximumSize(QtCore.QSize(16777215, 40))
self.groupBox_selectImg.setTitle("")
self.groupBox_selectImg.setObjectName("groupBox_selectImg")
self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.groupBox_selectImg)
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.label_imgTitle = QtWidgets.QLabel(self.groupBox_selectImg)
self.label_imgTitle.setStyleSheet("font-size: 20px;")
self.label_imgTitle.setObjectName("label_imgTitle")
self.horizontalLayout_5.addWidget(self.label_imgTitle)
self.lineEdit_imgPath = QtWidgets.QLineEdit(self.groupBox_selectImg)
self.lineEdit_imgPath.setMinimumSize(QtCore.QSize(0, 25))
self.lineEdit_imgPath.setMaximumSize(QtCore.QSize(300, 16777215))
self.lineEdit_imgPath.setObjectName("lineEdit_imgPath")
self.horizontalLayout_5.addWidget(self.lineEdit_imgPath)
spacerItem3 = QtWidgets.QSpacerItem(287, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_5.addItem(spacerItem3)
self.label_5 = QtWidgets.QLabel(self.groupBox_selectImg)
self.label_5.setText("")
self.label_5.setObjectName("label_5")
self.horizontalLayout_5.addWidget(self.label_5)
self.verticalLayout_3.addWidget(self.groupBox_selectImg)
self.horizontalLayout_imgs = QtWidgets.QHBoxLayout()
self.horizontalLayout_imgs.setObjectName("horizontalLayout_imgs")
self.label_showImg = QtWidgets.QLabel(self.groupBox_right)
self.label_showImg.setMinimumSize(QtCore.QSize(500, 0))
self.label_showImg.setText("")
self.label_showImg.setAlignment(QtCore.Qt.AlignCenter)
self.label_showImg.setObjectName("label_showImg")
self.horizontalLayout_imgs.addWidget(self.label_showImg)
spacerItem4 = QtWidgets.QSpacerItem(10, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.horizontalLayout_imgs.addItem(spacerItem4)
self.label_showImg2 = QtWidgets.QLabel(self.groupBox_right)
self.label_showImg2.setMinimumSize(QtCore.QSize(500, 0))
self.label_showImg2.setText("")
self.label_showImg2.setAlignment(QtCore.Qt.AlignCenter)
self.label_showImg2.setObjectName("label_showImg2")
self.horizontalLayout_imgs.addWidget(self.label_showImg2)
self.verticalLayout_3.addLayout(self.horizontalLayout_imgs)
self.horizontalLayout_pageTurning = QtWidgets.QHBoxLayout()
self.horizontalLayout_pageTurning.setObjectName("horizontalLayout_pageTurning")
self.prev_image_button = QtWidgets.QPushButton(self.groupBox_right)
self.prev_image_button.setMinimumSize(QtCore.QSize(0, 30))
self.prev_image_button.setObjectName("prev_image_button")
self.horizontalLayout_pageTurning.addWidget(self.prev_image_button)
self.label_pageNum = QtWidgets.QLabel(self.groupBox_right)
self.label_pageNum.setMaximumSize(QtCore.QSize(60, 16777215))
self.label_pageNum.setAlignment(QtCore.Qt.AlignCenter)
self.label_pageNum.setObjectName("label_pageNum")
self.horizontalLayout_pageTurning.addWidget(self.label_pageNum)
self.next_image_button = QtWidgets.QPushButton(self.groupBox_right)
self.next_image_button.setMinimumSize(QtCore.QSize(0, 30))
self.next_image_button.setObjectName("next_image_button")
self.horizontalLayout_pageTurning.addWidget(self.next_image_button)
self.verticalLayout_3.addLayout(self.horizontalLayout_pageTurning)
self.horizontalLayout_largest.addWidget(self.groupBox_right)
self.verticalLayout_2.addLayout(self.horizontalLayout_largest)
self.groupBox_botton = QtWidgets.QGroupBox(self.groupBox_main)
self.groupBox_botton.setTitle("")
self.groupBox_botton.setObjectName("groupBox_botton")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.groupBox_botton)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_condition = QtWidgets.QLabel(self.groupBox_botton)
self.label_condition.setObjectName("label_condition")
self.horizontalLayout_2.addWidget(self.label_condition)
self.verticalLayout_2.addWidget(self.groupBox_botton)
self.verticalLayout.addWidget(self.groupBox_main)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "痤疮检测"))
self.label_menu.setText(_translate("MainWindow", "菜单栏"))
self.pushButton_selectImg.setText(_translate("MainWindow", "选择图片"))
self.pushButton_selectDir.setText(_translate("MainWindow", "选择文件夹"))
self.pushButton_startAction.setText(_translate("MainWindow", "开始检测"))
self.pushButton_save.setText(_translate("MainWindow", "保存报告"))
self.pushButton_showPDF.setText(_translate("MainWindow", "显示输出文件夹"))
self.label_resultTitle.setText(_translate("MainWindow", "检测结果"))
__sortingEnabled = self.listWidget.isSortingEnabled()
self.listWidget.setSortingEnabled(False)
item = self.listWidget.item(0)
item.setText(_translate("MainWindow", "痤疮分级结果:待检测"))
item = self.listWidget.item(2)
item.setText(_translate("MainWindow", "各类别痤疮检测数量:"))
item = self.listWidget.item(4)
item.setText(_translate("MainWindow", " 闭口粉刺:待检测"))
item = self.listWidget.item(5)
item.setText(_translate("MainWindow", " 开口粉刺:待检测"))
item = self.listWidget.item(6)
item.setText(_translate("MainWindow", " 丘疹:待检测"))
item = self.listWidget.item(7)
item.setText(_translate("MainWindow", " 脓疱:待检测"))
item = self.listWidget.item(8)
item.setText(_translate("MainWindow", " 结节:待检测"))
item = self.listWidget.item(9)
item.setText(_translate("MainWindow", " 萎缩性瘢痕:待检测"))
item = self.listWidget.item(10)
item.setText(_translate("MainWindow", " 增生性瘢痕刺:待检测"))
item = self.listWidget.item(11)
item.setText(_translate("MainWindow", " 斑:待检测"))
item = self.listWidget.item(12)
item.setText(_translate("MainWindow", " 色素痣:待检测"))
item = self.listWidget.item(13)
item.setText(_translate("MainWindow", " 其他:待检测"))
self.listWidget.setSortingEnabled(__sortingEnabled)
self.label_adviceTitle.setText(_translate("MainWindow", "推荐治疗方案"))
self.label_adviceResult.setText(_translate("MainWindow", "用药指南"))
self.label_imgTitle.setText(_translate("MainWindow", "所选图片"))
self.prev_image_button.setText(_translate("MainWindow", "上一页"))
self.label_pageNum.setText(_translate("MainWindow", "共 0 页"))
self.next_image_button.setText(_translate("MainWindow", "下一页"))
self.label_condition.setText(_translate("MainWindow", "系统运行情况"))
# self.worker_thread = WorkerThread(self)
# self.worker_thread_pdf = WorkerThread_pdf(self)
# 按钮关联函数
self.pushButton_selectDir.clicked.connect(self.openImageFolder)
# self.pushButton_startAction.clicked.connect(self.startAction)
self.pushButton_selectImg.clicked.connect(self.openImage)
self.pushButton_startAction.clicked.connect(self.start_work)
# self.worker_thread.finished.connect(self.on_worker_finished)
self.pushButton_save.clicked.connect(self.start_work_pdf)
# self.worker_thread_pdf.finished.connect(self.on_worker_finished_pdf)
self.pushButton_showPDF.clicked.connect(self.button_show_dir)
self.prev_image_button.clicked.connect(self.showPrevImage)
self.next_image_button.clicked.connect(self.showNextImage)
self.timer = QTimer()
QApplication.processEvents()
def start_work(self):
# self.worker_thread = WorkerThread(self)
# 启动工作线程
QApplication.processEvents()
if len(self.selected_imgsPath) == 0:
return
self.pushButton_startAction.setEnabled(False) # 禁用按钮
self.pushButton_selectImg.setEnabled(False) # 禁用按钮
self.pushButton_save.setEnabled(False) # 禁用按钮
self.pushButton_showPDF.setEnabled(False) # 禁用按钮
self.pushButton_selectDir.setEnabled(False) # 禁用按钮
self.prev_image_button.setEnabled(False)
self.next_image_button.setEnabled(False)
# self.timer.start(1)
# self.worker_thread.start()
self.grading_result = 0 # 从1开始的痤疮等级
self.count = 0
# 判断是否选择了图片
if len(self.selected_imgsPath) == 0:
print("未选择图片")
return
for i in range(len(self.selected_imgsPath)):
QApplication.processEvents()
self.count = i+1
# self.label_showResult.setText(f'正在检测第{count}张图片')
self.label_condition.setText(f'请耐心等待,正在检测第{self.count}张图片')
image = cv2.imread(self.selected_imgsPath[i])
self.shared_dict['image'] = image
process = MyProcess(self.shared_dict)
process.start()
process.join()
# bboxes, labels, masks = self.acne_instance_seg(image, self.detector)
# cls_result = self.acne_severity_grading(image, self.classifier)
bboxes = self.shared_dict['bboxes']
labels = self.shared_dict['labels']
masks = self.shared_dict['masks']
cls_result = self.shared_dict['cls_result']
self.grading_result = cls_result[0][0]+1
indices = [i for i in range(len(bboxes))]
QApplication.processEvents()
category_count = {
'papule':0,
'nevus':0,
'nodule':0,
'open_comedo':0,
'closed_comedo':0,
'atrophic_scar':0,
'hypertrophic_scar':0,
'melasma':0,
'pustule':0,
'other':0
}
for index, bbox, label_id in zip(indices, bboxes, labels):
QApplication.processEvents()
# print(count,index,bbox,label_id)
[left, top, right, bottom], score = bbox[0:4].astype(int), bbox[4]
if score < BBOX_DISPLAY_CONFIDENCE:
continue
color = CATEGORY_COLOR[CATEGORY_INDEX[label_id]]
cv2.rectangle(image, (left, top), (right, bottom), color)
label_text = f"{CATEGORY_INDEX[label_id]} | {int(100 * score)}%" # 使用痤疮的标签作为标注信息
cv2.putText(image, label_text, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1) # 添加标注信息
if masks[index].size:
mask = masks[index]
if mask.shape == image.shape[:2]: # rtmdet-inst
mask_img = image.copy()
else: # maskrcnn
x0 = int(max(math.floor(bbox[0]) - 1, 0))
y0 = int(max(math.floor(bbox[1]) - 1, 0))
mask_img = image[y0:y0 + mask.shape[0], x0:x0 + mask.shape[1]]
mask_img[mask.astype(bool)] = color
category_count[CATEGORY_INDEX[label_id]] += 1
text = f'Prediction: {cls_result[0][0]}, {cls_result[0][1]:.4f} ' \
f'({acne_infer_config["classifier"]["classes"][cls_result[0][0]]})'
cv2.putText(image, text, (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), 2)
img_name = os.path.splitext(os.path.basename(self.selected_imgsPath[i]))[0]
# 获取当前日期和时间
now = datetime.now()
# 提取年、月和日
year = now.year
month = now.month
day = now.day
generated_image_path = f"./result/{img_name}_report_{year}_{month}_{day}.jpg"
cv2.imwrite(generated_image_path , image)
self.generated_image_paths.append(generated_image_path)
closed_comedo_count = category_count['closed_comedo']
open_comedo_count = category_count['open_comedo']
papule_count = category_count['papule']
pustule_count = category_count['pustule']
nodule_count = category_count['nodule']
atrophic_scar_count = category_count['atrophic_scar']
hypertrophic_scar_count = category_count['hypertrophic_scar']
melasma_count = category_count['melasma']
nevus_count = category_count['nevus']
other_count = category_count['other']
self.text[img_name] = {
"grading_result":self.grading_result,
"category_count":{
"closed_comedo":closed_comedo_count,
"open_comedo":open_comedo_count,
"papule":papule_count,
"pustule":pustule_count,
"nodule":nodule_count,
"atrophic_scar":atrophic_scar_count,
"hypertrophic_scar":hypertrophic_scar_count,
"melasma":melasma_count,
"nevus":nevus_count,
"other":other_count
}
}
grading_result = self.text[img_name]["grading_result"]
item = self.listWidget.item(0)
item.setText(f"痤疮分级结果:{grading_result}")
item = self.listWidget.item(2)
item.setText("各类别痤疮检测数量:")
item = self.listWidget.item(4)
closed_comedo_count = self.text[img_name]["category_count"]["closed_comedo"]
item.setText(f" 闭口粉刺:{closed_comedo_count}")
item = self.listWidget.item(5)
open_comedo_count = self.text[img_name]["category_count"]["open_comedo"]
item.setText(f" 开口粉刺:{open_comedo_count}")
item = self.listWidget.item(6)
papule_count = self.text[img_name]["category_count"]["papule"]
item.setText(f" 丘疹:{papule_count}")
item = self.listWidget.item(7)
pustule_count = self.text[img_name]["category_count"]["pustule"]
item.setText(f" 脓疱:{pustule_count}")
item = self.listWidget.item(8)
nodule_count = self.text[img_name]["category_count"]["nodule"]
item.setText(f" 结节:{nodule_count}")
item = self.listWidget.item(9)
atrophic_scar_count = self.text[img_name]["category_count"]["atrophic_scar"]
item.setText(f" 萎缩性瘢痕:{atrophic_scar_count}")
item = self.listWidget.item(10)
hypertrophic_scar_count = self.text[img_name]["category_count"]["hypertrophic_scar"]
item.setText(f" 增生性瘢痕刺:{hypertrophic_scar_count}")
item = self.listWidget.item(11)
melasma_count = self.text[img_name]["category_count"]["melasma"]
item.setText(f" 斑:{melasma_count}")
item = self.listWidget.item(12)
nevus_count = self.text[img_name]["category_count"]["nevus"]
item.setText(f" 色素痣:{nevus_count}")
item = self.listWidget.item(13)
other_count = self.text[img_name]["category_count"]["other"]
item.setText(f" 其他:{other_count}")
'''生成报告'''
#必须先生成docx文件再生成pdf文件,我的docx文件和产生的结果图片保存在同一个位置的
docx_generate = GenerateDocx(generated_image_path,'./result',self.grading_result,category_count)
self.generated_docxFilePath = docx_generate.predict()
self.generated_docxFilePath_List.append(self.generated_docxFilePath)
img_path = self.generated_image_paths[0]
# 通过文件路径获取图片文件,并设置图片长宽为label控件的长、宽
img = QtGui.QPixmap(img_path).scaled(self.label_showImg2.width(), self.label_showImg2.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation)
# 在label控件上显示选择的图片
self.label_showImg2.setPixmap(img)
#显示用药指南
self.label_adviceResult.setText(CLASSIFICATION_DRUG[self.grading_result])
self.label_condition.setText('所有图片检测结束,请保存报告')
# self.worker_thread.finished.connect(self.on_worker_finished)
self.pushButton_startAction.setEnabled(True) # 启动之前被禁用的按钮
self.pushButton_selectImg.setEnabled(True)
self.pushButton_save.setEnabled(True)
self.pushButton_showPDF.setEnabled(True)
self.pushButton_selectDir.setEnabled(True)
self.prev_image_button.setEnabled(True)
self.next_image_button.setEnabled(True)
img_path = self.selected_imgsPath[0]
img_name = os.path.splitext(os.path.basename(img_path))[0]
grading_result = self.text[img_name]["grading_result"]
item = self.listWidget.item(0)
item.setText(f"痤疮分级结果:{grading_result}")
item = self.listWidget.item(2)
item.setText("各类别痤疮检测数量:")
item = self.listWidget.item(4)
closed_comedo_count = self.text[img_name]["category_count"]["closed_comedo"]
item.setText(f" 闭口粉刺:{closed_comedo_count}")
item = self.listWidget.item(5)
open_comedo_count = self.text[img_name]["category_count"]["open_comedo"]
item.setText(f" 开口粉刺:{open_comedo_count}")
item = self.listWidget.item(6)
papule_count = self.text[img_name]["category_count"]["papule"]
item.setText(f" 丘疹:{papule_count}")
item = self.listWidget.item(7)
pustule_count = self.text[img_name]["category_count"]["pustule"]
item.setText(f" 脓疱:{pustule_count}")
item = self.listWidget.item(8)
nodule_count = self.text[img_name]["category_count"]["nodule"]
item.setText(f" 结节:{nodule_count}")
item = self.listWidget.item(9)
atrophic_scar_count = self.text[img_name]["category_count"]["atrophic_scar"]
item.setText(f" 萎缩性瘢痕:{atrophic_scar_count}")
item = self.listWidget.item(10)
hypertrophic_scar_count = self.text[img_name]["category_count"]["hypertrophic_scar"]
item.setText(f" 增生性瘢痕刺:{hypertrophic_scar_count}")
item = self.listWidget.item(11)
melasma_count = self.text[img_name]["category_count"]["melasma"]
item.setText(f" 斑:{melasma_count}")
item = self.listWidget.item(12)
nevus_count = self.text[img_name]["category_count"]["nevus"]
item.setText(f" 色素痣:{nevus_count}")
item = self.listWidget.item(13)
other_count = self.text[img_name]["category_count"]["other"]
item.setText(f" 其他:{other_count}")
# print("ok")
def start_work_pdf(self):
# 启动工作线程
self.worker_thread_pdf = WorkerThread_pdf(self)
if len(self.generated_image_paths) == 0:
return
self.worker_thread_pdf.start()
# self.worker_thread_pdf.finished.connect(self.on_worker_finished_pdf)
def on_worker_finished_pdf(self):
return
# self.delete_files_in_current_folder()
def acne_instance_seg(self,img: np.ndarray, detector: Detector):
win_gen = utils.WindowGenerator(img.shape[0], img.shape[1],
INFER_WINDOW_SIZE[0], INFER_WINDOW_SIZE[1],
INFER_WINDOW_STRIDES[0], INFER_WINDOW_STRIDES[1])
p_bboxes, p_labels, p_masks = [], [], []
for h_slice, w_slice in win_gen:
img_patch = img[h_slice, w_slice, :]
offset_x, offset_y = w_slice.start, h_slice.start
b, l, m = detector(img_patch)
b[:, [0, 2]] += offset_x
b[:, [1, 3]] += offset_y
p_bboxes.append(b)
p_labels.append(l)
p_masks.extend(m)
p_bboxes = np.concatenate(p_bboxes, axis=0)
p_labels = np.concatenate(p_labels, axis=0)
p_masks = np.array(p_masks, dtype=object)
bboxes, labels, masks = [], [], []
cls = np.unique(p_labels)
for c in cls:
idx = p_labels == c
b = p_bboxes[idx]
l = p_labels[idx]
m = p_masks[idx]
keep = utils.nms(b, OVERLAP_NMS_THRESHOLD)
bboxes.append(b[keep])
labels.append(l[keep])
masks.append(m[keep])
bboxes = np.concatenate(bboxes, axis=0)
labels = np.concatenate(labels, axis=0)
masks = np.concatenate(masks, axis=0)
return bboxes, labels, masks
def acne_severity_grading(self,img: np.ndarray, classifier: Classifier):
result = classifier(img)
return result
def showNextImage(self):
if len(self.selected_imgsPath) == 0:
return
self.current_image_index += 1
if self.current_image_index >= len(self.selected_imgsPath):
self.current_image_index = 0
img_path = self.selected_imgsPath[self.current_image_index]
img = QtGui.QPixmap(img_path).scaled(self.label_showImg.width(), self.label_showImg.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation)
self.label_showImg.setPixmap(img)
self.lineEdit_imgPath.setText(img_path)
pageNum = len(self.selected_imgsPath)
current_image_index_fact = self.current_image_index + 1
self.label_pageNum.setText(f"{current_image_index_fact} / {pageNum} ")
if len(self.text) != 0:
img_name = os.path.splitext(os.path.basename(img_path))[0]
# self.label_showResult.setText(self.text[img_name])
# print(self.text[img_name])
grading_result = self.text[img_name]["grading_result"]
item = self.listWidget.item(0)
item.setText(f"痤疮分级结果:{grading_result}")
item = self.listWidget.item(2)
item.setText("各类别痤疮检测数量:")
item = self.listWidget.item(4)
closed_comedo_count = self.text[img_name]["category_count"]["closed_comedo"]
item.setText(f" 闭口粉刺:{closed_comedo_count}")
item = self.listWidget.item(5)
open_comedo_count = self.text[img_name]["category_count"]["open_comedo"]
item.setText(f" 开口粉刺:{open_comedo_count}")
item = self.listWidget.item(6)
papule_count = self.text[img_name]["category_count"]["papule"]
item.setText(f" 丘疹:{papule_count}")
item = self.listWidget.item(7)
pustule_count = self.text[img_name]["category_count"]["pustule"]
item.setText(f" 脓疱:{pustule_count}")
item = self.listWidget.item(8)
nodule_count = self.text[img_name]["category_count"]["nodule"]
item.setText(f" 结节:{nodule_count}")
item = self.listWidget.item(9)
atrophic_scar_count = self.text[img_name]["category_count"]["atrophic_scar"]
item.setText(f" 萎缩性瘢痕:{atrophic_scar_count}")
item = self.listWidget.item(10)
hypertrophic_scar_count = self.text[img_name]["category_count"]["hypertrophic_scar"]
item.setText(f" 增生性瘢痕刺:{hypertrophic_scar_count}")
item = self.listWidget.item(11)
melasma_count = self.text[img_name]["category_count"]["melasma"]
item.setText(f" 斑:{melasma_count}")
item = self.listWidget.item(12)
nevus_count = self.text[img_name]["category_count"]["nevus"]
item.setText(f" 色素痣:{nevus_count}")
item = self.listWidget.item(13)
other_count = self.text[img_name]["category_count"]["other"]
item.setText(f" 其他:{other_count}")
img_path2 = self.generated_image_paths[self.current_image_index]
img2 = QtGui.QPixmap(img_path2).scaled(self.label_showImg2.width(), self.label_showImg2.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation)
self.label_showImg2.setPixmap(img2)
def showPrevImage(self):
if len(self.selected_imgsPath) == 0:
return
self.current_image_index -= 1
if self.current_image_index < 0:
self.current_image_index = len(self.selected_imgsPath) - 1
img_path = self.selected_imgsPath[self.current_image_index]
img = QtGui.QPixmap(img_path).scaled(self.label_showImg.width(), self.label_showImg.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation)
self.label_showImg.setPixmap(img)
self.lineEdit_imgPath.setText(img_path)
pageNum = len(self.selected_imgsPath)
current_image_index_fact = self.current_image_index + 1
self.label_pageNum.setText(f"{current_image_index_fact} / {pageNum} ")
if len(self.text) != 0:
img_name = os.path.splitext(os.path.basename(img_path))[0]
# self.label_showResult.setText(self.text[img_name])
# print(self.text[img_name])
grading_result = self.text[img_name]["grading_result"]
item = self.listWidget.item(0)
item.setText(f"痤疮分级结果:{grading_result}")
item = self.listWidget.item(2)
item.setText("各类别痤疮检测数量:")
item = self.listWidget.item(4)
closed_comedo_count = self.text[img_name]["category_count"]["closed_comedo"]
item.setText(f" 闭口粉刺:{closed_comedo_count}")
item = self.listWidget.item(5)
open_comedo_count = self.text[img_name]["category_count"]["open_comedo"]
item.setText(f" 开口粉刺:{open_comedo_count}")
item = self.listWidget.item(6)
papule_count = self.text[img_name]["category_count"]["papule"]
item.setText(f" 丘疹:{papule_count}")
item = self.listWidget.item(7)
pustule_count = self.text[img_name]["category_count"]["pustule"]
item.setText(f" 脓疱:{pustule_count}")
item = self.listWidget.item(8)
nodule_count = self.text[img_name]["category_count"]["nodule"]
item.setText(f" 结节:{nodule_count}")
item = self.listWidget.item(9)
atrophic_scar_count = self.text[img_name]["category_count"]["atrophic_scar"]
item.setText(f" 萎缩性瘢痕:{atrophic_scar_count}")
item = self.listWidget.item(10)
hypertrophic_scar_count = self.text[img_name]["category_count"]["hypertrophic_scar"]
item.setText(f" 增生性瘢痕刺:{hypertrophic_scar_count}")
item = self.listWidget.item(11)
melasma_count = self.text[img_name]["category_count"]["melasma"]
item.setText(f" 斑:{melasma_count}")
item = self.listWidget.item(12)
nevus_count = self.text[img_name]["category_count"]["nevus"]
item.setText(f" 色素痣:{nevus_count}")
item = self.listWidget.item(13)
other_count = self.text[img_name]["category_count"]["other"]
item.setText(f" 其他:{other_count}")
img_path2 = self.generated_image_paths[self.current_image_index]
img2 = QtGui.QPixmap(img_path2).scaled(self.label_showImg2.width(), self.label_showImg2.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation)
self.label_showImg2.setPixmap(img2)
def button_show_dir(self):
default_dir = "./report_pdf"
show_dirPath = 0
if self.dir_choose == '':
show_dirPath = default_dir
else:
show_dirPath = self.dir_choose
url = QUrl.fromLocalFile(show_dirPath)
if url:
print("选择的文件夹路径:", show_dirPath)
else:
print("未选择文件夹")
QDesktopServices.openUrl(url)
# 选择本地图片上传
def openImage(self):
self.selected_imgsPath = [] # 清空之前选择的所有图片
self.generated_image_paths = [] # 清空之前推理生成的所有图片
# 弹出一个文件选择框,第一个返回值imgName记录选中的文件路径+文件名,第二个返回值imgType记录文件的类型
# QFileDialog就是系统对话框的那个类第一个参数是上下文,第二个参数是弹框的名字,第三个参数是默认打开的路径,第四个参数是需要的格式
self.selected_imgsPath, _ = QtWidgets.QFileDialog.getOpenFileNames(
self, "打开图片", "./pending_images", "*.jpg;;*.png;;All Files(*)")
if len(self.selected_imgsPath) == 0:
print('没有选择图片!')
self.label_condition.setText("没有选择图片!") # 点击选择图片按钮后不选择图片会将存放图片路径的列表置为空
# 创建一个空的QPixmap对象
empty_pixmap = QPixmap()
self.label_showImg.setPixmap(empty_pixmap)
pageNum = 0
self.label_pageNum.setText(f"共 {pageNum} 页")
return
# 通过文件路径获取图片文件,并设置图片长宽为label控件的长、宽
img = QtGui.QPixmap(self.selected_imgsPath[0]).scaled(self.label_showImg.width(), self.label_showImg.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation)
# 提示点击生成
self.label_condition.setText("请点击 开始检测")
# 在label控件上显示选择的图片
self.label_showImg.setPixmap(img)
# 显示所选图片的路径
self.lineEdit_imgPath.setText(self.selected_imgsPath[0])
pageNum = len(self.selected_imgsPath)
self.label_pageNum.setText(f"共 {pageNum} 页")
# 选择图片文件夹上传
def openImageFolder(self):
self.selected_imgsPath = [] # 清空之前选择的所有图片
self.generated_image_paths = [] # 清空之前推理生成的所有图片
# 弹出一个文件夹选择框,返回选中的文件夹路径
selected_folder = QtWidgets.QFileDialog.getExistingDirectory(self, "选择文件夹", "./pending_images")
if selected_folder:
# 获取文件夹中的所有图片文件路径
for file_name in os.listdir(selected_folder):
if file_name.lower().endswith((".jpg", ".png")):
self.selected_imgsPath.append(os.path.join(selected_folder, file_name))
# 通过第一个图片文件路径获取图片,并设置图片长宽为label控件的长、宽
img = QtGui.QPixmap(self.selected_imgsPath[0]).scaled(self.label_showImg.width(), self.label_showImg.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation)
# 提示点击开始检测
self.label_condition.setText("请点击 开始检测")
# 在label控件上显示第一张图片
self.label_showImg.setPixmap(img)
# 显示所选图片的路径
self.lineEdit_imgPath.setText(self.selected_imgsPath[0])
pageNum = len(self.selected_imgsPath)
self.label_pageNum.setText(f"共 {pageNum} 页")
else:
print('没有选择图片!')
self.label_condition.setText("没有选择图片!")
empty_pixmap = QPixmap()
self.label_showImg.setPixmap(empty_pixmap)
pageNum = 0
self.label_pageNum.setText(f"共 {pageNum} 页")
return
def closeEvent(self, event):
# 在这里执行你的函数
self.delete_files_in_current_folder()
event.accept()
# 删除中途产生的图片和docx文件
def delete_files_in_current_folder(self):
# 获取当前文件夹路径
current_folder = os.path.dirname(self.generated_docxFilePath)
# 获取当前文件夹中的所有文件
if len(current_folder) == 0:
print('没有报告生成')
return
file_list = os.listdir(current_folder)
# 遍历文件列表
for file_name in file_list:
# 构建文件的绝对路径
file_path = os.path.join(current_folder, file_name)
# 判断是否是文件
if os.path.isfile(file_path):
# 删除文件
os.remove(file_path)
# # 自定义的工作线程类
# class WorkerThread(QThread):
# # 生成对应预测的数字
# # 定义一个信号,用于在工作完成后发送通知
# finished = pyqtSignal()
# QApplication.processEvents()
# def __init__(self):
# super().__init__()
# # self = main_window
# self.detector, self.classifier = self.create_acne_inference(acne_infer_config)
# def create_acne_inference(self,cfg):
# self.detector = Detector(model_path=cfg['detector']['model_path'],
# device_name=cfg['detector']['device_name'],
# device_id=cfg['detector']['device_id'])
# self.classifier = Classifier(model_path=cfg['classifier']['model_path'],
# device_name=cfg['classifier']['device_name'],
# device_id=cfg['classifier']['device_id'])
# return self.detector, self.classifier
# def run(self):
# # 执行耗时的任务
# self.do_work()
# # 发送工作完成的信号
# self.finished.emit()
# def do_work(self):
# # 模拟耗时的任务
# # '''人脸严重程度分级'''
# self.grading_result = 0 # 从1开始的痤疮等级
# self.count = 0
# # 判断是否选择了图片
# if self == 0:
# print("未选择图片")
# return
# for i in range(len(self.selected_imgsPath)):
# QApplication.processEvents()
# self.count = i+1
# # self.label_showResult.setText(f'正在检测第{count}张图片')
# self.label_condition.setText(f'请耐心等待,正在检测第{self.count}张图片')
# image = cv2.imread(self.selected_imgsPath[i])
# bboxes, labels, masks = self.acne_instance_seg(image, self.detector)
# cls_result = self.acne_severity_grading(image, self.classifier)
# self.grading_result = cls_result[0][0]+1
# indices = [i for i in range(len(bboxes))]
# QApplication.processEvents()
# category_count = {
# 'papule':0,
# 'nevus':0,
# 'nodule':0,
# 'open_comedo':0,
# 'closed_comedo':0,
# 'atrophic_scar':0,
# 'hypertrophic_scar':0,
# 'melasma':0,
# 'pustule':0,
# 'other':0
# }
# for index, bbox, label_id in zip(indices, bboxes, labels):
# QApplication.processEvents()
# # print(count,index,bbox,label_id)
# [left, top, right, bottom], score = bbox[0:4].astype(int), bbox[4]
# if score < BBOX_DISPLAY_CONFIDENCE:
# continue
# color = CATEGORY_COLOR[CATEGORY_INDEX[label_id]]
# cv2.rectangle(image, (left, top), (right, bottom), color)
# label_text = f"{CATEGORY_INDEX[label_id]} | {int(100 * score)}%" # 使用痤疮的标签作为标注信息
# cv2.putText(image, label_text, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1) # 添加标注信息
# if masks[index].size:
# mask = masks[index]
# if mask.shape == image.shape[:2]: # rtmdet-inst
# mask_img = image.copy()
# else: # maskrcnn
# x0 = int(max(math.floor(bbox[0]) - 1, 0))
# y0 = int(max(math.floor(bbox[1]) - 1, 0))
# mask_img = image[y0:y0 + mask.shape[0], x0:x0 + mask.shape[1]]
# mask_img[mask.astype(bool)] = color
# category_count[CATEGORY_INDEX[label_id]] += 1
# text = f'Prediction: {cls_result[0][0]}, {cls_result[0][1]:.4f} ' \
# f'({acne_infer_config["classifier"]["classes"][cls_result[0][0]]})'
# cv2.putText(image, text, (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), 2)
# img_name = os.path.splitext(os.path.basename(self.selected_imgsPath[i]))[0]
# # 获取当前日期和时间
# now = datetime.now()
# # 提取年、月和日
# year = now.year
# month = now.month
# day = now.day
# generated_image_path = f"./result/{img_name}_report_{year}_{month}_{day}.jpg"
# cv2.imwrite(generated_image_path , image)
# self.generated_image_paths.append(generated_image_path)
# closed_comedo_count = category_count['closed_comedo']
# open_comedo_count = category_count['open_comedo']
# papule_count = category_count['papule']
# pustule_count = category_count['pustule']
# nodule_count = category_count['nodule']
# atrophic_scar_count = category_count['atrophic_scar']
# hypertrophic_scar_count = category_count['hypertrophic_scar']
# melasma_count = category_count['melasma']
# nevus_count = category_count['nevus']
# other_count = category_count['other']
# self.text[img_name] = {
# "grading_result":self.grading_result,
# "category_count":{
# "closed_comedo":closed_comedo_count,
# "open_comedo":open_comedo_count,
# "papule":papule_count,
# "pustule":pustule_count,
# "nodule":nodule_count,
# "atrophic_scar":atrophic_scar_count,
# "hypertrophic_scar":hypertrophic_scar_count,
# "melasma":melasma_count,
# "nevus":nevus_count,
# "other":other_count
# }
# }
# grading_result = self.text[img_name]["grading_result"]
# item = self.listWidget.item(0)
# item.setText(f"痤疮分级结果:{grading_result}")
# item = self.listWidget.item(2)
# item.setText("各类别痤疮检测数量:")
# item = self.listWidget.item(4)
# closed_comedo_count = self.text[img_name]["category_count"]["closed_comedo"]
# item.setText(f" 闭口粉刺:{closed_comedo_count}")
# item = self.listWidget.item(5)
# open_comedo_count = self.text[img_name]["category_count"]["open_comedo"]
# item.setText(f" 开口粉刺:{open_comedo_count}")
# item = self.listWidget.item(6)
# papule_count = self.text[img_name]["category_count"]["papule"]
# item.setText(f" 丘疹:{papule_count}")
# item = self.listWidget.item(7)
# pustule_count = self.text[img_name]["category_count"]["pustule"]
# item.setText(f" 脓疱:{pustule_count}")
# item = self.listWidget.item(8)
# nodule_count = self.text[img_name]["category_count"]["nodule"]
# item.setText(f" 结节:{nodule_count}")
# item = self.listWidget.item(9)
# atrophic_scar_count = self.text[img_name]["category_count"]["atrophic_scar"]
# item.setText(f" 萎缩性瘢痕:{atrophic_scar_count}")
# item = self.listWidget.item(10)
# hypertrophic_scar_count = self.text[img_name]["category_count"]["hypertrophic_scar"]
# item.setText(f" 增生性瘢痕刺:{hypertrophic_scar_count}")
# item = self.listWidget.item(11)
# melasma_count = self.text[img_name]["category_count"]["melasma"]
# item.setText(f" 斑:{melasma_count}")
# item = self.listWidget.item(12)
# nevus_count = self.text[img_name]["category_count"]["nevus"]
# item.setText(f" 色素痣:{nevus_count}")
# item = self.listWidget.item(13)
# other_count = self.text[img_name]["category_count"]["other"]
# item.setText(f" 其他:{other_count}")
# '''生成报告'''
# #必须先生成docx文件再生成pdf文件,我的docx文件和产生的结果图片保存在同一个位置的
# docx_generate = GenerateDocx(generated_image_path,'./result',self.grading_result,category_count)
# self.generated_docxFilePath = docx_generate.predict()
# self.generated_docxFilePath_List.append(self.generated_docxFilePath)
# img_path = self.generated_image_paths[0]
# # 通过文件路径获取图片文件,并设置图片长宽为label控件的长、宽
# img = QtGui.QPixmap(img_path).scaled(self.label_showImg2.width(), self.label_showImg2.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation)
# # 在label控件上显示选择的图片
# self.label_showImg2.setPixmap(img)
# #显示用药指南
# self.label_adviceResult.setText(CLASSIFICATION_DRUG[self.grading_result])
# self.label_condition.setText('所有图片检测结束,请保存报告')
class MyProcess(multiprocessing.Process):
def __init__(self, shared_dict):
super().__init__()
self.shared_dict = shared_dict
def run(self):
# 在子进程中修改共享字典的值
image = self.shared_dict['image']
self.detector, self.classifier = self.create_acne_inference(acne_infer_config)
bboxes, labels, masks = self.acne_instance_seg(image, self.detector)
cls_result = self.acne_severity_grading(image, self.classifier)
self.shared_dict['bboxes'] = bboxes
self.shared_dict['labels'] = labels
self.shared_dict['masks'] = masks
self.shared_dict['cls_result'] = cls_result
def create_acne_inference(self,cfg):
self.detector = Detector(model_path=cfg['detector']['model_path'],
device_name=cfg['detector']['device_name'],
device_id=cfg['detector']['device_id'])
self.classifier = Classifier(model_path=cfg['classifier']['model_path'],
device_name=cfg['classifier']['device_name'],
device_id=cfg['classifier']['device_id'])
return self.detector, self.classifier
def acne_instance_seg(self,img: np.ndarray, detector: Detector):
win_gen = utils.WindowGenerator(img.shape[0], img.shape[1],
INFER_WINDOW_SIZE[0], INFER_WINDOW_SIZE[1],
INFER_WINDOW_STRIDES[0], INFER_WINDOW_STRIDES[1])
p_bboxes, p_labels, p_masks = [], [], []
for h_slice, w_slice in win_gen:
img_patch = img[h_slice, w_slice, :]
offset_x, offset_y = w_slice.start, h_slice.start
b, l, m = detector(img_patch)
b[:, [0, 2]] += offset_x
b[:, [1, 3]] += offset_y
p_bboxes.append(b)
p_labels.append(l)
p_masks.extend(m)
p_bboxes = np.concatenate(p_bboxes, axis=0)
p_labels = np.concatenate(p_labels, axis=0)
p_masks = np.array(p_masks, dtype=object)
bboxes, labels, masks = [], [], []
cls = np.unique(p_labels)
for c in cls:
idx = p_labels == c
b = p_bboxes[idx]
l = p_labels[idx]
m = p_masks[idx]
keep = utils.nms(b, OVERLAP_NMS_THRESHOLD)
bboxes.append(b[keep])
labels.append(l[keep])
masks.append(m[keep])
bboxes = np.concatenate(bboxes, axis=0)
labels = np.concatenate(labels, axis=0)
masks = np.concatenate(masks, axis=0)
return bboxes, labels, masks
def acne_severity_grading(self,img: np.ndarray, classifier: Classifier):
result = classifier(img)
return result
class WorkerThread_pdf(QThread):
# 定义一个信号,用于在工作完成后发送通知
finished = pyqtSignal()
def __init__(self, main_window):
super().__init__()
self.main_window = main_window
def run(self):
# 执行耗时的任务
self.do_work()
# 发送工作完成的信号
self.finished.emit()
def do_work(self):
# 模拟耗时的任务
self.main_window.dir_choose = QFileDialog.getExistingDirectory(self.main_window.centralwidget, "选取文件夹", "./report_pdf")
if self.main_window.dir_choose == '':
print("未生成报告,保存无效")
self.main_window.label_condition.setText('未生成报告,保存无效')
return
else:
# f=open(fd,'w')
for i in range(len(self.main_window.selected_imgsPath)):
QApplication.processEvents()
img_name = os.path.splitext(os.path.basename(self.main_window.generated_docxFilePath_List[i]))[0]
self.main_window.label_condition.setText(f"正在生成{img_name}.pdf报告")
convert(self.main_window.generated_docxFilePath_List[i], self.main_window.dir_choose+f"/{img_name}.pdf")
print(f"{img_name}.pdf 报告已生成")
print("pdf报告全部转换完毕")
self.main_window.lineEdit_savePath.setText(self.main_window.dir_choose)
self.main_window.label_condition.setText('已保存所有报告')
# self.main_window.delete_files_in_current_folder()
# self.main_window.pushButton_save.setEnabled(False)
enterTest2.py
import test2
from PyQt5.QtWidgets import QMainWindow
from infer_config import acne_infer_config
from mmdeploy_runtime import Detector, Classifier
import multiprocessing
INFER_WINDOW_SIZE = [1024, 1024]
INFER_WINDOW_STRIDES = (960, 960)
OVERLAP_NMS_THRESHOLD = 0.5
BBOX_DISPLAY_CONFIDENCE = 0.5
class SecondWindowActions(test2.Ui_MainWindow, QMainWindow):
def __init__(self):
super(test2.Ui_MainWindow, self).__init__()
self.main_window = 0
self.current_image_index = 0
self.generated_docxFilePath_List = []
self.text = {}
self.count = 0
self.dir_choose = ''
self.selected_imgsPath = []
self.generated_docxFilePath = '' #通过python-docx生成的docx文件
self.selected_imgPath = '' # 选择的图片的路径
self.generated_image_paths = []
self.grading_result = 0 # 最终判定级别数
# self.detector, self.classifier = self.create_acne_inference(acne_infer_config)
self.worker_thread = None
self.worker_thread_pdf = None
# 创建一个可以在主进程和子进程之间共享的字典
manager = multiprocessing.Manager()
self.shared_dict = manager.dict()
#调用 setupUi 方法来初始化和设置这个窗口类的用户界面。这里的 self 指的是主窗口类的实例
self.setupUi(self)
# def create_acne_inference(self,cfg):
# self.detector = Detector(model_path=cfg['detector']['model_path'],
# device_name=cfg['detector']['device_name'],
# device_id=cfg['detector']['device_id'])
# self.classifier = Classifier(model_path=cfg['classifier']['model_path'],
# device_name=cfg['classifier']['device_name'],
# device_id=cfg['classifier']['device_id'])
# return self.detector, self.classifier
在此给你一些关键词,你确保是否是类似这样使用的:
值得一题的是,数据交换用信号。
信号在pyqt叫做?pyqtSignal,在pyside叫做?Signal
信号定义在Worker里?
多线程处理sleep
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
import time
# 该 Worker 实现了乘法运算
class Worker(QObject):
# 一个参数float,复杂参数用object,2个参数就是(int, int)
on_result = pyqtSignal(float)
def __init__(self, a, b):
super().__init__()
self.a = a
self.b = b
def run(self):
print("开始处理(预计2秒)")
# 模拟原本会导致UI阻塞的阻塞语句
time.sleep(2)
result = self.a * self.b
self.on_result.emit(result)
class Window(QWidget):
def __init__(self):
super().__init__()
l = QVBoxLayout(self)
button = QPushButton("run")
button.clicked.connect(self.on_button_clicked)
l.addWidget(button)
def on_button_clicked(self):
# 写上self.或者设置Worker(QObject)的parent防止被GC掉
self.worker = Worker(7, 8)
thread = QThread(self)
self.worker.moveToThread(thread)
self.worker.on_result.connect(self.on_worker_done)
# 记得退出线程
self.worker.on_result.connect(lambda: thread.quit())
thread.started.connect(self.worker.run)
thread.start()
def on_worker_done(self, value: float):
print("我得到啦!", value)
if __name__ == "__main__":
qApp = QApplication([])
w = Window()
w.show()
qApp.exec()
多线程处理灰度化图片的任务
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
import cv2
import numpy as np
# Worker class to process the image
class Worker(QObject):
on_result = pyqtSignal(np.ndarray)
def __init__(self, image_path):
super().__init__()
self.image_path = image_path
def run(self):
print(f"开始处理图片: {self.image_path}")
# Load the image
image = cv2.imread(self.image_path)
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
print(f"图片处理完成: {self.image_path}")
self.on_result.emit(gray_image)
class Window(QWidget):
def __init__(self):
super().__init__()
self.layout = QVBoxLayout(self)
self.image_label = QLabel("点击按钮选择图片")
self.layout.addWidget(self.image_label)
button = QPushButton("选择图片")
button.clicked.connect(self.on_button_clicked)
self.layout.addWidget(button)
self.image_paths = []
self.current_image_index = 0
def on_button_clicked(self):
# Open a file dialog to select multiple image files
file_dialog = QFileDialog()
image_paths, _ = file_dialog.getOpenFileNames(self, "选择图片", "", "Images (*.png *.xpm *.jpg *.bmp)")
if image_paths:
self.image_paths = image_paths
self.current_image_index = 0
self.process_current_image()
def process_current_image(self):
if self.current_image_index < len(self.image_paths):
image_path = self.image_paths[self.current_image_index]
self.worker = Worker(image_path)
thread = QThread(self)
self.worker.moveToThread(thread)
self.worker.on_result.connect(self.on_worker_done)
self.worker.on_result.connect(lambda: thread.quit())
thread.started.connect(self.worker.run)
thread.start()
print("启动线程")
def on_worker_done(self, result_image: np.ndarray):
print("图片处理结果已获取")
# Convert the image array to QImage for display
height, width = result_image.shape
qimage = QImage(result_image.data, width, height, width, QImage.Format.Format_Grayscale8)
# Scale the image to fit the label
scaled_image = qimage.scaled(self.image_label.size(), Qt.AspectRatioMode.KeepAspectRatio)
# Set the processed image as the label's pixmap
self.image_label.setPixmap(QPixmap.fromImage(scaled_image))
# Resize the window to fit the screen
screen_rect = QGuiApplication.primaryScreen().availableGeometry()
self.resize(screen_rect.size())
self.move(screen_rect.topLeft())
# Increment the current image index
self.current_image_index += 1
# Process the next image
self.process_current_image()
if __name__ == "__main__":
qApp = QApplication([])
w = Window()
w.showMaximized() # 最大化窗口
qApp.exec()
多线程处理深度学习任务test2.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test2.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.
'''
这个文件用于显示PyQt,无需输入正确的账号和密码就可以登录进去
选择图片的时候只能选择28*28像素的图片,也就是TestDigitImgs的第一张图片
'''
'''PyQt'''
import cv2
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QDesktopServices
from docx2pdf import convert
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
from infer_config import acne_infer_config
import math
from datetime import datetime
from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QApplication, QFileDialog
from PyQt5.Qt import Qt, QIcon
from PyQt5 import QtCore, QtGui, QtWidgets
'''调用类'''
from Docx import GenerateDocx
import os
import cv2
import numpy as np
from mmdeploy_runtime import Detector, Classifier
import utils
INFER_WINDOW_SIZE = [1024, 1024]
INFER_WINDOW_STRIDES = (960, 960)
OVERLAP_NMS_THRESHOLD = 0.5
BBOX_DISPLAY_CONFIDENCE = 0.5
CATEGORY_COLOR = {
'papule':(140,240,255),
'nevus':(162,247,223),
'nodule':(152,192,250),
'open_comedo':(86,143,128),
'closed_comedo':(60,57,166),
'atrophic_scar':(103,49,132),
'hypertrophic_scar':(249,233,160),
'melasma':(164,93,222),
'pustule':(235,177,161),
'other':(233,222,252)
}
CATEGORY_INDEX = {0:'papule',
1:'nevus',
2:'nodule',
3:'open_comedo',
4:'closed_comedo',
5:'atrophic_scar',
6:'hypertrophic_scar',
7:'melasma',
8:'pustule',
9:'other'}
CLASSIFICATION_DRUG = {
1:"无需治疗",
2:"BPO或弱效维A酸类外用",
3:"BPO联合维A酸类外用",
4:"BPO联合维A酸,考虑口服抗生素",
5:"BPO联合维A酸,口服抗生素",
6:"BPO联合维A酸,口服抗生素,并开始考虑异维A酸",
7:"BPO联合维A酸,口服抗生素,推荐异维A酸",
8:"异维A酸"
}
QApplication.processEvents()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1410, 902)
MainWindow.setWindowIcon(QIcon('./icon/main.ico'))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.groupBox_main = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_main.setTitle("")
self.groupBox_main.setObjectName("groupBox_main")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.groupBox_main)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout_largest = QtWidgets.QHBoxLayout()
self.horizontalLayout_largest.setObjectName("horizontalLayout_largest")
self.groupBox_left = QtWidgets.QGroupBox(self.groupBox_main)
self.groupBox_left.setMinimumSize(QtCore.QSize(320, 0))
self.groupBox_left.setMaximumSize(QtCore.QSize(320, 16777215))
self.groupBox_left.setTitle("")
self.groupBox_left.setObjectName("groupBox_left")
self.verticalLayout_8 = QtWidgets.QVBoxLayout(self.groupBox_left)
self.verticalLayout_8.setObjectName("verticalLayout_8")
self.groupBox_menu = QtWidgets.QGroupBox(self.groupBox_left)
self.groupBox_menu.setMaximumSize(QtCore.QSize(16777215, 40))
self.groupBox_menu.setTitle("")
self.groupBox_menu.setObjectName("groupBox_menu")
self.horizontalLayout_6 = QtWidgets.QHBoxLayout(self.groupBox_menu)
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
self.label_menu = QtWidgets.QLabel(self.groupBox_menu)
self.label_menu.setStyleSheet("font-size: 22px;")
self.label_menu.setObjectName("label_menu")
self.horizontalLayout_6.addWidget(self.label_menu)
spacerItem = QtWidgets.QSpacerItem(205, 17, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_6.addItem(spacerItem)
self.verticalLayout_8.addWidget(self.groupBox_menu)
self.groupBox_startAction = QtWidgets.QGroupBox(self.groupBox_left)
self.groupBox_startAction.setMaximumSize(QtCore.QSize(16777215, 130))
self.groupBox_startAction.setTitle("")
self.groupBox_startAction.setObjectName("groupBox_startAction")
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.groupBox_startAction)
self.verticalLayout_4.setObjectName("verticalLayout_4")
self.horizontalLayout_imgAndDir = QtWidgets.QHBoxLayout()
self.horizontalLayout_imgAndDir.setObjectName("horizontalLayout_imgAndDir")
self.pushButton_selectImg = QtWidgets.QPushButton(self.groupBox_startAction)
self.pushButton_selectImg.setMinimumSize(QtCore.QSize(0, 40))
self.pushButton_selectImg.setStyleSheet("font-size: 18px;")
self.pushButton_selectImg.setObjectName("pushButton_selectImg")
self.horizontalLayout_imgAndDir.addWidget(self.pushButton_selectImg)
self.pushButton_selectDir = QtWidgets.QPushButton(self.groupBox_startAction)
self.pushButton_selectDir.setMinimumSize(QtCore.QSize(0, 40))
self.pushButton_selectDir.setStyleSheet("font-size: 18px;")
self.pushButton_selectDir.setObjectName("pushButton_selectDir")
self.horizontalLayout_imgAndDir.addWidget(self.pushButton_selectDir)
self.verticalLayout_4.addLayout(self.horizontalLayout_imgAndDir)
self.pushButton_startAction = QtWidgets.QPushButton(self.groupBox_startAction)
self.pushButton_startAction.setMinimumSize(QtCore.QSize(50, 40))
self.pushButton_startAction.setStyleSheet("font-size: 18px;")
self.pushButton_startAction.setObjectName("pushButton_startAction")
self.verticalLayout_4.addWidget(self.pushButton_startAction)
self.verticalLayout_8.addWidget(self.groupBox_startAction)
self.groupBox = QtWidgets.QGroupBox(self.groupBox_left)
self.groupBox.setTitle("")
self.groupBox.setObjectName("groupBox")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.groupBox)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton_save = QtWidgets.QPushButton(self.groupBox)
self.pushButton_save.setMinimumSize(QtCore.QSize(0, 40))
self.pushButton_save.setStyleSheet("font-size: 18px;")
self.pushButton_save.setObjectName("pushButton_save")
self.horizontalLayout.addWidget(self.pushButton_save)
self.lineEdit_savePath = QtWidgets.QLineEdit(self.groupBox)
self.lineEdit_savePath.setMinimumSize(QtCore.QSize(0, 40))
self.lineEdit_savePath.setObjectName("lineEdit_savePath")
self.horizontalLayout.addWidget(self.lineEdit_savePath)
self.horizontalLayout_3.addLayout(self.horizontalLayout)
self.verticalLayout_8.addWidget(self.groupBox)
self.groupBox_showDir = QtWidgets.QGroupBox(self.groupBox_left)
self.groupBox_showDir.setMaximumSize(QtCore.QSize(16777215, 55))
self.groupBox_showDir.setTitle("")
self.groupBox_showDir.setObjectName("groupBox_showDir")
self.horizontalLayout_10 = QtWidgets.QHBoxLayout(self.groupBox_showDir)
self.horizontalLayout_10.setObjectName("horizontalLayout_10")
self.pushButton_showPDF = QtWidgets.QPushButton(self.groupBox_showDir)
self.pushButton_showPDF.setMinimumSize(QtCore.QSize(0, 40))
self.pushButton_showPDF.setStyleSheet("font-size: 18px;")
self.pushButton_showPDF.setObjectName("pushButton_showPDF")
self.horizontalLayout_10.addWidget(self.pushButton_showPDF)
self.verticalLayout_8.addWidget(self.groupBox_showDir)
self.verticalLayout_result = QtWidgets.QVBoxLayout()
self.verticalLayout_result.setObjectName("verticalLayout_result")
self.groupBox_resultTitle = QtWidgets.QGroupBox(self.groupBox_left)
self.groupBox_resultTitle.setMinimumSize(QtCore.QSize(0, 50))
self.groupBox_resultTitle.setMaximumSize(QtCore.QSize(16777215, 50))
self.groupBox_resultTitle.setTitle("")
self.groupBox_resultTitle.setObjectName("groupBox_resultTitle")
self.horizontalLayout_8 = QtWidgets.QHBoxLayout(self.groupBox_resultTitle)
self.horizontalLayout_8.setObjectName("horizontalLayout_8")
self.label_resultTitle = QtWidgets.QLabel(self.groupBox_resultTitle)
self.label_resultTitle.setStyleSheet("font-size: 22px;")
self.label_resultTitle.setObjectName("label_resultTitle")
self.horizontalLayout_8.addWidget(self.label_resultTitle)
spacerItem1 = QtWidgets.QSpacerItem(181, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_8.addItem(spacerItem1)
self.verticalLayout_result.addWidget(self.groupBox_resultTitle)
self.groupBox_resultContent = QtWidgets.QGroupBox(self.groupBox_left)
self.groupBox_resultContent.setMinimumSize(QtCore.QSize(0, 50))
self.groupBox_resultContent.setTitle("")
self.groupBox_resultContent.setObjectName("groupBox_resultContent")
self.horizontalLayout_9 = QtWidgets.QHBoxLayout(self.groupBox_resultContent)
self.horizontalLayout_9.setObjectName("horizontalLayout_9")
self.listWidget = QtWidgets.QListWidget(self.groupBox_resultContent)
self.listWidget.setLayoutDirection(QtCore.Qt.LeftToRight)
self.listWidget.setStyleSheet("font-size: 14px;")
self.listWidget.setIconSize(QtCore.QSize(7, 7))
self.listWidget.setObjectName("listWidget")
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("./icon/closed_comedo.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("./icon/opend_comedo.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon1)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("./icon/papule.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon2)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap("./icon/pustule.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon3)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon4 = QtGui.QIcon()
icon4.addPixmap(QtGui.QPixmap("./icon/nodule.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon4)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon5 = QtGui.QIcon()
icon5.addPixmap(QtGui.QPixmap("./icon/atrophic_scar.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon5)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon6 = QtGui.QIcon()
icon6.addPixmap(QtGui.QPixmap("./icon/hypertrophic_scar.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon6)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon7 = QtGui.QIcon()
icon7.addPixmap(QtGui.QPixmap("./icon/melasma.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon7)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon8 = QtGui.QIcon()
icon8.addPixmap(QtGui.QPixmap("./icon/nevus.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon8)
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
icon9 = QtGui.QIcon()
icon9.addPixmap(QtGui.QPixmap("./icon/other.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon9)
self.listWidget.addItem(item)
self.horizontalLayout_9.addWidget(self.listWidget)
self.verticalLayout_result.addWidget(self.groupBox_resultContent)
self.verticalLayout_8.addLayout(self.verticalLayout_result)
self.verticalLayout_advice = QtWidgets.QVBoxLayout()
self.verticalLayout_advice.setObjectName("verticalLayout_advice")
self.groupBox_10 = QtWidgets.QGroupBox(self.groupBox_left)
self.groupBox_10.setMinimumSize(QtCore.QSize(0, 50))
self.groupBox_10.setMaximumSize(QtCore.QSize(16777215, 50))
self.groupBox_10.setTitle("")
self.groupBox_10.setObjectName("groupBox_10")
self.horizontalLayout_12 = QtWidgets.QHBoxLayout(self.groupBox_10)
self.horizontalLayout_12.setObjectName("horizontalLayout_12")
self.label_adviceTitle = QtWidgets.QLabel(self.groupBox_10)
self.label_adviceTitle.setStyleSheet("font-size: 22px;")
self.label_adviceTitle.setObjectName("label_adviceTitle")
self.horizontalLayout_12.addWidget(self.label_adviceTitle)
spacerItem2 = QtWidgets.QSpacerItem(137, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_12.addItem(spacerItem2)
self.verticalLayout_advice.addWidget(self.groupBox_10)
self.groupBox_12 = QtWidgets.QGroupBox(self.groupBox_left)
self.groupBox_12.setMinimumSize(QtCore.QSize(0, 50))
self.groupBox_12.setMaximumSize(QtCore.QSize(16777215, 120))
self.groupBox_12.setTitle("")
self.groupBox_12.setObjectName("groupBox_12")
self.horizontalLayout_13 = QtWidgets.QHBoxLayout(self.groupBox_12)
self.horizontalLayout_13.setObjectName("horizontalLayout_13")
self.label_adviceResult = QtWidgets.QLabel(self.groupBox_12)
self.label_adviceResult.setMinimumSize(QtCore.QSize(0, 50))
self.label_adviceResult.setMaximumSize(QtCore.QSize(16777215, 120))
self.label_adviceResult.setStyleSheet("font-size: 16px;")
self.label_adviceResult.setAlignment(QtCore.Qt.AlignCenter)
self.label_adviceResult.setObjectName("label_adviceResult")
self.horizontalLayout_13.addWidget(self.label_adviceResult)
self.verticalLayout_advice.addWidget(self.groupBox_12)
self.verticalLayout_8.addLayout(self.verticalLayout_advice)
self.horizontalLayout_largest.addWidget(self.groupBox_left)
self.groupBox_right = QtWidgets.QGroupBox(self.groupBox_main)
self.groupBox_right.setTitle("")
self.groupBox_right.setObjectName("groupBox_right")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.groupBox_right)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.groupBox_selectImg = QtWidgets.QGroupBox(self.groupBox_right)
self.groupBox_selectImg.setMaximumSize(QtCore.QSize(16777215, 40))
self.groupBox_selectImg.setTitle("")
self.groupBox_selectImg.setObjectName("groupBox_selectImg")
self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.groupBox_selectImg)
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.label_imgTitle = QtWidgets.QLabel(self.groupBox_selectImg)
self.label_imgTitle.setStyleSheet("font-size: 20px;")
self.label_imgTitle.setObjectName("label_imgTitle")
self.horizontalLayout_5.addWidget(self.label_imgTitle)
self.lineEdit_imgPath = QtWidgets.QLineEdit(self.groupBox_selectImg)
self.lineEdit_imgPath.setMinimumSize(QtCore.QSize(0, 25))
self.lineEdit_imgPath.setMaximumSize(QtCore.QSize(300, 16777215))
self.lineEdit_imgPath.setObjectName("lineEdit_imgPath")
self.horizontalLayout_5.addWidget(self.lineEdit_imgPath)
spacerItem3 = QtWidgets.QSpacerItem(287, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_5.addItem(spacerItem3)
self.label_5 = QtWidgets.QLabel(self.groupBox_selectImg)
self.label_5.setText("")
self.label_5.setObjectName("label_5")
self.horizontalLayout_5.addWidget(self.label_5)
self.verticalLayout_3.addWidget(self.groupBox_selectImg)
self.horizontalLayout_imgs = QtWidgets.QHBoxLayout()
self.horizontalLayout_imgs.setObjectName("horizontalLayout_imgs")
self.label_showImg = QtWidgets.QLabel(self.groupBox_right)
self.label_showImg.setMinimumSize(QtCore.QSize(500, 0))
self.label_showImg.setText("")
self.label_showImg.setAlignment(QtCore.Qt.AlignCenter)
self.label_showImg.setObjectName("label_showImg")
self.horizontalLayout_imgs.addWidget(self.label_showImg)
spacerItem4 = QtWidgets.QSpacerItem(10, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.horizontalLayout_imgs.addItem(spacerItem4)
self.label_showImg2 = QtWidgets.QLabel(self.groupBox_right)
self.label_showImg2.setMinimumSize(QtCore.QSize(500, 0))
self.label_showImg2.setText("")
self.label_showImg2.setAlignment(QtCore.Qt.AlignCenter)
self.label_showImg2.setObjectName("label_showImg2")
self.horizontalLayout_imgs.addWidget(self.label_showImg2)
self.verticalLayout_3.addLayout(self.horizontalLayout_imgs)
self.horizontalLayout_pageTurning = QtWidgets.QHBoxLayout()
self.horizontalLayout_pageTurning.setObjectName("horizontalLayout_pageTurning")
self.prev_image_button = QtWidgets.QPushButton(self.groupBox_right)
self.prev_image_button.setMinimumSize(QtCore.QSize(0, 30))
self.prev_image_button.setObjectName("prev_image_button")
self.horizontalLayout_pageTurning.addWidget(self.prev_image_button)
self.label_pageNum = QtWidgets.QLabel(self.groupBox_right)
self.label_pageNum.setMaximumSize(QtCore.QSize(60, 16777215))
self.label_pageNum.setAlignment(QtCore.Qt.AlignCenter)
self.label_pageNum.setObjectName("label_pageNum")
self.horizontalLayout_pageTurning.addWidget(self.label_pageNum)
self.next_image_button = QtWidgets.QPushButton(self.groupBox_right)
self.next_image_button.setMinimumSize(QtCore.QSize(0, 30))
self.next_image_button.setObjectName("next_image_button")
self.horizontalLayout_pageTurning.addWidget(self.next_image_button)
self.verticalLayout_3.addLayout(self.horizontalLayout_pageTurning)
self.horizontalLayout_largest.addWidget(self.groupBox_right)
self.verticalLayout_2.addLayout(self.horizontalLayout_largest)
self.groupBox_botton = QtWidgets.QGroupBox(self.groupBox_main)
self.groupBox_botton.setTitle("")
self.groupBox_botton.setObjectName("groupBox_botton")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.groupBox_botton)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_condition = QtWidgets.QLabel(self.groupBox_botton)
self.label_condition.setObjectName("label_condition")
self.horizontalLayout_2.addWidget(self.label_condition)
self.verticalLayout_2.addWidget(self.groupBox_botton)
self.verticalLayout.addWidget(self.groupBox_main)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "痤疮检测"))
self.label_menu.setText(_translate("MainWindow", "菜单栏"))
self.pushButton_selectImg.setText(_translate("MainWindow", "选择图片"))
self.pushButton_selectDir.setText(_translate("MainWindow", "选择文件夹"))
self.pushButton_startAction.setText(_translate("MainWindow", "开始检测"))
self.pushButton_save.setText(_translate("MainWindow", "保存报告"))
self.pushButton_showPDF.setText(_translate("MainWindow", "显示输出文件夹"))
self.label_resultTitle.setText(_translate("MainWindow", "检测结果"))
__sortingEnabled = self.listWidget.isSortingEnabled()
self.listWidget.setSortingEnabled(False)
item = self.listWidget.item(0)
item.setText(_translate("MainWindow", "痤疮分级结果:待检测"))
item = self.listWidget.item(2)
item.setText(_translate("MainWindow", "各类别痤疮检测数量:"))
item = self.listWidget.item(4)
item.setText(_translate("MainWindow", " 闭口粉刺:待检测"))
item = self.listWidget.item(5)
item.setText(_translate("MainWindow", " 开口粉刺:待检测"))
item = self.listWidget.item(6)
item.setText(_translate("MainWindow", " 丘疹:待检测"))
item = self.listWidget.item(7)
item.setText(_translate("MainWindow", " 脓疱:待检测"))
item = self.listWidget.item(8)
item.setText(_translate("MainWindow", " 结节:待检测"))
item = self.listWidget.item(9)
item.setText(_translate("MainWindow", " 萎缩性瘢痕:待检测"))
item = self.listWidget.item(10)
item.setText(_translate("MainWindow", " 增生性瘢痕刺:待检测"))
item = self.listWidget.item(11)
item.setText(_translate("MainWindow", " 斑:待检测"))
item = self.listWidget.item(12)
item.setText(_translate("MainWindow", " 色素痣:待检测"))
item = self.listWidget.item(13)
item.setText(_translate("MainWindow", " 其他:待检测"))
self.listWidget.setSortingEnabled(__sortingEnabled)
self.label_adviceTitle.setText(_translate("MainWindow", "推荐治疗方案"))
self.label_adviceResult.setText(_translate("MainWindow", "用药指南"))
self.label_imgTitle.setText(_translate("MainWindow", "所选图片"))
self.prev_image_button.setText(_translate("MainWindow", "上一页"))
self.label_pageNum.setText(_translate("MainWindow", "共 0 页"))
self.next_image_button.setText(_translate("MainWindow", "下一页"))
self.label_condition.setText(_translate("MainWindow", "系统运行情况"))
# self.worker_thread = WorkerThread(self)
# self.worker_thread_pdf = WorkerThread_pdf(self)
# 按钮关联函数
self.pushButton_selectDir.clicked.connect(self.openImageFolder)
# self.pushButton_startAction.clicked.connect(self.startAction)
self.pushButton_selectImg.clicked.connect(self.openImage)
self.pushButton_startAction.clicked.connect(self.start_work)
# self.worker_thread.finished.connect(self.on_worker_finished)
self.pushButton_save.clicked.connect(self.start_work_pdf)
# self.worker_thread_pdf.finished.connect(self.on_worker_finished_pdf)
self.pushButton_showPDF.clicked.connect(self.button_show_dir)
self.prev_image_button.clicked.connect(self.showPrevImage)
self.next_image_button.clicked.connect(self.showNextImage)
self.timer = QTimer()
QApplication.processEvents()
def start_work(self):
QApplication.processEvents()
self.pushButton_startAction.setEnabled(False) # 禁用按钮
self.pushButton_selectImg.setEnabled(False) # 禁用按钮
self.pushButton_save.setEnabled(False) # 禁用按钮
self.pushButton_showPDF.setEnabled(False) # 禁用按钮
self.pushButton_selectDir.setEnabled(False) # 禁用按钮
self.prev_image_button.setEnabled(False)
self.next_image_button.setEnabled(False)
self.label_condition.setText('正在检测,请耐心等待')
self.worker = Worker(self.selected_imgPaths)
thread = QThread(self)
self.worker.moveToThread(thread)
self.worker.on_result.connect(self.on_worker_done)
# 记得退出线程
self.worker.on_result.connect(lambda: thread.quit())
thread.started.connect(self.worker.run)
thread.start()
print("启动线程")
def on_worker_done(self,generated_image_paths:object,generated_docxFilePath_List:object,text:object,):
self.generated_image_paths = generated_image_paths
self.text = text
self.generated_docxFilePath_List = generated_docxFilePath_List
# print(self.generated_image_paths)
# print(self.text)
print("我得到啦")
self.label_condition.setText('检测完毕,请点击保存图片')
self.pushButton_startAction.setEnabled(True) # 启动之前被禁用的按钮
self.pushButton_selectImg.setEnabled(True)
self.pushButton_save.setEnabled(True)
self.pushButton_showPDF.setEnabled(True)
self.pushButton_selectDir.setEnabled(True)
self.prev_image_button.setEnabled(True)
self.next_image_button.setEnabled(True)
img_path = self.selected_imgPaths[0]
img_name = os.path.splitext(os.path.basename(img_path))[0]
grading_result = self.text[img_name]["grading_result"]
item = self.listWidget.item(0)
item.setText(f"痤疮分级结果:{grading_result}")
item = self.listWidget.item(2)
item.setText("各类别痤疮检测数量:")
item = self.listWidget.item(4)
closed_comedo_count = self.text[img_name]["category_count"]["closed_comedo"]
item.setText(f" 闭口粉刺:{closed_comedo_count}")
item = self.listWidget.item(5)
open_comedo_count = self.text[img_name]["category_count"]["open_comedo"]
item.setText(f" 开口粉刺:{open_comedo_count}")
item = self.listWidget.item(6)
papule_count = self.text[img_name]["category_count"]["papule"]
item.setText(f" 丘疹:{papule_count}")
item = self.listWidget.item(7)
pustule_count = self.text[img_name]["category_count"]["pustule"]
item.setText(f" 脓疱:{pustule_count}")
item = self.listWidget.item(8)
nodule_count = self.text[img_name]["category_count"]["nodule"]
item.setText(f" 结节:{nodule_count}")
item = self.listWidget.item(9)
atrophic_scar_count = self.text[img_name]["category_count"]["atrophic_scar"]
item.setText(f" 萎缩性瘢痕:{atrophic_scar_count}")
item = self.listWidget.item(10)
hypertrophic_scar_count = self.text[img_name]["category_count"]["hypertrophic_scar"]
item.setText(f" 增生性瘢痕刺:{hypertrophic_scar_count}")
item = self.listWidget.item(11)
melasma_count = self.text[img_name]["category_count"]["melasma"]
item.setText(f" 斑:{melasma_count}")
item = self.listWidget.item(12)
nevus_count = self.text[img_name]["category_count"]["nevus"]
item.setText(f" 色素痣:{nevus_count}")
item = self.listWidget.item(13)
other_count = self.text[img_name]["category_count"]["other"]
item.setText(f" 其他:{other_count}")
img_path = self.generated_image_paths[0]
# 通过文件路径获取图片文件,并设置图片长宽为label控件的长、宽
img = QtGui.QPixmap(img_path).scaled(self.label_showImg2.width(), self.label_showImg2.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation)
# 在label控件上显示选择的图片
self.label_showImg2.setPixmap(img)
#显示用药指南
self.label_adviceResult.setText(CLASSIFICATION_DRUG[grading_result])
# print("ok")
def start_work_pdf(self):
# 启动工作线程
self.worker_thread_pdf = WorkerThread_pdf(self)
if len(self.generated_image_paths) == 0:
return
self.worker_thread_pdf.start()
self.worker_thread_pdf.finished.connect(self.on_worker_finished_pdf)
def on_worker_finished_pdf(self):
return
# self.delete_files_in_current_folder()
def showNextImage(self):
if len(self.selected_imgPaths) == 0:
return
self.current_image_index += 1
if self.current_image_index >= len(self.selected_imgPaths):
self.current_image_index = 0
img_path = self.selected_imgPaths[self.current_image_index]
img = QtGui.QPixmap(img_path).scaled(self.label_showImg.width(), self.label_showImg.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation)
self.label_showImg.setPixmap(img)
self.lineEdit_imgPath.setText(img_path)
pageNum = len(self.selected_imgPaths)
current_image_index_fact = self.current_image_index + 1
self.label_pageNum.setText(f"{current_image_index_fact} / {pageNum} ")
img_path = self.selected_imgPaths[self.current_image_index]
img_name = os.path.splitext(os.path.basename(img_path))[0]
grading_result = self.text[img_name]["grading_result"]
self.label_adviceResult.setText(CLASSIFICATION_DRUG[grading_result])
if len(self.text) != 0:
img_name = os.path.splitext(os.path.basename(img_path))[0]
# self.label_showResult.setText(self.text[img_name])
# print(self.text[img_name])
grading_result = self.text[img_name]["grading_result"]
item = self.listWidget.item(0)
item.setText(f"痤疮分级结果:{grading_result}")
item = self.listWidget.item(2)
item.setText("各类别痤疮检测数量:")
item = self.listWidget.item(4)
closed_comedo_count = self.text[img_name]["category_count"]["closed_comedo"]
item.setText(f" 闭口粉刺:{closed_comedo_count}")
item = self.listWidget.item(5)
open_comedo_count = self.text[img_name]["category_count"]["open_comedo"]
item.setText(f" 开口粉刺:{open_comedo_count}")
item = self.listWidget.item(6)
papule_count = self.text[img_name]["category_count"]["papule"]
item.setText(f" 丘疹:{papule_count}")
item = self.listWidget.item(7)
pustule_count = self.text[img_name]["category_count"]["pustule"]
item.setText(f" 脓疱:{pustule_count}")
item = self.listWidget.item(8)
nodule_count = self.text[img_name]["category_count"]["nodule"]
item.setText(f" 结节:{nodule_count}")
item = self.listWidget.item(9)
atrophic_scar_count = self.text[img_name]["category_count"]["atrophic_scar"]
item.setText(f" 萎缩性瘢痕:{atrophic_scar_count}")
item = self.listWidget.item(10)
hypertrophic_scar_count = self.text[img_name]["category_count"]["hypertrophic_scar"]
item.setText(f" 增生性瘢痕刺:{hypertrophic_scar_count}")
item = self.listWidget.item(11)
melasma_count = self.text[img_name]["category_count"]["melasma"]
item.setText(f" 斑:{melasma_count}")
item = self.listWidget.item(12)
nevus_count = self.text[img_name]["category_count"]["nevus"]
item.setText(f" 色素痣:{nevus_count}")
item = self.listWidget.item(13)
other_count = self.text[img_name]["category_count"]["other"]
item.setText(f" 其他:{other_count}")
img_path2 = self.generated_image_paths[self.current_image_index]
img2 = QtGui.QPixmap(img_path2).scaled(self.label_showImg2.width(), self.label_showImg2.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation)
self.label_showImg2.setPixmap(img2)
def showPrevImage(self):
if len(self.selected_imgPaths) == 0:
return
self.current_image_index -= 1
if self.current_image_index < 0:
self.current_image_index = len(self.selected_imgPaths) - 1
img_path = self.selected_imgPaths[self.current_image_index]
img = QtGui.QPixmap(img_path).scaled(self.label_showImg.width(), self.label_showImg.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation)
self.label_showImg.setPixmap(img)
self.lineEdit_imgPath.setText(img_path)
pageNum = len(self.selected_imgPaths)
current_image_index_fact = self.current_image_index + 1
self.label_pageNum.setText(f"{current_image_index_fact} / {pageNum} ")
img_path = self.selected_imgPaths[self.current_image_index]
img_name = os.path.splitext(os.path.basename(img_path))[0]
grading_result = self.text[img_name]["grading_result"]
self.label_adviceResult.setText(CLASSIFICATION_DRUG[grading_result])
if len(self.text) != 0:
img_name = os.path.splitext(os.path.basename(img_path))[0]
# self.label_showResult.setText(self.text[img_name])
# print(self.text[img_name])
grading_result = self.text[img_name]["grading_result"]
item = self.listWidget.item(0)
item.setText(f"痤疮分级结果:{grading_result}")
item = self.listWidget.item(2)
item.setText("各类别痤疮检测数量:")
item = self.listWidget.item(4)
closed_comedo_count = self.text[img_name]["category_count"]["closed_comedo"]
item.setText(f" 闭口粉刺:{closed_comedo_count}")
item = self.listWidget.item(5)
open_comedo_count = self.text[img_name]["category_count"]["open_comedo"]
item.setText(f" 开口粉刺:{open_comedo_count}")
item = self.listWidget.item(6)
papule_count = self.text[img_name]["category_count"]["papule"]
item.setText(f" 丘疹:{papule_count}")
item = self.listWidget.item(7)
pustule_count = self.text[img_name]["category_count"]["pustule"]
item.setText(f" 脓疱:{pustule_count}")
item = self.listWidget.item(8)
nodule_count = self.text[img_name]["category_count"]["nodule"]
item.setText(f" 结节:{nodule_count}")
item = self.listWidget.item(9)
atrophic_scar_count = self.text[img_name]["category_count"]["atrophic_scar"]
item.setText(f" 萎缩性瘢痕:{atrophic_scar_count}")
item = self.listWidget.item(10)
hypertrophic_scar_count = self.text[img_name]["category_count"]["hypertrophic_scar"]
item.setText(f" 增生性瘢痕刺:{hypertrophic_scar_count}")
item = self.listWidget.item(11)
melasma_count = self.text[img_name]["category_count"]["melasma"]
item.setText(f" 斑:{melasma_count}")
item = self.listWidget.item(12)
nevus_count = self.text[img_name]["category_count"]["nevus"]
item.setText(f" 色素痣:{nevus_count}")
item = self.listWidget.item(13)
other_count = self.text[img_name]["category_count"]["other"]
item.setText(f" 其他:{other_count}")
img_path2 = self.generated_image_paths[self.current_image_index]
img2 = QtGui.QPixmap(img_path2).scaled(self.label_showImg2.width(), self.label_showImg2.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation)
self.label_showImg2.setPixmap(img2)
def button_show_dir(self):
default_dir = "./report_pdf"
show_dirPath = 0
if self.dir_choose == '':
show_dirPath = default_dir
else:
show_dirPath = self.dir_choose
url = QUrl.fromLocalFile(show_dirPath)
if url:
print("选择的文件夹路径:", show_dirPath)
else:
print("未选择文件夹")
QDesktopServices.openUrl(url)
# 选择本地图片上传
def openImage(self):
self.selected_imgPaths = [] # 清空之前选择的所有图片
self.generated_image_paths = [] # 清空之前推理生成的所有图片
# 弹出一个文件选择框,第一个返回值imgName记录选中的文件路径+文件名,第二个返回值imgType记录文件的类型
# QFileDialog就是系统对话框的那个类第一个参数是上下文,第二个参数是弹框的名字,第三个参数是默认打开的路径,第四个参数是需要的格式
self.selected_imgPaths, _ = QtWidgets.QFileDialog.getOpenFileNames(
self, "打开图片", "./pending_images", "*.jpg;;*.JPG;;*.png;;*.png;;All Files(*)")
if len(self.selected_imgPaths) == 0:
print('没有选择图片!')
self.label_condition.setText("没有选择图片!") # 点击选择图片按钮后不选择图片会将存放图片路径的列表置为空
# 创建一个空的QPixmap对象
empty_pixmap = QPixmap()
self.label_showImg.setPixmap(empty_pixmap)
pageNum = 0
self.label_pageNum.setText(f"共 {pageNum} 页")
return
# 通过文件路径获取图片文件,并设置图片长宽为label控件的长、宽
img = QtGui.QPixmap(self.selected_imgPaths[0]).scaled(self.label_showImg.width(), self.label_showImg.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation)
# 提示点击生成
self.label_condition.setText("请点击 开始检测")
# 在label控件上显示选择的图片
self.label_showImg.setPixmap(img)
# 显示所选图片的路径
self.lineEdit_imgPath.setText(self.selected_imgPaths[0])
pageNum = len(self.selected_imgPaths)
self.label_pageNum.setText(f"共 {pageNum} 页")
# 选择图片文件夹上传
def openImageFolder(self):
self.selected_imgPaths = [] # 清空之前选择的所有图片
self.generated_image_paths = [] # 清空之前推理生成的所有图片
# 弹出一个文件夹选择框,返回选中的文件夹路径
selected_folder = QtWidgets.QFileDialog.getExistingDirectory(self, "选择文件夹", "./pending_images")
if selected_folder:
# 获取文件夹中的所有图片文件路径
for file_name in os.listdir(selected_folder):
if file_name.lower().endswith((".jpg", ".png")):
self.selected_imgPaths.append(os.path.join(selected_folder, file_name))
# 通过第一个图片文件路径获取图片,并设置图片长宽为label控件的长、宽
img = QtGui.QPixmap(self.selected_imgPaths[0]).scaled(self.label_showImg.width(), self.label_showImg.height(), Qt.KeepAspectRatio,Qt.SmoothTransformation)
# 提示点击开始检测
self.label_condition.setText("请点击 开始检测")
# 在label控件上显示第一张图片
self.label_showImg.setPixmap(img)
# 显示所选图片的路径
self.lineEdit_imgPath.setText(self.selected_imgPaths[0])
pageNum = len(self.selected_imgPaths)
self.label_pageNum.setText(f"共 {pageNum} 页")
else:
print('没有选择图片!')
self.label_condition.setText("没有选择图片!")
empty_pixmap = QPixmap()
self.label_showImg.setPixmap(empty_pixmap)
pageNum = 0
self.label_pageNum.setText(f"共 {pageNum} 页")
return
def closeEvent(self, event):
# 在这里执行你的函数
self.delete_files_in_current_folder()
event.accept()
# 删除中途产生的图片和docx文件
def delete_files_in_current_folder(self):
# 获取当前文件夹路径
current_folder = os.path.dirname(self.generated_docxFilePath)
# 获取当前文件夹中的所有文件
if len(current_folder) == 0:
print('没有报告生成')
return
file_list = os.listdir(current_folder)
# 遍历文件列表
for file_name in file_list:
# 构建文件的绝对路径
file_path = os.path.join(current_folder, file_name)
# 判断是否是文件
if os.path.isfile(file_path):
# 删除文件
os.remove(file_path)
# 自定义的工作线程类
class Worker(QThread):
# 生成对应预测的数字
# 定义一个信号,用于在工作完成后发送通知
on_result = pyqtSignal(object,object,object)
QApplication.processEvents()
def __init__(self, selected_imgPaths):
super().__init__()
self.selected_imgPaths = selected_imgPaths
self.generated_image_paths = []
self.text = {}
self.generated_docxFilePath_List = []
def create_acne_inference(self,cfg):
self.detector = Detector(model_path=cfg['detector']['model_path'],
device_name=cfg['detector']['device_name'],
device_id=cfg['detector']['device_id'])
self.classifier = Classifier(model_path=cfg['classifier']['model_path'],
device_name=cfg['classifier']['device_name'],
device_id=cfg['classifier']['device_id'])
return self.detector, self.classifier
def acne_instance_seg(self,img: np.ndarray, detector: Detector):
win_gen = utils.WindowGenerator(img.shape[0], img.shape[1],
INFER_WINDOW_SIZE[0], INFER_WINDOW_SIZE[1],
INFER_WINDOW_STRIDES[0], INFER_WINDOW_STRIDES[1])
p_bboxes, p_labels, p_masks = [], [], []
for h_slice, w_slice in win_gen:
img_patch = img[h_slice, w_slice, :]
offset_x, offset_y = w_slice.start, h_slice.start
b, l, m = detector(img_patch)
b[:, [0, 2]] += offset_x
b[:, [1, 3]] += offset_y
p_bboxes.append(b)
p_labels.append(l)
p_masks.extend(m)
p_bboxes = np.concatenate(p_bboxes, axis=0)
p_labels = np.concatenate(p_labels, axis=0)
p_masks = np.array(p_masks, dtype=object)
bboxes, labels, masks = [], [], []
cls = np.unique(p_labels)
for c in cls:
idx = p_labels == c
b = p_bboxes[idx]
l = p_labels[idx]
m = p_masks[idx]
keep = utils.nms(b, OVERLAP_NMS_THRESHOLD)
bboxes.append(b[keep])
labels.append(l[keep])
masks.append(m[keep])
bboxes = np.concatenate(bboxes, axis=0)
labels = np.concatenate(labels, axis=0)
masks = np.concatenate(masks, axis=0)
return bboxes, labels, masks
def acne_severity_grading(self,img: np.ndarray, classifier: Classifier):
result = classifier(img)
return result
def run(self):
# 执行耗时的任务
print("开始处理图片")
# 模拟耗时的任务
# '''人脸严重程度分级'''
grading_result = 0 # 从1开始的痤疮等级
# count = 0
# 判断是否选择了图片
if len(self.selected_imgPaths)== 0:
print("未选择图片")
return
for i in range(len(self.selected_imgPaths)):
QApplication.processEvents()
# count = i+1
# self.label_showResult.setText(f'正在检测第{count}张图片')
# self.label_condition.setText(f'请耐心等待,正在检测第{count}张图片')
image = cv2.imread(self.selected_imgPaths[i])
self.detector, self.classifier = self.create_acne_inference(acne_infer_config)
bboxes, labels, masks = self.acne_instance_seg(image, self.detector)
cls_result = self.acne_severity_grading(image, self.classifier)
grading_result = cls_result[0][0]+1
indices = [i for i in range(len(bboxes))]
QApplication.processEvents()
category_count = {
'papule':0,
'nevus':0,
'nodule':0,
'open_comedo':0,
'closed_comedo':0,
'atrophic_scar':0,
'hypertrophic_scar':0,
'melasma':0,
'pustule':0,
'other':0
}
for index, bbox, label_id in zip(indices, bboxes, labels):
QApplication.processEvents()
# print(count,index,bbox,label_id)
[left, top, right, bottom], score = bbox[0:4].astype(int), bbox[4]
if score < BBOX_DISPLAY_CONFIDENCE:
continue
color = CATEGORY_COLOR[CATEGORY_INDEX[label_id]]
cv2.rectangle(image, (left, top), (right, bottom), color)
label_text = f"{CATEGORY_INDEX[label_id]} | {int(100 * score)}%" # 使用痤疮的标签作为标注信息
cv2.putText(image, label_text, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1) # 添加标注信息
if masks[index].size:
mask = masks[index]
if mask.shape == image.shape[:2]: # rtmdet-inst
mask_img = image.copy()
else: # maskrcnn
x0 = int(max(math.floor(bbox[0]) - 1, 0))
y0 = int(max(math.floor(bbox[1]) - 1, 0))
mask_img = image[y0:y0 + mask.shape[0], x0:x0 + mask.shape[1]]
mask_img[mask.astype(bool)] = color
category_count[CATEGORY_INDEX[label_id]] += 1
text = f'Prediction: {cls_result[0][0]}, {cls_result[0][1]:.4f} ' \
f'({acne_infer_config["classifier"]["classes"][cls_result[0][0]]})'
cv2.putText(image, text, (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), 2)
img_name = os.path.splitext(os.path.basename(self.selected_imgPaths[i]))[0]
# 获取当前日期和时间
now = datetime.now()
# 提取年、月和日
year = now.year
month = now.month
day = now.day
generated_image_path = f"./result/{img_name}_report_{year}_{month}_{day}.jpg"
cv2.imwrite(generated_image_path , image)
self.generated_image_paths.append(generated_image_path)
closed_comedo_count = category_count['closed_comedo']
open_comedo_count = category_count['open_comedo']
papule_count = category_count['papule']
pustule_count = category_count['pustule']
nodule_count = category_count['nodule']
atrophic_scar_count = category_count['atrophic_scar']
hypertrophic_scar_count = category_count['hypertrophic_scar']
melasma_count = category_count['melasma']
nevus_count = category_count['nevus']
other_count = category_count['other']
self.text[img_name] = {
"grading_result":grading_result,
"category_count":{
"closed_comedo":closed_comedo_count,
"open_comedo":open_comedo_count,
"papule":papule_count,
"pustule":pustule_count,
"nodule":nodule_count,
"atrophic_scar":atrophic_scar_count,
"hypertrophic_scar":hypertrophic_scar_count,
"melasma":melasma_count,
"nevus":nevus_count,
"other":other_count
}
}
'''生成报告'''
#必须先生成docx文件再生成pdf文件,我的docx文件和产生的结果图片保存在同一个位置的
grading_result = self.text[img_name]["grading_result"]
category_count = self.text[img_name]["category_count"]
docx_generate = GenerateDocx(generated_image_path,'./result',grading_result,category_count)
generated_docxFilePath = docx_generate.predict()
self.generated_docxFilePath_List.append(generated_docxFilePath)
self.on_result.emit(self.generated_image_paths,self.generated_docxFilePath_List,self.text)
class WorkerThread_pdf(QThread):
# 定义一个信号,用于在工作完成后发送通知
finished = pyqtSignal()
def __init__(self, main_window):
super().__init__()
self.main_window = main_window
def run(self):
# 执行耗时的任务
self.do_work()
# 发送工作完成的信号
self.finished.emit()
def do_work(self):
# 模拟耗时的任务
self.main_window.dir_choose = QFileDialog.getExistingDirectory(self.main_window.centralwidget, "选取文件夹", "./report_pdf")
if self.main_window.dir_choose == '':
print("未生成报告,保存无效")
self.main_window.label_condition.setText('未生成报告,保存无效')
return
else:
# f=open(fd,'w')
for i in range(len(self.main_window.selected_imgPaths)):
QApplication.processEvents()
img_name = os.path.splitext(os.path.basename(self.main_window.generated_docxFilePath_List[i]))[0]
self.main_window.label_condition.setText(f"正在生成{img_name}.pdf报告")
convert(self.main_window.generated_docxFilePath_List[i], self.main_window.dir_choose+f"/{img_name}.pdf")
print(f"{img_name}.pdf 报告已生成")
print("pdf报告全部转换完毕")
self.main_window.lineEdit_savePath.setText(self.main_window.dir_choose)
self.main_window.label_condition.setText('已保存所有报告')
# self.main_window.delete_files_in_current_folder()
# self.main_window.pushButton_save.setEnabled(False)
中途点击退出按钮后会出现这样的报错
说明启动了多线程,但就是太卡了
文章来源:https://blog.csdn.net/llf000000/article/details/135379912
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!