您现在的位置是:首页 >学无止境 >使用record_msg保存binary点云出错解决办法网站首页学无止境

使用record_msg保存binary点云出错解决办法

王牧犊 2024-08-11 12:01:03
简介使用record_msg保存binary点云出错解决办法

1. 问题现象

使用python的record_msg包解析record包时,会出现无法保存的报错。

Traceback (most recent call last):
File “scripts/parse_record.py”, line 35, in
main(sys.argv)
File “scripts/parse_record.py”, line 32, in main
cloud_parser.parse(message,mode=“binary”)
File “/usr/local/lib/python3.8/dist-packages/record_msg/parser.py”, line 198, in parse
self.save_pointcloud_meta_to_file(pc_meta=self._parsed_data,
File “/usr/local/lib/python3.8/dist-packages/record_msg/parser.py”, line 180, in save_pointcloud_meta_to_file
pypcd.save_point_cloud_bin(pc_meta, pcd_file)
File “/usr/local/lib/python3.8/dist-packages/record_msg/pypcd.py”, line 385, in save_point_cloud_bin
point_cloud_to_fileobj(pc, f, ‘binary’)
File “/usr/local/lib/python3.8/dist-packages/record_msg/pypcd.py”, line 333, in point_cloud_to_fileobj
fileobj.write(pc.pc_data.tostring(‘C’))
TypeError: write() argument must be str, not bytes

2. 主要原因

record_msg包中的pcd保存代码,在保存binary格式时,打开文件的方式是open(fname, ‘w’),默认的写入模式是asci,但是在随后保存的时候直接写入二进制格式的点云数据,导致写入报错。

3. 解决办法

修改文件/usr/local/lib/python3.8/dist-packages/record_msg/pypcd.py

  1. 修改第384行:with open(fname, 'w') as f: —> with open(fname, 'wb') as f
  2. 注释第328行# fileobj.write(header)
  3. 在第329行和332行后分别添加一行代码:fileobj.write(header)fileobj.write(bytes(header,encoding='utf-8'))
  • 修改前:
...
327	    header = write_header(metadata)
328	    fileobj.write(header)
329	    if metadata['data'].lower() == 'ascii':
330	        fmtstr = build_ascii_fmtstr(pc)
331	        np.savetxt(fileobj, pc.pc_data, fmt=fmtstr)
332	    elif metadata['data'].lower() == 'binary':
333	        fileobj.write(pc.pc_data.tostring('C'))
...
381	def save_point_cloud_bin(pc, fname):
382	    """ Save pointcloud to fname in binary format.
383	    """
384	    with open(fname, 'w') as f:
385	        point_cloud_to_fileobj(pc, f, 'binary')
...
  • 修改后:
...
327	    header = write_header(metadata)
328	    # fileobj.write(header)
329	    if metadata['data'].lower() == 'ascii':
330	        fileobj.write(header)
331	        fmtstr = build_ascii_fmtstr(pc)
332	        np.savetxt(fileobj, pc.pc_data, fmt=fmtstr)
333	    elif metadata['data'].lower() == 'binary':
334	        fileobj.write(bytes(header,encoding='utf-8'))
335	        fileobj.write(pc.pc_data.tostring('C'))
...
383	    """
384	    with open(fname, 'wb') as f:
385	        point_cloud_to_fileobj(pc, f, 'binary')
...

REF
[1]. record_msg

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。