wu_xinjun
2022-06-14 d8e5c7c93ed28afa2af1d68ea20cff962599626f
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from email import header
import os, sys
import numpy as np
import time
from tqdm import tqdm
 
def file_fliter(folder:str, file_ext:str, sort_format = None):
 
    files_List = os.listdir(folder)
    target_files = []
 
    # flitered by file extention name
    for file in files_List:
        if file.split('.')[-1] == file_ext:
            target_files.append(file)
 
    if sort_format is not None:
        # sort the list by the sort_format
        target_files.sort(key=sort_format)
 
    return target_files
 
def get_skip_rows(path):
    with open(path,"r") as ply_file:
        count = 0
        # read the line
        for line in ply_file.readlines():
            # strip the str of the header
            flag = line.strip()
            if  flag == "end_header":
                return count + 1
            else:
                if count >= 50:
                    return None
                count += 1
    return None
 
def read_ply(path:str):
    start_line = get_skip_rows(path)
    if start_line is not None:
        ply_array = np.loadtxt(path,skiprows=start_line)
    else:
        ply_array = None
    return ply_array
 
 
def write_ply(data:np.array,folder:str,name:str):
    file = os.path.join(folder,name+".ply")
    n_p = data.shape[0]
    n_cols = data.shape[1]
 
    if n_cols == 3:
        header = ["ply\nformat ascii 1.0\n",
            "comment write_ply generated\n",
            f"element vertex {n_p}\n",
            "property float x\n",
            "property float y\n",
            "property float z\n",
            "end_header"]
        fmt = '%.6f','%.6f','%.6f'
 
    elif n_cols == 6:
        header = ["ply\nformat ascii 1.0\n",
            "comment write_ply generated\n",
            f"element vertex {n_p}\n",
            "property float x\n",
            "property float y\n",
            "property float z\n",
            "property uchar red\n",
            "property uchar green\n",
            "property uchar blue\n",
            "end_header"]
        fmt = '%.6f','%.6f','%.6f','%d','%d','%d'
    np.savetxt(file,data,
                delimiter=" ",
                header="".join(header),
                comments="",
                fmt=fmt)
    print(f"{time.asctime()} {name}.ply have been saved with {n_p} points!")
 
 
 
if __name__ == "__main__":
    ply_array = read_ply('src\\microwave\\demo\\demoData\\2522.ply')
    
    print(type(ply_array))
    print(ply_array)