3
3
"""
4
4
5
5
# Needed when SLAM is disabled (no final map flag to indicate end of data)
6
- EXIT_AFTER_NO_DATA_FOR_N_SECONDS = 5
6
+ HEADER_TIMEOUT = 5
7
+ HEADER_SIZE = 16
7
8
8
9
import struct
9
10
import json
10
11
import numpy as np
11
12
import spectacularAI
12
13
import time
13
14
15
+ def read_bytes (in_stream , n , timeout = 0 ):
16
+ dt = 0
17
+ result = b"" # Initialize an empty bytes object
18
+ while n > 0 :
19
+ chunk = in_stream .read (n )
20
+ if len (chunk ) > 0 :
21
+ result += chunk
22
+ n -= len (chunk )
23
+ else :
24
+ time .sleep (0.01 )
25
+ dt += 0.01
26
+ if timeout > 0 and dt > timeout : return False
27
+ return result
28
+
14
29
def input_stream_reader (in_stream ):
15
30
MAGIC_BYTES = 2727221974
16
31
shouldQuit = False
17
- exitCounter = 0
18
32
19
33
while not shouldQuit :
20
- messageHeader = in_stream .read (16 )
21
-
22
- if len (messageHeader ) == 0 :
23
- time .sleep (0.01 )
24
- exitCounter += 0.01
25
- shouldQuit = exitCounter >= EXIT_AFTER_NO_DATA_FOR_N_SECONDS
26
- continue
27
- exitCounter = 0
34
+ messageHeader = read_bytes (in_stream , HEADER_SIZE , HEADER_TIMEOUT )
35
+ if messageHeader is False : break
28
36
29
37
magicBytes , messageId , jsonSize , binarySize = struct .unpack ('@4I' , messageHeader )
30
38
if magicBytes != MAGIC_BYTES :
31
39
raise Exception (f"Wrong magic bytes! Expected { MAGIC_BYTES } and received { magicBytes } " )
32
- json_output = json .loads (in_stream . read ( jsonSize ).decode ('ascii' ))
40
+ json_output = json .loads (read_bytes ( in_stream , jsonSize ).decode ('ascii' ))
33
41
34
42
if 'cameraPoses' in json_output : # Vio output
35
43
assert (binarySize == 0 )
@@ -41,13 +49,13 @@ def input_stream_reader(in_stream):
41
49
if "pointCloud" in keyFrame :
42
50
pointCloud = keyFrame ["pointCloud" ]
43
51
points = pointCloud ["size" ]
44
- pointCloud ["positionData" ] = np .frombuffer (in_stream . read ( points * 4 * 3 ), dtype = np .float32 )
52
+ pointCloud ["positionData" ] = np .frombuffer (read_bytes ( in_stream , points * 4 * 3 ), dtype = np .float32 )
45
53
pointCloud ["positionData" ].shape = (points , 3 )
46
54
if pointCloud ["hasNormals" ]:
47
- pointCloud ["normalData" ] = np .frombuffer (in_stream . read ( points * 4 * 3 ), dtype = np .float32 )
55
+ pointCloud ["normalData" ] = np .frombuffer (read_bytes ( in_stream , points * 4 * 3 ), dtype = np .float32 )
48
56
pointCloud ["normalData" ].shape = (points , 3 )
49
57
if pointCloud ["hasColors" ]:
50
- pointCloud ["rgb24Data" ] = np .frombuffer (in_stream . read ( points * 3 ), dtype = np .ubyte )
58
+ pointCloud ["rgb24Data" ] = np .frombuffer (read_bytes ( in_stream , points * 3 ), dtype = np .ubyte )
51
59
pointCloud ["rgb24Data" ].shape = (points , 3 )
52
60
yield json_output
53
61
0 commit comments