Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +287 -76
- inference.py +3 -1
app.py
CHANGED
|
@@ -83,14 +83,18 @@ class UNetNoCondWrapper(nn.Module):
|
|
| 83 |
# b = base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 84 |
# return f"data:image/png;base64,{b}"
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
def build_textured_cube(pil_imgs, face_rotations=None):
|
| 89 |
"""
|
| 90 |
-
Crée
|
| 91 |
-
-
|
| 92 |
-
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
"""
|
| 95 |
import os
|
| 96 |
import tempfile
|
|
@@ -99,7 +103,7 @@ def build_textured_cube(pil_imgs, face_rotations=None):
|
|
| 99 |
if not (isinstance(pil_imgs, (list, tuple)) and len(pil_imgs) >= 4):
|
| 100 |
raise ValueError("build_textured_cube attend une liste/tuple de 4 images PIL (front, right, back, left).")
|
| 101 |
|
| 102 |
-
# rotations
|
| 103 |
default_rots = {"front": 0, "right": 270, "back": 180, "left": 90, "top": 0, "bottom": 0}
|
| 104 |
if face_rotations is None:
|
| 105 |
face_rotations = default_rots
|
|
@@ -107,13 +111,14 @@ def build_textured_cube(pil_imgs, face_rotations=None):
|
|
| 107 |
for k, v in default_rots.items():
|
| 108 |
face_rotations.setdefault(k, v)
|
| 109 |
|
| 110 |
-
# ---
|
| 111 |
base_dir = "/tmp/gradio"
|
| 112 |
if os.path.isdir(base_dir) and os.access(base_dir, os.W_OK):
|
| 113 |
-
tmpdir = tempfile.mkdtemp(prefix="
|
| 114 |
else:
|
| 115 |
-
tmpdir = tempfile.mkdtemp(prefix="
|
| 116 |
|
|
|
|
| 117 |
tex_names = {
|
| 118 |
"front": "tex_front.png",
|
| 119 |
"right": "tex_right.png",
|
|
@@ -123,17 +128,39 @@ def build_textured_cube(pil_imgs, face_rotations=None):
|
|
| 123 |
"bottom": "tex_bottom.png",
|
| 124 |
}
|
| 125 |
|
| 126 |
-
#
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
mapping_order = ["front", "right", "back", "left"]
|
|
|
|
|
|
|
| 131 |
for img, face_name in zip(pil_imgs[:4], mapping_order):
|
| 132 |
im = img.convert("RGB")
|
| 133 |
-
|
| 134 |
-
im = im.resize((base_w, base_h), Image.LANCZOS)
|
| 135 |
angle = face_rotations.get(face_name, 0)
|
| 136 |
if angle % 360 != 0:
|
|
|
|
| 137 |
im = im.rotate(angle, resample=Image.BICUBIC, expand=False)
|
| 138 |
path = os.path.join(tmpdir, tex_names[face_name])
|
| 139 |
im.save(path, format="PNG")
|
|
@@ -142,8 +169,8 @@ def build_textured_cube(pil_imgs, face_rotations=None):
|
|
| 142 |
except Exception:
|
| 143 |
pass
|
| 144 |
|
| 145 |
-
# top / bottom
|
| 146 |
-
black = Image.new("RGB", (
|
| 147 |
for face_name in ("top", "bottom"):
|
| 148 |
im = black
|
| 149 |
angle = face_rotations.get(face_name, 0)
|
|
@@ -156,10 +183,10 @@ def build_textured_cube(pil_imgs, face_rotations=None):
|
|
| 156 |
except Exception:
|
| 157 |
pass
|
| 158 |
|
| 159 |
-
# --- écrire le
|
| 160 |
-
mtl_path = os.path.join(tmpdir, "
|
| 161 |
with open(mtl_path, "w", encoding="utf-8") as f:
|
| 162 |
-
f.write("#
|
| 163 |
for mat_name, tex_file in tex_names.items():
|
| 164 |
f.write(f"newmtl m_{mat_name}\n")
|
| 165 |
f.write("Ka 1.000 1.000 1.000\n")
|
|
@@ -173,67 +200,88 @@ def build_textured_cube(pil_imgs, face_rotations=None):
|
|
| 173 |
except Exception:
|
| 174 |
pass
|
| 175 |
|
| 176 |
-
# ---
|
| 177 |
-
#
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
with open(obj_path, "w", encoding="utf-8") as f:
|
| 180 |
-
f.write("#
|
| 181 |
-
f.write("mtllib
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
(-s, s, s),
|
| 188 |
-
( s, s, s),
|
| 189 |
-
( s, s, -s),
|
| 190 |
-
# right (+X)
|
| 191 |
-
( s, -s, -s),
|
| 192 |
-
( s, s, -s),
|
| 193 |
-
( s, s, s),
|
| 194 |
-
( s, -s, s),
|
| 195 |
-
# back (-Y)
|
| 196 |
-
( s, -s, -s),
|
| 197 |
-
( s, -s, s),
|
| 198 |
-
(-s, -s, s),
|
| 199 |
-
(-s, -s, -s),
|
| 200 |
-
# left (-X)
|
| 201 |
-
(-s, -s, s),
|
| 202 |
-
(-s, s, s),
|
| 203 |
-
(-s, s, -s),
|
| 204 |
-
(-s, -s, -s),
|
| 205 |
-
# top (+Z)
|
| 206 |
-
(-s, -s, s),
|
| 207 |
-
( s, -s, s),
|
| 208 |
-
( s, s, s),
|
| 209 |
-
(-s, s, s),
|
| 210 |
-
# bottom (-Z)
|
| 211 |
-
(-s, s, -s),
|
| 212 |
-
( s, s, -s),
|
| 213 |
-
( s, -s, -s),
|
| 214 |
-
(-s, -s, -s),
|
| 215 |
-
]
|
| 216 |
-
for v in verts:
|
| 217 |
-
f.write("v {:.6f} {:.6f} {:.6f}\n".format(*v))
|
| 218 |
f.write("\n")
|
| 219 |
|
| 220 |
-
# écrire
|
| 221 |
-
|
|
|
|
| 222 |
for _ in range(6):
|
| 223 |
-
for (u,v) in uvs:
|
| 224 |
-
f.write("vt {:.6f} {:.6f}\n".format(u,v))
|
| 225 |
f.write("\n")
|
| 226 |
|
| 227 |
-
# faces
|
| 228 |
-
# face_order = ["front", "right", "back", "left", "top", "bottom"]
|
| 229 |
-
face_order = ["top", "right", "bottom", "left", "front", "back"]
|
| 230 |
for i, face_name in enumerate(face_order):
|
| 231 |
f.write(f"usemtl m_{face_name}\n")
|
| 232 |
-
v_base = i*4 + 1
|
| 233 |
-
t_base = i*4 + 1
|
| 234 |
-
v1, v2, v3, v4 = v_base, v_base+1, v_base+2, v_base+3
|
| 235 |
-
t1, t2, t3, t4 = t_base, t_base+1, t_base+2, t_base+3
|
| 236 |
-
# deux triangles (
|
| 237 |
f.write(f"f {v1}/{t1} {v2}/{t2} {v3}/{t3}\n")
|
| 238 |
f.write(f"f {v1}/{t1} {v3}/{t3} {v4}/{t4}\n\n")
|
| 239 |
try:
|
|
@@ -241,13 +289,176 @@ def build_textured_cube(pil_imgs, face_rotations=None):
|
|
| 241 |
except Exception:
|
| 242 |
pass
|
| 243 |
|
| 244 |
-
#
|
| 245 |
-
for fname in ["
|
| 246 |
p = os.path.join(tmpdir, fname)
|
| 247 |
if not os.path.exists(p):
|
| 248 |
raise FileNotFoundError(f"Fichier attendu introuvable : {p}")
|
| 249 |
|
| 250 |
return (os.path.abspath(obj_path), tmpdir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
# -------------------------
|
| 253 |
# Fonction appelée par Gradio
|
|
|
|
| 83 |
# b = base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 84 |
# return f"data:image/png;base64,{b}"
|
| 85 |
|
|
|
|
|
|
|
| 86 |
def build_textured_cube(pil_imgs, face_rotations=None):
|
| 87 |
"""
|
| 88 |
+
Crée un parallélépipède texturé (OBJ + MTL + textures).
|
| 89 |
+
- pil_imgs: liste/tuple de 4 PIL.Image dans l'ordre [front, right, back, left]
|
| 90 |
+
- Retour: (chemin_absolu_obj, tmpdir)
|
| 91 |
+
- Defaults (comme demandé) :
|
| 92 |
+
default_rots = {"front": 0, "right": 270, "back": 180, "left": 90, "top": 0, "bottom": 0}
|
| 93 |
+
face_order = ["top","right","bottom","left","front","back"]
|
| 94 |
+
Notes:
|
| 95 |
+
- Les textures sont écrites dans un dossier temporaire (préférentiellement sous /tmp/gradio).
|
| 96 |
+
- Les faces front/back utiliseront la largeur/hauteur de pil_imgs[0].
|
| 97 |
+
- Les faces right/left utiliseront la largeur de pil_imgs[1] et pil_imgs[3] (moyenne si différentes).
|
| 98 |
"""
|
| 99 |
import os
|
| 100 |
import tempfile
|
|
|
|
| 103 |
if not (isinstance(pil_imgs, (list, tuple)) and len(pil_imgs) >= 4):
|
| 104 |
raise ValueError("build_textured_cube attend une liste/tuple de 4 images PIL (front, right, back, left).")
|
| 105 |
|
| 106 |
+
# rotations & ordre demandés
|
| 107 |
default_rots = {"front": 0, "right": 270, "back": 180, "left": 90, "top": 0, "bottom": 0}
|
| 108 |
if face_rotations is None:
|
| 109 |
face_rotations = default_rots
|
|
|
|
| 111 |
for k, v in default_rots.items():
|
| 112 |
face_rotations.setdefault(k, v)
|
| 113 |
|
| 114 |
+
# --- creer dossier temporaire preferentiellement sous /tmp/gradio (HF Spaces) ---
|
| 115 |
base_dir = "/tmp/gradio"
|
| 116 |
if os.path.isdir(base_dir) and os.access(base_dir, os.W_OK):
|
| 117 |
+
tmpdir = tempfile.mkdtemp(prefix="parallelep_", dir=base_dir)
|
| 118 |
else:
|
| 119 |
+
tmpdir = tempfile.mkdtemp(prefix="parallelep_")
|
| 120 |
|
| 121 |
+
# noms de fichiers textures (relatifs)
|
| 122 |
tex_names = {
|
| 123 |
"front": "tex_front.png",
|
| 124 |
"right": "tex_right.png",
|
|
|
|
| 128 |
"bottom": "tex_bottom.png",
|
| 129 |
}
|
| 130 |
|
| 131 |
+
# récupérer tailles (on accepte que les images aient déjà été redimensionnées
|
| 132 |
+
# (ex: left/right résized par ton code d'inference à new_width x original_height))
|
| 133 |
+
front_w, front_h = pil_imgs[0].size
|
| 134 |
+
right_w, right_h = pil_imgs[1].size
|
| 135 |
+
back_w, back_h = pil_imgs[2].size
|
| 136 |
+
left_w, left_h = pil_imgs[3].size
|
| 137 |
+
|
| 138 |
+
# On définit les dimensions principales du box :
|
| 139 |
+
# width (X) = front_w
|
| 140 |
+
# depth (Y) = moyenne des largeurs de left/right (i.e. largeur le long Y)
|
| 141 |
+
# height (Z) = front_h
|
| 142 |
+
width_px = float(front_w)
|
| 143 |
+
height_px = float(front_h)
|
| 144 |
+
# depth: moyenne des largeurs horizontales des textures latérales (right & left)
|
| 145 |
+
depth_px = float((right_w + left_w) / 2.0)
|
| 146 |
+
|
| 147 |
+
# Normaliser pour garder la boîte à des tailles raisonnables (max dimension -> 1.0)
|
| 148 |
+
max_dim = max(width_px, depth_px, height_px, 1.0)
|
| 149 |
+
scale = 1.0 / max_dim
|
| 150 |
+
half_x = (width_px * 0.5) * scale
|
| 151 |
+
half_y = (depth_px * 0.5) * scale
|
| 152 |
+
half_z = (height_px * 0.5) * scale
|
| 153 |
+
|
| 154 |
+
# Mapping attendu pour pil_imgs
|
| 155 |
mapping_order = ["front", "right", "back", "left"]
|
| 156 |
+
|
| 157 |
+
# Sauvegarder les textures avec rotation demandée (et permissions)
|
| 158 |
for img, face_name in zip(pil_imgs[:4], mapping_order):
|
| 159 |
im = img.convert("RGB")
|
| 160 |
+
# si la taille diffère (sécurité), on la respecte : on n'uniformise pas ici
|
|
|
|
| 161 |
angle = face_rotations.get(face_name, 0)
|
| 162 |
if angle % 360 != 0:
|
| 163 |
+
# PIL rotate: angle en degrés, positif = CCW
|
| 164 |
im = im.rotate(angle, resample=Image.BICUBIC, expand=False)
|
| 165 |
path = os.path.join(tmpdir, tex_names[face_name])
|
| 166 |
im.save(path, format="PNG")
|
|
|
|
| 169 |
except Exception:
|
| 170 |
pass
|
| 171 |
|
| 172 |
+
# top / bottom textures noires (même hauteur que front pour homogénéité)
|
| 173 |
+
black = Image.new("RGB", (front_w, front_h), (0, 0, 0))
|
| 174 |
for face_name in ("top", "bottom"):
|
| 175 |
im = black
|
| 176 |
angle = face_rotations.get(face_name, 0)
|
|
|
|
| 183 |
except Exception:
|
| 184 |
pass
|
| 185 |
|
| 186 |
+
# --- écrire le .mtl (références relatives) ---
|
| 187 |
+
mtl_path = os.path.join(tmpdir, "parallelep.mtl")
|
| 188 |
with open(mtl_path, "w", encoding="utf-8") as f:
|
| 189 |
+
f.write("# Material file for parallelepiped\n")
|
| 190 |
for mat_name, tex_file in tex_names.items():
|
| 191 |
f.write(f"newmtl m_{mat_name}\n")
|
| 192 |
f.write("Ka 1.000 1.000 1.000\n")
|
|
|
|
| 200 |
except Exception:
|
| 201 |
pass
|
| 202 |
|
| 203 |
+
# --- construire la géométrie : on écrit 24 vertices (4 par face) dans l'ordre
|
| 204 |
+
# On utilise l'ordre des faces demandé par toi :
|
| 205 |
+
face_order = ["top", "right", "bottom", "left", "front", "back"]
|
| 206 |
+
|
| 207 |
+
# Pour éviter toute confusion d'indices, on définit pour chaque face les 4 sommets (en coordonnées)
|
| 208 |
+
# Convention de coordonnées : +X = right, +Y = front, +Z = up
|
| 209 |
+
# coins du parallélépipède (en coordonnées globales) si on utilisait 8 sommets :
|
| 210 |
+
# but ici on énumère 4-verts par face afin d'avoir des UVs distincts
|
| 211 |
+
# Définition des quads (ordre: v0, v1, v2, v3) orientés CCW quand on regarde la face (face avant)
|
| 212 |
+
quads = {
|
| 213 |
+
# top (+Z) : côté supérieur, couvrira width x depth
|
| 214 |
+
"top": [
|
| 215 |
+
(-half_x, -half_y, half_z),
|
| 216 |
+
( half_x, -half_y, half_z),
|
| 217 |
+
( half_x, half_y, half_z),
|
| 218 |
+
(-half_x, half_y, half_z),
|
| 219 |
+
],
|
| 220 |
+
# right (+X) : face latérale droite, couvrira depth x height (u=0..1 mappe largeur depth)
|
| 221 |
+
"right": [
|
| 222 |
+
( half_x, -half_y, -half_z),
|
| 223 |
+
( half_x, half_y, -half_z),
|
| 224 |
+
( half_x, half_y, half_z),
|
| 225 |
+
( half_x, -half_y, half_z),
|
| 226 |
+
],
|
| 227 |
+
# bottom (-Z)
|
| 228 |
+
"bottom": [
|
| 229 |
+
(-half_x, half_y, -half_z),
|
| 230 |
+
( half_x, half_y, -half_z),
|
| 231 |
+
( half_x, -half_y, -half_z),
|
| 232 |
+
(-half_x, -half_y, -half_z),
|
| 233 |
+
],
|
| 234 |
+
# left (-X)
|
| 235 |
+
"left": [
|
| 236 |
+
(-half_x, -half_y, half_z),
|
| 237 |
+
(-half_x, half_y, half_z),
|
| 238 |
+
(-half_x, half_y, -half_z),
|
| 239 |
+
(-half_x, -half_y, -half_z),
|
| 240 |
+
],
|
| 241 |
+
# front (+Y)
|
| 242 |
+
"front": [
|
| 243 |
+
(-half_x, half_y, -half_z),
|
| 244 |
+
(-half_x, half_y, half_z),
|
| 245 |
+
( half_x, half_y, half_z),
|
| 246 |
+
( half_x, half_y, -half_z),
|
| 247 |
+
],
|
| 248 |
+
# back (-Y)
|
| 249 |
+
"back": [
|
| 250 |
+
( half_x, -half_y, -half_z),
|
| 251 |
+
( half_x, -half_y, half_z),
|
| 252 |
+
(-half_x, -half_y, half_z),
|
| 253 |
+
(-half_x, -half_y, -half_z),
|
| 254 |
+
],
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
# écrire l'OBJ (triangulé, 24 vertices, 24 vt)
|
| 258 |
+
obj_path = os.path.join(tmpdir, "parallelep.obj")
|
| 259 |
with open(obj_path, "w", encoding="utf-8") as f:
|
| 260 |
+
f.write("# Parallelepiped OBJ generated by build_textured_cube\n")
|
| 261 |
+
f.write("mtllib parallelep.mtl\n\n")
|
| 262 |
+
|
| 263 |
+
# écrire vertices (4 par face dans l'ordre face_order)
|
| 264 |
+
for face_name in face_order:
|
| 265 |
+
for v in quads[face_name]:
|
| 266 |
+
f.write("v {:.6f} {:.6f} {:.6f}\n".format(*v))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
f.write("\n")
|
| 268 |
|
| 269 |
+
# écrire UVs : pour chaque face on écrit 4 UVs (0..1)
|
| 270 |
+
# note : si la texture est rectangulaire, UVs restent 0..1 et mapperont l'image entière
|
| 271 |
+
uvs = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]
|
| 272 |
for _ in range(6):
|
| 273 |
+
for (u, v) in uvs:
|
| 274 |
+
f.write("vt {:.6f} {:.6f}\n".format(u, v))
|
| 275 |
f.write("\n")
|
| 276 |
|
| 277 |
+
# faces (2 triangles par face), indices 1-based
|
|
|
|
|
|
|
| 278 |
for i, face_name in enumerate(face_order):
|
| 279 |
f.write(f"usemtl m_{face_name}\n")
|
| 280 |
+
v_base = i * 4 + 1
|
| 281 |
+
t_base = i * 4 + 1
|
| 282 |
+
v1, v2, v3, v4 = v_base, v_base + 1, v_base + 2, v_base + 3
|
| 283 |
+
t1, t2, t3, t4 = t_base, t_base + 1, t_base + 2, t_base + 3
|
| 284 |
+
# écrire deux triangles (v/vt)
|
| 285 |
f.write(f"f {v1}/{t1} {v2}/{t2} {v3}/{t3}\n")
|
| 286 |
f.write(f"f {v1}/{t1} {v3}/{t3} {v4}/{t4}\n\n")
|
| 287 |
try:
|
|
|
|
| 289 |
except Exception:
|
| 290 |
pass
|
| 291 |
|
| 292 |
+
# vérifications rapides
|
| 293 |
+
for fname in ["parallelep.obj", "parallelep.mtl"] + list(tex_names.values()):
|
| 294 |
p = os.path.join(tmpdir, fname)
|
| 295 |
if not os.path.exists(p):
|
| 296 |
raise FileNotFoundError(f"Fichier attendu introuvable : {p}")
|
| 297 |
|
| 298 |
return (os.path.abspath(obj_path), tmpdir)
|
| 299 |
+
|
| 300 |
+
# def build_textured_cube(pil_imgs, face_rotations=None):
|
| 301 |
+
# """
|
| 302 |
+
# Crée cube.obj + cube.mtl + 6 textures (4 fournies + top/bottom noires).
|
| 303 |
+
# - Attendu: pil_imgs = [front, right, back, left] (PIL.Image)
|
| 304 |
+
# - Les faces latérales seront tournée de +90° (par défaut) pour être toutes dans le même sens.
|
| 305 |
+
# Retour: (chemin_absolu_obj, tmpdir)
|
| 306 |
+
# """
|
| 307 |
+
# import os
|
| 308 |
+
# import tempfile
|
| 309 |
+
# from PIL import Image
|
| 310 |
+
|
| 311 |
+
# if not (isinstance(pil_imgs, (list, tuple)) and len(pil_imgs) >= 4):
|
| 312 |
+
# raise ValueError("build_textured_cube attend une liste/tuple de 4 images PIL (front, right, back, left).")
|
| 313 |
+
|
| 314 |
+
# # rotations par défaut : +90° pour les 4 faces latérales, 0 pour top/bottom
|
| 315 |
+
# default_rots = {"front": 0, "right": 270, "back": 180, "left": 90, "top": 0, "bottom": 0}
|
| 316 |
+
# if face_rotations is None:
|
| 317 |
+
# face_rotations = default_rots
|
| 318 |
+
# else:
|
| 319 |
+
# for k, v in default_rots.items():
|
| 320 |
+
# face_rotations.setdefault(k, v)
|
| 321 |
+
|
| 322 |
+
# # --- créer un dossier dans /tmp/gradio si possible (compat HF Spaces) ---
|
| 323 |
+
# base_dir = "/tmp/gradio"
|
| 324 |
+
# if os.path.isdir(base_dir) and os.access(base_dir, os.W_OK):
|
| 325 |
+
# tmpdir = tempfile.mkdtemp(prefix="cube_", dir=base_dir)
|
| 326 |
+
# else:
|
| 327 |
+
# tmpdir = tempfile.mkdtemp(prefix="cube_")
|
| 328 |
+
|
| 329 |
+
# tex_names = {
|
| 330 |
+
# "front": "tex_front.png",
|
| 331 |
+
# "right": "tex_right.png",
|
| 332 |
+
# "back": "tex_back.png",
|
| 333 |
+
# "left": "tex_left.png",
|
| 334 |
+
# "top": "tex_top.png",
|
| 335 |
+
# "bottom": "tex_bottom.png",
|
| 336 |
+
# }
|
| 337 |
+
|
| 338 |
+
# # uniformiser la taille sur la première image (référence)
|
| 339 |
+
# base_w, base_h = pil_imgs[0].size
|
| 340 |
+
|
| 341 |
+
# # mapping : pil_imgs est [front, right, back, left]
|
| 342 |
+
# mapping_order = ["front", "right", "back", "left"]
|
| 343 |
+
# for img, face_name in zip(pil_imgs[:4], mapping_order):
|
| 344 |
+
# im = img.convert("RGB")
|
| 345 |
+
# if im.size != (base_w, base_h):
|
| 346 |
+
# im = im.resize((base_w, base_h), Image.LANCZOS)
|
| 347 |
+
# angle = face_rotations.get(face_name, 0)
|
| 348 |
+
# if angle % 360 != 0:
|
| 349 |
+
# im = im.rotate(angle, resample=Image.BICUBIC, expand=False)
|
| 350 |
+
# path = os.path.join(tmpdir, tex_names[face_name])
|
| 351 |
+
# im.save(path, format="PNG")
|
| 352 |
+
# try:
|
| 353 |
+
# os.chmod(path, 0o644)
|
| 354 |
+
# except Exception:
|
| 355 |
+
# pass
|
| 356 |
+
|
| 357 |
+
# # top / bottom : textures noires
|
| 358 |
+
# black = Image.new("RGB", (base_w, base_h), (0, 0, 0))
|
| 359 |
+
# for face_name in ("top", "bottom"):
|
| 360 |
+
# im = black
|
| 361 |
+
# angle = face_rotations.get(face_name, 0)
|
| 362 |
+
# if angle % 360 != 0:
|
| 363 |
+
# im = im.rotate(angle, resample=Image.BICUBIC, expand=False)
|
| 364 |
+
# p = os.path.join(tmpdir, tex_names[face_name])
|
| 365 |
+
# im.save(p, format="PNG")
|
| 366 |
+
# try:
|
| 367 |
+
# os.chmod(p, 0o644)
|
| 368 |
+
# except Exception:
|
| 369 |
+
# pass
|
| 370 |
+
|
| 371 |
+
# # --- écrire le MTL (références relatives simples) ---
|
| 372 |
+
# mtl_path = os.path.join(tmpdir, "cube.mtl")
|
| 373 |
+
# with open(mtl_path, "w", encoding="utf-8") as f:
|
| 374 |
+
# f.write("# cube material file\n")
|
| 375 |
+
# for mat_name, tex_file in tex_names.items():
|
| 376 |
+
# f.write(f"newmtl m_{mat_name}\n")
|
| 377 |
+
# f.write("Ka 1.000 1.000 1.000\n")
|
| 378 |
+
# f.write("Kd 1.000 1.000 1.000\n")
|
| 379 |
+
# f.write("Ks 0.000 0.000 0.000\n")
|
| 380 |
+
# f.write("Ns 10.000\n")
|
| 381 |
+
# f.write("illum 2\n")
|
| 382 |
+
# f.write(f"map_Kd {tex_file}\n\n")
|
| 383 |
+
# try:
|
| 384 |
+
# os.chmod(mtl_path, 0o644)
|
| 385 |
+
# except Exception:
|
| 386 |
+
# pass
|
| 387 |
+
|
| 388 |
+
# # --- écrire l'OBJ ---
|
| 389 |
+
# # Nous définissons les blocs de 4 vertices dans l'ordre : front, right, back, left, top, bottom
|
| 390 |
+
# obj_path = os.path.join(tmpdir, "cube.obj")
|
| 391 |
+
# with open(obj_path, "w", encoding="utf-8") as f:
|
| 392 |
+
# f.write("# Cube OBJ generated by build_textured_cube (front,right,back,left,top,bottom)\n")
|
| 393 |
+
# f.write("mtllib cube.mtl\n\n")
|
| 394 |
+
|
| 395 |
+
# s = 0.5
|
| 396 |
+
# verts = [
|
| 397 |
+
# # front (+Y)
|
| 398 |
+
# (-s, s, -s),
|
| 399 |
+
# (-s, s, s),
|
| 400 |
+
# ( s, s, s),
|
| 401 |
+
# ( s, s, -s),
|
| 402 |
+
# # right (+X)
|
| 403 |
+
# ( s, -s, -s),
|
| 404 |
+
# ( s, s, -s),
|
| 405 |
+
# ( s, s, s),
|
| 406 |
+
# ( s, -s, s),
|
| 407 |
+
# # back (-Y)
|
| 408 |
+
# ( s, -s, -s),
|
| 409 |
+
# ( s, -s, s),
|
| 410 |
+
# (-s, -s, s),
|
| 411 |
+
# (-s, -s, -s),
|
| 412 |
+
# # left (-X)
|
| 413 |
+
# (-s, -s, s),
|
| 414 |
+
# (-s, s, s),
|
| 415 |
+
# (-s, s, -s),
|
| 416 |
+
# (-s, -s, -s),
|
| 417 |
+
# # top (+Z)
|
| 418 |
+
# (-s, -s, s),
|
| 419 |
+
# ( s, -s, s),
|
| 420 |
+
# ( s, s, s),
|
| 421 |
+
# (-s, s, s),
|
| 422 |
+
# # bottom (-Z)
|
| 423 |
+
# (-s, s, -s),
|
| 424 |
+
# ( s, s, -s),
|
| 425 |
+
# ( s, -s, -s),
|
| 426 |
+
# (-s, -s, -s),
|
| 427 |
+
# ]
|
| 428 |
+
# for v in verts:
|
| 429 |
+
# f.write("v {:.6f} {:.6f} {:.6f}\n".format(*v))
|
| 430 |
+
# f.write("\n")
|
| 431 |
+
|
| 432 |
+
# # écrire 24 UVs (4 par face)
|
| 433 |
+
# uvs = [(0.0,0.0),(1.0,0.0),(1.0,1.0),(0.0,1.0)]
|
| 434 |
+
# for _ in range(6):
|
| 435 |
+
# for (u,v) in uvs:
|
| 436 |
+
# f.write("vt {:.6f} {:.6f}\n".format(u,v))
|
| 437 |
+
# f.write("\n")
|
| 438 |
+
|
| 439 |
+
# # faces dans le même ordre que les vertices ci-dessus
|
| 440 |
+
# face_order = ["top", "right", "bottom", "left", "front", "back"]
|
| 441 |
+
# for i, face_name in enumerate(face_order):
|
| 442 |
+
# f.write(f"usemtl m_{face_name}\n")
|
| 443 |
+
# v_base = i*4 + 1
|
| 444 |
+
# t_base = i*4 + 1
|
| 445 |
+
# v1, v2, v3, v4 = v_base, v_base+1, v_base+2, v_base+3
|
| 446 |
+
# t1, t2, t3, t4 = t_base, t_base+1, t_base+2, t_base+3
|
| 447 |
+
# # deux triangles (triangulation)
|
| 448 |
+
# f.write(f"f {v1}/{t1} {v2}/{t2} {v3}/{t3}\n")
|
| 449 |
+
# f.write(f"f {v1}/{t1} {v3}/{t3} {v4}/{t4}\n\n")
|
| 450 |
+
# try:
|
| 451 |
+
# os.chmod(obj_path, 0o644)
|
| 452 |
+
# except Exception:
|
| 453 |
+
# pass
|
| 454 |
+
|
| 455 |
+
# # vérification rapide
|
| 456 |
+
# for fname in ["cube.obj", "cube.mtl"] + list(tex_names.values()):
|
| 457 |
+
# p = os.path.join(tmpdir, fname)
|
| 458 |
+
# if not os.path.exists(p):
|
| 459 |
+
# raise FileNotFoundError(f"Fichier attendu introuvable : {p}")
|
| 460 |
+
|
| 461 |
+
# return (os.path.abspath(obj_path), tmpdir)
|
| 462 |
|
| 463 |
# -------------------------
|
| 464 |
# Fonction appelée par Gradio
|
inference.py
CHANGED
|
@@ -14,6 +14,7 @@ from transformers import CLIPTokenizer, CLIPTextModel, CLIPImageProcessor
|
|
| 14 |
from PIL import Image
|
| 15 |
import random
|
| 16 |
from contextlib import nullcontext
|
|
|
|
| 17 |
|
| 18 |
def pil_from(x):
|
| 19 |
"""Return a PIL.Image given either a PIL.Image or a path string."""
|
|
@@ -87,7 +88,8 @@ def inference(fiber_imgs, ring_imgs, num_steps):
|
|
| 87 |
bl = ring_imgs.crop((0, tile, tile, canvas_size))
|
| 88 |
br = ring_imgs.crop((tile, tile, canvas_size, canvas_size))
|
| 89 |
|
| 90 |
-
|
|
|
|
| 91 |
|
| 92 |
# close opened images to free handles
|
| 93 |
fiber_imgs.close()
|
|
|
|
| 14 |
from PIL import Image
|
| 15 |
import random
|
| 16 |
from contextlib import nullcontext
|
| 17 |
+
import cv2
|
| 18 |
|
| 19 |
def pil_from(x):
|
| 20 |
"""Return a PIL.Image given either a PIL.Image or a path string."""
|
|
|
|
| 88 |
bl = ring_imgs.crop((0, tile, tile, canvas_size))
|
| 89 |
br = ring_imgs.crop((tile, tile, canvas_size, canvas_size))
|
| 90 |
|
| 91 |
+
tr = cv2.resize(tr, (new_width, original_height), interpolation=cv2.INTER_LANCZOS4)
|
| 92 |
+
br = cv2.resize(br, (new_width, original_height), interpolation=cv2.INTER_LANCZOS4)
|
| 93 |
|
| 94 |
# close opened images to free handles
|
| 95 |
fiber_imgs.close()
|