本文的代码的功能是:可以对图片文件、视频批量增强清晰度,对老照片进行修复、复原操作、对黑白照片进行上色处理、人脸祛斑、美颜、人体瘦身、图像去噪等图像处理功能,使用了人工智能的算法。
本文与前几篇博文关联性较强,请事先阅读前几篇。 对此文感兴趣的可以加微深入探讨:herbert156
可运行的试用版本下载:https://pan.baidu.com/s/1nHvh_fd5yT5Spb2Faknn3Q 提取码: vu6d
如果提示过期,可以向博主索要新的SN文件。
一、主要功能:
以下的Python代码的功能:批量选择视频、批量选择图片,主要包括:
1、对图片、视频进行高清化、修复、上色并输出变换后的文件;
2、可以批量处理,在选择文件的对话框里可以选择多个文件,进行批量操作;
3、如果电脑有GPU,则会自动选择GPU处理,加快处理速度;
4、信息统计里面可以实时显示处理的各种统计信息;
5、视频处理完毕后自动进行音频的处理与合成。
二、主要代码:
话不多说,上代码!
UI的Python代码:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Repire_UI.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_ai_repire(object):
def setupUi(self, ai_repire):
ai_repire.setObjectName("ai_repire")
ai_repire.setEnabled(True)
ai_repire.resize(912, 823)
font = QtGui.QFont()
font.setFamily("宋体")
font.setPointSize(12)
ai_repire.setFont(font)
ai_repire.setMouseTracking(False)
self.layoutWidget = QtWidgets.QWidget(ai_repire)
self.layoutWidget.setGeometry(QtCore.QRect(360, 760, 531, 41))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout_5.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.startButton = QtWidgets.QPushButton(self.layoutWidget)
font = QtGui.QFont()
font.setFamily("宋体")
font.setPointSize(12)
self.startButton.setFont(font)
self.startButton.setObjectName("startButton")
self.horizontalLayout_5.addWidget(self.startButton)
self.stopButton = QtWidgets.QPushButton(self.layoutWidget)
font = QtGui.QFont()
font.setFamily("宋体")
font.setPointSize(12)
self.stopButton.setFont(font)
self.stopButton.setObjectName("stopButton")
self.horizontalLayout_5.addWidget(self.stopButton)
spacerItem = QtWidgets.QSpacerItem(60, 20, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_5.addItem(spacerItem)
self.helpButton = QtWidgets.QPushButton(self.layoutWidget)
font = QtGui.QFont()
font.setFamily("宋体")
font.setPointSize(12)
self.helpButton.setFont(font)
self.helpButton.setObjectName("helpButton")
self.horizontalLayout_5.addWidget(self.helpButton)
self.quitButton = QtWidgets.QPushButton(self.layoutWidget)
font = QtGui.QFont()
font.setFamily("宋体")
font.setPointSize(12)
self.quitButton.setFont(font)
self.quitButton.setObjectName("quitButton")
self.horizontalLayout_5.addWidget(self.quitButton)
self.groupBox_2 = QtWidgets.QGroupBox(ai_repire)
self.groupBox_2.setGeometry(QtCore.QRect(10, 20, 881, 281))
self.groupBox_2.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
self.groupBox_2.setAlignment(QtCore.Qt.AlignCenter)
self.groupBox_2.setObjectName("groupBox_2")
self.my_label1 = QtWidgets.QLabel(self.groupBox_2)
self.my_label1.setGeometry(QtCore.QRect(12, 30, 427, 240))
self.my_label1.setObjectName("my_label1")
self.my_label2 = QtWidgets.QLabel(self.groupBox_2)
self.my_label2.setGeometry(QtCore.QRect(443, 30, 427, 240))
self.my_label2.setObjectName("my_label2")
self.groupBox_4 = QtWidgets.QGroupBox(ai_repire)
self.groupBox_4.setGeometry(QtCore.QRect(10, 320, 881, 111))
self.groupBox_4.setAlignment(QtCore.Qt.AlignCenter)
self.groupBox_4.setObjectName("groupBox_4")
self.filesButton = QtWidgets.QPushButton(self.groupBox_4)
self.filesButton.setGeometry(QtCore.QRect(20, 30, 78, 24))
font = QtGui.QFont()
font.setFamily("宋体")
font.setPointSize(12)
self.filesButton.setFont(font)
self.filesButton.setObjectName("filesButton")
self.outButton = QtWidgets.QPushButton(self.groupBox_4)
self.outButton.setGeometry(QtCore.QRect(20, 70, 78, 24))
font = QtGui.QFont()
font.setFamily("宋体")
font.setPointSize(12)
self.outButton.setFont(font)
self.outButton.setObjectName("outButton")
self.txt1 = QtWidgets.QLabel(self.groupBox_4)
self.txt1.setGeometry(QtCore.QRect(110, 32, 771, 20))
self.txt1.setObjectName("txt1")
self.txt2 = QtWidgets.QLabel(self.groupBox_4)
self.txt2.setGeometry(QtCore.QRect(110, 72, 771, 20))
self.txt2.setObjectName("txt2")
self.groupBox_5 = QtWidgets.QGroupBox(ai_repire)
self.groupBox_5.setGeometry(QtCore.QRect(10, 640, 881, 81))
self.groupBox_5.setAlignment(QtCore.Qt.AlignCenter)
self.groupBox_5.setObjectName("groupBox_5")
self.txt11 = QtWidgets.QLabel(self.groupBox_5)
self.txt11.setGeometry(QtCore.QRect(20, 20, 861, 16))
self.txt11.setObjectName("txt11")
self.txt12 = QtWidgets.QLabel(self.groupBox_5)
self.txt12.setGeometry(QtCore.QRect(20, 50, 861, 21))
self.txt12.setObjectName("txt12")
self.groupBox_6 = QtWidgets.QGroupBox(ai_repire)
self.groupBox_6.setGeometry(QtCore.QRect(10, 450, 881, 71))
self.groupBox_6.setAlignment(QtCore.Qt.AlignCenter)
self.groupBox_6.setObjectName("groupBox_6")
self.checkBox_1 = QtWidgets.QCheckBox(self.groupBox_6)
self.checkBox_1.setGeometry(QtCore.QRect(40, 30, 141, 16))
self.checkBox_1.setObjectName("checkBox_1")
self.checkBox_2 = QtWidgets.QCheckBox(self.groupBox_6)
self.checkBox_2.setGeometry(QtCore.QRect(210, 30, 141, 16))
self.checkBox_2.setObjectName("checkBox_2")
self.groupBox_7 = QtWidgets.QGroupBox(ai_repire)
self.groupBox_7.setGeometry(QtCore.QRect(10, 550, 881, 71))
self.groupBox_7.setAlignment(QtCore.Qt.AlignCenter)
self.groupBox_7.setObjectName("groupBox_7")
self.checkBox_3 = QtWidgets.QCheckBox(self.groupBox_7)
self.checkBox_3.setGeometry(QtCore.QRect(40, 30, 141, 16))
self.checkBox_3.setObjectName("checkBox_3")
self.checkBox_4 = QtWidgets.QCheckBox(self.groupBox_7)
self.checkBox_4.setGeometry(QtCore.QRect(210, 30, 141, 16))
self.checkBox_4.setObjectName("checkBox_4")
self.checkBox_5 = QtWidgets.QCheckBox(self.groupBox_7)
self.checkBox_5.setGeometry(QtCore.QRect(390, 30, 181, 16))
self.checkBox_5.setObjectName("checkBox_5")
self.comboBox_1 = QtWidgets.QComboBox(self.groupBox_7)
self.comboBox_1.setGeometry(QtCore.QRect(560, 28, 71, 22))
self.comboBox_1.setObjectName("comboBox_1")
self.retranslateUi(ai_repire)
QtCore.QMetaObject.connectSlotsByName(ai_repire)
def retranslateUi(self, ai_repire):
_translate = QtCore.QCoreApplication.translate
ai_repire.setWindowTitle(_translate("ai_repire", "iCANX图像修复工具"))
self.startButton.setText(_translate("ai_repire", "开始处理"))
self.stopButton.setText(_translate("ai_repire", "停止处理"))
self.helpButton.setText(_translate("ai_repire", "帮助"))
self.quitButton.setText(_translate("ai_repire", "退出"))
self.groupBox_2.setTitle(_translate("ai_repire", "预览窗口"))
self.my_label1.setText(_translate("ai_repire", "原图"))
self.my_label2.setText(_translate("ai_repire", "卡通"))
self.groupBox_4.setTitle(_translate("ai_repire", "文件设置"))
self.filesButton.setText(_translate("ai_repire", "选择文件"))
self.outButton.setText(_translate("ai_repire", "输出目录"))
self.txt1.setText(_translate("ai_repire", "请选择图像文件[Ctrl+A全选、Ctrl/Shift+鼠标可多选]......"))
self.txt2.setText(_translate("ai_repire", "输出目录"))
self.groupBox_5.setTitle(_translate("ai_repire", "信息统计"))
self.txt11.setText(_translate("ai_repire", "【图像信息】"))
self.txt12.setText(_translate("ai_repire", "【运行信息】"))
self.groupBox_6.setTitle(_translate("ai_repire", "功能选择"))
self.checkBox_1.setText(_translate("ai_repire", "照片高清修复"))
self.checkBox_2.setText(_translate("ai_repire", "黑白照片上色"))
self.groupBox_7.setTitle(_translate("ai_repire", "输出设置"))
self.checkBox_3.setText(_translate("ai_repire", "原始分辨率"))
self.checkBox_4.setText(_translate("ai_repire", "2倍分辨率"))
self.checkBox_5.setText(_translate("ai_repire", "手动设置(图片宽度):"))
主处理代码:
class MainWin(QWidget, Ui_ai_repire):
def __init__(self):
super(MainWin, self).__init__()
self.setupUi(self)
global hwnd, run_flag
self.createLayout()
self.setWindowIcon(QIcon("damo/anime.ico"))
self.setWindowFlags(Qt.WindowMinimizeButtonHint)
self.show(); run_flag = 1
def CV2toPIL(self, img): # cv2转PIL
return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGRA2RGBA))
def PILtoCV2(self, img): # PIL转cv2
return cv2.cvtColor(np.array(img), cv2.COLOR_RGBA2BGRA)
def two_pic_combine_PIL(self, back_img, fore_img): #2个图片合并
back_img = self.CV2toPIL(back_img); fore_img = self.CV2toPIL(fore_img); r,g,b,alpha = fore_img.split()
return cv2.cvtColor(self.PILtoCV2(Image.composite(fore_img, back_img, alpha)), cv2.COLOR_BGRA2BGR)
def ShowCarton(self, result, img_path, style): # 卡通转换
if self.checkBox_1.isChecked(): multiple = 2 # 图片修复返回的图片分辨率是原图的2倍
else: multiple = 1 # 图片上色返回的图片分辨率是原图的1倍
size_x = result.shape[1] # 宽度
size_y = result.shape[0] # 高度
if self.checkBox_3.isChecked(): # 原始分辨率
img_w = int(size_x/multiple)
img_h = int(size_y/multiple)
elif self.checkBox_4.isChecked(): # 2倍分辨率
img_w = int(size_x*2/multiple)
img_h = int(size_y*2/multiple)
elif self.checkBox_5.isChecked(): # 手动设置
set_w = int(self.comboBox_1.currentText())
img_w = set_w
img_h = int(size_y * (set_w / result.shape[1]))
else:
img_w = int(size_x*2/multiple)
img_h = int(size_y*2/multiple)
jpg_name = out_dir + '/' + os.path.splitext(os.path.split(img_path)[1])[0] + '_'+style+'.jpg'
# print(img_w, img_h)
outfile = cv2.resize(result, (img_w, img_h))
cv2.imencode('.jpg', outfile)[1].tofile(jpg_name)
if size_x / size_y > 1.7778: fx = 427 / size_x; fy = fx # 计算16:9的比例,以便缩放不变形
else: fx = 240 / size_y; fy = fx
self.my_label2.setPixmap(self.CvMatToQImage(cv2.resize(result, (0, 0), fx=fx, fy=fy)))
cv2.waitKey(1)
# cv2.imshow('Test', result1.astype(np.uint8)); cv2.waitKey(0)
def image_change_background(self, imagefile):
global iii, stop_flag, t0
try:
img1= cv2.imdecode(np.fromfile(imagefile, dtype=np.uint8), -1)
if img1.shape[2] == 4: img1 = cv2.cvtColor(img1, cv2.COLOR_BGRA2BGR)
except:
self.show_error('读取图片文件【'+imagefile+'】时,出现错误!\n\n原因:目录/文件名不能包含中文...'); return
cv2.waitKey(1)
if img1 is None: self.show_error('读取图片文件【'+imagefile+'】时,出现错误!\n\n原因:目录/文件名不能包含中文...'); return
size_x = img1.shape[1] # 宽度
size_y = img1.shape[0] # 高度
if size_x > 3840 or size_x > 3840:
self.show_error('【文件】:'+ imagefile + '\n【分辨率】:' + str(size_x) + 'x' + str(size_y) +
'\n\n【错误信息】:图片文件的长边不能大于3840像素!\n'); return
if self.checkBox_3.isChecked() or self.checkBox_4.isChecked() or self.checkBox_5.isChecked(): pass
else: self.show_error('\n您必须在【输出设置】里面,选择一项【输出分辨率】的选项...\n'); return
imageinfo = '【图片信息】 文件总数:%d | 正在处理(%d/%d):'%(filesnums,iii,filesnums) + os.path.split(imagefile)[1]+\
' | 图像分辨率:%dx%d '%(size_x,size_y)
self.txt11.setText(imageinfo)
t1 = time.time()
if size_x / size_y > 1.7778: fx = 427 / size_x; fy = fx # 计算16:9的比例,以便缩放不变形
else: fx = 240 / size_y; fy = fx
if stop_flag:
self.txt11.setText('【图片信息】 文件总s数:%d个 | 处理完成:%d个' % (filesnums, iii))
self.txt12.setText('【运行信息】 用户终止了正在运行的程序......')
return
self.my_label1.setPixmap(self.CvMatToQImage(cv2.resize(img1, (0, 0), fx=fx, fy=fy)))
if not DEBUG_FLAG: self.RunCarton(imagefile)
else: pass #img3 = img1
cv2.waitKey(1); t2 = time.time()
runinfo = '【运行信息】 当前图片处理耗时:%.3f秒 | 总处理耗时:%.1f秒 | 处理进度:%.1f%%'%((t2-t1),(t2-t0),100*(iii/filesnums))
self.txt12.setText(runinfo); cv2.waitKey(1)
self.txt12.setText('【运行信息】 处理完毕!总消耗时间:%d秒'%(t2-t0))
self.txt11.setText('【图片信息】 文件总数:%d个 | 处理完成:%d个'%(filesnums,iii))
def CvMatToQImage(self, ptr): # Converts an opencv MAT format into a QImage
ptr = cv2.cvtColor(ptr, cv2.COLOR_BGRA2RGBA) # 颜色格式转换
QtImg = QtGui.QImage(ptr.data, ptr.shape[1], ptr.shape[0], QtGui.QImage.Format_RGBA8888)
return QtGui.QPixmap.fromImage(QtImg)
# def show_error(self,str):
# r_button = QMessageBox.question(self, my_title,'\n\n'+str+'\n\n', QMessageBox.Ok)
def show_error(self, str):
infoBox = QMessageBox()
infoBox.setIcon(QMessageBox.Information)
infoBox.setText(str)
infoBox.setStandardButtons(QMessageBox.Ok)
infoBox.button(QMessageBox.Ok).animateClick(30000) # 10秒自动关闭
infoBox.exec_()
def set_False_Btn(self):
self.filesButton.setEnabled(False); self.outButton.setEnabled(False)
self.startButton.setEnabled(False); self.stopButton.setEnabled(True)
self.quitButton.setEnabled(False)
def set_True_Btn(self):
self.filesButton.setEnabled(True); self.outButton.setEnabled(True)
self.startButton.setEnabled(True); self.stopButton.setEnabled(False)
self.quitButton.setEnabled(True)
def startrun(self):
global iii,stop_flag,t0
iii = 0; stop_flag = False
self.txt12.setText('【运行信息】 正在初始化AI模型......');cv2.waitKey(1)
t0 = time.time()
if files == []: self.show_error('请选择需要变换的图像文件!'); return
if not os.path.exists(out_dir): self.show_error('输出目录不存在,请重新选择!'); return
self.set_False_Btn()
for file in files:
iii += 1
if stop_flag: break
self.image_change_background(file)
self.set_True_Btn()
def stoprun(self):
global stop_flag
r_button = QMessageBox.question(self, my_title,
"\n\n 确定要停止图片变换吗?\n\n", QMessageBox.Yes | QMessageBox.No)
if r_button == QMessageBox.Yes: stop_flag = True
def helpWin(self):
str="\n\n\n1、【选择文件】选择需要修复的图像文件(可多选);\n2、【输出目录】修复后的文件目录,文件名:源文件_1.jpg;\n"+\
"3、如没有Nvidia系列GPU,AI算法自动选择CPU处理;\n4、文件的长边不能大于3840像素;\n\n\n"+\
" 本软件著作权归属:XXX 网址:xxx.com\n\n"
QMessageBox.question(self, my_title, str, QMessageBox.Ok)
def quitWin(self):
r_button = QMessageBox.question(self, my_title,
"\n\n退出将终止修复过程...... \n\n确认退出吗?\n\n", QMessageBox.Yes | QMessageBox.No)
if r_button == QMessageBox.Yes: sys.exit()
def filesButton_fuc(self):
global files,filesnums, input_path
files, ok1 = QFileDialog.getOpenFileNames(self,'请选择图像文件[全选:Ctrl+A、多选:Ctrl/Shift+鼠标]',
input_path,"*.jpg;;*.png")
filesnums = len(files)
if files!=[]:
txt='目录:'+os.path.split(files[0])[0]+' | 已选文件:'+str(filesnums)+'个 | 文件名:'
for file in files: txt=txt+ os.path.split(file)[1]+'; '
self.txt1.setText(txt)
input_path = os.path.split(files[0])[0]
else: self.txt1.setText('请选择图像文件[全选:Ctrl+A、多选:Ctrl/Shift+鼠标]......')
def outButton_fuc(self):
global out_dir, work_path
out_dir = QFileDialog.getExistingDirectory(self,'选择修复后的输出文件夹', work_path)
if out_dir == '':
self.txt2.setText('请选择图片修复后的文件保存目录......')
work_path = out_dir
else: self.txt2.setText(out_dir)
def box_choose1(self): # 照片高清修复
if self.checkBox_1.isChecked(): self.checkBox_2.setChecked(False)
else: self.checkBox_2.setChecked(True)
def box_choose2(self): # 黑白照片上色
if self.checkBox_2.isChecked(): self.checkBox_1.setChecked(False)
else: self.checkBox_1.setChecked(True)
def box_choose3(self): # 原始分辨率
self.checkBox_4.setChecked(False)
self.checkBox_5.setChecked(False)
def box_choose4(self): # 2倍分辨率
self.checkBox_3.setChecked(False)
self.checkBox_5.setChecked(False)
def box_choose5(self): # 手动设置
self.comboBox_1.setEnabled(True)
self.checkBox_3.setChecked(False)
self.checkBox_4.setChecked(False)
def click_comboBox1(self, text): # 手动分辨率
pass
def createLayout(self):
self.my_label1.setPixmap(self.CvMatToQImage(img_start))
self.my_label2.setPixmap(self.CvMatToQImage(img_start))
self.my_label1.setAlignment(Qt.AlignCenter); self.my_label2.setAlignment(Qt.AlignCenter)
self.my_label1.setFixedSize(427, 240); self.my_label2.setFixedSize(427, 240)
self.my_label1.setAlignment(Qt.AlignCenter); self.my_label2.setAlignment(Qt.AlignCenter)
self.my_label1.setToolTip("本区域,显示的是原始图片缩略图...")
self.my_label2.setToolTip("本区域,显示的是变换后的缩略图...")
self.checkBox_1.setChecked(True)
self.checkBox_4.setChecked(True)
self.checkBox_1.stateChanged.connect(self.box_choose1)
self.checkBox_2.stateChanged.connect(self.box_choose2)
self.checkBox_3.stateChanged.connect(self.box_choose3)
self.checkBox_4.stateChanged.connect(self.box_choose4)
self.checkBox_5.stateChanged.connect(self.box_choose5)
self.comboBox_1.addItems(['3840','2560','1920','1280','640'])
self.comboBox_1.setEnabled(False)
self.comboBox_1.activated[str].connect(self.click_comboBox1)
self.filesButton.setToolTip("选择即将被变换的的图片文件,可单选、多选...")
self.outButton.setToolTip("选择输出文件目录,变换后的文件将存在此目录...")
self.txt2.setText(out_dir)
self.filesButton.clicked.connect(self.filesButton_fuc)
self.outButton.clicked.connect(self.outButton_fuc)
self.stopButton.setEnabled(False)
self.startButton.clicked.connect(self.startrun)
self.stopButton.clicked.connect(self.stoprun)
self.helpButton.clicked.connect(self.helpWin)
self.quitButton.clicked.connect(self.quitWin)
#if __name__ == '__main__':
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
app = QtWidgets.QApplication(sys.argv)
MainWin = MainWin()
sys.exit(app.exec_())