Spaces:
Configuration error
Configuration error
Update genmake_voxel_engine.py
Browse files- genmake_voxel_engine.py +22 -13
genmake_voxel_engine.py
CHANGED
|
@@ -1,9 +1,6 @@
|
|
| 1 |
import numpy as np
|
| 2 |
import cv2
|
| 3 |
|
| 4 |
-
# ----------------------------
|
| 5 |
-
# GenMake Core Tunables
|
| 6 |
-
# ----------------------------
|
| 7 |
DEFAULT_VOXEL_MM = 0.024
|
| 8 |
DEFAULT_DETAIL_BOOST = 0.5
|
| 9 |
BAYER_STRENGTH = 0.75
|
|
@@ -44,6 +41,15 @@ def build_importance_map(rgb_bgr: np.ndarray, depth01: np.ndarray, fg_mask01: np
|
|
| 44 |
imp = (sharp * 0.70) + (edges * 0.20) + (depth_edges * 0.10)
|
| 45 |
imp = _norm01(imp)
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
if fg_mask01 is not None:
|
| 48 |
m = _to_mask01(fg_mask01)
|
| 49 |
m = cv2.GaussianBlur(m, (0, 0), 1.2)
|
|
@@ -98,22 +104,23 @@ def sample_points_from_volume(V: tuple, total_points: int, voxel_mm: float, sx_m
|
|
| 98 |
prob = np.clip(imp_grid * scale, 0.0, 1.0).astype(np.float32)
|
| 99 |
|
| 100 |
score = prob - (bayer * BAYER_STRENGTH)
|
| 101 |
-
flat = score.
|
| 102 |
|
| 103 |
if not diorama_mode:
|
| 104 |
-
flat_mask = mask_grid.
|
| 105 |
flat[flat_mask < 0.5] = -9999.0
|
| 106 |
|
| 107 |
valid_point_count = np.sum(flat > -9000.0)
|
| 108 |
actual_target = min(int(total_points), int(valid_point_count))
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
| 115 |
else:
|
| 116 |
-
final_mask = (
|
| 117 |
|
| 118 |
ys, xs = np.nonzero(final_mask)
|
| 119 |
if xs.size == 0:
|
|
@@ -122,7 +129,8 @@ def sample_points_from_volume(V: tuple, total_points: int, voxel_mm: float, sx_m
|
|
| 122 |
xx = (xs.astype(np.float32) / (grid_w - 1) - 0.5) * float(sx_mm)
|
| 123 |
yy = (0.5 - ys.astype(np.float32) / (grid_h - 1)) * float(sy_mm)
|
| 124 |
|
| 125 |
-
|
|
|
|
| 126 |
|
| 127 |
zbase = dep_grid[ys, xs].astype(np.float32)
|
| 128 |
p5 = float(np.percentile(zbase, 5))
|
|
@@ -141,8 +149,9 @@ def sample_points_from_volume(V: tuple, total_points: int, voxel_mm: float, sx_m
|
|
| 141 |
|
| 142 |
zz -= float(zz.min())
|
| 143 |
|
|
|
|
| 144 |
rng = np.random.default_rng(int(seed))
|
| 145 |
-
xy_j = float(voxel_mm) * 0.
|
| 146 |
z_j = 0.05
|
| 147 |
|
| 148 |
xx = (xx + (rng.random(xx.shape, dtype=np.float32) - 0.5) * xy_j).astype(np.float32)
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import cv2
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
DEFAULT_VOXEL_MM = 0.024
|
| 5 |
DEFAULT_DETAIL_BOOST = 0.5
|
| 6 |
BAYER_STRENGTH = 0.75
|
|
|
|
| 41 |
imp = (sharp * 0.70) + (edges * 0.20) + (depth_edges * 0.10)
|
| 42 |
imp = _norm01(imp)
|
| 43 |
|
| 44 |
+
# --- UPGRADE 2: Face Priority Boost ---
|
| 45 |
+
h, w = rgb_bgr.shape[:2]
|
| 46 |
+
cx, cy = w // 2, h // 2
|
| 47 |
+
Y, X = np.ogrid[:h, :w]
|
| 48 |
+
dist_sq = (X - cx)**2 + (Y - cy)**2
|
| 49 |
+
max_dist_sq = cx**2 + cy**2 + 1e-5
|
| 50 |
+
face_priority = 1.0 - (dist_sq / max_dist_sq)
|
| 51 |
+
imp = imp * (0.7 + 0.3 * face_priority).astype(np.float32)
|
| 52 |
+
|
| 53 |
if fg_mask01 is not None:
|
| 54 |
m = _to_mask01(fg_mask01)
|
| 55 |
m = cv2.GaussianBlur(m, (0, 0), 1.2)
|
|
|
|
| 104 |
prob = np.clip(imp_grid * scale, 0.0, 1.0).astype(np.float32)
|
| 105 |
|
| 106 |
score = prob - (bayer * BAYER_STRENGTH)
|
| 107 |
+
flat = score.flatten()
|
| 108 |
|
| 109 |
if not diorama_mode:
|
| 110 |
+
flat_mask = mask_grid.flatten()
|
| 111 |
flat[flat_mask < 0.5] = -9999.0
|
| 112 |
|
| 113 |
valid_point_count = np.sum(flat > -9000.0)
|
| 114 |
actual_target = min(int(total_points), int(valid_point_count))
|
| 115 |
|
| 116 |
+
# --- UPGRADE 3: Deterministic Point Targeting ---
|
| 117 |
+
if actual_target > 0:
|
| 118 |
+
top_idx = np.argpartition(flat, -actual_target)[-actual_target:]
|
| 119 |
+
final_mask_flat = np.zeros_like(flat, dtype=bool)
|
| 120 |
+
final_mask_flat[top_idx] = True
|
| 121 |
+
final_mask = final_mask_flat.reshape(grid_h, grid_w)
|
| 122 |
else:
|
| 123 |
+
final_mask = np.zeros((grid_h, grid_w), dtype=bool)
|
| 124 |
|
| 125 |
ys, xs = np.nonzero(final_mask)
|
| 126 |
if xs.size == 0:
|
|
|
|
| 129 |
xx = (xs.astype(np.float32) / (grid_w - 1) - 0.5) * float(sx_mm)
|
| 130 |
yy = (0.5 - ys.astype(np.float32) / (grid_h - 1)) * float(sy_mm)
|
| 131 |
|
| 132 |
+
# --- UPGRADE 4: Z-Axis Plaque Fix ---
|
| 133 |
+
depth_span_mm = min(float(sz_mm) * depth_vol, float(sz_mm) * 0.90)
|
| 134 |
|
| 135 |
zbase = dep_grid[ys, xs].astype(np.float32)
|
| 136 |
p5 = float(np.percentile(zbase, 5))
|
|
|
|
| 149 |
|
| 150 |
zz -= float(zz.min())
|
| 151 |
|
| 152 |
+
# --- UPGRADE 5: UV-Optimized Micro Jitter ---
|
| 153 |
rng = np.random.default_rng(int(seed))
|
| 154 |
+
xy_j = float(voxel_mm) * 0.08
|
| 155 |
z_j = 0.05
|
| 156 |
|
| 157 |
xx = (xx + (rng.random(xx.shape, dtype=np.float32) - 0.5) * xy_j).astype(np.float32)
|