Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,7 @@ import openpyxl
|
|
| 11 |
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
|
| 12 |
from openpyxl.drawing.image import Image as XLImage
|
| 13 |
import math
|
|
|
|
| 14 |
|
| 15 |
# ---- Hardcoded parameters (RELAXED for broader detection) ----
|
| 16 |
PREVIEW_MAX_POINTS = 80_000
|
|
@@ -40,29 +41,50 @@ MIN_VOLUME = 0.001
|
|
| 40 |
# minimal density (points per cubic meter) to accept cluster
|
| 41 |
MIN_DENSITY = 5
|
| 42 |
|
|
|
|
|
|
|
|
|
|
| 43 |
# ---- Helper functions ----
|
| 44 |
def pts_to_npy(file_path, pts_npy, cols_npy):
|
| 45 |
-
"""
|
| 46 |
-
pts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
with open(file_path, 'r') as f:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
return pts_npy, cols_npy
|
| 67 |
|
| 68 |
def rgb_to_hsv_array(rgb_uint8):
|
|
@@ -258,8 +280,11 @@ def process_point_cloud(uploaded_file):
|
|
| 258 |
# Process the file from the path Gradio gave us
|
| 259 |
pts_npy = os.path.join(temp_root, "cloud_points.npy")
|
| 260 |
cols_npy = os.path.join(temp_root, "cloud_colors.npy")
|
|
|
|
|
|
|
| 261 |
pts_to_npy(orig_path, pts_npy, cols_npy)
|
| 262 |
|
|
|
|
| 263 |
points = np.load(pts_npy, mmap_mode='r')
|
| 264 |
colors = np.load(cols_npy, mmap_mode='r')
|
| 265 |
n_points = points.shape[0]
|
|
|
|
| 11 |
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
|
| 12 |
from openpyxl.drawing.image import Image as XLImage
|
| 13 |
import math
|
| 14 |
+
import csv
|
| 15 |
|
| 16 |
# ---- Hardcoded parameters (RELAXED for broader detection) ----
|
| 17 |
PREVIEW_MAX_POINTS = 80_000
|
|
|
|
| 41 |
# minimal density (points per cubic meter) to accept cluster
|
| 42 |
MIN_DENSITY = 5
|
| 43 |
|
| 44 |
+
# Chunk size for memory-efficient file reading
|
| 45 |
+
CHUNK_SIZE = 1_000_000 # lines
|
| 46 |
+
|
| 47 |
# ---- Helper functions ----
|
| 48 |
def pts_to_npy(file_path, pts_npy, cols_npy):
|
| 49 |
+
"""
|
| 50 |
+
Converts a .pts file to a numpy array for faster processing,
|
| 51 |
+
reading in chunks to handle very large files efficiently.
|
| 52 |
+
"""
|
| 53 |
+
total_pts = []
|
| 54 |
+
total_cols = []
|
| 55 |
+
|
| 56 |
with open(file_path, 'r') as f:
|
| 57 |
+
reader = csv.reader(f, delimiter=' ')
|
| 58 |
+
while True:
|
| 59 |
+
chunk_pts = []
|
| 60 |
+
chunk_cols = []
|
| 61 |
+
lines_read = 0
|
| 62 |
+
for row in reader:
|
| 63 |
+
if not row or row[0].startswith('#'):
|
| 64 |
+
continue
|
| 65 |
+
try:
|
| 66 |
+
x, y, z = map(float, row[:3])
|
| 67 |
+
r, g, b = map(int, row[3:6])
|
| 68 |
+
chunk_pts.append((x, y, z))
|
| 69 |
+
chunk_cols.append((r, g, b))
|
| 70 |
+
lines_read += 1
|
| 71 |
+
except (ValueError, IndexError):
|
| 72 |
+
continue
|
| 73 |
+
if lines_read >= CHUNK_SIZE:
|
| 74 |
+
break
|
| 75 |
+
|
| 76 |
+
if not chunk_pts:
|
| 77 |
+
break
|
| 78 |
+
|
| 79 |
+
total_pts.extend(chunk_pts)
|
| 80 |
+
total_cols.extend(chunk_cols)
|
| 81 |
+
|
| 82 |
+
pts_arr = np.array(total_pts, dtype=np.float32)
|
| 83 |
+
cols_arr = np.array(total_cols, dtype=np.uint8)
|
| 84 |
+
|
| 85 |
+
np.save(pts_npy, pts_arr)
|
| 86 |
+
np.save(cols_npy, cols_arr)
|
| 87 |
+
|
| 88 |
return pts_npy, cols_npy
|
| 89 |
|
| 90 |
def rgb_to_hsv_array(rgb_uint8):
|
|
|
|
| 280 |
# Process the file from the path Gradio gave us
|
| 281 |
pts_npy = os.path.join(temp_root, "cloud_points.npy")
|
| 282 |
cols_npy = os.path.join(temp_root, "cloud_colors.npy")
|
| 283 |
+
|
| 284 |
+
# Use memory-efficient conversion
|
| 285 |
pts_to_npy(orig_path, pts_npy, cols_npy)
|
| 286 |
|
| 287 |
+
# Use memory-mapped numpy arrays for processing
|
| 288 |
points = np.load(pts_npy, mmap_mode='r')
|
| 289 |
colors = np.load(cols_npy, mmap_mode='r')
|
| 290 |
n_points = points.shape[0]
|