MVPavanKumar commited on
Commit
2bb0f93
·
verified ·
1 Parent(s): 0cd6aa3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -20
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
- """Converts a .pts file to a numpy array for faster processing."""
46
- pts, cols = [], []
 
 
 
 
 
47
  with open(file_path, 'r') as f:
48
- for line in f:
49
- line = line.strip()
50
- if not line or line.startswith('#'):
51
- continue
52
- parts = line.split()
53
- if len(parts) < 6:
54
- continue
55
- try:
56
- x, y, z = map(float, parts[:3])
57
- r, g, b = map(int, parts[3:6])
58
- except (ValueError, IndexError):
59
- continue
60
- pts.append((x, y, z))
61
- cols.append((r, g, b))
62
- pts = np.asarray(pts, dtype=np.float32)
63
- cols = np.asarray(cols, dtype=np.uint8)
64
- np.save(pts_npy, pts)
65
- np.save(cols_npy, cols)
 
 
 
 
 
 
 
 
 
 
 
 
 
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]