-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_NR_demo.py
253 lines (168 loc) · 7.84 KB
/
test_NR_demo.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# -*- coding: utf-8 -*-
import argparse
import os
import numpy as np
import torch
import torch.nn
import UGCVQA_NR_model
import cv2
from PIL import Image
from torchvision import transforms
def video_processing(dist):
video_name = dist
video_name_dis = video_name
video_capture = cv2.VideoCapture()
video_capture.open(video_name)
cap=cv2.VideoCapture(video_name)
video_channel = 3
video_height_crop = 448
video_width_crop = 448
video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
video_frame_rate = int(round(cap.get(cv2.CAP_PROP_FPS)))
video_length_read = int(video_length/video_frame_rate)
transformations = transforms.Compose([transforms.Resize(520),transforms.CenterCrop(448),transforms.ToTensor(),\
transforms.Normalize(mean = [0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225])])
transformed_video = torch.zeros([video_length_read, video_channel, video_height_crop, video_width_crop])
video_read_index = 0
frame_idx = 0
for i in range(video_length):
has_frames, frame = video_capture.read()
if has_frames:
# key frame
if (video_read_index < video_length_read) and (frame_idx % video_frame_rate == 0):
read_frame = Image.fromarray(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB))
read_frame = transformations(read_frame)
transformed_video[video_read_index] = read_frame
video_read_index += 1
frame_idx += 1
if video_read_index < video_length_read:
for i in range(video_read_index, video_length_read):
transformed_video[i] = transformed_video[video_read_index - 1]
video_capture.release()
video = transformed_video
return video, video_name_dis
def video_processing_multi_scale(dist):
video_name = dist
video_name_dis = video_name
video_capture = cv2.VideoCapture()
video_capture.open(video_name)
cap=cv2.VideoCapture(video_name)
video_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
video_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
if video_height > video_width:
video_width_resize = 540
video_height_resize = int(video_width_resize/video_width*video_height)
else:
video_height_resize = 540
video_width_resize = int(video_height_resize/video_height*video_width)
dim1 = (video_height_resize, video_width_resize)
if video_height > video_width:
video_width_resize = 720
video_height_resize = int(video_width_resize/video_width*video_height)
else:
video_height_resize = 720
video_width_resize = int(video_height_resize/video_height*video_width)
dim2 = (video_height_resize, video_width_resize)
if video_height > video_width:
video_width_resize = 1080
video_height_resize = int(video_width_resize/video_width*video_height)
else:
video_height_resize = 1080
video_width_resize = int(video_height_resize/video_height*video_width)
dim3 = (video_height_resize, video_width_resize)
video_channel = 3
video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
video_frame_rate = int(round(cap.get(cv2.CAP_PROP_FPS)))
video_length_read = int(video_length/video_frame_rate)
transformed_video1 = torch.zeros([video_length_read, video_channel, dim1[0], dim1[1]])
transformed_video2 = torch.zeros([video_length_read, video_channel, dim2[0], dim2[1]])
transformed_video3 = torch.zeros([video_length_read, video_channel, dim3[0], dim3[1]])
transformations1 = transforms.Compose([transforms.Resize(dim1), transforms.ToTensor(),\
transforms.Normalize(mean = [0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225])])
transformations2 = transforms.Compose([transforms.Resize(dim2), transforms.ToTensor(),\
transforms.Normalize(mean = [0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225])])
transformations3 = transforms.Compose([transforms.Resize(dim3), transforms.ToTensor(),\
transforms.Normalize(mean = [0.485, 0.456, 0.406], std = [0.229, 0.224, 0.225])])
video_read_index = 0
frame_idx = 0
for i in range(video_length):
has_frames, frame = video_capture.read()
if has_frames:
# key frame
if (video_read_index < video_length_read) and (frame_idx % video_frame_rate == 0):
read_frame = Image.fromarray(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB))
read_frame1 = transformations1(read_frame)
transformed_video1[video_read_index] = read_frame1
read_frame2 = transformations2(read_frame)
transformed_video2[video_read_index] = read_frame2
read_frame3 = transformations3(read_frame)
transformed_video3[video_read_index] = read_frame3
video_read_index += 1
frame_idx += 1
if video_read_index < video_length_read:
for i in range(video_read_index, video_length_read):
transformed_video1[i] = transformed_video1[video_read_index - 1]
transformed_video2[i] = transformed_video2[video_read_index - 1]
transformed_video3[i] = transformed_video3[video_read_index - 1]
video_capture.release()
video1= transformed_video1
video2= transformed_video2
video3 = transformed_video3
return video1, video2, video3, video_name_dis
def main(config):
device = torch.device('cuda' if config.is_gpu else 'cpu')
print('using ' + str(device))
model = UGCVQA_NR_model.resnet50(pretrained=True)
# model = torch.nn.DataParallel(model)
model = model.to(device=device)
model.load_state_dict(torch.load('ckpts/UGCVQA_NR_model.pth'))
if config.method_name == 'single-scale':
video_dist, video_name = video_processing(config.dist)
with torch.no_grad():
model.eval()
video_dist = video_dist.to(device)
video_dist = video_dist.unsqueeze(dim=0)
outputs = model(video_dist)
y_val = outputs.item()
print('The video name: ' + video_name)
print('The quality socre: {:.4f}'.format(y_val))
if config.method_name == 'multi-scale':
video_dist1, video_dist2, video_dist3, video_name = video_processing_multi_scale(config.dist)
with torch.no_grad():
model.eval()
video_dist1 = video_dist1.to(device)
video_dist1 = video_dist1.unsqueeze(dim=0)
outputs1 = model(video_dist1)
y_val1 = outputs1.item()
video_dist2 = video_dist2.to(device)
video_dist2 = video_dist2.unsqueeze(dim=0)
outputs2 = model(video_dist2)
y_val2 = outputs2.item()
video_dist3 = video_dist3.to(device)
video_dist3 = video_dist3.unsqueeze(dim=0)
outputs3 = model(video_dist3)
y_val3 = outputs3.item()
w1_csf = 0.8317
w2_csf = 0.0939
w3_csf = 0.0745
y_val = pow(y_val1, w1_csf) * pow(y_val2, w2_csf) * pow(y_val3, w3_csf)
print('The video name: ' + video_name)
print('The quality socre: {:.4f}'.format(y_val))
output_name = config.output
if not os.path.exists(output_name):
os.system(r"touch {}".format(output_name))
f = open(output_name,'w')
f.write(video_name)
f.write(',')
f.write(str(y_val))
f.write('\n')
f.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# input parameters
parser.add_argument('--method_name', type=str, default='single-scale')
parser.add_argument('--dist', type=str, default='videos/UGC0034_1280x720_30_crf_22.mp4')
parser.add_argument('--output', type=str, default='output')
parser.add_argument('--is_gpu', action='store_true')
config = parser.parse_args()
main(config)