-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathread.py
78 lines (55 loc) · 2.45 KB
/
read.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
import sys
import numpy as np
def read_gvr_groundtruth(path):
with open(path, "rb") as fid:
version = np.fromfile(fid, count=1, dtype=np.uint8)
assert version == 1
is_big_endian = np.fromfile(fid, count=1, dtype=np.uint8)
assert (is_big_endian == 1 and sys.byteorder == "big") or \
(is_big_endian == 0 and sys.byteorder == "little")
uint_size = np.fromfile(fid, count=1, dtype=np.uint8)
assert uint_size == 4
elem_size = np.fromfile(fid, count=1, dtype=np.uint32)
if elem_size == 4:
dtype = np.float32
elif elem_size == 8:
dtype = np.float64
else:
raise ValueError("Unsupported data type")
num_labels = np.fromfile(fid, count=1, dtype=np.uint32)[0]
height = np.fromfile(fid, count=1, dtype=np.uint32)[0]
width = np.fromfile(fid, count=1, dtype=np.uint32)[0]
depth = np.fromfile(fid, count=1, dtype=np.uint32)[0]
num_elems = width * height * depth * num_labels
assert num_elems > 0
grid = np.fromfile(fid, count=num_elems, dtype=dtype)
grid = grid.reshape(num_labels, height, width, depth)
grid = grid.transpose(1, 2, 3, 0)
return grid
def read_gvr_datacost(path, num_labels):
with open(path, "rb") as fid:
version = np.fromfile(fid, count=1, dtype=np.uint8)
assert version == 1
is_big_endian = np.fromfile(fid, count=1, dtype=np.uint8)
assert (is_big_endian == 1 and sys.byteorder == "big") or \
(is_big_endian == 0 and sys.byteorder == "little")
uint_size = np.fromfile(fid, count=1, dtype=np.uint8)
assert uint_size == 4
elem_size = np.fromfile(fid, count=1, dtype=np.uint32)
if elem_size == 4:
dtype = np.float32
elif elem_size == 8:
dtype = np.float64
else:
raise ValueError("Unsupported data type")
height = np.fromfile(fid, count=1, dtype=np.uint32)[0]
width = np.fromfile(fid, count=1, dtype=np.uint32)[0]
depth = np.fromfile(fid, count=1, dtype=np.uint32)[0]
assert (height % num_labels) == 0
height //= num_labels
num_elems = width * height * depth * num_labels
assert num_elems > 0
grid = np.fromfile(fid, count=num_elems, dtype=dtype)
grid = grid.reshape(num_labels, height, width, depth)
grid = grid.transpose(1, 2, 3, 0)
return grid