Spaces:
Running on Zero
Running on Zero
Download src/utils/postprocess.py from hugging-apps/flexpart: direct link, hf CLI and curl.
- Browser
- Download file 1.68 kB
-
https://huggingface.co/spaces/hugging-apps/flexpart/resolve/main/src/utils/postprocess.py
- Command line
-
hf download hf://spaces/hugging-apps/flexpart/src/utils/postprocess.py
-
curl -L -o postprocess.py https://huggingface.co/spaces/hugging-apps/flexpart/resolve/main/src/utils/postprocess.py
1.68 kB
| import trimesh | |
| try: # kiui is only used by postprocess_mesh (mesh cleaning/decimation); the demo | |
| # pipeline only calls filter_mesh, so keep this import lazy/optional. | |
| from kiui.mesh_utils import clean_mesh, decimate_mesh | |
| except ImportError: # pragma: no cover | |
| def clean_mesh(*args, **kwargs): | |
| raise ImportError("kiui is required for postprocess_mesh but is not installed") | |
| def decimate_mesh(*args, **kwargs): | |
| raise ImportError("kiui is required for postprocess_mesh but is not installed") | |
| def postprocess_mesh(mesh: trimesh.Trimesh, decimate_target=100000): | |
| vertices = mesh.vertices | |
| triangles = mesh.faces | |
| if vertices.shape[0] > 0 and triangles.shape[0] > 0: | |
| vertices, triangles = clean_mesh(vertices, triangles, remesh=False, min_f=25, min_d=5) | |
| if decimate_target > 0 and triangles.shape[0] > decimate_target: | |
| vertices, triangles = decimate_mesh(vertices, triangles, decimate_target, optimalplacement=False) | |
| if vertices.shape[0] > 0 and triangles.shape[0] > 0: | |
| vertices, triangles = clean_mesh(vertices, triangles, remesh=False, min_f=25, min_d=5) | |
| mesh.vertices = vertices | |
| mesh.faces = triangles | |
| return mesh | |
| def filter_mesh(mesh): | |
| submeshes = mesh.split(only_watertight=False) | |
| if len(submeshes) == 1: | |
| return submeshes[0] | |
| face_counts = [len(m.faces) for m in submeshes] | |
| max_faces = max(face_counts) | |
| filtered = [m for m in submeshes if len(m.faces) > max_faces*0.1] | |
| if len(filtered) == 1: | |
| return filtered[0] | |
| merged = trimesh.util.concatenate(filtered) | |
| merged.merge_vertices() | |
| _ = merged.vertex_normals | |
| return merged | |