diff --git a/helloworld.py b/helloworld.py index d1239a5..fc33b92 100644 --- a/helloworld.py +++ b/helloworld.py @@ -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__) \ No newline at end of file +image = cv2.imread("nst/ori/1.jpg") +cv2.imshow("Image", image) +cv2.waitKey(0) \ No newline at end of file diff --git a/nst/Models.py b/nst/Models.py index 91ec664..1329423 100644 --- a/nst/Models.py +++ b/nst/Models.py @@ -13,6 +13,7 @@ from tools import Tool tl = Tool() + class ContentLoss(nn.Module): """内容损失""" @@ -68,24 +69,25 @@ 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() - """ + net = Transfer('picasso.jpg','dancing.jpg') + dt, img = net.fit() + """ self.use_cuda, dtype, imsize = tl.config() self.content_img = tl.image_loader(fn_content).type(dtype) 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,21 +290,22 @@ 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() - print(time.time()-t) + print(time.time() - t) # print(dt,img) - img = np.array(img)[:,:,::-1] + img = np.array(img)[:, :, ::-1] - cv2.imwrite('1.jpg',img) - cv2.imshow('1',img) - cv2.waitKey(0) \ No newline at end of file + cv2.imwrite("1.jpg", img) + cv2.imshow("1", img) + cv2.waitKey(0) diff --git a/nst/helloworld.py b/nst/helloworld.py new file mode 100644 index 0000000..9f72e94 --- /dev/null +++ b/nst/helloworld.py @@ -0,0 +1,5 @@ +import cv2 + +image = cv2.imread("ori/1.jpg") +cv2.imshow("Image", image) +cv2.waitKey(0) \ No newline at end of file diff --git a/sub_windows/ui_sub_window_10.py b/sub_windows/ui_sub_window_10.py index a035f1f..4370efa 100644 --- a/sub_windows/ui_sub_window_10.py +++ b/sub_windows/ui_sub_window_10.py @@ -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", "\n" "\n" -"

本课题针对图像处理的难点,设计了一款界面友好的图像处理仿真系统,意在帮助初学者形象的理解图像处理所涉及的部分知识点。

\n" +"

本数字图像处理系统针对图像处理的难点,设计了一款界面友好的图像处理仿真系统,意在帮助初学者形象的理解图像处理所涉及的部分知识点。

\n" "

该软件包含常见的图像处理实例,例如图像缩放、加噪、平滑锐化、直方图均衡、压缩编码,边缘检测、人脸检测等。这些模块涉及到较多图像处理知识,通过每个模块设计的实例,循序渐进的让初学者加深理解图像处理方法间的联系,激发初学者对图像处理的兴趣。

\n" "

本设计利用PyCharmQT Designer开发平台,基于Python编程语言以及OpenCV计算机视觉库,利用Qt5作为GUI框架,构建了一个兼容Windows-x86平台的软件。

")) - self.label_2.setText(_translate("Form", "2019-06-03")) + self.label_2.setText(_translate("Form", "2023-07-02"))