1212import re
1313import struct
1414import copy
15- import cStringIO as sio
1615import numpy as np
1716import warnings
1817import lzf
2423except ImportError :
2524 HAS_SENSOR_MSGS = False
2625
26+ try :
27+ from io import StringIO as sio
28+ except :
29+ import cStringIO as sio
30+
2731__all__ = ['PointCloud' ,
2832 'point_cloud_to_path' ,
2933 'point_cloud_to_buffer' ,
@@ -91,11 +95,11 @@ def parse_header(lines):
9195 elif key in ('fields' , 'type' ):
9296 metadata [key ] = value .split ()
9397 elif key in ('size' , 'count' ):
94- metadata [key ] = map (int , value .split ())
98+ metadata [key ] = list ( map (int , value .split () ))
9599 elif key in ('width' , 'height' , 'points' ):
96100 metadata [key ] = int (value )
97101 elif key == 'viewpoint' :
98- metadata [key ] = map (float , value .split ())
102+ metadata [key ] = list ( map (float , value .split () ))
99103 elif key == 'data' :
100104 metadata [key ] = value .strip ().lower ()
101105 # TODO apparently count is not required?
@@ -205,9 +209,9 @@ def _build_dtype(metadata):
205209 fieldnames .append (f )
206210 typenames .append (np_type )
207211 else :
208- fieldnames .extend (['%s_%04d' % (f , i ) for i in xrange (c )])
212+ fieldnames .extend (['%s_%04d' % (f , i ) for i in range (c )])
209213 typenames .extend ([np_type ]* c )
210- dtype = np .dtype (zip (fieldnames , typenames ))
214+ dtype = np .dtype (list ( zip (fieldnames , typenames ) ))
211215 return dtype
212216
213217
@@ -278,6 +282,8 @@ def point_cloud_from_fileobj(f):
278282 header = []
279283 while True :
280284 ln = f .readline ().strip ()
285+ if not isinstance (ln , str ):
286+ ln = ln .decode ('utf-8' )
281287 header .append (ln )
282288 if ln .startswith ('DATA' ):
283289 metadata = parse_header (header )
@@ -320,13 +326,13 @@ def point_cloud_to_fileobj(pc, fileobj, data_compression=None):
320326 assert (data_compression in ('ascii' , 'binary' , 'binary_compressed' ))
321327 metadata ['data' ] = data_compression
322328
323- header = write_header (metadata )
329+ header = write_header (metadata ). encode ( 'utf-8' )
324330 fileobj .write (header )
325331 if metadata ['data' ].lower () == 'ascii' :
326332 fmtstr = build_ascii_fmtstr (pc )
327333 np .savetxt (fileobj , pc .pc_data , fmt = fmtstr )
328334 elif metadata ['data' ].lower () == 'binary' :
329- fileobj .write (pc .pc_data .tostring ('C' ))
335+ fileobj .write (pc .pc_data .tostring ())
330336 elif metadata ['data' ].lower () == 'binary_compressed' :
331337 # TODO
332338 # a '_' field is ignored by pcl and breakes compressed point clouds.
@@ -335,9 +341,9 @@ def point_cloud_to_fileobj(pc, fileobj, data_compression=None):
335341 # reorder to column-by-column
336342 uncompressed_lst = []
337343 for fieldname in pc .pc_data .dtype .names :
338- column = np .ascontiguousarray (pc .pc_data [fieldname ]).tostring ('C' )
344+ column = np .ascontiguousarray (pc .pc_data [fieldname ]).tostring ()
339345 uncompressed_lst .append (column )
340- uncompressed = '' .join (uncompressed_lst )
346+ uncompressed = b '' .join (uncompressed_lst )
341347 uncompressed_size = len (uncompressed )
342348 # print("uncompressed_size = %r"%(uncompressed_size))
343349 buf = lzf .compress (uncompressed )
@@ -357,7 +363,7 @@ def point_cloud_to_fileobj(pc, fileobj, data_compression=None):
357363
358364
359365def point_cloud_to_path (pc , fname ):
360- with open (fname , 'w ' ) as f :
366+ with open (fname , 'wb ' ) as f :
361367 point_cloud_to_fileobj (pc , f )
362368
363369
@@ -370,21 +376,21 @@ def point_cloud_to_buffer(pc, data_compression=None):
370376def save_point_cloud (pc , fname ):
371377 """ Save pointcloud to fname in ascii format.
372378 """
373- with open (fname , 'w ' ) as f :
379+ with open (fname , 'wb ' ) as f :
374380 point_cloud_to_fileobj (pc , f , 'ascii' )
375381
376382
377383def save_point_cloud_bin (pc , fname ):
378384 """ Save pointcloud to fname in binary format.
379385 """
380- with open (fname , 'w ' ) as f :
386+ with open (fname , 'wb ' ) as f :
381387 point_cloud_to_fileobj (pc , f , 'binary' )
382388
383389
384390def save_point_cloud_bin_compressed (pc , fname ):
385391 """ Save pointcloud to fname in binary compressed format.
386392 """
387- with open (fname , 'w ' ) as f :
393+ with open (fname , 'wb ' ) as f :
388394 point_cloud_to_fileobj (pc , f , 'binary_compressed' )
389395
390396
@@ -481,7 +487,7 @@ def add_fields(pc, metadata, pc_data):
481487 else :
482488 fieldnames .extend (['%s_%04d' % (f , i ) for i in xrange (c )])
483489 typenames .extend ([np_type ]* c )
484- dtype = zip (fieldnames , typenames )
490+ dtype = list ( zip (fieldnames , typenames ) )
485491 # new dtype. could be inferred?
486492 new_dtype = [(f , pc .pc_data .dtype [f ])
487493 for f in pc .pc_data .dtype .names ] + dtype
@@ -693,7 +699,7 @@ def save_pcd(self, fname, compression=None, **kwargs):
693699 warnings .warn ('data_compression keyword is deprecated for'
694700 ' compression' )
695701 compression = kwargs ['data_compression' ]
696- with open (fname , 'w ' ) as f :
702+ with open (fname , 'wb ' ) as f :
697703 point_cloud_to_fileobj (self , f , compression )
698704
699705 def save_pcd_to_fileobj (self , fileobj , compression = None , ** kwargs ):
0 commit comments