wu_xinjun
2022-03-21 e82c2a3abd0f1bd450bea46bc67b84a07017028e
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
import os
import sys
import time
 
# add python path of src to sys.path
src_path = os.path.join(__file__, *(['..'] * 2))
src_path = os.path.abspath(src_path)
sys.path.insert(0, src_path)
 
from microwave.parsers import MicroWaveParser
from .tools import file_fliter
 
def bin2ply_tool(folder):
    bin_files = file_fliter(folder,'bin')
 
    for file in bin_files:
 
        filename = file
        # (fn, ext) = os.path.splitext(filename)
        with open(os.path.join(folder,file), "rb") as bin_file:
            buf = bin_file.read()
            buf_data = MicroWaveParser.ParserFrame(buf)
 
 
            for frame in buf_data:
                Frame_id = frame[0]
                FrameNumber = frame[1]
                point_cloud = frame[2]
                if len(point_cloud) != 0 :
                    with open(os.path.join(folder,str(FrameNumber) +".ply"), "w") as f:
                        f.write("ply\n")
                        f.write("format ascii 1.0\n")
                        f.write("comment MicroWave2Ply generated\n")
                        f.write("element vertex %d\n" % len(point_cloud))
                        f.write("property float x\n")
                        f.write("property float y\n")
                        f.write("property float z\n")
                        f.write("element face 0\n")
                        f.write("property list uchar int vertex_indices\n")
                        f.write("end_header\n")
                        n_p = 0
                        for p in point_cloud:
                            x, y, z = p
                            f.write("{:.5f} {:.5f} {:.5f}\n".format(x, y, z))
                            n_p += 1
                        print(time.asctime(),f"File {file} Frame {FrameNumber}.ply have been saved with {n_p} points!")
 
 
 
if __name__ == "__main__":
    def help():
        print("Usage: bin2ply.py [BINfolder]")
        print("EXAMPLE: BIN2ply.py ./manual_ply/pod1-2012-12-12-12-12-12/")
        exit(1)
 
    if len(sys.argv) != 2:
        help()
    try:
        BINfolder = str(sys.argv[1])
 
    except Exception as e:
        print(e)
        help()
    
    bin2ply_tool(BINfolder)