Spaces:
Build error
Build error
Upload streamer.py
Browse files- lib/pymaf/utils/streamer.py +142 -0
lib/pymaf/utils/streamer.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import torch
|
3 |
+
import numpy as np
|
4 |
+
import imageio
|
5 |
+
|
6 |
+
|
7 |
+
def aug_matrix(w1, h1, w2, h2):
|
8 |
+
dx = (w2 - w1) / 2.0
|
9 |
+
dy = (h2 - h1) / 2.0
|
10 |
+
|
11 |
+
matrix_trans = np.array([[1.0, 0, dx],
|
12 |
+
[0, 1.0, dy],
|
13 |
+
[0, 0, 1.0]])
|
14 |
+
|
15 |
+
scale = np.min([float(w2)/w1, float(h2)/h1])
|
16 |
+
|
17 |
+
M = get_affine_matrix(
|
18 |
+
center=(w2 / 2.0, h2 / 2.0),
|
19 |
+
translate=(0, 0),
|
20 |
+
scale=scale)
|
21 |
+
|
22 |
+
M = np.array(M + [0., 0., 1.]).reshape(3, 3)
|
23 |
+
M = M.dot(matrix_trans)
|
24 |
+
|
25 |
+
return M
|
26 |
+
|
27 |
+
|
28 |
+
def get_affine_matrix(center, translate, scale):
|
29 |
+
cx, cy = center
|
30 |
+
tx, ty = translate
|
31 |
+
|
32 |
+
M = [1, 0, 0,
|
33 |
+
0, 1, 0]
|
34 |
+
M = [x * scale for x in M]
|
35 |
+
|
36 |
+
# Apply translation and of center translation: RSS * C^-1
|
37 |
+
M[2] += M[0] * (-cx) + M[1] * (-cy)
|
38 |
+
M[5] += M[3] * (-cx) + M[4] * (-cy)
|
39 |
+
|
40 |
+
# Apply center translation: T * C * RSS * C^-1
|
41 |
+
M[2] += cx + tx
|
42 |
+
M[5] += cy + ty
|
43 |
+
return M
|
44 |
+
|
45 |
+
|
46 |
+
class BaseStreamer():
|
47 |
+
"""This streamer will return images at 512x512 size.
|
48 |
+
"""
|
49 |
+
|
50 |
+
def __init__(self,
|
51 |
+
width=512, height=512, pad=True,
|
52 |
+
mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5),
|
53 |
+
**kwargs):
|
54 |
+
self.width = width
|
55 |
+
self.height = height
|
56 |
+
self.pad = pad
|
57 |
+
self.mean = np.array(mean)
|
58 |
+
self.std = np.array(std)
|
59 |
+
|
60 |
+
self.loader = self.create_loader()
|
61 |
+
|
62 |
+
def create_loader(self):
|
63 |
+
raise NotImplementedError
|
64 |
+
yield np.zeros((600, 400, 3)) # in RGB (0, 255)
|
65 |
+
|
66 |
+
def __getitem__(self, index):
|
67 |
+
image = next(self.loader)
|
68 |
+
in_height, in_width, _ = image.shape
|
69 |
+
M = aug_matrix(in_width, in_height, self.width, self.height, self.pad)
|
70 |
+
image = cv2.warpAffine(
|
71 |
+
image, M[0:2, :], (self.width, self.height), flags=cv2.INTER_CUBIC)
|
72 |
+
|
73 |
+
input = np.float32(image)
|
74 |
+
input = (input / 255.0 - self.mean) / self.std # TO [-1.0, 1.0]
|
75 |
+
input = input.transpose(2, 0, 1) # TO [3 x H x W]
|
76 |
+
return torch.from_numpy(input).float()
|
77 |
+
|
78 |
+
def __len__(self):
|
79 |
+
raise NotImplementedError
|
80 |
+
|
81 |
+
|
82 |
+
class CaptureStreamer(BaseStreamer):
|
83 |
+
"""This streamer takes webcam as input.
|
84 |
+
"""
|
85 |
+
|
86 |
+
def __init__(self, id=0, width=512, height=512, pad=True, **kwargs):
|
87 |
+
super().__init__(width, height, pad, **kwargs)
|
88 |
+
self.capture = cv2.VideoCapture(id)
|
89 |
+
|
90 |
+
def create_loader(self):
|
91 |
+
while True:
|
92 |
+
_, image = self.capture.read()
|
93 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # RGB
|
94 |
+
yield image
|
95 |
+
|
96 |
+
def __len__(self):
|
97 |
+
return 100_000_000
|
98 |
+
|
99 |
+
def __del__(self):
|
100 |
+
self.capture.release()
|
101 |
+
|
102 |
+
|
103 |
+
class VideoListStreamer(BaseStreamer):
|
104 |
+
"""This streamer takes a list of video files as input.
|
105 |
+
"""
|
106 |
+
|
107 |
+
def __init__(self, files, width=512, height=512, pad=True, **kwargs):
|
108 |
+
super().__init__(width, height, pad, **kwargs)
|
109 |
+
self.files = files
|
110 |
+
self.captures = [imageio.get_reader(f) for f in files]
|
111 |
+
self.nframes = sum([int(cap._meta["fps"] * cap._meta["duration"])
|
112 |
+
for cap in self.captures])
|
113 |
+
|
114 |
+
def create_loader(self):
|
115 |
+
for capture in self.captures:
|
116 |
+
for image in capture: # RGB
|
117 |
+
yield image
|
118 |
+
|
119 |
+
def __len__(self):
|
120 |
+
return self.nframes
|
121 |
+
|
122 |
+
def __del__(self):
|
123 |
+
for capture in self.captures:
|
124 |
+
capture.close()
|
125 |
+
|
126 |
+
|
127 |
+
class ImageListStreamer(BaseStreamer):
|
128 |
+
"""This streamer takes a list of image files as input.
|
129 |
+
"""
|
130 |
+
|
131 |
+
def __init__(self, files, width=512, height=512, pad=True, **kwargs):
|
132 |
+
super().__init__(width, height, pad, **kwargs)
|
133 |
+
self.files = files
|
134 |
+
|
135 |
+
def create_loader(self):
|
136 |
+
for f in self.files:
|
137 |
+
image = cv2.imread(f, cv2.IMREAD_UNCHANGED)[:, :, 0:3]
|
138 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # RGB
|
139 |
+
yield image
|
140 |
+
|
141 |
+
def __len__(self):
|
142 |
+
return len(self.files)
|