Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,65 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import tensorflow as tf
|
| 3 |
-
import numpy as np
|
| 4 |
-
import cv2
|
| 5 |
-
import os
|
| 6 |
-
from main import tensor_to_image,StyleContentModel, run_style_transfer
|
| 7 |
-
|
| 8 |
-
# Define layers and instantiate the model once globally
|
| 9 |
-
content_layers = ['block5_conv2']
|
| 10 |
-
style_layers = ['block1_conv1', 'block2_conv1', 'block3_conv1', 'block4_conv1', 'block5_conv1']
|
| 11 |
-
extractor = StyleContentModel(style_layers, content_layers)
|
| 12 |
-
|
| 13 |
-
def style_transfer_wrapper(content_img_np, style_img_np):
|
| 14 |
-
"""
|
| 15 |
-
A wrapper to handle I/O for the Gradio interface.
|
| 16 |
-
Saves numpy arrays to temp files to use with the main function.
|
| 17 |
-
"""
|
| 18 |
-
if content_img_np is None or style_img_np is None:
|
| 19 |
-
return None # Return None if either image is missing
|
| 20 |
-
|
| 21 |
-
# Save numpy arrays to temporary files
|
| 22 |
-
content_path = "temp_content.jpg"
|
| 23 |
-
style_path = "temp_style.jpg"
|
| 24 |
-
|
| 25 |
-
# Gradio provides RGB, but cv2 saves in BGR order
|
| 26 |
-
cv2.imwrite(content_path, cv2.cvtColor(content_img_np, cv2.COLOR_RGB2BGR))
|
| 27 |
-
cv2.imwrite(style_path, cv2.cvtColor(style_img_np, cv2.COLOR_RGB2BGR))
|
| 28 |
-
|
| 29 |
-
# Run the main process (using fewer iterations for a faster demo)
|
| 30 |
-
final_tensor = run_style_transfer(content_path, style_path, iterations=500)
|
| 31 |
-
|
| 32 |
-
# Convert tensor to displayable image
|
| 33 |
-
final_image = tensor_to_image(final_tensor)
|
| 34 |
-
|
| 35 |
-
# Clean up temporary files
|
| 36 |
-
os.remove(content_path)
|
| 37 |
-
os.remove(style_path)
|
| 38 |
-
|
| 39 |
-
return final_image
|
| 40 |
-
|
| 41 |
-
## 4. GRADIO UI DEFINITION ##
|
| 42 |
-
|
| 43 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 44 |
-
gr.Markdown("# 🎨 Neural Style Transfer")
|
| 45 |
-
gr.Markdown("Combine the content of one image with the artistic style of another. This demo uses a VGG19 model. Processing can take a minute, especially on CPU.")
|
| 46 |
-
|
| 47 |
-
with gr.Row():
|
| 48 |
-
content_img = gr.Image(label="Content Image", type="numpy"
|
| 49 |
-
style_img = gr.Image(label="Style Image", type="numpy"
|
| 50 |
-
|
| 51 |
-
run_button = gr.Button("Generate Image", variant="primary")
|
| 52 |
-
|
| 53 |
-
output_img = gr.Image(label="Result")
|
| 54 |
-
|
| 55 |
-
run_button.click(
|
| 56 |
-
fn=style_transfer_wrapper,
|
| 57 |
-
inputs=[content_img, style_img],
|
| 58 |
-
outputs=output_img
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
gr.Markdown("---")
|
| 62 |
-
gr.Markdown("Based on the paper '[A Neural Algorithm of Artistic Style](https://arxiv.org/abs/1508.06576)' by Gatys et al.")
|
| 63 |
-
|
| 64 |
-
# Launch the Gradio app
|
| 65 |
demo.launch(debug=True)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
import os
|
| 6 |
+
from main import tensor_to_image,StyleContentModel, run_style_transfer
|
| 7 |
+
|
| 8 |
+
# Define layers and instantiate the model once globally
|
| 9 |
+
content_layers = ['block5_conv2']
|
| 10 |
+
style_layers = ['block1_conv1', 'block2_conv1', 'block3_conv1', 'block4_conv1', 'block5_conv1']
|
| 11 |
+
extractor = StyleContentModel(style_layers, content_layers)
|
| 12 |
+
|
| 13 |
+
def style_transfer_wrapper(content_img_np, style_img_np):
|
| 14 |
+
"""
|
| 15 |
+
A wrapper to handle I/O for the Gradio interface.
|
| 16 |
+
Saves numpy arrays to temp files to use with the main function.
|
| 17 |
+
"""
|
| 18 |
+
if content_img_np is None or style_img_np is None:
|
| 19 |
+
return None # Return None if either image is missing
|
| 20 |
+
|
| 21 |
+
# Save numpy arrays to temporary files
|
| 22 |
+
content_path = "temp_content.jpg"
|
| 23 |
+
style_path = "temp_style.jpg"
|
| 24 |
+
|
| 25 |
+
# Gradio provides RGB, but cv2 saves in BGR order
|
| 26 |
+
cv2.imwrite(content_path, cv2.cvtColor(content_img_np, cv2.COLOR_RGB2BGR))
|
| 27 |
+
cv2.imwrite(style_path, cv2.cvtColor(style_img_np, cv2.COLOR_RGB2BGR))
|
| 28 |
+
|
| 29 |
+
# Run the main process (using fewer iterations for a faster demo)
|
| 30 |
+
final_tensor = run_style_transfer(content_path, style_path, iterations=500)
|
| 31 |
+
|
| 32 |
+
# Convert tensor to displayable image
|
| 33 |
+
final_image = tensor_to_image(final_tensor)
|
| 34 |
+
|
| 35 |
+
# Clean up temporary files
|
| 36 |
+
os.remove(content_path)
|
| 37 |
+
os.remove(style_path)
|
| 38 |
+
|
| 39 |
+
return final_image
|
| 40 |
+
|
| 41 |
+
## 4. GRADIO UI DEFINITION ##
|
| 42 |
+
|
| 43 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 44 |
+
gr.Markdown("# 🎨 Neural Style Transfer")
|
| 45 |
+
gr.Markdown("Combine the content of one image with the artistic style of another. This demo uses a VGG19 model. Processing can take a minute, especially on CPU.")
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
content_img = gr.Image(label="Content Image", type="numpy")
|
| 49 |
+
style_img = gr.Image(label="Style Image", type="numpy")
|
| 50 |
+
|
| 51 |
+
run_button = gr.Button("Generate Image", variant="primary")
|
| 52 |
+
|
| 53 |
+
output_img = gr.Image(label="Result")
|
| 54 |
+
|
| 55 |
+
run_button.click(
|
| 56 |
+
fn=style_transfer_wrapper,
|
| 57 |
+
inputs=[content_img, style_img],
|
| 58 |
+
outputs=output_img
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
gr.Markdown("---")
|
| 62 |
+
gr.Markdown("Based on the paper '[A Neural Algorithm of Artistic Style](https://arxiv.org/abs/1508.06576)' by Gatys et al.")
|
| 63 |
+
|
| 64 |
+
# Launch the Gradio app
|
| 65 |
demo.launch(debug=True)
|