添加了关于内容

modified:   helloworld.py
	modified:   nst/Models.py
	new file:   nst/helloworld.py
	modified:   sub_windows/ui_sub_window_10.py
master
shawn-sheep 2 years ago
parent e477014549
commit 23d27c297a

@ -1,4 +1,6 @@
import cv2
def detect_cameras(max_cameras=10):
cameras = []
for i in range(max_cameras):
@ -10,7 +12,10 @@ def detect_cameras(max_cameras=10):
cap.release()
return cameras
# available_cameras = detect_cameras()
# print(f'Found {len(available_cameras)} camera(s): {available_cameras}'
print(cv2.__version__)
image = cv2.imread("nst/ori/1.jpg")
cv2.imshow("Image", image)
cv2.waitKey(0)

@ -13,6 +13,7 @@ from tools import Tool
tl = Tool()
class ContentLoss(nn.Module):
"""内容损失"""
@ -68,7 +69,9 @@ class StyleLoss(ContentLoss):
class Transfer(object):
def __init__(self, fn_content, fn_style, model_path=r'weights/squeezenet1_0-a815701f.pth'):
def __init__(
self, fn_content, fn_style, model_path=r"weights/squeezenet1_0-a815701f.pth"
):
"""usage:
net = Transfer('picasso.jpg','dancing.jpg')
dt, img = net.fit()
@ -79,13 +82,12 @@ class Transfer(object):
self.style_img = tl.image_loader(fn_style).type(dtype)
self.input_img = self.content_img.clone()
"""
get_style_model_and_losses函数是针对vgg模型的,
要应用到其他模型,需要改写该函数;"""
if 'vgg19' in model_path:
if "vgg19" in model_path:
self.seq = self.load_vgg19(model_path)
elif 'resnet18' in model_path:
elif "resnet18" in model_path:
self.seq = self.load_resnet18(model_path)
elif "squeezenet1_0" in model_path:
self.seq = self.load_squeezenet(model_path, "1_0")
@ -104,7 +106,6 @@ class Transfer(object):
cnn.load_state_dict(torch.load(model_path))
return cnn.features[:23]
def load_squeezenet(self, model_path, version):
"""加载SqueezeNet1.0预训练模型;"""
model = models.SqueezeNet(version=version)
@ -113,10 +114,12 @@ class Transfer(object):
def load_densenet(self, model_path):
"""加载densenet121预训练模型;"""
model = models.DenseNet(num_init_features=64, growth_rate=32,
block_config=(6, 12, 24, 16))
model = models.DenseNet(
num_init_features=64, growth_rate=32, block_config=(6, 12, 24, 16)
)
pattern = re.compile(
r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$')
r"^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$"
)
state_dict = torch.load(model_path)
for key in list(state_dict.keys()):
res = pattern.match(key)
@ -146,19 +149,33 @@ class Transfer(object):
outout_img:PIL.Image.Image;
style_weight需要远远大于content_weight;"""
t0 = time.time()
cnn, tensor = self.rebuild(self.seq, self.content_img,
self.style_img, self.input_img,
num_steps, content_weight,
style_weight)
cnn, tensor = self.rebuild(
self.seq,
self.content_img,
self.style_img,
self.input_img,
num_steps,
content_weight,
style_weight,
)
output_img = tl.batch_tensor2pil(tensor)
dt = time.time() - t0
return dt, output_img
def rebuild(self, cnn, content_img, style_img, input_img, num_steps,
content_weight, style_weight):
def rebuild(
self,
cnn,
content_img,
style_img,
input_img,
num_steps,
content_weight,
style_weight,
):
"""run the style transfer."""
model, style_losses, content_losses = self.get_losses(cnn, style_img, content_img,
style_weight, content_weight)
model, style_losses, content_losses = self.get_losses(
cnn, style_img, content_img, style_weight, content_weight
)
input_param, optimizer = self.get_optimizer(input_img)
run = [0]
@ -180,8 +197,11 @@ class Transfer(object):
run[0] += 1
if run[0] % 50 == 0:
print('Style Loss : {:4f} Content Loss: {:4f}'.format(style_score, content_score))
print(
"Style Loss : {:4f} Content Loss: {:4f}".format(
style_score, content_score
)
)
return style_score + content_score
@ -192,10 +212,16 @@ class Transfer(object):
return model, input_param.data
def get_losses(self, cnn, style_img, content_img,
style_weight, content_weight,
content_layers=['conv_4'],
style_layers=['conv_1', 'conv_2', 'conv_3', 'conv_4', 'conv_5']):
def get_losses(
self,
cnn,
style_img,
content_img,
style_weight,
content_weight,
content_layers=["conv_4"],
style_layers=["conv_1", "conv_2", "conv_3", "conv_4", "conv_5"],
):
cnn = copy.deepcopy(cnn)
# 仅为了有一个可迭代的列表 内容/风格 损失
@ -264,12 +290,13 @@ class Transfer(object):
input_param = nn.Parameter(input_img.data)
optimizer = optim.LBFGS([input_param])
return input_param, optimizer
if __name__ == '__main__':
if __name__ == "__main__":
# model = models.vgg19(pretrained=True)
ph = 'weights/vgg19-dcbb9e9d.pth'
ph = "weights/vgg19-dcbb9e9d.pth"
transfer = Transfer('ori/2.jpg', 'art/2.jpg',ph)
transfer = Transfer("./ori/2.jpg", "./art/2.jpg", ph)
t = time.time()
dt, img = transfer.fit()
@ -279,6 +306,6 @@ if __name__ == '__main__':
img = np.array(img)[:, :, ::-1]
cv2.imwrite('1.jpg',img)
cv2.imshow('1',img)
cv2.imwrite("1.jpg", img)
cv2.imshow("1", img)
cv2.waitKey(0)

@ -0,0 +1,5 @@
import cv2
image = cv2.imread("ori/1.jpg")
cv2.imshow("Image", image)
cv2.waitKey(0)

@ -42,13 +42,13 @@ class Ui_Form(object):
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "关于本软件"))
self.label.setText(_translate("Form", "1561130423-阳旭 的毕业设计"))
self.label.setText(_translate("Form", "作者10215101526-许青阳"))
self.textBrowser.setHtml(_translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'黑体\'; font-size:10pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"justify\" style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:\'宋体\';\">本课题针对图像处理的难点,设计了一款界面友好的图像处理仿真系统,意在帮助初学者形象的理解图像处理所涉及的部分知识点。</span></p>\n"
"<p align=\"justify\" style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:\'宋体\';\">本数字图像处理系统针对图像处理的难点,设计了一款界面友好的图像处理仿真系统,意在帮助初学者形象的理解图像处理所涉及的部分知识点。</span></p>\n"
"<p align=\"justify\" style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:\'宋体\';\">该软件包含常见的图像处理实例,例如图像缩放、加噪、平滑锐化、直方图均衡、压缩编码,边缘检测、人脸检测等。这些模块涉及到较多图像处理知识,通过每个模块设计的实例,循序渐进的让初学者加深理解图像处理方法间的联系,激发初学者对图像处理的兴趣。</span></p>\n"
"<p align=\"justify\" style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-family:\'宋体\';\">本设计利用</span><span style=\" font-family:\'Times New Roman,serif\';\">PyCharm</span><span style=\" font-family:\'宋体\';\">和</span><span style=\" font-family:\'Times New Roman,serif\';\">QT Designer</span><span style=\" font-family:\'宋体\';\">开发平台,基于</span><span style=\" font-family:\'Times New Roman,serif\';\">Python</span><span style=\" font-family:\'宋体\';\">编程语言以及</span><span style=\" font-family:\'Times New Roman,serif\';\">OpenCV</span><span style=\" font-family:\'宋体\';\">计算机视觉库,利用</span><span style=\" font-family:\'Times New Roman,serif\';\">Qt5</span><span style=\" font-family:\'宋体\';\">作为</span><span style=\" font-family:\'Times New Roman,serif\';\">GUI</span><span style=\" font-family:\'宋体\';\">框架,构建了一个兼容</span><span style=\" font-family:\'Times New Roman,serif\';\">Windows-x86</span><span style=\" font-family:\'宋体\';\">平台的软件。</span></p></body></html>"))
self.label_2.setText(_translate("Form", "2019-06-03"))
self.label_2.setText(_translate("Form", "2023-07-02"))

Loading…
Cancel
Save