Update app.py
Browse files
app.py
CHANGED
|
@@ -86,58 +86,85 @@ def create_frames_zip(frames_dir):
|
|
| 86 |
|
| 87 |
return zip_path
|
| 88 |
|
| 89 |
-
def create_3d_model(frames, width, height, depth_scale=0.
|
| 90 |
"""
|
| 91 |
-
تبدیل فریمهای Canny به مدل 3D (GLB)
|
| 92 |
"""
|
| 93 |
try:
|
| 94 |
# انتخاب تعدادی از فریمها برای جلوگیری از سنگین شدن مدل
|
| 95 |
-
step = max(1, len(frames) //
|
| 96 |
selected_frames = frames[::step]
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
|
| 102 |
# تبدیل هر فریم به یک لایه 3D
|
| 103 |
for idx, frame in enumerate(selected_frames):
|
| 104 |
# کاهش وضوح برای سبکتر کردن مدل
|
| 105 |
-
scale =
|
| 106 |
small_frame = cv2.resize(frame, (width // scale, height // scale))
|
| 107 |
|
| 108 |
h, w = small_frame.shape
|
| 109 |
z = idx * depth_scale # فاصله بین لایهها
|
| 110 |
|
| 111 |
-
# ایجاد
|
|
|
|
|
|
|
|
|
|
| 112 |
for y in range(h):
|
| 113 |
for x in range(w):
|
| 114 |
if small_frame[y, x] > 128: # پیکسلهای روشن
|
| 115 |
# نرمالسازی مختصات
|
| 116 |
nx = (x / w - 0.5) * 2
|
| 117 |
ny = (0.5 - y / h) * 2
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
for i in range(vertex_offset, len(vertices) - 1):
|
| 124 |
-
if i + 1 < len(vertices):
|
| 125 |
-
faces.append([i, i + 1, vertex_offset])
|
| 126 |
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
if len(
|
| 130 |
return None
|
| 131 |
|
| 132 |
-
# ساخت
|
| 133 |
-
vertices = np.array(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
# ا
|
| 136 |
-
|
| 137 |
|
| 138 |
# ذخیره به فرمت GLB
|
| 139 |
glb_path = tempfile.mktemp(suffix='.glb')
|
| 140 |
-
|
| 141 |
|
| 142 |
return glb_path
|
| 143 |
|
|
|
|
| 86 |
|
| 87 |
return zip_path
|
| 88 |
|
| 89 |
+
def create_3d_model(frames, width, height, depth_scale=0.05):
|
| 90 |
"""
|
| 91 |
+
تبدیل فریمهای Canny به مدل 3D (GLB) با Mesh واقعی
|
| 92 |
"""
|
| 93 |
try:
|
| 94 |
# انتخاب تعدادی از فریمها برای جلوگیری از سنگین شدن مدل
|
| 95 |
+
step = max(1, len(frames) // 20) # حداکثر 20 لایه
|
| 96 |
selected_frames = frames[::step]
|
| 97 |
|
| 98 |
+
all_vertices = []
|
| 99 |
+
all_faces = []
|
| 100 |
+
vertex_count = 0
|
| 101 |
|
| 102 |
# تبدیل هر فریم به یک لایه 3D
|
| 103 |
for idx, frame in enumerate(selected_frames):
|
| 104 |
# کاهش وضوح برای سبکتر کردن مدل
|
| 105 |
+
scale = 8
|
| 106 |
small_frame = cv2.resize(frame, (width // scale, height // scale))
|
| 107 |
|
| 108 |
h, w = small_frame.shape
|
| 109 |
z = idx * depth_scale # فاصله بین لایهها
|
| 110 |
|
| 111 |
+
# ایجاد یک grid از vertices برای این فریم
|
| 112 |
+
layer_vertices = []
|
| 113 |
+
layer_indices = {}
|
| 114 |
+
|
| 115 |
for y in range(h):
|
| 116 |
for x in range(w):
|
| 117 |
if small_frame[y, x] > 128: # پیکسلهای روشن
|
| 118 |
# نرمالسازی مختصات
|
| 119 |
nx = (x / w - 0.5) * 2
|
| 120 |
ny = (0.5 - y / h) * 2
|
| 121 |
+
|
| 122 |
+
vertex_idx = len(all_vertices)
|
| 123 |
+
all_vertices.append([nx, ny, z])
|
| 124 |
+
layer_vertices.append(vertex_idx)
|
| 125 |
+
layer_indices[(x, y)] = vertex_idx
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
# ایجاد faces درون هر لایه (اتصال پیکسلهای مجاور)
|
| 128 |
+
for y in range(h - 1):
|
| 129 |
+
for x in range(w - 1):
|
| 130 |
+
# بررسی اگر این پیکسل و همسایگانش سفید باشند
|
| 131 |
+
if ((x, y) in layer_indices and
|
| 132 |
+
(x + 1, y) in layer_indices and
|
| 133 |
+
(x, y + 1) in layer_indices):
|
| 134 |
+
|
| 135 |
+
v1 = layer_indices[(x, y)]
|
| 136 |
+
v2 = layer_indices[(x + 1, y)]
|
| 137 |
+
v3 = layer_indices[(x, y + 1)]
|
| 138 |
+
|
| 139 |
+
all_faces.append([v1, v2, v3])
|
| 140 |
+
|
| 141 |
+
# مثلث دوم
|
| 142 |
+
if ((x + 1, y) in layer_indices and
|
| 143 |
+
(x + 1, y + 1) in layer_indices and
|
| 144 |
+
(x, y + 1) in layer_indices):
|
| 145 |
+
|
| 146 |
+
v1 = layer_indices[(x + 1, y)]
|
| 147 |
+
v2 = layer_indices[(x + 1, y + 1)]
|
| 148 |
+
v3 = layer_indices[(x, y + 1)]
|
| 149 |
+
|
| 150 |
+
all_faces.append([v1, v2, v3])
|
| 151 |
|
| 152 |
+
if len(all_vertices) < 3 or len(all_faces) < 1:
|
| 153 |
return None
|
| 154 |
|
| 155 |
+
# ساخت Mesh واقعی
|
| 156 |
+
vertices = np.array(all_vertices)
|
| 157 |
+
faces = np.array(all_faces)
|
| 158 |
+
|
| 159 |
+
# ایجاد mesh با trimesh
|
| 160 |
+
mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
|
| 161 |
|
| 162 |
+
# اصلاح نرمالها
|
| 163 |
+
mesh.fix_normals()
|
| 164 |
|
| 165 |
# ذخیره به فرمت GLB
|
| 166 |
glb_path = tempfile.mktemp(suffix='.glb')
|
| 167 |
+
mesh.export(glb_path, file_type='glb')
|
| 168 |
|
| 169 |
return glb_path
|
| 170 |
|