图像风格迁移已经属于比较成熟的领域了,现在连实时的风格迁移都不成问题。之前一直想出一篇这样的文章,但无奈于大部分开源项目配置起来非常麻烦,比如 luanfujun/deep-photo-styletransfer 项目,需要安装 CUDA、pytorch、cudnn等等,配置完一天都过去了。

不过现在我们有了非常好的开源应用项目,那就是OpenCV的DNN图像风格迁移。你只需要安装OpenCV就可以使用了,在cmd/terminal中输入(如果你还没有安装Python,请看这篇文章:Python安装):

 pip install python-opencv

不过它也是有局限性的,我们只能用别人训练好的模型进行风格迁移,如果我们要自定义风格,那就必须配置cudn等工具,使用 deep-photo-styletransfer 等项目的方法进行训练,今天的教程我们拿fast-neural-style训练好的模型对下面的图片做一次风格迁移。

喵喵喵

1.选择模型

fast-neural-style放出的模型风格一共有9种,我们将一一尝试,其中部分风格如下比如:

candy.jpg
candy
mosaic.jpg
mosaic
starry_night.jpg
starry_night
udnie.jpg
udnie

模型文件可以关注我们下方公众号 Python实用宝典,回复 风格迁移 下载,里面有全部10个模型风格的资源。

2.克隆OpenCV源码

我们直接克隆OpenCV开源项目中关于DNN图像迁移的例子,地址是:

https://github.com/opencv/opencv/blob/3.4.0/samples/dnn/fast_neural_style.py

代码如下:

import cv2 as cv
import numpy as np
import argparse

parser = argparse.ArgumentParser(
        description='This script is used to run style transfer models from '
                    'https://github.com/jcjohnson/fast-neural-style using OpenCV')
parser.add_argument('--input', help='Path to image or video. Skip to capture frames from camera')
parser.add_argument('--model', help='Path to .t7 model')
parser.add_argument('--width', default=-1, type=int, help='Resize input to specific width.')
parser.add_argument('--height', default=-1, type=int, help='Resize input to specific height.')
parser.add_argument('--median_filter', default=0, type=int, help='Kernel size of postprocessing blurring.')
args = parser.parse_args()

net = cv.dnn.readNetFromTorch(args.model)

if args.input:
    cap = cv.VideoCapture(args.input)
else:
    cap = cv.VideoCapture(0)

cv.namedWindow('Styled image', cv.WINDOW_NORMAL)
while cv.waitKey(1) < 0:
    hasFrame, frame = cap.read()
    if not hasFrame:
        cv.waitKey()
        break

    inWidth = args.width if args.width != -1 else frame.shape[1]
    inHeight = args.height if args.height != -1 else frame.shape[0]
    inp = cv.dnn.blobFromImage(frame, 1.0, (inWidth, inHeight),
                              (103.939, 116.779, 123.68), swapRB=False, crop=False)

    net.setInput(inp)
    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 /= 255
    out = out.transpose(1, 2, 0)

    t, _ = net.getPerfProfile()
    freq = cv.getTickFrequency() / 1000
    print(t / freq, 'ms')

    if args.median_filter:
        out = cv.medianBlur(out, args.median_filter)

    cv.imshow('Styled image', out) 

注意,源代码是基于Python2的,所以第46行少了括号,如果你是Python3请注意补上括号。

这份代码可以直接使用, parser 里定义了5个参数:

–input输入要迁移的图像位置,

–model指要使用的模型,

–width/–height指的是迁移后的图像宽度和高度,

median_filter 是中值滤波器, 基本思想是用像素点邻域灰度值的中值来代替该像素点的灰度值,因此理论上数值越大,图像越平滑,输出的结果细节越好(不确定)。

median_filter 亲自试了一下,对结果的影响不大。

3.开始迁移

将上述代码保存到一个文件中,命名为1.py,在CMD/Terminal中带参数运行脚本即可迁移。如:

python 1.py --input 1.jpg --model udnie.t7

效果如下:

全部9种风格的迁移效果:

文章到此就结束啦,如果你喜欢今天的Python 教程,请持续关注Python实用宝典,如果对你有帮助,麻烦在下面点一个赞/在看哦有任何问题都可以在下方留言区留言,我们会耐心解答的!


​Python实用宝典 (pythondict.com)
不只是一个宝典
欢迎关注公众号:Python实用宝典

Python实用宝典
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。