zoukankan      html  css  js  c++  java
  • 【人工智能】python图片风格迁移,来欣赏梵高风格的石原里美吧!

    图像的风格迁移,心心念念好久了啊!

    简单几个步骤,就可以转换图片风格啦。

    1. 在github下载模型,模型后缀名是 t7,新建 model 文件夹用来存放模型

    2. 新建 fengge.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)
    

    3. 打开 cmd ,cd 到目标文件夹

    4. 准备好图片 1.jpg ,在 cmd 中输入命令

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

    用石原里美的图片来做效果展示:

    这张就是梵高风格的石原里美啦 !

    当然,自己训练一个模型也是没有问题的。

    如果有看到其他好看的模型,欢迎评论留言哦!

  • 相关阅读:
    leetcode Simplify Path
    leetcode Evaluate Reverse Polish Notation
    leetcode Swap Nodes in Pairs
    leetcode MinStack
    leetcode length of the last word
    empty能否代替isset?
    thinkphp框架的路径
    PHP 反射类的简单使用!
    在windows下配置redis扩展
    phpmyadmin的windows下和linux下的安装。
  • 原文地址:https://www.cnblogs.com/helenlee01/p/12707287.html
Copyright © 2011-2022 走看看