-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpyramid_b_decoding.py
218 lines (128 loc) · 6.07 KB
/
pyramid_b_decoding.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
'''
MIT License
Copyright (c) [2020] [Duin BAEK]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
import numpy as np
import cv2
import os
def rotate_img(img, iteration = 1):
#0: remain same, +n: rotate left in 90*n degree, -n: rotate right in 90*n degree (some part of img is sliced off)
if len(img.shape) == 3:
height, width, _ = img.shape
else:
height, width = img.shape
rotated_img = np.zeros(img.shape, dtype = np.uint8)
if iteration == -1:
for idx in range(height):
rotated_img[:, height-1-idx] = img[idx, :]
elif iteration == 1:
for idx in range(height):
rotated_img[:, idx] = np.flip(img[idx, :], axis = 0)
elif iteration == 0:
rotated_img = img
else:
for idx in range(height):
rotated_img[height-1-idx, :] = np.flip(img[idx, :], axis = 0)
return rotated_img
def trapezoid_1_decoding(img): # starting from small frame / 2 + 2
if len(img.shape) == 3:
row, col, channel = img.shape
else:
row, col = img.shape
channel = 1
empty_pallete = np.zeros((row, col, channel), dtype = np.uint8)
for idx in range(int(col / 4)):
valid_layer = img[idx:row-idx, idx, :]
valid_row, valid_col = valid_layer.shape
layer = cv2.resize(valid_layer.reshape(valid_row, -1, channel), (4, row))
empty_pallete[:, idx*4:(idx+1)*4] = layer
return empty_pallete #shape of row, col, channel (1024, 1024, 3)
def trapezoid_2_decoding(img): # starting from small frame / 2
if len(img.shape) == 3:
row, col, channel = img.shape
else:
row, col = img.shape
channel = 1
empty_pallete = np.zeros((row, col, channel), dtype = np.uint8)
for idx in range(int(row / 4)):
valid_layer = img[255-idx, idx+1:col-1-idx, :]
valid_row, valid_col = valid_layer.shape
layer = cv2.resize(valid_layer.reshape(-1, valid_row, channel), (col, 4))
empty_pallete[(255-idx)*4: (255-idx+1)*4, :] = layer
return empty_pallete #shape of row, col, channel (1024, 1024, 3)
def pyramid_b_decoding(img):
row, col, channel = img.shape
front_img = img[:, :int(col/2)]
pyramid_img = img[:, int(col/2):]
height, width, channel = pyramid_img.shape
back_img = pyramid_img[int(height/4):int(height/4)+int(height/2), int(width/4):int(width/4)+int(width/2)]
back_img = cv2.resize(back_img, None, fx = 2, fy = 2)
right_img = pyramid_img[:, :int(width/4), :]
right_pallete = np.zeros(pyramid_img.shape, dtype = np.uint8)
right_pallete[:, :int(width/4), :] = right_img
right_img = trapezoid_1_decoding(right_pallete)
left_img = cv2.flip(pyramid_img[:, -int(width/4):, :], 1)
left_pallete = np.zeros(pyramid_img.shape, dtype = np.uint8)
left_pallete[:, :int(width/4), :] = left_img
left_img = trapezoid_1_decoding(left_pallete)
left_img = cv2.flip(left_img, 1)
up_img = pyramid_img[:int(height/4), :, :]
up_pallete = np.zeros(pyramid_img.shape, dtype = np.uint8)
up_pallete[-int(height/4):, :, :] = up_img
up_pallete = rotate_img(up_pallete, iteration = 2)
up_img = trapezoid_2_decoding(up_pallete)
down_img = pyramid_img[-int(height/4):, :, :]
down_pallete = np.zeros(pyramid_img.shape, dtype = np.uint8)
down_pallete[:int(height/4), :, :] = down_img
down_img = trapezoid_2_decoding(down_pallete)
down_img = rotate_img(down_img, 2)
first_layer = np.concatenate([right_img, left_img, up_img], axis = 1)
second_layer = np.concatenate([down_img, front_img, back_img], axis = 1)
output = np.concatenate([first_layer, second_layer], axis = 0)
return output
video_path = 'video/segments/pyra'
decoded_path = 'video/segments/pyra_decoded'
fourcc = cv2.VideoWriter_fourcc(*'X264')
for y_p_combo in os.listdir(video_path):
y_p_combo_path = os.path.join(video_path, y_p_combo)
y_p_write_path = os.path.join(decoded_path, y_p_combo)
for duration in os.listdir(y_p_combo_path):
duration_path = os.path.join(y_p_combo_path, duration)
duration_write_path = os.path.join(y_p_write_path, duration)
if not os.path.exists(duration_write_path):
os.makedirs(duration_write_path)
for file_name in os.listdir(duration_path):
file_path = os.path.join(duration_path, file_name)
file_write_path = os.path.join(duration_write_path, file_name)
capture = cv2.VideoCapture(file_path)
fps = round(capture.get(cv2.CAP_PROP_FPS))
frame_idx = 0
while(capture.isOpened()):
retval, frame = capture.read()
if retval == True:
pyramid_b_cube = pyramid_b_decoding(frame)
if frame_idx == 0:
frame_height, frame_width, _ = pyramid_b_cube.shape
writer = cv2.VideoWriter(file_write_path, fourcc, fps, (frame_width, frame_height))
writer.write(pyramid_b_cube)
frame_idx += 1
else:
break
capture.release()
writer.release()
#end