This repository was archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloaders.py
More file actions
executable file
·106 lines (90 loc) · 3.1 KB
/
Copy pathloaders.py
File metadata and controls
executable file
·106 lines (90 loc) · 3.1 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
from constants import defaultkey
from PIL import Image
import os
import cv2
import numpy as np
def get_loader(path, typeloader, interval):
# check if path exists
if not os.path.exists(path):
raise ValueError("path: {} does not exist".format(path))
contents = os.listdir(path)
# check if path is empty
if len(contents) == 0:
raise ValueError("nothing in {}".format(path))
if typeloader == "videos":
contents = [
name for name in contents
if not os.path.isdir(os.path.join(path, name))
]
return VideoLoader(path, contents, interval=interval)
elif typeloader == "frames":
contents = [
name for name in contents
if os.path.isdir(os.path.join(path, name))
]
return FrameLoader(path, contents, interval=interval)
class Loader:
def __init__(self):
pass
def get_vid_names(self):
return self.videos.keys()
class VideoLoader(Loader):
def __init__(self, path, vids, interval=1):
super().__init__()
self.path = path
names = [os.path.splitext(key)[0] for key in vids]
self.videos = {
name: cv2.VideoCapture(os.path.join(path, file))
for name, file in zip(names, vids)
}
self.index = 0
self.length = min([
vid.get(cv2.CAP_PROP_FRAME_COUNT)
for name, vid in self.videos.items()
])
self.interval = interval
def __iter__(self):
self.index = 0
return self
def __next__(self):
retval = dict()
for name, vid in self.videos.items():
vid.set(cv2.CAP_PROP_POS_FRAMES, self.index)
success, retval[name] = vid.read()
if not success:
raise StopIteration
indtosend = self.index
self.index = self.index + self.interval
return indtosend, retval
def __len__(self):
return int(self.length / self.interval)
class FrameLoader(Loader):
def __init__(self, path, dirs, interval=1):
super().__init__()
self.path = path
# dirs = [os.path.join(path, name) for name in dirs]
if not isinstance(dirs, list):
raise TypeError("dirs must be a list of directorys")
self.videos = {name: sorted(os.listdir(os.path.join(path, name))) for name in dirs}
self.index = 0
self.length = min([
len(vid)
for name, vid in self.videos.items()
])
self.interval = interval
def __iter__(self):
self.index = 0
return self
def __next__(self):
if self.index >= self.length:
raise StopIteration
retval = dict()
for name, imgs in self.videos.items():
pilimg = Image.open(os.path.join(self.path, name, imgs[self.index]))
# convert to cv2 numpy format
retval[name] = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)
indtosend = self.index
self.index = self.index + self.interval
return indtosend, retval
def __len__(self):
return int(self.length / self.interval)