-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtest_video.py
115 lines (90 loc) · 3.93 KB
/
test_video.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import argparse
from pathlib import Path
from tqdm import tqdm
import torch
import torch.nn as nn
import numpy as np
from PIL import Image
import cv2
import imageio
from torchvision import transforms
from torchvision.utils import save_image
import fast_stylenet
from torchvision.transforms.functional import adjust_contrast
def test_transform(width,height, crop):
transform_list = []
if width != 0:
transform_list.append(transforms.Resize((width,height)))
if crop:
transform_list.append(transforms.CenterCrop(size))
transform_list.append(transforms.ToTensor())
transform = transforms.Compose(transform_list)
return transform
parser = argparse.ArgumentParser()
# Basic options
parser.add_argument('--content_path', type=str,
help='File path to the content video')
parser.add_argument('--vgg', type=str, default='models/vgg_normalised.pth')
parser.add_argument('--decoder', type=str, default='./model_fast/clip_decoder_sketch_blackpencil.pth.tar')
# Additional options
parser.add_argument('--content_width', type=int, default=0,
help='New (minimum) width for the content image, \
keeping the original size if set to 0')
parser.add_argument('--content_height', type=int, default=0,
help='New (minimum) height for the content image, \
keeping the original size if set to 0')
parser.add_argument('--crop', action='store_true',
help='do center crop to create squared image')
parser.add_argument('--save_ext', default='.mp4',
help='The extension name of the output video')
parser.add_argument('--output', type=str, default='video_out',
help='Directory to save the output video')
args = parser.parse_args()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
output_dir = Path(args.output)
output_dir.mkdir(exist_ok = True, parents = True)
decoder_path = Path(args.decoder)
assert (args.content_path)
if args.content_path:
content_path = Path(args.content_path)
decoder = fast_stylenet.decoder
vgg = fast_stylenet.vgg
decoder.eval()
vgg.eval()
decoder.load_state_dict(torch.load(args.decoder))
vgg.load_state_dict(torch.load(args.vgg))
vgg = nn.Sequential(*list(vgg.children())[:31])
network = fast_stylenet.Net(vgg, decoder)
network.eval()
network.to(device)
content_tf = test_transform(args.content_width,args.content_height, args.crop)
#get video fps & video size
content_video = cv2.VideoCapture(args.content_path)
fps = int(content_video.get(cv2.CAP_PROP_FPS))
content_video_length = int(content_video.get(cv2.CAP_PROP_FRAME_COUNT))
output_width = int(content_video.get(cv2.CAP_PROP_FRAME_WIDTH))
output_height = int(content_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
assert fps != 0, 'Fps is zero, Please enter proper video path'
pbar = tqdm(total = content_video_length)
if content_path.suffix in [".mp4", ".avi", ".mpg"]:
output_video_path = output_name = output_dir / '{:s}_{:s}{:s}'.format(
content_path.stem, decoder_path.stem, args.save_ext)
writer = imageio.get_writer(output_video_path, mode='I', fps=fps)
while(True):
ret, content_img = content_video.read()
if not ret:
break
content = content_tf(Image.fromarray(content_img))
content = content.to(device).unsqueeze(0)
with torch.no_grad():
_,output = network(content)
output = adjust_contrast(output,1.5)
output = output.cpu()
output = output.squeeze(0)
output = np.array(output)*255
#output = np.uint8(output)
output = np.transpose(output, (1,2,0))
output = cv2.resize(output, (output_width, output_height), interpolation=cv2.INTER_CUBIC)
writer.append_data(np.array(output))
pbar.update(1)
content_video.release()