赞
踩
纯干货、复制可用
先上图
修复前
修复后
前置条件
环境python3.10 cuda12.1
1、同学们去github下载codeformer的源码。下载并安装
源码地址
2、安装pyside6、pyinstaller
3、安装torch torchvision
pip install pyside6
pip install pyinstaller
pip install torch torchvision --extra-index-url https://download.pytorch.org/whl/cu121
3、gui源码
ui文件
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1620</width>
<height>539</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QWidget" name="horizontalLayoutWidget">
<property name="geometry">
<rect>
<x>110</x>
<y>10</y>
<width>931</width>
<height>31</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QCheckBox" name="single">
<property name="text">
<string>单人图像增强</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="multiple">
<property name="text">
<string>多人图像增强</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="monochrome">
<property name="text">
<string>单色图像彩色化</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="damaged">
<property name="text">
<string>破损图像修复</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="definition">
<property name="text">
<string>视频清晰度增强</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>40</x>
<y>10</y>
<width>71</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>修复模式:</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>40</x>
<y>80</y>
<width>61</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>注意事项:</string>
</property>
</widget>
<widget class="QLabel" name="repair_mode_tips">
<property name="geometry">
<rect>
<x>110</x>
<y>80</y>
<width>911</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>请选择修复模式</string>
</property>
</widget>
<widget class="QWidget" name="horizontalLayoutWidget_2">
<property name="geometry">
<rect>
<x>40</x>
<y>150</y>
<width>491</width>
<height>51</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="input_button">
<property name="text">
<string>选择输入文件</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="input_path">
<property name="text">
<string>输入文件</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="horizontalLayoutWidget_3">
<property name="geometry">
<rect>
<x>560</x>
<y>150</y>
<width>481</width>
<height>51</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QPushButton" name="output_button">
<property name="text">
<string>选择输出文件夹</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="output_path">
<property name="text">
<string>输出文件夹</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QLabel" name="label_5">
<property name="geometry">
<rect>
<x>40</x>
<y>240</y>
<width>71</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>选择保真度:</string>
</property>
</widget>
<widget class="QLabel" name="label_6">
<property name="geometry">
<rect>
<x>40</x>
<y>310</y>
<width>611</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>保真度越小生成的质量越高,生成的时间越长;越高越贴近原始的效果,生成的时间越短</string>
</property>
</widget>
<widget class="QPushButton" name="start_repair_button">
<property name="geometry">
<rect>
<x>30</x>
<y>410</y>
<width>471</width>
<height>91</height>
</rect>
</property>
<property name="text">
<string>开始修复</string>
</property>
</widget>
<widget class="QPushButton" name="check_result_button">
<property name="geometry">
<rect>
<x>570</x>
<y>410</y>
<width>471</width>
<height>91</height>
</rect>
</property>
<property name="text">
<string>查看结果</string>
</property>
</widget>
<widget class="QSlider" name="fidelity_slider">
<property name="geometry">
<rect>
<x>120</x>
<y>250</y>
<width>891</width>
<height>22</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QLabel" name="fidelity_label">
<property name="geometry">
<rect>
<x>1020</x>
<y>250</y>
<width>16</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>10</string>
</property>
</widget>
<widget class="QPushButton" name="stop_repair_button">
<property name="geometry">
<rect>
<x>1460</x>
<y>510</y>
<width>151</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>立即停止</string>
</property>
</widget>
<widget class="QCheckBox" name="back">
<property name="geometry">
<rect>
<x>880</x>
<y>90</y>
<width>91</width>
<height>19</height>
</rect>
</property>
<property name="mouseTracking">
<bool>true</bool>
</property>
<property name="text">
<string>背景增强</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
<widget class="QTextEdit" name="repair_view">
<property name="geometry">
<rect>
<x>1050</x>
<y>10</y>
<width>561</width>
<height>491</height>
</rect>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
ui.py
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'repair.ui'
##
## Created by: Qt User Interface Compiler version 6.6.3
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QCheckBox, QHBoxLayout, QLabel,
QPushButton, QSizePolicy, QSlider, QTextEdit,
QWidget)
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(1620, 539)
self.horizontalLayoutWidget = QWidget(Form)
self.horizontalLayoutWidget.setObjectName(u"horizontalLayoutWidget")
self.horizontalLayoutWidget.setGeometry(QRect(110, 10, 931, 31))
self.horizontalLayout = QHBoxLayout(self.horizontalLayoutWidget)
self.horizontalLayout.setObjectName(u"horizontalLayout")
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.single = QCheckBox(self.horizontalLayoutWidget)
self.single.setObjectName(u"single")
self.horizontalLayout.addWidget(self.single)
self.multiple = QCheckBox(self.horizontalLayoutWidget)
self.multiple.setObjectName(u"multiple")
self.horizontalLayout.addWidget(self.multiple)
self.monochrome = QCheckBox(self.horizontalLayoutWidget)
self.monochrome.setObjectName(u"monochrome")
self.horizontalLayout.addWidget(self.monochrome)
self.damaged = QCheckBox(self.horizontalLayoutWidget)
self.damaged.setObjectName(u"damaged")
self.horizontalLayout.addWidget(self.damaged)
self.definition = QCheckBox(self.horizontalLayoutWidget)
self.definition.setObjectName(u"definition")
self.horizontalLayout.addWidget(self.definition)
self.label = QLabel(Form)
self.label.setObjectName(u"label")
self.label.setGeometry(QRect(40, 10, 71, 31))
self.label_2 = QLabel(Form)
self.label_2.setObjectName(u"label_2")
self.label_2.setGeometry(QRect(40, 80, 61, 31))
self.repair_mode_tips = QLabel(Form)
self.repair_mode_tips.setObjectName(u"repair_mode_tips")
self.repair_mode_tips.setGeometry(QRect(110, 80, 911, 31))
self.horizontalLayoutWidget_2 = QWidget(Form)
self.horizontalLayoutWidget_2.setObjectName(u"horizontalLayoutWidget_2")
self.horizontalLayoutWidget_2.setGeometry(QRect(40, 150, 491, 51))
self.horizontalLayout_2 = QHBoxLayout(self.horizontalLayoutWidget_2)
self.horizontalLayout_2.setObjectName(u"horizontalLayout_2")
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.input_button = QPushButton(self.horizontalLayoutWidget_2)
self.input_button.setObjectName(u"input_button")
self.horizontalLayout_2.addWidget(self.input_button)
self.input_path = QLabel(self.horizontalLayoutWidget_2)
self.input_path.setObjectName(u"input_path")
self.horizontalLayout_2.addWidget(self.input_path)
self.horizontalLayoutWidget_3 = QWidget(Form)
self.horizontalLayoutWidget_3.setObjectName(u"horizontalLayoutWidget_3")
self.horizontalLayoutWidget_3.setGeometry(QRect(560, 150, 481, 51))
self.horizontalLayout_3 = QHBoxLayout(self.horizontalLayoutWidget_3)
self.horizontalLayout_3.setObjectName(u"horizontalLayout_3")
self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
self.output_button = QPushButton(self.horizontalLayoutWidget_3)
self.output_button.setObjectName(u"output_button")
self.horizontalLayout_3.addWidget(self.output_button)
self.output_path = QLabel(self.horizontalLayoutWidget_3)
self.output_path.setObjectName(u"output_path")
self.horizontalLayout_3.addWidget(self.output_path)
self.label_5 = QLabel(Form)
self.label_5.setObjectName(u"label_5")
self.label_5.setGeometry(QRect(40, 240, 71, 41))
self.label_6 = QLabel(Form)
self.label_6.setObjectName(u"label_6")
self.label_6.setGeometry(QRect(40, 310, 611, 51))
self.start_repair_button = QPushButton(Form)
self.start_repair_button.setObjectName(u"start_repair_button")
self.start_repair_button.setGeometry(QRect(30, 410, 471, 91))
self.check_result_button = QPushButton(Form)
self.check_result_button.setObjectName(u"check_result_button")
self.check_result_button.setGeometry(QRect(570, 410, 471, 91))
self.fidelity_slider = QSlider(Form)
self.fidelity_slider.setObjectName(u"fidelity_slider")
self.fidelity_slider.setGeometry(QRect(120, 250, 891, 22))
self.fidelity_slider.setOrientation(Qt.Horizontal)
self.fidelity_label = QLabel(Form)
self.fidelity_label.setObjectName(u"fidelity_label")
self.fidelity_label.setGeometry(QRect(1020, 250, 16, 16))
self.stop_repair_button = QPushButton(Form)
self.stop_repair_button.setObjectName(u"stop_repair_button")
self.stop_repair_button.setGeometry(QRect(1460, 510, 151, 23))
self.back = QCheckBox(Form)
self.back.setObjectName(u"back")
self.back.setGeometry(QRect(880, 90, 91, 19))
self.back.setMouseTracking(True)
self.back.setChecked(False)
self.repair_view = QTextEdit(Form)
self.repair_view.setObjectName(u"repair_view")
self.repair_view.setGeometry(QRect(1050, 10, 561, 491))
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
# setupUi
def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
self.single.setText(QCoreApplication.translate("Form", u"\u5355\u4eba\u56fe\u50cf\u589e\u5f3a", None))
self.multiple.setText(QCoreApplication.translate("Form", u"\u591a\u4eba\u56fe\u50cf\u589e\u5f3a", None))
self.monochrome.setText(QCoreApplication.translate("Form", u"\u5355\u8272\u56fe\u50cf\u5f69\u8272\u5316", None))
self.damaged.setText(QCoreApplication.translate("Form", u"\u7834\u635f\u56fe\u50cf\u4fee\u590d", None))
self.definition.setText(QCoreApplication.translate("Form", u"\u89c6\u9891\u6e05\u6670\u5ea6\u589e\u5f3a", None))
self.label.setText(QCoreApplication.translate("Form", u"\u4fee\u590d\u6a21\u5f0f\uff1a", None))
self.label_2.setText(QCoreApplication.translate("Form", u"\u6ce8\u610f\u4e8b\u9879\uff1a", None))
self.repair_mode_tips.setText(QCoreApplication.translate("Form", u"\u8bf7\u9009\u62e9\u4fee\u590d\u6a21\u5f0f", None))
self.input_button.setText(QCoreApplication.translate("Form", u"\u9009\u62e9\u8f93\u5165\u6587\u4ef6", None))
self.input_path.setText(QCoreApplication.translate("Form", u"\u8f93\u5165\u6587\u4ef6", None))
self.output_button.setText(QCoreApplication.translate("Form", u"\u9009\u62e9\u8f93\u51fa\u6587\u4ef6\u5939", None))
self.output_path.setText(QCoreApplication.translate("Form", u"\u8f93\u51fa\u6587\u4ef6\u5939", None))
self.label_5.setText(QCoreApplication.translate("Form", u"\u9009\u62e9\u4fdd\u771f\u5ea6\uff1a", None))
self.label_6.setText(QCoreApplication.translate("Form", u"\u4fdd\u771f\u5ea6\u8d8a\u5c0f\u751f\u6210\u7684\u8d28\u91cf\u8d8a\u9ad8\uff0c\u751f\u6210\u7684\u65f6\u95f4\u8d8a\u957f\uff1b\u8d8a\u9ad8\u8d8a\u8d34\u8fd1\u539f\u59cb\u7684\u6548\u679c\uff0c\u751f\u6210\u7684\u65f6\u95f4\u8d8a\u77ed", None))
self.start_repair_button.setText(QCoreApplication.translate("Form", u"\u5f00\u59cb\u4fee\u590d", None))
self.check_result_button.setText(QCoreApplication.translate("Form", u"\u67e5\u770b\u7ed3\u679c", None))
self.fidelity_label.setText(QCoreApplication.translate("Form", u"10", None))
self.stop_repair_button.setText(QCoreApplication.translate("Form", u"\u7acb\u5373\u505c\u6b62", None))
self.back.setText(QCoreApplication.translate("Form", u"\u80cc\u666f\u589e\u5f3a", None))
# retranslateUi
main.py
"""
@Project movie_hd
@Filename Repair.py
@Create by PyCharm
@CreateDate: 2024/4/29 21:11
@Description
"""
import os
import subprocess
import sys
import threading
from PySide6 import QtWidgets
from PySide6.QtCore import Signal, Slot
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QMessageBox
from ui.ui_repair import Ui_Form
class Repair(QtWidgets.QWidget):
# 修复信号
repair_signal = Signal(str)
def __init__(self, main=None):
super(Repair, self).__init__()
self.Repair = main
self.ui = Ui_Form()
self.ui.setupUi(self)
self.setWindowTitle("AI图像修复工具")
self.setWindowIcon(QIcon("logo.ico"))
self.setFixedSize(1620, 540)
self.base_path = os.path.dirname(os.path.abspath(__file__))
ffmpeg_path = os.path.join(self.base_path, "ffmpeg")
# 设置环境变量
os.environ["PATH"] += os.pathsep + ffmpeg_path
self.conda_path = os.path.join(self.base_path, "conda")
self.vendor_path = os.path.join(self.base_path, "vendor")
self.process = None
# 选择修复模式
self.ui.single.clicked.connect(lambda: self.repair_mode_select("single"))
self.ui.multiple.clicked.connect(lambda: self.repair_mode_select("multiple"))
self.ui.monochrome.clicked.connect(lambda: self.repair_mode_select("monochrome"))
self.ui.damaged.clicked.connect(lambda: self.repair_mode_select("damaged"))
self.ui.definition.clicked.connect(lambda: self.repair_mode_select("definition"))
# 预览修复
self.back_flag = True
self.ui.back.clicked.connect(lambda: self.repair_back_select("back"))
# 默认一个复选框被选中
self.repair_mode = "single"
self.ui.single.setChecked(True)
self.ui.repair_mode_tips.setText("请选择单人的图像所在的文件") # 设置提示信息
# 选择输入文件
self.input_path = False
self.ui.input_button.clicked.connect(self.select_input_file)
# 选择输出文件
self.output_path = False
self.ui.output_button.clicked.connect(self.select_output_folder)
# fidelity_slider 0-10
self.fidelity_value = 0.5
self.fidelity_value_label = int(self.fidelity_value * 10)
self.ui.fidelity_slider.valueChanged.connect(self.fidelity_slider_change)
self.ui.fidelity_slider.setMinimum(0)
self.ui.fidelity_slider.setMaximum(10)
# step 0.1
self.ui.fidelity_slider.setSingleStep(1)
self.ui.fidelity_slider.setValue(self.fidelity_value_label)
self.ui.fidelity_label.setText(str(self.fidelity_value_label))
# 点击开始修复
self.ui.start_repair_button.clicked.connect(self.start_repair)
# 点击查看结果按钮
self.ui.check_result_button.clicked.connect(self.check_result)
# 点击停止修复按钮
self.ui.stop_repair_button.clicked.connect(self.stop_repair)
# 设置文本框只读
self.ui.repair_view.setReadOnly(True)
self.thread_cmd = None
# 信号槽
self.repair_signal.connect(self.update)
self.repair_signal_flg = False
# 显示窗口
self.show()
# 选择背景增强
def repair_back_select(self, check_name):
if check_name == "back":
# 判断是否选中
if self.ui.back.isChecked():
self.back_flag = True
else:
self.back_flag = False
# 选择修复模式
def repair_mode_select(self, check_name):
repair_mode_tips_label = {
"single": "请选择单人的图像文件",
"multiple": "请选择多人的图像文件",
"monochrome": "请选择单色的图像文件",
"damaged": "请选择损坏的图像的文件",
"definition": "请选择低清晰度的视频的文件"
}
repair_mode_check = ["single", "multiple", "monochrome", "damaged", "definition"]
# 把所有的复选框设置为未选中
for check in repair_mode_check:
if check != check_name:
getattr(self.ui, check).setChecked(False)
else:
getattr(self.ui, check).setChecked(True)
self.ui.repair_mode_tips.setText(repair_mode_tips_label[check_name])
self.repair_mode = check_name
# 选择输入文件
def select_input_file(self):
# 根据修复模式选择文件
if self.repair_mode == "definition":
input_file = QtWidgets.QFileDialog.getOpenFileName(self, "选择输入文件", self.base_path,
"mp4(*.mp4);;avi(*.avi);;mov(*.mov);;")
else:
# 图片
input_file = QtWidgets.QFileDialog.getOpenFileName(self, "选择输入文件", self.base_path,
"Image(*.jpg *.png)")
if input_file:
self.ui.input_path.setText(input_file[0])
self.input_path = input_file[0]
# 选择输出文件夹
def select_output_folder(self):
output_folder = QtWidgets.QFileDialog.getExistingDirectory(self, "选择输出文件夹")
if output_folder:
self.ui.output_path.setText(output_folder)
self.output_path = output_folder
# 保真度滑块
def fidelity_slider_change(self):
self.fidelity_value_label = self.ui.fidelity_slider.value()
self.ui.fidelity_label.setText(str(self.fidelity_value_label))
self.fidelity_value = self.fidelity_value_label / 10
# 开始修复
def start_repair(self):
if not self.repair_mode:
self.show_dialog("警告", "请选择修复模式")
return
if not self.input_path:
self.show_dialog("警告", "请选择输入文件")
return
if not self.output_path:
self.show_dialog("警告", "请选择输出文件夹")
return
# 如果正在修复
if self.repair_signal_flg:
self.show_dialog("警告", "正在修复中,请稍后,或者停止修复")
return
# 修复精细度
if self.fidelity_value == 0:
self.show_dialog("提示", "您选择了最高的保真度,修复时间可能会很长,请耐心等待")
self.ui.start_repair_button.setEnabled(False)
self.ui.check_result_button.setEnabled(False)
# 清空文本框
self.ui.repair_view.clear()
self.ui.repair_view.append("开始修复")
# 修复模式
command_dict = {
"single": f"python.exe inference_codeformer.py -w {self.fidelity_value} --has_aligned -i {self.input_path} -o {self.output_path} ",
"multiple": f"python.exe inference_codeformer.py -w {self.fidelity_value} --bg_upsampler realesrgan --face_upsample -i {self.input_path} -o {self.output_path}",
"monochrome": f"python.exe inference_colorization.py -i {self.input_path} -o {self.output_path} ",
"damaged": f"python.exe inference_inpainting.py -i {self.input_path} -o {self.output_path} ",
"definition": f"python.exe inference_codeformer.py -w {self.fidelity_value} --bg_upsampler realesrgan --face_upsample -i {self.input_path} -o {self.output_path} "
}
# 命令行参数
command = command_dict[self.repair_mode]
command = self.conda_path + "/" + command
# command = f"python Repair.py --repair_mode {self.repair_mode} --input_path {self.input_path} --output_path {self.output_path} --fidelity_value {self.fidelity_value}"
# os.system(command)
self.start_thread(command, self.vendor_path)
self.ui.start_repair_button.setEnabled(True)
self.ui.check_result_button.setEnabled(True)
# 运行命令
def run_cmd(self, command, command_path):
# 命令行参数
result = self.run_process(command, command_path)
for line in result:
self.repair_signal.emit(line)
# 运行命令
def run_process(self, command, command_path):
if self.repair_signal_flg is False:
self.repair_signal_flg = True
self.process = subprocess.Popen(command, cwd=command_path, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
bufsize=1,
shell=True,
text=True)
while self.process.poll() is None:
output = self.process.stdout.readline()
if output:
# [0m 结束标记
output = output.strip().replace("[0m", "")
yield output.strip() # 打印输出并去除尾随的换行符
self.process.wait()
# 启动服务的线程
def start_thread(self, command, command_path):
# 创建线程执行函数
self.thread_cmd = threading.Thread(target=self.run_cmd, args=(command, command_path))
self.thread_cmd.start()
@Slot(str)
def update(self, str):
# 在文本输入框末尾添加新收到的字符数据
self.ui.repair_view.append(str)
if "All results are saved in" in str:
self.repair_signal_flg = False
self.ui.start_repair_button.setEnabled(True)
self.ui.check_result_button.setEnabled(True)
self.ui.repair_view.append("修复完成")
def closeEvent(self, event):
self.stop_repair()
event.accept()
# 查看结果
def check_result(self):
if not self.output_path:
self.show_dialog("警告", "请选择输出文件夹")
return
# 用资源管理器打开文件
os.startfile(self.output_path)
# 停止修复
def stop_repair(self):
try:
# 停止python进程
self.process.terminate()
self.repair_signal_flg = False
self.ui.start_repair_button.setEnabled(True)
self.ui.check_result_button.setEnabled(True)
self.ui.repair_view.append("停止修复")
# 停止线程
self.thread_cmd.quit()
except:
pass
# 弹出提示框
def show_dialog(self, title, text):
dlg = QMessageBox(self)
# 设icon
dlg.setWindowIcon(QIcon("logo.ico"))
dlg.setWindowTitle(title)
dlg.setText(text)
dlg.exec()
if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = Repair()
window.show()
sys.exit(app.exec())
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。