You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
7.7 KiB

import sys
import cv2
import numpy as np
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(1000, 800)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.pushButton = QPushButton(self.centralwidget)
self.pushButton.setObjectName(u"pushButton")
self.pushButton.setGeometry(QRect(750, 500, 201, 71))
self.graphicsView = QGraphicsView(self.centralwidget)
self.graphicsView.setObjectName(u"graphicsView")
self.graphicsView.setGeometry(QRect(90, 80, 401, 400))
self.pushButton_2 = QPushButton(self.centralwidget)
self.pushButton_2.setObjectName(u"pushButton_2")
self.pushButton_2.setGeometry(QRect(100, 500, 201, 71))
self.graphicsView_2 = QGraphicsView(self.centralwidget)
self.graphicsView_2.setObjectName(u"graphicsView_2")
self.graphicsView_2.setGeometry(QRect(520, 80, 401, 400))
self.comboBox = QComboBox(self.centralwidget)
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.setObjectName(u"comboBox")
self.comboBox.setGeometry(QRect(150, 620, 101, 31))
self.label = QLabel(self.centralwidget)
self.label.setObjectName(u"label")
self.label.setGeometry(QRect(150, 580, 200, 31))
self.pushButton_3 = QPushButton(self.centralwidget)
self.pushButton_3.setObjectName(u"pushButton_3")
self.pushButton_3.setGeometry(QRect(750, 600, 201, 71))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(MainWindow)
self.menubar.setObjectName(u"menubar")
self.menubar.setGeometry(QRect(0, 0, 1000, 23))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName(u"statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
# setupUi
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
self.pushButton.setText(QCoreApplication.translate("MainWindow", u"转换", None))
self.pushButton_2.setText(QCoreApplication.translate("MainWindow", u"选择图片路径", None))
self.comboBox.setItemText(0, "")
self.comboBox.setItemText(1, QCoreApplication.translate("MainWindow", u"呐喊", None))
self.comboBox.setItemText(2, QCoreApplication.translate("MainWindow", u"星空", None))
self.comboBox.setItemText(3, QCoreApplication.translate("MainWindow", u"镶嵌风", None))
self.comboBox.setItemText(4, QCoreApplication.translate("MainWindow", u"构图VII", None))
self.comboBox.setItemText(5, QCoreApplication.translate("MainWindow", u"The muse", None))
self.comboBox.setItemText(6, QCoreApplication.translate("MainWindow", u"udnie", None))
self.comboBox.setItemText(7, QCoreApplication.translate("MainWindow", u"神奈川冲浪里", None))
self.label.setText(QCoreApplication.translate("MainWindow", u"请选择图片风格", None))
self.pushButton_3.setText(QCoreApplication.translate("MainWindow", u"保存转换后图片", None))
# retranslateUi
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.image = None
self.transformed_image = None
#转换
self.pushButton.clicked.connect(self.transform_image)
self.pushButton_2.clicked.connect(self.load_image)
self.pushButton_3.clicked.connect(self.save_image)
def load_image(self):
options = QFileDialog.Options()
options |= QFileDialog.ReadOnly
file_name, _ = QFileDialog.getOpenFileName(self, "选择图片", "", "Images (*.png *.xpm *.jpg);;All Files (*)", options=options)
if file_name:
self.image = cv2.imread(file_name)
self.display_image(self.image, self.graphicsView)
def display_image(self, img, view):
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
h, w = view.height(), view.width()
img_resized = cv2.resize(img_rgb, (w, h))
h, w, ch = img_resized.shape
bytes_per_line = ch * w
q_img = QImage(img_resized.data, w, h, bytes_per_line, QImage.Format_RGB888)
pixmap = QPixmap.fromImage(q_img)
scene = QGraphicsScene()
scene.addItem(QGraphicsPixmapItem(pixmap))
view.setScene(scene)
def transform_image(self):
# 检查图像是否已加载
if self.image is not None:
#获取用户选择的风格
style = self.comboBox.currentIndex()
#使用OpenCV的深度学习模块cv2.dnn来加载预训练的神经网络模型
if style == 1:
#net = cv2.dnn.readNetFromTorch('../models/the_scream.t7') 因为exe文件在exe目录下所以需要改一下路径打包的时候使用的是这行。
net = cv2.dnn.readNetFromTorch('./models/the_scream.t7')
elif style == 2:
net = cv2.dnn.readNetFromTorch('./models/starry_night.t7')
elif style == 3:
net = cv2.dnn.readNetFromTorch('./models/mosaic.t7')
elif style == 4:
net = cv2.dnn.readNetFromTorch('./models/composition_vii.t7')
elif style == 5:
net = cv2.dnn.readNetFromTorch('./models/la_muse.t7')
elif style == 6:
net = cv2.dnn.readNetFromTorch('./models/udnie.t7')
elif style == 7:
net = cv2.dnn.readNetFromTorch('./models/the_wave.t7')
else:
QMessageBox.warning(self, "Warning", "请选择一个风格")
return
# 获取图像的高度和宽度
(h, w) = self.image.shape[:2]
# 将图像转换为blob用于神经网络输入
blob = cv2.dnn.blobFromImage(self.image, 1.0, (w, h), (103.939, 116.779, 123.680), swapRB=False, crop=False)
net.setInput(blob)
out = net.forward() # 得到输出
out = out.reshape(3, out.shape[2], out.shape[3])
#恢复均值
out[0] += 103.939
out[1] += 116.779
out[2] += 123.68
#调整通道的顺序
out = out.transpose(1, 2, 0)
self.transformed_image = np.clip(out, 0, 255).astype('uint8')
self.display_image(self.transformed_image, self.graphicsView_2)
def save_image(self):
if self.transformed_image is not None:
options = QFileDialog.Options()
options |= QFileDialog.ShowDirsOnly
file_name, _ = QFileDialog.getSaveFileName(self, "保存图片", "", "Images (*.png *.xpm *.jpg);;All Files (*)", options=options)
if file_name:
cv2.imwrite(file_name, self.transformed_image)
else:
QMessageBox.warning(self, "Warning", "没有转换后的图片可保存")
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())