How to Make Point Cloud Visualization Work in Streamlit: Using Streamlit to open a .pcd file and make a 3D visualization with stpyvista

Hi everyone,

I’m trying to create a point cloud visualization in Streamlit using the following code:

def pcd_visualization(self):
    try:
        uploaded_file = st.file_uploader("Choose a PCD file", type=["pcd"])
        if uploaded_file is not None:
            
            # Load point cloud from temporary file
            pcd = o3d.io.read_point_cloud(uploaded_file)
        
            # Check if point cloud contains data
            if len(pcd.points) == 0:
                st.warning("The PCD file does not contain any points.")
                return
        
            # Convert point cloud to numpy array
            points = np.asarray(pcd.points)
            
            # Create a plot using PyVista
            cloud = pv.PolyData(points)
            
            if cloud.n_points == 0:
                st.warning("The point cloud resulted in an empty mesh.")
                return
            
            plotter = pv.Plotter()
            plotter.add_mesh(cloud, color='white', point_size=2)
            plotter.view_isometric()
            plotter.show_bounds(grid='front', location='all', color='gray')
            
            # Display the visualization in Streamlit
            stpyvista(plotter, key="3d_plot")
            
    except Exception as e:
        st.error(f"Error visualizing the point cloud: {e}")

However, the visualization does not seem to work as expected. The file uploader works, but the point cloud is not displayed. I’m not sure if I’m missing something or if there’s an issue with how I’m integrating PyVista with Streamlit.

Could someone please help me understand what might be going wrong and how to fix it?

Thanks in advance!


Feel free to adjust the question as needed.

Perhaps it’s the white points over the white background.

Making the plotter background dark and using this PCD file:

Code:
import streamlit as st
import numpy as np
import pyvista as pv
import open3d as o3d
from stpyvista import stpyvista, dataview


def pcd_visualization():
    # Load point cloud from file
    pcd = o3d.io.read_point_cloud("./points.pcd")

    # Check if point cloud contains data
    if len(pcd.points) == 0:
        st.warning("The PCD file does not contain any points.")
        return

    # Convert point cloud to numpy array
    points = np.asarray(pcd.points)

    # Create a plot using PyVista
    cloud = pv.PolyData(points)

    if cloud.n_points == 0:
        st.warning("The point cloud resulted in an empty mesh.")
        return

    plotter = pv.Plotter(window_size=[400, 200])
    plotter.add_mesh(cloud, color="white", point_size=8)

    ## Some final touches
    plotter.background_color = "#111111"
    plotter.view_isometric()

    # Display the visualization in Streamlit
    st.title("Point Cloud")
    dataview(cloud)
    stpyvista(plotter, key="3d_plot")


if __name__ == "__main__":
    pcd_visualization()