Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Error 'TypeError: sequence item 0: expected str instance, bytes found' #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions pypcd/pypcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ def parse_header(lines):
elif key in ('fields', 'type'):
metadata[key] = value.split()
elif key in ('size', 'count'):
metadata[key] = map(int, value.split())
metadata[key] = list(map(int, value.split()))
elif key in ('width', 'height', 'points'):
metadata[key] = int(value)
elif key == 'viewpoint':
metadata[key] = map(float, value.split())
metadata[key] = list(map(float, value.split()))
elif key == 'data':
metadata[key] = value.strip().lower()
# TODO apparently count is not required?
Expand Down Expand Up @@ -162,9 +162,9 @@ def _metadata_is_consistent(metadata):
checks.append((lambda m: len(m['type']) == len(m['count']) ==
len(m['fields']),
'length of type, count and fields must be equal'))
checks.append((lambda m: m['height'] > 0,
checks.append((lambda m: m['height'] >= 0,
'height must be greater than 0'))
checks.append((lambda m: m['width'] > 0,
checks.append((lambda m: m['width'] >= 0,
'width must be greater than 0'))
checks.append((lambda m: m['points'] > 0,
'points must be greater than 0'))
Expand Down Expand Up @@ -206,9 +206,9 @@ def _build_dtype(metadata):
fieldnames.append(f)
typenames.append(np_type)
else:
fieldnames.extend(['%s_%04d' % (f, i) for i in xrange(c)])
fieldnames.extend(['%s_%04d' % (f, i) for i in range(c)])
typenames.extend([np_type]*c)
dtype = np.dtype(zip(fieldnames, typenames))
dtype = np.dtype([x for x in zip(fieldnames, typenames)])
return dtype


Expand Down Expand Up @@ -278,7 +278,7 @@ def point_cloud_from_fileobj(f):
"""
header = []
while True:
ln = f.readline().strip()
ln = f.readline().strip().decode(encoding = 'utf-8')
header.append(ln)
if ln.startswith('DATA'):
metadata = parse_header(header)
Expand Down Expand Up @@ -322,6 +322,10 @@ def point_cloud_to_fileobj(pc, fileobj, data_compression=None):
metadata['data'] = data_compression

header = write_header(metadata)

if data_compression == 'binary':
header = str.encode(header)

fileobj.write(header)
if metadata['data'].lower() == 'ascii':
fmtstr = build_ascii_fmtstr(pc)
Expand All @@ -338,7 +342,7 @@ def point_cloud_to_fileobj(pc, fileobj, data_compression=None):
for fieldname in pc.pc_data.dtype.names:
column = np.ascontiguousarray(pc.pc_data[fieldname]).tostring('C')
uncompressed_lst.append(column)
uncompressed = ''.join(uncompressed_lst)
uncompressed = ''.join(str(uncompressed_lst))
uncompressed_size = len(uncompressed)
# print("uncompressed_size = %r"%(uncompressed_size))
buf = lzf.compress(uncompressed)
Expand All @@ -350,8 +354,8 @@ def point_cloud_to_fileobj(pc, fileobj, data_compression=None):
else:
compressed_size = len(buf)
fmt = 'II'
fileobj.write(struct.pack(fmt, compressed_size, uncompressed_size))
fileobj.write(buf)
fileobj.write(str(struct.pack(fmt, compressed_size, uncompressed_size)))
fileobj.write(str(buf))
else:
raise ValueError('unknown DATA type')
# we can't close because if it's stringio buf then we can't get value after
Expand All @@ -378,7 +382,7 @@ def save_point_cloud(pc, fname):
def save_point_cloud_bin(pc, fname):
""" Save pointcloud to fname in binary format.
"""
with open(fname, 'w') as f:
with open(fname, 'wb') as f:
point_cloud_to_fileobj(pc, f, 'binary')


Expand All @@ -397,7 +401,7 @@ def save_xyz_label(pc, fname, use_default_lbl=False):
if not use_default_lbl and ('label' not in md['fields']):
raise Exception('label is not a field in this point cloud')
with open(fname, 'w') as f:
for i in xrange(pc.points):
for i in range(pc.points):
x, y, z = ['%.4f' % d for d in (
pc.pc_data['x'][i], pc.pc_data['y'][i], pc.pc_data['z'][i]
)]
Expand All @@ -414,7 +418,7 @@ def save_xyz_intensity_label(pc, fname, use_default_lbl=False):
if 'intensity' not in md['fields']:
raise Exception('intensity is not a field in this point cloud')
with open(fname, 'w') as f:
for i in xrange(pc.points):
for i in range(pc.points):
x, y, z = ['%.4f' % d for d in (
pc.pc_data['x'][i], pc.pc_data['y'][i], pc.pc_data['z'][i]
)]
Expand All @@ -437,7 +441,7 @@ def save_txt(pc, fname, header=True):
if cnt == 1:
header_lst.append(field_name)
else:
for c in xrange(cnt):
for c in range(cnt):
header_lst.append('%s_%04d' % (field_name, c))
f.write(' '.join(header_lst)+'\n')
fmtstr = build_ascii_fmtstr(pc)
Expand Down Expand Up @@ -480,7 +484,7 @@ def add_fields(pc, metadata, pc_data):
fieldnames.append(f)
typenames.append(np_type)
else:
fieldnames.extend(['%s_%04d' % (f, i) for i in xrange(c)])
fieldnames.extend(['%s_%04d' % (f, i) for i in range(c)])
typenames.extend([np_type]*c)
dtype = zip(fieldnames, typenames)
# new dtype. could be inferred?
Expand Down Expand Up @@ -604,13 +608,13 @@ def decode_rgb_from_pcl(rgb):
return rgb_arr


def make_xyz_label_point_cloud(xyzl, label_type='f'):
def make_xyz_label_point_cloud(xyzl, label_type='f', label = 'label'):
""" Make XYZL point cloud from numpy array.

TODO i labels?
"""
md = {'version': .7,
'fields': ['x', 'y', 'z', 'label'],
'fields': ['x', 'y', 'z', label],
'count': [1, 1, 1, 1],
'width': len(xyzl),
'height': 1,
Expand All @@ -628,7 +632,7 @@ def make_xyz_label_point_cloud(xyzl, label_type='f'):
# TODO use .view()
xyzl = xyzl.astype(np.float32)
dt = np.dtype([('x', np.float32), ('y', np.float32), ('z', np.float32),
('label', np.float32)])
(label, np.float32)])
pc_data = np.rec.fromarrays([xyzl[:, 0], xyzl[:, 1], xyzl[:, 2],
xyzl[:, 3]], dtype=dt)
pc = PointCloud(md, pc_data)
Expand Down Expand Up @@ -682,7 +686,7 @@ def check_sanity(self):
md = self.get_metadata()
assert(_metadata_is_consistent(md))
assert(len(self.pc_data) == self.points)
assert(self.width*self.height == self.points)
# assert(self.width*self.height == self.points)
assert(len(self.fields) == len(self.count))
assert(len(self.fields) == len(self.type))

Expand Down