Simplify app to automatic 10-photo studio generation
Browse files- README.md +29 -34
- app.py +68 -333
- model_runtime.py +0 -3
- multiview_runtime.py +61 -37
- studio_pipeline.py +52 -0
- tests/test_studio_pipeline.py +31 -0
README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title: Product
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
|
@@ -15,48 +15,43 @@ models:
|
|
| 15 |
- black-forest-labs/FLUX.2-klein-4B
|
| 16 |
---
|
| 17 |
|
| 18 |
-
#
|
| 19 |
|
| 20 |
-
|
| 21 |
|
| 22 |
-
##
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
-
|
| 28 |
-
|
| 29 |
-
- AI-generated front three-quarter, rear three-quarter, top, and underside views
|
| 30 |
-
- View-specific physical support rules and attached contact shadows to prevent floating products
|
| 31 |
|
| 32 |
-
The
|
| 33 |
|
| 34 |
-
##
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
## Hardware
|
| 47 |
-
|
| 48 |
-
ZeroGPU A10G or a classical GPU with at least 16 GB VRAM is required for AI multi-view generation. Low-memory classical GPUs use model CPU offload. The deterministic studio path retains a CPU fallback. Queue concurrency is fixed at one to keep peak memory predictable.
|
| 49 |
-
|
| 50 |
-
## Local development
|
| 51 |
-
|
| 52 |
-
```bash
|
| 53 |
-
python -m venv .venv
|
| 54 |
-
source .venv/bin/activate
|
| 55 |
-
pip install -r requirements.txt
|
| 56 |
-
python app.py
|
| 57 |
-
```
|
| 58 |
-
|
| 59 |
-
Run the deterministic pipeline tests without downloading model weights:
|
| 60 |
|
| 61 |
```bash
|
| 62 |
pytest -q
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Studio10 Product Photography
|
| 3 |
+
emoji: 📸
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
|
|
|
| 15 |
- black-forest-labs/FLUX.2-klein-4B
|
| 16 |
---
|
| 17 |
|
| 18 |
+
# Studio10
|
| 19 |
|
| 20 |
+
Studio10 converts one ordinary product photograph into a fixed set of ten white-background ecommerce studio photographs.
|
| 21 |
|
| 22 |
+
## Workflow
|
| 23 |
|
| 24 |
+
1. Normalize the uploaded image and EXIF orientation.
|
| 25 |
+
2. Isolate the product with BiRefNet.
|
| 26 |
+
3. Present the isolated product to FLUX.2 Klein as an identity reference.
|
| 27 |
+
4. Re-render ten complete studio photographs with distinct camera setups.
|
| 28 |
+
5. Return ten PNG files and one ZIP archive.
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
The application has no user-facing angle selector, background editor, shadow controls, or deterministic cut-and-paste mode. One upload and one button always produce the complete set.
|
| 31 |
|
| 32 |
+
## Ten fixed photographs
|
| 33 |
|
| 34 |
+
1. Front-left three-quarter
|
| 35 |
+
2. Front-right three-quarter
|
| 36 |
+
3. Straight front
|
| 37 |
+
4. Left profile
|
| 38 |
+
5. Right profile
|
| 39 |
+
6. Rear three-quarter
|
| 40 |
+
7. Low hero angle
|
| 41 |
+
8. High angle
|
| 42 |
+
9. Direct top-down
|
| 43 |
+
10. Physically supported underside view
|
| 44 |
|
| 45 |
+
Every prompt requires a coherent horizontal studio floor, real support contact, and an attached contact shadow. Generated unseen surfaces can differ from the real product, so identity-critical details must be reviewed before publication.
|
| 46 |
|
| 47 |
+
## Reproducibility and hardware
|
| 48 |
|
| 49 |
+
- BiRefNet revision: `e2bf8e4460fc8fa32bba5ea4d94b3233d367b0e4`
|
| 50 |
+
- FLUX.2 Klein 4B revision: `e7b7dc27f91deacad38e78976d1f2b499d76a294`
|
| 51 |
+
- Recommended runtime: ZeroGPU A10G or a classical CUDA GPU with sufficient VRAM
|
| 52 |
+
- Queue concurrency: 1
|
| 53 |
|
| 54 |
+
## Tests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
```bash
|
| 57 |
pytest -q
|
app.py
CHANGED
|
@@ -1,384 +1,119 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
from
|
| 8 |
-
from
|
| 9 |
-
from
|
| 10 |
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
state,
|
| 14 |
-
aspect_ratio,
|
| 15 |
-
resolution,
|
| 16 |
-
occupancy,
|
| 17 |
-
background_color,
|
| 18 |
-
yaw,
|
| 19 |
-
pitch,
|
| 20 |
-
roll,
|
| 21 |
-
camera_zoom,
|
| 22 |
-
position_x,
|
| 23 |
-
position_y,
|
| 24 |
-
shadow_enabled,
|
| 25 |
-
shadow_intensity,
|
| 26 |
-
shadow_blur,
|
| 27 |
-
shadow_x,
|
| 28 |
-
shadow_y,
|
| 29 |
-
):
|
| 30 |
-
if not state:
|
| 31 |
-
raise gr.Error("Önce bir fotoğraf yükleyip ürünü hazırlayın.")
|
| 32 |
-
result = compose_product(
|
| 33 |
-
image=state["image"],
|
| 34 |
-
mask=state["mask"],
|
| 35 |
-
aspect_ratio=aspect_ratio,
|
| 36 |
-
long_edge=int(resolution),
|
| 37 |
-
occupancy=occupancy,
|
| 38 |
-
background_color=background_color,
|
| 39 |
-
shadow_enabled=shadow_enabled,
|
| 40 |
-
shadow_intensity=shadow_intensity,
|
| 41 |
-
shadow_blur=shadow_blur,
|
| 42 |
-
shadow_x=shadow_x,
|
| 43 |
-
shadow_y=shadow_y,
|
| 44 |
-
yaw=yaw,
|
| 45 |
-
pitch=pitch,
|
| 46 |
-
roll=roll,
|
| 47 |
-
camera_zoom=camera_zoom,
|
| 48 |
-
position_x=position_x,
|
| 49 |
-
position_y=position_y,
|
| 50 |
-
)
|
| 51 |
-
files, archive = save_outputs(result)
|
| 52 |
-
return (
|
| 53 |
-
(result.before, result.composite),
|
| 54 |
-
result.transparent,
|
| 55 |
-
result.composite,
|
| 56 |
-
result.mask_preview,
|
| 57 |
-
files,
|
| 58 |
-
archive,
|
| 59 |
-
"✓ Açı ve konum çıktılara uygulandı. Görünmeyen ürün yüzeyleri üretilmedi.",
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
def adjust_product(*args):
|
| 64 |
-
try:
|
| 65 |
-
return _render_product(*args)
|
| 66 |
-
except gr.Error:
|
| 67 |
-
raise
|
| 68 |
-
except Exception as exc:
|
| 69 |
-
raise gr.Error(f"Ön izleme güncellenemedi: {exc}") from exc
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
def process_product(
|
| 73 |
-
uploaded_image,
|
| 74 |
-
aspect_ratio,
|
| 75 |
-
resolution,
|
| 76 |
-
occupancy,
|
| 77 |
-
background_color,
|
| 78 |
-
yaw,
|
| 79 |
-
pitch,
|
| 80 |
-
roll,
|
| 81 |
-
camera_zoom,
|
| 82 |
-
position_x,
|
| 83 |
-
position_y,
|
| 84 |
-
shadow_enabled,
|
| 85 |
-
shadow_intensity,
|
| 86 |
-
shadow_blur,
|
| 87 |
-
shadow_x,
|
| 88 |
-
shadow_y,
|
| 89 |
-
):
|
| 90 |
try:
|
| 91 |
image = prepare_input(uploaded_image)
|
| 92 |
mask = predict_mask(image)
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
occupancy,
|
| 99 |
-
background_color,
|
| 100 |
-
yaw,
|
| 101 |
-
pitch,
|
| 102 |
-
roll,
|
| 103 |
-
camera_zoom,
|
| 104 |
-
position_x,
|
| 105 |
-
position_y,
|
| 106 |
-
shadow_enabled,
|
| 107 |
-
shadow_intensity,
|
| 108 |
-
shadow_blur,
|
| 109 |
-
shadow_x,
|
| 110 |
-
shadow_y,
|
| 111 |
-
)
|
| 112 |
-
return (*rendered, state)
|
| 113 |
-
except gr.Error:
|
| 114 |
-
raise
|
| 115 |
-
except Exception as exc:
|
| 116 |
-
raise gr.Error(f"İşlem tamamlanamadı: {exc}") from exc
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
def generate_multiview_ui(uploaded_image, state, selected_views, seed):
|
| 120 |
-
try:
|
| 121 |
-
if not state:
|
| 122 |
-
image = prepare_input(uploaded_image)
|
| 123 |
-
mask = predict_mask(image)
|
| 124 |
-
state = {"image": image, "mask": mask}
|
| 125 |
-
isolated = compose_product(
|
| 126 |
-
image=state["image"],
|
| 127 |
-
mask=state["mask"],
|
| 128 |
-
aspect_ratio="1:1",
|
| 129 |
-
long_edge=1024,
|
| 130 |
-
occupancy=82,
|
| 131 |
-
background_color="#FFFFFF",
|
| 132 |
-
shadow_enabled=False,
|
| 133 |
-
shadow_intensity=0,
|
| 134 |
-
shadow_blur=1,
|
| 135 |
-
shadow_x=0,
|
| 136 |
-
shadow_y=0,
|
| 137 |
-
).transparent
|
| 138 |
-
results = generate_product_views(
|
| 139 |
-
isolated, selected_views or [], int(seed)
|
| 140 |
-
)
|
| 141 |
-
files, archive = save_multiview_outputs(results)
|
| 142 |
-
gallery = [(image, name) for name, image in results]
|
| 143 |
return (
|
| 144 |
gallery,
|
| 145 |
files,
|
| 146 |
archive,
|
| 147 |
-
"✓
|
| 148 |
-
state,
|
| 149 |
)
|
| 150 |
except gr.Error:
|
| 151 |
raise
|
| 152 |
except Exception as exc:
|
| 153 |
-
raise gr.Error(f"
|
| 154 |
|
| 155 |
|
| 156 |
CSS = """
|
| 157 |
-
:root { --
|
| 158 |
-
.gradio-container { max-width:
|
| 159 |
-
#hero { padding:
|
| 160 |
-
#hero h1 { font-size: clamp(
|
| 161 |
-
#hero p { color:
|
| 162 |
-
.panel { border: 1px solid
|
| 163 |
-
#
|
| 164 |
-
#
|
| 165 |
-
#
|
| 166 |
-
.
|
| 167 |
-
|
| 168 |
-
.fineprint { color: #748091; font-size: .84rem; }
|
| 169 |
-
@media (max-width: 800px) { .output-tabs { min-height: auto; } }
|
| 170 |
"""
|
| 171 |
|
| 172 |
-
with gr.Blocks(css=CSS, title="
|
| 173 |
-
editor_state = gr.State()
|
| 174 |
gr.Markdown(
|
| 175 |
"""
|
| 176 |
-
#
|
| 177 |
-
**
|
| 178 |
""",
|
| 179 |
elem_id="hero",
|
| 180 |
)
|
| 181 |
|
| 182 |
with gr.Row(equal_height=False):
|
| 183 |
-
with gr.Column(scale=
|
| 184 |
source = gr.Image(
|
| 185 |
-
label="Ürün
|
| 186 |
type="pil",
|
| 187 |
image_mode=None,
|
| 188 |
sources=["upload", "clipboard"],
|
| 189 |
-
height=
|
| 190 |
)
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
value=78,
|
| 204 |
-
step=1,
|
| 205 |
-
label="Ürünün tuvalde kapladığı alan (%)",
|
| 206 |
)
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
"Kaydırıcıyı bıraktığında ön izleme güncellenir. Tek fotoğrafta görünmeyen yüzeyler üretilmez.",
|
| 212 |
-
elem_classes="camera-note",
|
| 213 |
-
)
|
| 214 |
-
with gr.Row():
|
| 215 |
-
yaw = gr.Slider(-35, 35, 0, step=1, label="Sağa / sola açı")
|
| 216 |
-
pitch = gr.Slider(-25, 25, 0, step=1, label="Yukarı / aşağı açı")
|
| 217 |
-
with gr.Row():
|
| 218 |
-
roll = gr.Slider(-15, 15, 0, step=1, label="Eksen dönüşü")
|
| 219 |
-
camera_zoom = gr.Slider(70, 130, 100, step=1, label="Kamera yakınlığı")
|
| 220 |
-
with gr.Row():
|
| 221 |
-
position_x = gr.Slider(-25, 25, 0, step=1, label="Yatay hareket")
|
| 222 |
-
position_y = gr.Slider(-25, 25, 0, step=1, label="Dikey hareket")
|
| 223 |
-
reset_camera = gr.Button("Açıyı ve konumu sıfırla", size="sm")
|
| 224 |
-
|
| 225 |
-
with gr.Accordion("Temas gölgesi", open=True):
|
| 226 |
-
shadow_enabled = gr.Checkbox(value=True, label="Gölgeyi kullan")
|
| 227 |
-
with gr.Row():
|
| 228 |
-
shadow_intensity = gr.Slider(0, 100, 30, step=1, label="Yoğunluk")
|
| 229 |
-
shadow_blur = gr.Slider(1, 40, 16, step=1, label="Bulanıklık")
|
| 230 |
-
with gr.Row():
|
| 231 |
-
shadow_x = gr.Slider(-100, 100, 0, step=1, label="Yatay konum")
|
| 232 |
-
shadow_y = gr.Slider(-60, 120, 14, step=1, label="Dikey konum")
|
| 233 |
-
|
| 234 |
-
run = gr.Button(
|
| 235 |
-
"Hızlı arka plan temizle (kesim)",
|
| 236 |
-
variant="secondary",
|
| 237 |
-
elem_id="run-button",
|
| 238 |
)
|
| 239 |
-
status = gr.Markdown(
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
with gr.Tabs():
|
| 243 |
-
with gr.Tab("Önce / Sonra"):
|
| 244 |
-
comparison = gr.ImageSlider(
|
| 245 |
-
label="Karşılaştırmak için sürükleyin",
|
| 246 |
-
height=590,
|
| 247 |
-
)
|
| 248 |
-
with gr.Tab("Şeffaf PNG"):
|
| 249 |
-
transparent = gr.Image(
|
| 250 |
-
label="Şeffaf tuval", format="png", height=590, buttons=["download", "fullscreen"]
|
| 251 |
-
)
|
| 252 |
-
with gr.Tab("Stüdyo çıktısı"):
|
| 253 |
-
final_image = gr.Image(
|
| 254 |
-
label="PNG / JPEG ön izlemesi", format="png", height=590, buttons=["download", "fullscreen"]
|
| 255 |
-
)
|
| 256 |
-
with gr.Tab("Maske"):
|
| 257 |
-
mask_preview = gr.Image(
|
| 258 |
-
label="Alpha maskesi", format="png", height=590, buttons=["download", "fullscreen"]
|
| 259 |
-
)
|
| 260 |
-
|
| 261 |
-
with gr.Row():
|
| 262 |
-
output_files = gr.File(label="Tekil çıktılar", file_count="multiple")
|
| 263 |
-
zip_file = gr.File(label="Tümünü ZIP olarak indir")
|
| 264 |
-
|
| 265 |
-
with gr.Group(elem_classes="panel"):
|
| 266 |
-
gr.Markdown(
|
| 267 |
-
"""
|
| 268 |
-
## Gerçekçi stüdyo çekimi · AI ile 4 farklı açı
|
| 269 |
-
Ürünü yalnızca beyaz tuvale yapıştırmaz; perspektifi, yerçekimini, zemin temasını, ışığı ve ürüne bağlı temas gölgesini yeni bir stüdyo fotoğrafı olarak kurar. Ön çapraz, arka çapraz, üstten ve taban görünüşlerini tek seferde üretir.
|
| 270 |
-
"""
|
| 271 |
-
)
|
| 272 |
-
with gr.Row():
|
| 273 |
-
view_choices = gr.CheckboxGroup(
|
| 274 |
-
["Ön çapraz", "Arka çapraz", "Üstten", "Taban / alt"],
|
| 275 |
-
value=["Ön çapraz", "Arka çapraz", "Üstten", "Taban / alt"],
|
| 276 |
-
label="Üretilecek çekim açıları",
|
| 277 |
-
scale=4,
|
| 278 |
)
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
object_fit="contain",
|
| 293 |
-
)
|
| 294 |
-
with gr.Row():
|
| 295 |
-
multiview_files = gr.File(label="Açı görselleri", file_count="multiple")
|
| 296 |
-
multiview_zip = gr.File(label="Tüm açıları ZIP olarak indir")
|
| 297 |
|
| 298 |
gr.Markdown(
|
| 299 |
-
"
|
| 300 |
elem_classes="fineprint",
|
| 301 |
)
|
| 302 |
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
background,
|
| 309 |
-
yaw,
|
| 310 |
-
pitch,
|
| 311 |
-
roll,
|
| 312 |
-
camera_zoom,
|
| 313 |
-
position_x,
|
| 314 |
-
position_y,
|
| 315 |
-
shadow_enabled,
|
| 316 |
-
shadow_intensity,
|
| 317 |
-
shadow_blur,
|
| 318 |
-
shadow_x,
|
| 319 |
-
shadow_y,
|
| 320 |
-
]
|
| 321 |
-
outputs = [
|
| 322 |
-
comparison,
|
| 323 |
-
transparent,
|
| 324 |
-
final_image,
|
| 325 |
-
mask_preview,
|
| 326 |
-
output_files,
|
| 327 |
-
zip_file,
|
| 328 |
-
status,
|
| 329 |
-
]
|
| 330 |
-
run.click(
|
| 331 |
-
fn=process_product,
|
| 332 |
-
inputs=inputs,
|
| 333 |
-
outputs=[*outputs, editor_state],
|
| 334 |
-
api_name="process_product",
|
| 335 |
concurrency_limit=1,
|
| 336 |
scroll_to_output=True,
|
| 337 |
)
|
| 338 |
-
adjustment_inputs = [editor_state, *inputs[1:]]
|
| 339 |
-
for camera_control in [yaw, pitch, roll, camera_zoom, position_x, position_y]:
|
| 340 |
-
camera_control.release(
|
| 341 |
-
fn=adjust_product,
|
| 342 |
-
inputs=adjustment_inputs,
|
| 343 |
-
outputs=outputs,
|
| 344 |
-
show_progress="hidden",
|
| 345 |
-
trigger_mode="always_last",
|
| 346 |
-
concurrency_limit=1,
|
| 347 |
-
)
|
| 348 |
-
reset_event = reset_camera.click(
|
| 349 |
-
fn=lambda: (0, 0, 0, 100, 0, 0),
|
| 350 |
-
outputs=[yaw, pitch, roll, camera_zoom, position_x, position_y],
|
| 351 |
-
show_progress="hidden",
|
| 352 |
-
)
|
| 353 |
-
reset_event.then(
|
| 354 |
-
fn=adjust_product,
|
| 355 |
-
inputs=adjustment_inputs,
|
| 356 |
-
outputs=outputs,
|
| 357 |
-
show_progress="hidden",
|
| 358 |
-
concurrency_limit=1,
|
| 359 |
-
)
|
| 360 |
-
generate_multiview.click(
|
| 361 |
-
fn=generate_multiview_ui,
|
| 362 |
-
inputs=[source, editor_state, view_choices, multiview_seed],
|
| 363 |
-
outputs=[
|
| 364 |
-
multiview_gallery,
|
| 365 |
-
multiview_files,
|
| 366 |
-
multiview_zip,
|
| 367 |
-
multiview_status,
|
| 368 |
-
editor_state,
|
| 369 |
-
],
|
| 370 |
-
api_name="generate_product_views",
|
| 371 |
-
concurrency_limit=1,
|
| 372 |
-
scroll_to_output=True,
|
| 373 |
-
)
|
| 374 |
-
source.upload(
|
| 375 |
-
fn=lambda: None,
|
| 376 |
-
outputs=editor_state,
|
| 377 |
-
js="() => window.scrollTo({top: 0, behavior: 'smooth'})",
|
| 378 |
-
)
|
| 379 |
-
demo.load(fn=model_status, outputs=status, concurrency_limit=1)
|
| 380 |
|
| 381 |
-
demo.queue(default_concurrency_limit=1, max_size=
|
| 382 |
|
| 383 |
if __name__ == "__main__":
|
| 384 |
demo.launch(
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
import os
|
| 4 |
+
import secrets
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
+
from model_runtime import predict_mask
|
| 9 |
+
from multiview_runtime import generate_studio_photos, save_studio_outputs
|
| 10 |
+
from studio_pipeline import isolate_product, prepare_input
|
| 11 |
|
| 12 |
|
| 13 |
+
def create_studio_set(uploaded_image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
image = prepare_input(uploaded_image)
|
| 16 |
mask = predict_mask(image)
|
| 17 |
+
identity_reference = isolate_product(image, mask)
|
| 18 |
+
seed = secrets.randbelow(2**31 - 1)
|
| 19 |
+
results = generate_studio_photos(identity_reference, seed)
|
| 20 |
+
files, archive = save_studio_outputs(results)
|
| 21 |
+
gallery = [(photo, title) for title, photo in results]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return (
|
| 23 |
gallery,
|
| 24 |
files,
|
| 25 |
archive,
|
| 26 |
+
f"✓ 10 stüdyo çekimi hazır · üretim kimliği: {seed}",
|
|
|
|
| 27 |
)
|
| 28 |
except gr.Error:
|
| 29 |
raise
|
| 30 |
except Exception as exc:
|
| 31 |
+
raise gr.Error(f"Stüdyo çekimleri üretilemedi: {exc}") from exc
|
| 32 |
|
| 33 |
|
| 34 |
CSS = """
|
| 35 |
+
:root { --ink: #0f172a; --muted: #64748b; --line: #e2e8f0; }
|
| 36 |
+
.gradio-container { max-width: 1320px !important; margin: 0 auto !important; }
|
| 37 |
+
#hero { padding: 30px 6px 18px; }
|
| 38 |
+
#hero h1 { font-size: clamp(2.25rem, 5vw, 4.2rem); letter-spacing: -.055em; margin: 0 0 8px; }
|
| 39 |
+
#hero p { max-width: 760px; color: var(--muted); font-size: 1.08rem; line-height: 1.65; }
|
| 40 |
+
.panel { border: 1px solid var(--line) !important; border-radius: 22px !important; box-shadow: 0 14px 42px rgba(15,23,42,.06); }
|
| 41 |
+
#generate { min-height: 58px; font-weight: 800; font-size: 1.02rem; }
|
| 42 |
+
#status { color: #475569; font-size: .92rem; }
|
| 43 |
+
#result-title { margin-top: 28px; }
|
| 44 |
+
.fineprint { color: var(--muted); font-size: .84rem; text-align: center; padding: 14px; }
|
| 45 |
+
@media(max-width: 800px) { #hero { padding-top: 18px; } }
|
|
|
|
|
|
|
| 46 |
"""
|
| 47 |
|
| 48 |
+
with gr.Blocks(css=CSS, title="Studio10 · AI Product Photography") as demo:
|
|
|
|
| 49 |
gr.Markdown(
|
| 50 |
"""
|
| 51 |
+
# Studio10
|
| 52 |
+
**Rastgele çekilmiş tek ürün fotoğrafından 10 profesyonel stüdyo çekimi.** Ürün ayrıştırılır; ardından farklı kamera konumları, fiziksel zemin teması, softbox ışığı ve doğal gölgeyle yeniden fotoğraflanır.
|
| 53 |
""",
|
| 54 |
elem_id="hero",
|
| 55 |
)
|
| 56 |
|
| 57 |
with gr.Row(equal_height=False):
|
| 58 |
+
with gr.Column(scale=6, min_width=340, elem_classes="panel"):
|
| 59 |
source = gr.Image(
|
| 60 |
+
label="Ürün fotoğrafını yükle",
|
| 61 |
type="pil",
|
| 62 |
image_mode=None,
|
| 63 |
sources=["upload", "clipboard"],
|
| 64 |
+
height=470,
|
| 65 |
)
|
| 66 |
+
with gr.Column(scale=4, min_width=300, elem_classes="panel"):
|
| 67 |
+
gr.Markdown(
|
| 68 |
+
"""
|
| 69 |
+
### Tek fotoğraf yeterli
|
| 70 |
+
|
| 71 |
+
Fotoğraf evde, mağazada, masada veya karmaşık bir ortamda çekilmiş olabilir. Sistem otomatik olarak:
|
| 72 |
+
|
| 73 |
+
1. Ürünü arka plandan ayırır
|
| 74 |
+
2. Aynı ürünü 10 farklı stüdyo kamerasıyla yeniden kurar
|
| 75 |
+
3. Ürünü beyaz zemine fiziksel olarak oturtur
|
| 76 |
+
4. PNG dosyalarını ve toplu ZIP çıktısını hazırlar
|
| 77 |
+
"""
|
|
|
|
|
|
|
|
|
|
| 78 |
)
|
| 79 |
+
generate = gr.Button(
|
| 80 |
+
"10 stüdyo fotoğrafı üret",
|
| 81 |
+
variant="primary",
|
| 82 |
+
elem_id="generate",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
)
|
| 84 |
+
status = gr.Markdown(
|
| 85 |
+
"BiRefNet + FLUX.2 Klein 4B · İlk çalıştırma model yüklemesi nedeniyle daha uzun sürebilir.",
|
| 86 |
+
elem_id="status",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
)
|
| 88 |
+
|
| 89 |
+
gr.Markdown("## Üretilen stüdyo çekimleri", elem_id="result-title")
|
| 90 |
+
gallery = gr.Gallery(
|
| 91 |
+
label="10 farklı stüdyo fotoğrafı",
|
| 92 |
+
columns=2,
|
| 93 |
+
rows=5,
|
| 94 |
+
height=1350,
|
| 95 |
+
object_fit="contain",
|
| 96 |
+
elem_classes="panel",
|
| 97 |
+
)
|
| 98 |
+
with gr.Row():
|
| 99 |
+
output_files = gr.File(label="10 PNG dosyası", file_count="multiple")
|
| 100 |
+
zip_file = gr.File(label="Tümünü ZIP olarak indir")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
gr.Markdown(
|
| 103 |
+
"Görünmeyen yüzeyler yapay zekâ tarafından tahmin edilir. Logo, etiket, taban deseni ve küçük ürün ayrıntılarını yayınlamadan önce kontrol edin.",
|
| 104 |
elem_classes="fineprint",
|
| 105 |
)
|
| 106 |
|
| 107 |
+
generate.click(
|
| 108 |
+
fn=create_studio_set,
|
| 109 |
+
inputs=source,
|
| 110 |
+
outputs=[gallery, output_files, zip_file, status],
|
| 111 |
+
api_name="generate_10_studio_photos",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
concurrency_limit=1,
|
| 113 |
scroll_to_output=True,
|
| 114 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
demo.queue(default_concurrency_limit=1, max_size=6)
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
demo.launch(
|
model_runtime.py
CHANGED
|
@@ -115,6 +115,3 @@ def predict_mask(image: Image.Image) -> Image.Image:
|
|
| 115 |
"""Run segmentation on ZeroGPU/classical GPU, with transparent CPU fallback."""
|
| 116 |
return get_runtime().predict(image)
|
| 117 |
|
| 118 |
-
|
| 119 |
-
def model_status() -> str:
|
| 120 |
-
return "● BiRefNet ilk işlemde yüklenir · AI çoklu açı modu GPU gerektirir"
|
|
|
|
| 115 |
"""Run segmentation on ZeroGPU/classical GPU, with transparent CPU fallback."""
|
| 116 |
return get_runtime().predict(image)
|
| 117 |
|
|
|
|
|
|
|
|
|
multiview_runtime.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
"""
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
@@ -29,27 +29,48 @@ OUTPUT_ROOT = Path("/tmp/product-studio-multiview")
|
|
| 29 |
_PIPE = None
|
| 30 |
_LOCK = threading.Lock()
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
"
|
| 35 |
-
"
|
| 36 |
-
"resting flat on the horizontal studio floor"
|
| 37 |
),
|
| 38 |
-
|
| 39 |
-
"
|
| 40 |
-
"
|
| 41 |
-
"flat on the horizontal studio floor"
|
| 42 |
),
|
| 43 |
-
|
| 44 |
-
"
|
| 45 |
-
"
|
| 46 |
),
|
| 47 |
-
|
| 48 |
-
"
|
| 49 |
-
"
|
| 50 |
-
"floor, with visible physical contact and a naturally attached contact shadow"
|
| 51 |
),
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
def _load_pipeline():
|
|
@@ -109,23 +130,20 @@ def _pure_white_finish(image: Image.Image) -> Image.Image:
|
|
| 109 |
|
| 110 |
|
| 111 |
@gpu_task
|
| 112 |
-
def
|
| 113 |
isolated_reference: Image.Image,
|
| 114 |
-
selected_views: list[str],
|
| 115 |
seed: int,
|
| 116 |
) -> list[tuple[str, Image.Image]]:
|
| 117 |
import torch
|
| 118 |
|
| 119 |
-
selected = [name for name in selected_views if name in VIEW_PROMPTS]
|
| 120 |
-
if not selected:
|
| 121 |
-
raise ValueError("En az bir çekim açısı seçin.")
|
| 122 |
pipe = _load_pipeline()
|
| 123 |
references = [_identity_card(isolated_reference)]
|
| 124 |
results: list[tuple[str, Image.Image]] = []
|
| 125 |
|
| 126 |
-
for index,
|
| 127 |
prompt = (
|
| 128 |
-
"
|
|
|
|
| 129 |
"coherent ecommerce studio photograph; do not paste, warp or float the source cutout on "
|
| 130 |
"a white canvas, and do not preserve the source camera pose when it conflicts with the "
|
| 131 |
"requested view. Generate the exact same single product. Keep the "
|
|
@@ -133,7 +151,7 @@ def generate_product_views(
|
|
| 133 |
"laces, hardware, sole geometry, logos, labels and every visible distinctive detail as "
|
| 134 |
"consistent as possible. Do not redesign, simplify, beautify or add parts. Do not create "
|
| 135 |
"a pair, duplicate, prop, stand, hand, person, packaging or text outside the product. "
|
| 136 |
-
f"Required camera
|
| 137 |
"Gravity and support are mandatory. The product must be visibly supported by the horizontal "
|
| 138 |
"white studio floor, never hovering, levitating or floating. There must be no white gap "
|
| 139 |
"between the lowest physical contact points and the floor. Render a short, soft, realistic "
|
|
@@ -153,11 +171,11 @@ def generate_product_views(
|
|
| 153 |
num_inference_steps=4,
|
| 154 |
generator=generator,
|
| 155 |
).images[0]
|
| 156 |
-
results.append((
|
| 157 |
return results
|
| 158 |
|
| 159 |
|
| 160 |
-
def
|
| 161 |
OUTPUT_ROOT.mkdir(parents=True, exist_ok=True)
|
| 162 |
now = time.time()
|
| 163 |
for directory in OUTPUT_ROOT.iterdir():
|
|
@@ -171,19 +189,25 @@ def save_multiview_outputs(results: list[tuple[str, Image.Image]]) -> tuple[list
|
|
| 171 |
|
| 172 |
run_dir = OUTPUT_ROOT / uuid.uuid4().hex
|
| 173 |
run_dir.mkdir()
|
| 174 |
-
filenames = {
|
| 175 |
-
"Ön çapraz": "01-on-capraz.png",
|
| 176 |
-
"Arka çapraz": "02-arka-capraz.png",
|
| 177 |
-
"Üstten": "03-ustten.png",
|
| 178 |
-
"Taban / alt": "04-taban-alt.png",
|
| 179 |
-
}
|
| 180 |
paths: list[str] = []
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
image.save(path, optimize=True)
|
| 184 |
paths.append(str(path))
|
| 185 |
|
| 186 |
-
archive_path = run_dir / "urun-
|
| 187 |
with zipfile.ZipFile(archive_path, "w", compression=zipfile.ZIP_DEFLATED) as archive:
|
| 188 |
for path in paths:
|
| 189 |
archive.write(path, arcname=Path(path).name)
|
|
|
|
| 1 |
+
"""Generate a fixed ten-photo ecommerce studio set with FLUX.2."""
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 29 |
_PIPE = None
|
| 30 |
_LOCK = threading.Lock()
|
| 31 |
|
| 32 |
+
STUDIO_SHOTS = (
|
| 33 |
+
(
|
| 34 |
+
"01 · Ön sol çapraz",
|
| 35 |
+
"front-left three-quarter hero view, camera slightly above the product; show the front and left side",
|
|
|
|
| 36 |
),
|
| 37 |
+
(
|
| 38 |
+
"02 · Ön sağ çapraz",
|
| 39 |
+
"front-right three-quarter hero view, camera slightly above the product; show the front and right side",
|
|
|
|
| 40 |
),
|
| 41 |
+
(
|
| 42 |
+
"03 · Tam önden",
|
| 43 |
+
"straight-on front elevation at product eye level with symmetrical catalog framing",
|
| 44 |
),
|
| 45 |
+
(
|
| 46 |
+
"04 · Sol profil",
|
| 47 |
+
"clean orthographic-style left side profile at product eye level",
|
|
|
|
| 48 |
),
|
| 49 |
+
(
|
| 50 |
+
"05 · Sağ profil",
|
| 51 |
+
"clean orthographic-style right side profile at product eye level",
|
| 52 |
+
),
|
| 53 |
+
(
|
| 54 |
+
"06 · Arka çapraz",
|
| 55 |
+
"rear three-quarter view showing the back construction and one side, camera slightly above",
|
| 56 |
+
),
|
| 57 |
+
(
|
| 58 |
+
"07 · Alçak kahraman açı",
|
| 59 |
+
"subtle low-angle hero photograph from the front three-quarter direction, realistic not dramatic",
|
| 60 |
+
),
|
| 61 |
+
(
|
| 62 |
+
"08 · Yüksek açı",
|
| 63 |
+
"high front three-quarter camera angle revealing the upper surfaces while keeping the full product visible",
|
| 64 |
+
),
|
| 65 |
+
(
|
| 66 |
+
"09 · Tam üstten",
|
| 67 |
+
"direct top-down flat-lay camera view; the product remains normally supported by its functional base",
|
| 68 |
+
),
|
| 69 |
+
(
|
| 70 |
+
"10 · Alt yüzey",
|
| 71 |
+
"underside view with the bottom surface facing the camera; place the product stably on one real side edge on the floor because the underside cannot face the camera while standing on it",
|
| 72 |
+
),
|
| 73 |
+
)
|
| 74 |
|
| 75 |
|
| 76 |
def _load_pipeline():
|
|
|
|
| 130 |
|
| 131 |
|
| 132 |
@gpu_task
|
| 133 |
+
def generate_studio_photos(
|
| 134 |
isolated_reference: Image.Image,
|
|
|
|
| 135 |
seed: int,
|
| 136 |
) -> list[tuple[str, Image.Image]]:
|
| 137 |
import torch
|
| 138 |
|
|
|
|
|
|
|
|
|
|
| 139 |
pipe = _load_pipeline()
|
| 140 |
references = [_identity_card(isolated_reference)]
|
| 141 |
results: list[tuple[str, Image.Image]] = []
|
| 142 |
|
| 143 |
+
for index, (title, camera_direction) in enumerate(STUDIO_SHOTS):
|
| 144 |
prompt = (
|
| 145 |
+
"The reference is a product identity sheet extracted from an ordinary environment photo. "
|
| 146 |
+
"Use it only to identify the exact product. Re-render a complete new, physically "
|
| 147 |
"coherent ecommerce studio photograph; do not paste, warp or float the source cutout on "
|
| 148 |
"a white canvas, and do not preserve the source camera pose when it conflicts with the "
|
| 149 |
"requested view. Generate the exact same single product. Keep the "
|
|
|
|
| 151 |
"laces, hardware, sole geometry, logos, labels and every visible distinctive detail as "
|
| 152 |
"consistent as possible. Do not redesign, simplify, beautify or add parts. Do not create "
|
| 153 |
"a pair, duplicate, prop, stand, hand, person, packaging or text outside the product. "
|
| 154 |
+
f"Required camera setup: {camera_direction}. "
|
| 155 |
"Gravity and support are mandatory. The product must be visibly supported by the horizontal "
|
| 156 |
"white studio floor, never hovering, levitating or floating. There must be no white gap "
|
| 157 |
"between the lowest physical contact points and the floor. Render a short, soft, realistic "
|
|
|
|
| 171 |
num_inference_steps=4,
|
| 172 |
generator=generator,
|
| 173 |
).images[0]
|
| 174 |
+
results.append((title, _pure_white_finish(output)))
|
| 175 |
return results
|
| 176 |
|
| 177 |
|
| 178 |
+
def save_studio_outputs(results: list[tuple[str, Image.Image]]) -> tuple[list[str], str]:
|
| 179 |
OUTPUT_ROOT.mkdir(parents=True, exist_ok=True)
|
| 180 |
now = time.time()
|
| 181 |
for directory in OUTPUT_ROOT.iterdir():
|
|
|
|
| 189 |
|
| 190 |
run_dir = OUTPUT_ROOT / uuid.uuid4().hex
|
| 191 |
run_dir.mkdir()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
paths: list[str] = []
|
| 193 |
+
filenames = (
|
| 194 |
+
"01-on-sol-capraz.png",
|
| 195 |
+
"02-on-sag-capraz.png",
|
| 196 |
+
"03-tam-onden.png",
|
| 197 |
+
"04-sol-profil.png",
|
| 198 |
+
"05-sag-profil.png",
|
| 199 |
+
"06-arka-capraz.png",
|
| 200 |
+
"07-alcak-kahraman-aci.png",
|
| 201 |
+
"08-yuksek-aci.png",
|
| 202 |
+
"09-tam-ustten.png",
|
| 203 |
+
"10-alt-yuzey.png",
|
| 204 |
+
)
|
| 205 |
+
for (_, image), filename in zip(results, filenames):
|
| 206 |
+
path = run_dir / filename
|
| 207 |
image.save(path, optimize=True)
|
| 208 |
paths.append(str(path))
|
| 209 |
|
| 210 |
+
archive_path = run_dir / "urun-10-studyo-fotografi.zip"
|
| 211 |
with zipfile.ZipFile(archive_path, "w", compression=zipfile.ZIP_DEFLATED) as archive:
|
| 212 |
for path in paths:
|
| 213 |
archive.write(path, arcname=Path(path).name)
|
studio_pipeline.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import math
|
| 4 |
+
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image, ImageOps
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
MAX_INPUT_SIDE = 6_000
|
| 10 |
+
MAX_INPUT_PIXELS = 36_000_000
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def prepare_input(image: Image.Image) -> Image.Image:
|
| 14 |
+
if image is None:
|
| 15 |
+
raise ValueError("Lütfen bir ürün fotoğrafı yükleyin.")
|
| 16 |
+
image = ImageOps.exif_transpose(image)
|
| 17 |
+
if image.width * image.height > MAX_INPUT_PIXELS:
|
| 18 |
+
ratio = math.sqrt(MAX_INPUT_PIXELS / (image.width * image.height))
|
| 19 |
+
image = image.resize(
|
| 20 |
+
(max(1, round(image.width * ratio)), max(1, round(image.height * ratio))),
|
| 21 |
+
Image.Resampling.LANCZOS,
|
| 22 |
+
)
|
| 23 |
+
if max(image.size) > MAX_INPUT_SIDE:
|
| 24 |
+
image.thumbnail((MAX_INPUT_SIDE, MAX_INPUT_SIDE), Image.Resampling.LANCZOS)
|
| 25 |
+
if image.mode in {"RGBA", "LA"} or "transparency" in image.info:
|
| 26 |
+
rgba = image.convert("RGBA")
|
| 27 |
+
image = Image.alpha_composite(Image.new("RGBA", rgba.size, "white"), rgba)
|
| 28 |
+
return image.convert("RGB")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def isolate_product(image: Image.Image, mask: Image.Image) -> Image.Image:
|
| 32 |
+
if image.size != mask.size:
|
| 33 |
+
mask = mask.resize(image.size, Image.Resampling.LANCZOS)
|
| 34 |
+
alpha = np.asarray(mask.convert("L"), dtype=np.float32) / 255.0
|
| 35 |
+
alpha = np.clip((alpha - 0.012) / 0.976, 0.0, 1.0)
|
| 36 |
+
alpha = alpha * alpha * (3.0 - 2.0 * alpha)
|
| 37 |
+
alpha_image = Image.fromarray(np.round(alpha * 255).astype(np.uint8), mode="L")
|
| 38 |
+
bbox = alpha_image.getbbox()
|
| 39 |
+
if bbox is None:
|
| 40 |
+
raise ValueError("Fotoğrafta ayrıştırılabilir bir ürün bulunamadı.")
|
| 41 |
+
|
| 42 |
+
left, top, right, bottom = bbox
|
| 43 |
+
pad = max(3, round(max(right - left, bottom - top) * 0.02))
|
| 44 |
+
crop_box = (
|
| 45 |
+
max(0, left - pad),
|
| 46 |
+
max(0, top - pad),
|
| 47 |
+
min(image.width, right + pad),
|
| 48 |
+
min(image.height, bottom + pad),
|
| 49 |
+
)
|
| 50 |
+
product = image.crop(crop_box).convert("RGBA")
|
| 51 |
+
product.putalpha(alpha_image.crop(crop_box))
|
| 52 |
+
return product
|
tests/test_studio_pipeline.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from PIL import Image
|
| 3 |
+
|
| 4 |
+
from multiview_runtime import STUDIO_SHOTS
|
| 5 |
+
from studio_pipeline import isolate_product, prepare_input
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_exactly_ten_studio_shots_are_defined():
|
| 9 |
+
assert len(STUDIO_SHOTS) == 10
|
| 10 |
+
assert len({title for title, _ in STUDIO_SHOTS}) == 10
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def test_isolate_product_returns_cropped_rgba():
|
| 14 |
+
image = Image.new("RGB", (500, 350), "#d9dde5")
|
| 15 |
+
pixels = np.asarray(image).copy()
|
| 16 |
+
pixels[70:310, 170:340] = (25, 70, 150)
|
| 17 |
+
image = Image.fromarray(pixels)
|
| 18 |
+
mask = Image.new("L", image.size, 0)
|
| 19 |
+
mask_pixels = np.asarray(mask).copy()
|
| 20 |
+
mask_pixels[70:310, 170:340] = 255
|
| 21 |
+
result = isolate_product(image, Image.fromarray(mask_pixels))
|
| 22 |
+
assert result.mode == "RGBA"
|
| 23 |
+
assert result.width < image.width
|
| 24 |
+
assert result.getchannel("A").getextrema() == (0, 255)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def test_prepare_input_flattens_transparency_to_white():
|
| 28 |
+
image = Image.new("RGBA", (20, 20), (255, 0, 0, 0))
|
| 29 |
+
result = prepare_input(image)
|
| 30 |
+
assert result.mode == "RGB"
|
| 31 |
+
assert result.getpixel((0, 0)) == (255, 255, 255)
|