dylanebert commited on
Commit
ed98cbf
·
1 Parent(s): 95c2265

initial commit

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. app.py +219 -0
README.md CHANGED
@@ -8,7 +8,7 @@ sdk_version: 5.13.1
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
- short_description: Convert Gaussian Splats from `.ply` to `.splat` format.
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
+ short_description: Convert gaussian splats from `.ply` to `.splat` format.
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,219 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import os
3
+ import struct
4
+
5
+ import gradio as gr
6
+
7
+ SH_C0 = 0.28209479177387814
8
+
9
+ def read_ply_header(fileobj, max_lines=50000):
10
+ header_lines = []
11
+ for _ in range(max_lines):
12
+ line_bytes = fileobj.readline()
13
+ if not line_bytes:
14
+ raise ValueError("Unexpected EOF while searching for end_header.")
15
+
16
+ line_str = line_bytes.decode("ascii", errors="replace")
17
+ header_lines.append(line_str)
18
+
19
+ if "end_header" in line_str:
20
+ break
21
+ else:
22
+ raise ValueError(f"No 'end_header' found in the first {max_lines} header lines")
23
+
24
+ return header_lines
25
+
26
+ def parse_ply_header_lines(header_lines):
27
+ vertex_count = 0
28
+ prop_list = []
29
+ is_binary_le = False
30
+
31
+ in_vertex_element = False
32
+
33
+ for line in header_lines:
34
+ line = line.strip()
35
+
36
+ if line.startswith("format "):
37
+ if "binary_little_endian" in line:
38
+ is_binary_le = True
39
+ elif "ascii" in line:
40
+ raise ValueError("ASCII PLY is not supported by this example.")
41
+ else:
42
+ raise ValueError("Only binary_little_endian PLY is handled here.")
43
+
44
+ elif line.startswith("element vertex"):
45
+ parts = line.split()
46
+ vertex_count = int(parts[2])
47
+ in_vertex_element = True
48
+
49
+ elif line.startswith("element ") and not line.startswith("element vertex"):
50
+ in_vertex_element = False
51
+
52
+ elif in_vertex_element and line.startswith("property "):
53
+ parts = line.split()
54
+ prop_type = parts[1]
55
+ prop_name = parts[2]
56
+ prop_list.append((prop_name, prop_type))
57
+
58
+ return vertex_count, prop_list, is_binary_le
59
+
60
+ def ply_to_splat(ply_path):
61
+ if not ply_path.endswith(".ply"):
62
+ raise ValueError("Input file must be a .ply file")
63
+
64
+ with open(ply_path, "rb") as f:
65
+ header_lines = read_ply_header(f)
66
+
67
+ vertex_count, properties, is_bin_le = parse_ply_header_lines(header_lines)
68
+ if not is_bin_le:
69
+ raise ValueError("PLY is not binary_little_endian; cannot continue.")
70
+
71
+ type_map = {
72
+ "float": ("f", 4),
73
+ "float32":("f", 4),
74
+ "double": ("d", 8),
75
+ "float64":("d", 8),
76
+ "int": ("i", 4),
77
+ "int32": ("i", 4),
78
+ "uint": ("I", 4),
79
+ "uint32": ("I", 4),
80
+ "short": ("h", 2),
81
+ "ushort": ("H", 2),
82
+ "int16": ("h", 2),
83
+ "uint16": ("H", 2),
84
+ "char": ("b", 1),
85
+ "uchar": ("B", 1),
86
+ "int8": ("b", 1),
87
+ "uint8": ("B", 1),
88
+ }
89
+ prop_structs = []
90
+ row_size = 0
91
+ for (name, ptype) in properties:
92
+ if ptype not in type_map:
93
+ raise ValueError(f"Unsupported property type: {ptype}")
94
+ fmt, size = type_map[ptype]
95
+ prop_structs.append((name, fmt, size))
96
+ row_size += size
97
+
98
+ splat_data = bytearray(32 * vertex_count)
99
+
100
+ def clamp_byte(x):
101
+ return max(0, min(255, int(round(x))))
102
+
103
+ def write_float(offset, value):
104
+ struct.pack_into("<f", splat_data, offset, value)
105
+
106
+ total_vertex_bytes = row_size * vertex_count
107
+ vertex_block = f.read(total_vertex_bytes)
108
+ if len(vertex_block) < total_vertex_bytes:
109
+ raise ValueError("Not enough data for all vertices in the file.")
110
+
111
+ idx = 0
112
+ for i in range(vertex_count):
113
+ out_offset = i * 32
114
+ px, py, pz = 0.0, 0.0, 0.0
115
+ sx, sy, sz = 1.0, 1.0, 1.0
116
+ cr, cg, cb, ca = 255, 255, 255, 255
117
+ rw, rx, ry, rz = 1.0, 0.0, 0.0, 0.0
118
+
119
+ prop_offset = 0
120
+ for (prop_name, prop_fmt, prop_size) in prop_structs:
121
+ raw_value = struct.unpack_from("<" + prop_fmt, vertex_block, idx + prop_offset)[0]
122
+ prop_offset += prop_size
123
+
124
+ if prop_name == "x":
125
+ px = float(raw_value)
126
+ elif prop_name == "y":
127
+ py = float(raw_value)
128
+ elif prop_name == "z":
129
+ pz = float(raw_value)
130
+
131
+ elif prop_name in ["red", "r"]:
132
+ cr = clamp_byte(raw_value)
133
+ elif prop_name in ["green", "g"]:
134
+ cg = clamp_byte(raw_value)
135
+ elif prop_name in ["blue", "b"]:
136
+ cb = clamp_byte(raw_value)
137
+ elif prop_name in ["alpha", "a"]:
138
+ ca = clamp_byte(raw_value)
139
+
140
+ elif prop_name in ["f_dc_0", "features_0"]:
141
+ cr = clamp_byte((0.5 + SH_C0 * raw_value) * 255)
142
+ elif prop_name in ["f_dc_1", "features_1"]:
143
+ cg = clamp_byte((0.5 + SH_C0 * raw_value) * 255)
144
+ elif prop_name in ["f_dc_2", "features_2"]:
145
+ cb = clamp_byte((0.5 + SH_C0 * raw_value) * 255)
146
+ elif prop_name == "f_dc_3":
147
+ ca = clamp_byte((0.5 + SH_C0 * raw_value) * 255)
148
+
149
+ elif prop_name in ["scale_0","scaling_0"]:
150
+ sx = math.exp(raw_value)
151
+ elif prop_name in ["scale_1","scaling_1"]:
152
+ sy = math.exp(raw_value)
153
+ elif prop_name in ["scale_2","scaling_2"]:
154
+ sz = math.exp(raw_value)
155
+
156
+ elif prop_name in ["opacity","opacity_0"]:
157
+ val = 1.0 / (1.0 + math.exp(-float(raw_value)))
158
+ ca = clamp_byte(val*255.0)
159
+
160
+ elif prop_name in ["rot_0","rotation_0"]:
161
+ rw = float(raw_value)
162
+ elif prop_name in ["rot_1","rotation_1"]:
163
+ rx = float(raw_value)
164
+ elif prop_name in ["rot_2","rotation_2"]:
165
+ ry = float(raw_value)
166
+ elif prop_name in ["rot_3","rotation_3"]:
167
+ rz = float(raw_value)
168
+
169
+ idx += row_size
170
+
171
+ length = math.sqrt(rw*rw + rx*rx + ry*ry + rz*rz)
172
+ if length > 1e-9:
173
+ rw /= length
174
+ rx /= length
175
+ ry /= length
176
+ rz /= length
177
+
178
+ rot0 = clamp_byte(rw * 128 + 128)
179
+ rot1 = clamp_byte(rx * 128 + 128)
180
+ rot2 = clamp_byte(ry * 128 + 128)
181
+ rot3 = clamp_byte(rz * 128 + 128)
182
+
183
+ write_float(out_offset + 0, px)
184
+ write_float(out_offset + 4, py)
185
+ write_float(out_offset + 8, pz)
186
+
187
+ write_float(out_offset + 12, sx)
188
+ write_float(out_offset + 16, sy)
189
+ write_float(out_offset + 20, sz)
190
+
191
+ splat_data[out_offset + 24] = cr
192
+ splat_data[out_offset + 25] = cg
193
+ splat_data[out_offset + 26] = cb
194
+ splat_data[out_offset + 27] = ca
195
+
196
+ splat_data[out_offset + 28] = rot0
197
+ splat_data[out_offset + 29] = rot1
198
+ splat_data[out_offset + 30] = rot2
199
+ splat_data[out_offset + 31] = rot3
200
+
201
+ base_dir = os.path.dirname(ply_path)
202
+ filename = os.path.basename(ply_path)
203
+ splat_path = os.path.join(base_dir, filename.replace(".ply", ".splat"))
204
+ with open(splat_path, "wb") as out:
205
+ out.write(splat_data)
206
+
207
+ return splat_path
208
+
209
+
210
+ app = gr.Interface(
211
+ fn=ply_to_splat,
212
+ title="PLY to SPLAT",
213
+ description="Convert a .ply gaussian splat file to a .splat file",
214
+ inputs=gr.Model3D(label="Input .ply file"),
215
+ outputs=gr.Model3D(label="Output .splat file"),
216
+ )
217
+
218
+ if __name__ == "__main__":
219
+ app.launch()