-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoxtrain_dataset.py
63 lines (45 loc) · 1.52 KB
/
voxtrain_dataset.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
import os
import math
import numpy as np
import cv2
import glob
import torch
import torch.utils.data as data
class VoxCelebDataset(data.Dataset):
def __init__(self, root_dir, is_train):
self.root_dir = root_dir
if is_train:
self.videos = glob.glob(os.path.join(self.root_dir, 'train/*/*.png'))
else:
self.videos = glob.glob(os.path.join(self.root_dir, 'test/*/*.png'))
def load_img(self, image_path):
img = cv2.imread(image_path)
if img is None:
raise Exception('None Image')
img = cv2.resize(img, (512, 512))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img / 255
return img
def __len__(self):
return len(self.videos)
def __getitem__(self, idx):
image = self.load_img(self.videos[idx])
out = {}
out['image'] = image
out['path'] = os.path.join(*os.path.normpath(self.videos[idx]).split(os.sep)[-2:])
return out
class VideoDataset(data.Dataset):
def __init__(self, video_path):
self.capture = cv2.VideoCapture(video_path)
self.len = int(self.capture.get(cv2.CAP_PROP_FRAME_COUNT))
def __len__(self):
return self.len
def __getitem__(self, idx):
self.capture.set(cv2.CAP_PROP_POS_FRAMES, idx)
ret, frame = self.capture.read()
if not ret:
raise Exception('None Image')
data = {}
data['frame'] = frame[..., ::-1] / 255
data['id'] = idx
return data