-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.py
54 lines (40 loc) · 1.39 KB
/
video.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
"""
Video related utility functions
"""
import numpy as np
import settings
import blosc
def write_blp(data, file_name):
file_name += '.blp'
print('Saving %s' % file_name)
bytes_array = data.tostring()
packed = blosc.compress(bytes_array)
with open(file_name, 'wb') as fd:
fd.write(packed)
def read_blp(file_name, shape):
with open(file_name, 'rb') as fd:
bytes_array = fd.read()
unpacked = blosc.decompress(bytes_array)
return np.frombuffer(unpacked, dtype=np.uint8).reshape((shape))
def write_data(vid, file_name):
write_blp(vid, file_name)
def clip(image, min_bound, max_bound):
image[image > max_bound] = max_bound
image[image < min_bound] = min_bound
def normalize(image, min_bound, max_bound):
image = np.float32(image)
image = (image - min_bound) / (max_bound - min_bound)
assert image[image > 1].sum() == 0
assert image[image < 0].sum() == 0
image *= 255
return np.uint8(np.rint(image))
def copy_to_frame(video, frame_shape):
in_shape = video.shape
out_shape = frame_shape
frame = np.zeros(out_shape, dtype=video.dtype)
for idx in range(3):
assert in_shape[idx] <= out_shape[idx]
starts = [(out_shape[i] - in_shape[i]) // 2 for i in range(3)]
ends = [starts[i] + in_shape[i] for i in range(3)]
frame[starts[0]:ends[0], starts[1]:ends[1], starts[2]:ends[2]] = video
return frame