File size: 9,859 Bytes
95d69c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105c55a
95d69c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import csv
import os
import sys
import tempfile
from pathlib import Path

import gradio as gr
import laspy
import numpy as np
import plotly.graph_objects as go

csv.field_size_limit(sys.maxsize)

POINT_CLOUD_CACHE = {}

# Define classification categories and colors
CLASSIFICATION_COLORS = {
    1: ("Soil", "saddlebrown"),
    2: ("Terrain", "peru"),
    3: ("Vegetation", "green"),
    4: ("Building", "red"),
    5: ("Street elements", "yellow"),
    6: ("Water", "blue"),
}


def load_point_cloud(las_path):
    if las_path in POINT_CLOUD_CACHE:
        return POINT_CLOUD_CACHE[las_path]

    with laspy.open(las_path) as fh:
        las = fh.read()
        point_count = len(las.points)

        x, y, z = las.x, las.y, las.z
        classification = las.classification
        intensity = las.intensity
        intensity = np.clip(np.log(intensity + 1) / 10.0, 0, 1)

        try:
            r, g, b = (
                las.red / 65535,
                las.green / 65535,
                las.blue / 65535,
            )
            has_color = True
        except:
            has_color = False
            r = g = b = None

    point_cloud_data = {
        "x": x,
        "y": y,
        "z": z,
        "intensity": intensity,
        "classification": classification,
        "has_color": has_color,
        "r": r,
        "g": g,
        "b": b,
        "point_count": len(x),
        "original_count": point_count,
    }

    POINT_CLOUD_CACHE[las_path] = point_cloud_data
    return point_cloud_data


def visualize_point_cloud(point_cloud_data, point_size=2, color_mode="RGB"):
    x, y, z = point_cloud_data["x"], point_cloud_data["y"], point_cloud_data["z"]
    legend_title = None
    fig_title_suffix = color_mode
    traces = []
    if color_mode == "RGB" and point_cloud_data["has_color"]:
        fig_title_suffix = "RGB"
        rgb_colors = [
            f"rgb({int(r*255)},{int(g*255)},{int(b*255)})"
            for r, g, b in zip(
                point_cloud_data["r"], point_cloud_data["g"], point_cloud_data["b"]
            )
        ]
        traces.append(
            go.Scatter3d(
                x=x,
                y=y,
                z=z,
                mode="markers",
                marker=dict(size=point_size, color=rgb_colors, opacity=0.8),
                name="Points (RGB)",
                showlegend=False,
            )
        )

    elif color_mode == "Intensity":
        fig_title_suffix = "Intensity"
        traces.append(
            go.Scatter3d(
                x=x,
                y=y,
                z=z,
                mode="markers",
                marker=dict(
                    size=point_size,
                    color=point_cloud_data["intensity"],
                    colorscale="Hot",  # Popular heat palettes: Hot, YlOrRd, Inferno, Magma
                    opacity=0.8,
                    colorbar=dict(
                        title="Intensity",
                        thickness=15,
                        len=0.75,
                        yanchor="middle",
                        y=0.5,
                    ),
                ),
                name="Points (Intensity)",
                showlegend=False,
            )
        )

    else:
        # Assign colors based on classification

        fig_title_suffix = "Classification"
        legend_title = "Classes"
        for class_value, info in CLASSIFICATION_COLORS.items():

            label, color = info
            mask = point_cloud_data["classification"] == class_value
            print(mask.shape)
            print(z.shape)
            x_c, y_c, z_c = x[mask], y[mask], z[mask]
            traces.append(
                go.Scatter3d(
                    x=x_c,
                    y=y_c,
                    z=z_c,
                    mode="markers",
                    marker=dict(size=point_size, color=color, opacity=0.8),
                    name=f"{label} ({class_value})",  # This name appears in the legend
                )
            )

    fig = go.Figure(data=traces)

    # Add a legend for classification colors
    fig.update_layout(
        scene=dict(
            xaxis=dict(visible=False, showgrid=False, zeroline=False, title=""),
            yaxis=dict(visible=False, showgrid=False, zeroline=False, title=""),
            zaxis=dict(visible=False, showgrid=False, zeroline=False, title=""),
            aspectmode="data",  # Crucial for correct 3D aspect ratio
        ),
        margin=dict(l=0, r=0, b=0, t=50),  # Adjust top margin for title
        title=dict(text=f"Point Cloud ({fig_title_suffix})", x=0.5, xanchor="center"),
        legend_title_text=legend_title,
        legend=dict(
            traceorder="normal",  # Or "reversed"
            itemsizing="constant",  # Makes legend markers a consistent size
            # You can also position the legend, e.g.:
            # x=1.05, y=1,
            # xanchor='left', yanchor='top'
        ),
    )

    return fig


def process_upload(file, point_size, color_mode):
    if file is None:
        return None

    file_path = Path(file.name)

    pc_data = load_point_cloud(file_path)
    # os.unlink(file_path)

    return visualize_point_cloud(pc_data, point_size, color_mode)


EXAMPLES_DIR = Path("examples")
EXAMPLES_DIR.mkdir(exist_ok=True)

example_files = [
    EXAMPLES_DIR / "Hillside.las",
    EXAMPLES_DIR / "Park.las",
    EXAMPLES_DIR / "Apartments.las",
]

for example in example_files:
    if not example.exists():
        for test_file in example_files:
            if test_file.exists():
                POINT_CLOUD_CACHE[str(example)] = POINT_CLOUD_CACHE.get(str(test_file))
                break

with gr.Blocks(title="Turin3D Dataset Visualizer") as app:
    gr.Markdown(
        """
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Turin3D Dataset</title>
    <style>
        body {
            font-family: 'Segoe UI', sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 0;
            color: #333;
        }
        header {
            background-color: #FFEE8C;
            color: white !important;
            padding: 20px;
            text-align: center;
        }
        section {
            padding: 20px;
            margin: 20px auto;
            color-scheme: light;
        }
        ul {
            padding-left: 20px;
        }
        body, p, ul, li, strong, code {
            color: #333;
        }
    </style>
</head>
<body>
    <header>
        <h1 style="color:white !important;">🏙️ Turin3D Dataset</h1>
        <p style="color:white !important;">Showcase of some portions of Turin3D dataset</p>
    </header>
</body>
</html>
"""
    )  # Updated main page title

    gr.Markdown(
        """
        ## About the Turin3D Dataset and Our Research
        This application showcases portions of the **Turin3D dataset**, which is introduced in our paper:
        **"Turin3D: Evaluating Adaptation Strategies under Label Scarcity in Urban LiDAR Segmentation with Semi-Supervised Techniques"**

        Our work presents Turin3D, a new large-scale aerial LiDAR dataset for 3D point cloud semantic segmentation. It covers approximately 1.43 km² in the city centre of Turin, Italy, and contains almost 70 million points.
        The dataset is specifically designed to address the challenges of label scarcity in urban environments. While the validation and test sets are manually annotated to ensure reliable evaluation of segmentation techniques, the training set is intentionally left unlabelled. This encourages the development and benchmarking of semi-supervised and self-supervised learning approaches, which are crucial when extensive ground truth annotations are not available.

        The full Turin3D dataset will be made publicly available to support further research in outdoor point cloud segmentation.
        For more details, please refer to our paper.
        """
    )

    gr.Markdown(
        """
        ## About this Interactive Visualizer
        This visualizer allows you to explore selected example point clouds from the Turin3D dataset.
        You can interact with the 3D data, switch between different color modes (RGB, Intensity, and Classification for ground truth labels on provided examples), and adjust the point size for visualization.

        **Important Note:** To ensure a smooth and responsive experience in this web application, the point clouds displayed here are **sub-sampled to a lower resolution** compared to the original high-density dataset. The example files are illustrative portions chosen for this demonstration.
        """
    )

    gr.Markdown(
        "--- \nUse the controls below to load an example or adjust the visualization."
    )

    with gr.Row():
        with gr.Column(scale=1):
            example_dropdown = gr.Dropdown(
                choices=[f.name for f in example_files], label="Example Point Clouds"
            )
            point_size = gr.Slider(
                minimum=1, maximum=10, value=2, step=1, label="Point Size"
            )

            color_mode = gr.Radio(
                choices=["RGB", "Intensity", "Classification"],
                value="RGB",
                label="Color Mode",
            )

            with gr.Row():
                example_btn = gr.Button("Load Example")

        with gr.Column(scale=2):
            plot_output = gr.Plot(label="Point Cloud Visualization")

    example_btn.click(
        fn=lambda example, size, mode: visualize_point_cloud(
            load_point_cloud(str(EXAMPLES_DIR / example)),
            point_size=size,
            color_mode=mode,
        ),
        inputs=[example_dropdown, point_size, color_mode],
        outputs=plot_output,
    )


if __name__ == "__main__":
    app.launch(share=False)