Spaces:
Sleeping
Sleeping
github-actions[bot]
commited on
Commit
·
3c090ce
1
Parent(s):
95ad503
Deploy from GitHub - 2026-01-21 22:35:36
Browse files
app.py
CHANGED
|
@@ -782,10 +782,18 @@ def apply_region_style_impl(
|
|
| 782 |
mask_tensor = torch.from_numpy(mask_np).float() / 255.0
|
| 783 |
mask_tensor = mask_tensor.unsqueeze(0).unsqueeze(0).to(get_device())
|
| 784 |
|
| 785 |
-
# Stylize with both models
|
|
|
|
| 786 |
with torch.no_grad():
|
| 787 |
output1 = model1(img_tensor)
|
| 788 |
output2 = model2(img_tensor)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 789 |
|
| 790 |
# Blend based on mask
|
| 791 |
# mask_tensor is [1, 1, H, W] with values 0-1
|
|
@@ -1446,6 +1454,7 @@ def stylize_image_impl(
|
|
| 1446 |
input_image: Optional[Image.Image],
|
| 1447 |
style: str,
|
| 1448 |
backend: str,
|
|
|
|
| 1449 |
show_comparison: bool,
|
| 1450 |
add_watermark: bool
|
| 1451 |
) -> Tuple[Optional[Image.Image], str, Optional[str]]:
|
|
@@ -1496,6 +1505,22 @@ def stylize_image_impl(
|
|
| 1496 |
# Postprocess
|
| 1497 |
output_image = postprocess_tensor(output_tensor.cpu())
|
| 1498 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1499 |
# Add watermark if requested
|
| 1500 |
if add_watermark:
|
| 1501 |
output_image = add_watermark(output_image, style_display)
|
|
@@ -1587,7 +1612,7 @@ else:
|
|
| 1587 |
# apply_region_style_ui = apply_region_style_ui_impl
|
| 1588 |
|
| 1589 |
|
| 1590 |
-
def process_webcam_frame(image: Image.Image, style: str, backend: str) -> Image.Image:
|
| 1591 |
"""Process webcam frame in real-time."""
|
| 1592 |
if image is None:
|
| 1593 |
return image
|
|
@@ -1624,6 +1649,20 @@ def process_webcam_frame(image: Image.Image, style: str, backend: str) -> Image.
|
|
| 1624 |
|
| 1625 |
output_image = postprocess_tensor(output_tensor.cpu())
|
| 1626 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1627 |
webcam_state.frame_count += 1
|
| 1628 |
actual_backend = 'cuda' if backend == 'cuda' or (backend == 'auto' and CUDA_KERNELS_AVAILABLE) else 'pytorch'
|
| 1629 |
perf_tracker.record(10, actual_backend)
|
|
@@ -1806,11 +1845,19 @@ def create_style_blend_output_impl(
|
|
| 1806 |
alpha = blend_ratio / 100
|
| 1807 |
model = get_blended_model(style1, style2, alpha, backend)
|
| 1808 |
|
| 1809 |
-
# Process
|
| 1810 |
input_tensor = preprocess_image(input_image).to(get_device())
|
| 1811 |
|
|
|
|
| 1812 |
with torch.no_grad():
|
| 1813 |
output_tensor = model(input_tensor)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1814 |
|
| 1815 |
output_image = postprocess_tensor(output_tensor.cpu())
|
| 1816 |
return output_image
|
|
@@ -2353,6 +2400,14 @@ with gr.Blocks(
|
|
| 2353 |
label="Processing Backend"
|
| 2354 |
)
|
| 2355 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2356 |
with gr.Row():
|
| 2357 |
quick_compare = gr.Checkbox(
|
| 2358 |
label="Side-by-side",
|
|
@@ -2632,6 +2687,14 @@ with gr.Blocks(
|
|
| 2632 |
label="Backend"
|
| 2633 |
)
|
| 2634 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2635 |
webcam_stream = gr.Image(
|
| 2636 |
sources=["webcam"],
|
| 2637 |
label="Webcam Feed",
|
|
@@ -2726,12 +2789,12 @@ with gr.Blocks(
|
|
| 2726 |
|
| 2727 |
gr.Examples(
|
| 2728 |
examples=[
|
| 2729 |
-
[example_img, "candy", "auto", False, False],
|
| 2730 |
-
[example_img, "mosaic", "auto", False, False],
|
| 2731 |
-
[example_img, "rain_princess", "auto", True, False],
|
| 2732 |
-
[example_img, "udnie", "auto", False, False],
|
| 2733 |
],
|
| 2734 |
-
inputs=[quick_image, quick_style, quick_backend, quick_compare, quick_watermark],
|
| 2735 |
label="Style Presets (click to load)"
|
| 2736 |
)
|
| 2737 |
|
|
@@ -2857,7 +2920,7 @@ with gr.Blocks(
|
|
| 2857 |
|
| 2858 |
quick_btn.click(
|
| 2859 |
fn=stylize_image,
|
| 2860 |
-
inputs=[quick_image, quick_style, quick_backend, quick_compare, quick_watermark],
|
| 2861 |
outputs=[quick_output, quick_stats, quick_download]
|
| 2862 |
)
|
| 2863 |
|
|
@@ -2921,7 +2984,7 @@ with gr.Blocks(
|
|
| 2921 |
# Users can still upload/process webcam images manually
|
| 2922 |
webcam_stream.change(
|
| 2923 |
fn=process_webcam_frame,
|
| 2924 |
-
inputs=[webcam_stream, webcam_style, webcam_backend],
|
| 2925 |
outputs=[webcam_output],
|
| 2926 |
)
|
| 2927 |
|
|
|
|
| 782 |
mask_tensor = torch.from_numpy(mask_np).float() / 255.0
|
| 783 |
mask_tensor = mask_tensor.unsqueeze(0).unsqueeze(0).to(get_device())
|
| 784 |
|
| 785 |
+
# Stylize with both models (with timing)
|
| 786 |
+
start = time.perf_counter()
|
| 787 |
with torch.no_grad():
|
| 788 |
output1 = model1(img_tensor)
|
| 789 |
output2 = model2(img_tensor)
|
| 790 |
+
if get_device().type == 'cuda':
|
| 791 |
+
torch.cuda.synchronize()
|
| 792 |
+
elapsed_ms = (time.perf_counter() - start) * 1000
|
| 793 |
+
|
| 794 |
+
# Record performance
|
| 795 |
+
actual_backend = 'cuda' if (backend == 'cuda' or (backend == 'auto' and CUDA_KERNELS_AVAILABLE)) else 'pytorch'
|
| 796 |
+
perf_tracker.record(elapsed_ms, actual_backend)
|
| 797 |
|
| 798 |
# Blend based on mask
|
| 799 |
# mask_tensor is [1, 1, H, W] with values 0-1
|
|
|
|
| 1454 |
input_image: Optional[Image.Image],
|
| 1455 |
style: str,
|
| 1456 |
backend: str,
|
| 1457 |
+
intensity: float,
|
| 1458 |
show_comparison: bool,
|
| 1459 |
add_watermark: bool
|
| 1460 |
) -> Tuple[Optional[Image.Image], str, Optional[str]]:
|
|
|
|
| 1505 |
# Postprocess
|
| 1506 |
output_image = postprocess_tensor(output_tensor.cpu())
|
| 1507 |
|
| 1508 |
+
# Apply intensity blending (blend original with stylized output)
|
| 1509 |
+
# intensity 0-100, where 100 = full style, 0 = original
|
| 1510 |
+
intensity_factor = intensity / 100.0
|
| 1511 |
+
if intensity_factor < 1.0:
|
| 1512 |
+
import torchvision.transforms as transforms
|
| 1513 |
+
to_tensor = transforms.ToTensor()
|
| 1514 |
+
to_pil = transforms.ToPILImage()
|
| 1515 |
+
|
| 1516 |
+
original_tensor = to_tensor(input_image)
|
| 1517 |
+
output_tensor_pil = to_tensor(output_image)
|
| 1518 |
+
|
| 1519 |
+
# Blend: output * intensity + original * (1 - intensity)
|
| 1520 |
+
blended_tensor = output_tensor_pil * intensity_factor + original_tensor * (1 - intensity_factor)
|
| 1521 |
+
blended_tensor = torch.clamp(blended_tensor, 0, 1)
|
| 1522 |
+
output_image = to_pil(blended_tensor)
|
| 1523 |
+
|
| 1524 |
# Add watermark if requested
|
| 1525 |
if add_watermark:
|
| 1526 |
output_image = add_watermark(output_image, style_display)
|
|
|
|
| 1612 |
# apply_region_style_ui = apply_region_style_ui_impl
|
| 1613 |
|
| 1614 |
|
| 1615 |
+
def process_webcam_frame(image: Image.Image, style: str, backend: str, intensity: float = 70) -> Image.Image:
|
| 1616 |
"""Process webcam frame in real-time."""
|
| 1617 |
if image is None:
|
| 1618 |
return image
|
|
|
|
| 1649 |
|
| 1650 |
output_image = postprocess_tensor(output_tensor.cpu())
|
| 1651 |
|
| 1652 |
+
# Apply intensity blending
|
| 1653 |
+
intensity_factor = intensity / 100.0
|
| 1654 |
+
if intensity_factor < 1.0:
|
| 1655 |
+
import torchvision.transforms as transforms
|
| 1656 |
+
to_tensor = transforms.ToTensor()
|
| 1657 |
+
to_pil = transforms.ToPILImage()
|
| 1658 |
+
|
| 1659 |
+
original_tensor = to_tensor(image)
|
| 1660 |
+
output_tensor_pil = to_tensor(output_image)
|
| 1661 |
+
|
| 1662 |
+
blended_tensor = output_tensor_pil * intensity_factor + original_tensor * (1 - intensity_factor)
|
| 1663 |
+
blended_tensor = torch.clamp(blended_tensor, 0, 1)
|
| 1664 |
+
output_image = to_pil(blended_tensor)
|
| 1665 |
+
|
| 1666 |
webcam_state.frame_count += 1
|
| 1667 |
actual_backend = 'cuda' if backend == 'cuda' or (backend == 'auto' and CUDA_KERNELS_AVAILABLE) else 'pytorch'
|
| 1668 |
perf_tracker.record(10, actual_backend)
|
|
|
|
| 1845 |
alpha = blend_ratio / 100
|
| 1846 |
model = get_blended_model(style1, style2, alpha, backend)
|
| 1847 |
|
| 1848 |
+
# Process with timing
|
| 1849 |
input_tensor = preprocess_image(input_image).to(get_device())
|
| 1850 |
|
| 1851 |
+
start = time.perf_counter()
|
| 1852 |
with torch.no_grad():
|
| 1853 |
output_tensor = model(input_tensor)
|
| 1854 |
+
if get_device().type == 'cuda':
|
| 1855 |
+
torch.cuda.synchronize()
|
| 1856 |
+
elapsed_ms = (time.perf_counter() - start) * 1000
|
| 1857 |
+
|
| 1858 |
+
# Record performance
|
| 1859 |
+
actual_backend = 'cuda' if (backend == 'cuda' or (backend == 'auto' and CUDA_KERNELS_AVAILABLE)) else 'pytorch'
|
| 1860 |
+
perf_tracker.record(elapsed_ms, actual_backend)
|
| 1861 |
|
| 1862 |
output_image = postprocess_tensor(output_tensor.cpu())
|
| 1863 |
return output_image
|
|
|
|
| 2400 |
label="Processing Backend"
|
| 2401 |
)
|
| 2402 |
|
| 2403 |
+
quick_intensity = gr.Slider(
|
| 2404 |
+
minimum=0,
|
| 2405 |
+
maximum=100,
|
| 2406 |
+
value=70,
|
| 2407 |
+
step=5,
|
| 2408 |
+
label="Style Intensity (0% = Original, 100% = Full Style)"
|
| 2409 |
+
)
|
| 2410 |
+
|
| 2411 |
with gr.Row():
|
| 2412 |
quick_compare = gr.Checkbox(
|
| 2413 |
label="Side-by-side",
|
|
|
|
| 2687 |
label="Backend"
|
| 2688 |
)
|
| 2689 |
|
| 2690 |
+
webcam_intensity = gr.Slider(
|
| 2691 |
+
minimum=0,
|
| 2692 |
+
maximum=100,
|
| 2693 |
+
value=70,
|
| 2694 |
+
step=5,
|
| 2695 |
+
label="Style Intensity (0% = Original, 100% = Full Style)"
|
| 2696 |
+
)
|
| 2697 |
+
|
| 2698 |
webcam_stream = gr.Image(
|
| 2699 |
sources=["webcam"],
|
| 2700 |
label="Webcam Feed",
|
|
|
|
| 2789 |
|
| 2790 |
gr.Examples(
|
| 2791 |
examples=[
|
| 2792 |
+
[example_img, "candy", "auto", 70, False, False],
|
| 2793 |
+
[example_img, "mosaic", "auto", 70, False, False],
|
| 2794 |
+
[example_img, "rain_princess", "auto", 70, True, False],
|
| 2795 |
+
[example_img, "udnie", "auto", 70, False, False],
|
| 2796 |
],
|
| 2797 |
+
inputs=[quick_image, quick_style, quick_backend, quick_intensity, quick_compare, quick_watermark],
|
| 2798 |
label="Style Presets (click to load)"
|
| 2799 |
)
|
| 2800 |
|
|
|
|
| 2920 |
|
| 2921 |
quick_btn.click(
|
| 2922 |
fn=stylize_image,
|
| 2923 |
+
inputs=[quick_image, quick_style, quick_backend, quick_intensity, quick_compare, quick_watermark],
|
| 2924 |
outputs=[quick_output, quick_stats, quick_download]
|
| 2925 |
)
|
| 2926 |
|
|
|
|
| 2984 |
# Users can still upload/process webcam images manually
|
| 2985 |
webcam_stream.change(
|
| 2986 |
fn=process_webcam_frame,
|
| 2987 |
+
inputs=[webcam_stream, webcam_style, webcam_backend, webcam_intensity],
|
| 2988 |
outputs=[webcam_output],
|
| 2989 |
)
|
| 2990 |
|