Python image style migration is a mature area, and real-time style migration is now not a problem. I’ve been thinking of an article like this for a while, but unfortunately most open source projects like Luanfujun/deep-photo-StyleTransfer require CUDA, PyTorch, CUDNN, etc. It took a day to configure.
But now we have a great open source application project, which is OpenCV’s DNN image style transfer. You just need to install OpenCV to use it, type in CMD /terminal (if you haven’t already installed Python, see this article :Python Installation) :
pip install python-opencv
Copy the code
However, it also has limitations. We can only use the model trained by others for styletransfer. If we want to customize the style, we must configure cudn and use deep-photo-styletransfer and other project methods for training. In today’s tutorial we will use the **fast-neural-style** trained model to do a style transfer for the following images.
Meow meow meow
1. Select the model
** Fast-neural-style ** There are 9 types of model styles released, and we will try one by one. Some of the styles are as follows:
The model file can be downloaded from the following public account Python utility, which contains all 10 model style resources.
2. Clone OpenCV source code
We directly clone the Python DNN image migration example from the OpenCV open source project.
Github.com/opencv/open…
The code is as follows:
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 elseFrame. 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], Shape out[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() / 1000print(t / freq, 'ms')
if args.median_filter:
out = cv.medianBlur(out, args.median_filter)
cv.imshow('Styled image', out)
Copy the code
Note that the source code is Python2 based, so the parentheses are missing on line 46. Fill in the parentheses if you are Python3. The code can be used directly. Parser defines 5 parameters: –input input the location of the image to be migrated, –model indicates the model to be used, –width/–height indicates the width and height of the image after migration, and median_filter is the median filter. The basic idea is to replace the gray value of the pixel with the median gray value of the pixel’s neighborhood. Therefore, in theory, the larger the gray value is, the smoother the image and the better the details of the output result (uncertain). Median_filter tried it out for itself, and it didn’t make much difference.
3. Start migration
Save the above code ina file named 1.py and run the script in CMD/Terminal with parameters to migrate. Such as:
$ python 1.py --input 1.jpg --model udnie.t7
Copy the code
The effect is as follows:
Migration effects for all 9 styles:
That’s the end of this article. If you enjoyed today’s Python tutorial, please keep checking out the Python Utility Guide. If it was helpful, please give it a thumbs up/check it out below
Python Dict.com Is more than a dictatorial model
Python migrates 9 image styles