-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
169 lines (125 loc) · 6.02 KB
/
Copy pathdata_loader.py
File metadata and controls
169 lines (125 loc) · 6.02 KB
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
# -*- coding: utf-8
import os
import pandas as pd
from PIL import Image
from torchvision import transforms, models
import torch
from torch.utils import data
import cv2
import numpy as np
import random
import json
from torchvision import transforms
class VideoDataset_NR(data.Dataset):
"""Read data from the original dataset for feature extraction"""
def __init__(self, data_dir, json_path, transform, size, is_train):
super(VideoDataset_NR, self).__init__()
with open(json_path, 'r') as f:
mos_file_content = json.loads(f.read())
if is_train:
self.video_names = mos_file_content['train']['dis']
self.score = mos_file_content['train']['mos']
else:
self.video_names = mos_file_content['test']['dis']
self.score = mos_file_content['test']['mos']
self.videos_dir = data_dir
self.transform = transform
self.size = size
self.length = len(self.video_names)
def __len__(self):
return self.length
def __getitem__(self, idx):
video_name = self.video_names[idx]
video_score = torch.FloatTensor(np.array(float(self.score[idx])))
filename=os.path.join(self.videos_dir, video_name.replace('.yuv', '.mp4'))
video_capture = cv2.VideoCapture()
video_capture.open(filename)
cap=cv2.VideoCapture(filename)
video_channel = 3
video_height_crop = self.size
video_width_crop = self.size
# video_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# video_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
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*2/video_frame_rate)
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*2) % video_frame_rate == 0):
read_frame = Image.fromarray(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB))
read_frame = self.transform(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()
return transformed_video, video_score, video_name
class VideoDataset_FR(data.Dataset):
"""Read data from the original dataset for feature extraction"""
def __init__(self, data_dir, json_path, transform, size ,is_train):
super(VideoDataset_FR, self).__init__()
with open(json_path, 'r') as f:
mos_file_content = json.loads(f.read())
if is_train:
self.video_names_ref = mos_file_content['train']['ref']
self.video_names_dis = mos_file_content['train']['dis']
self.score = mos_file_content['train']['mos']
else:
self.video_names_ref = mos_file_content['test']['ref']
self.video_names_dis = mos_file_content['test']['dis']
self.score = mos_file_content['test']['mos']
self.videos_dir = data_dir
self.transform = transform
self.size = size
self.length = len(self.score)
def __len__(self):
return self.length
def __getitem__(self, idx):
video = {}
video_type = ['ref', 'dis']
video_score = torch.FloatTensor(np.array(float(self.score[idx])))
for i_type in video_type:
if i_type == 'ref':
video_name = self.video_names_ref[idx]
else:
video_name = self.video_names_dis[idx]
video_name_dis = video_name
video_score = torch.FloatTensor(np.array(float(self.score[idx])))
filename=os.path.join(self.videos_dir, video_name.replace('.yuv', '.mp4'))
video_capture = cv2.VideoCapture()
video_capture.open(filename)
cap=cv2.VideoCapture(filename)
video_channel = 3
video_height_crop = self.size
video_width_crop = self.size
# video_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# video_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
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*2/video_frame_rate)
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*2) % video_frame_rate == 0):
read_frame = Image.fromarray(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB))
read_frame = self.transform(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[i_type] = transformed_video
return video['ref'], video['dis'], video_score, video_name_dis