Create app py
Browse files
app py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import tensorflow_hub as hub
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image, ImageEnhance, ImageFilter
|
| 6 |
+
from rembg import remove
|
| 7 |
+
|
| 8 |
+
# Load the neural style transfer model from TensorFlow Hub
|
| 9 |
+
model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
|
| 10 |
+
|
| 11 |
+
# Function to convert tensor to image
|
| 12 |
+
def tensor_to_image(tensor):
|
| 13 |
+
tensor = tensor * 255
|
| 14 |
+
tensor = np.array(tensor, dtype=np.uint8)
|
| 15 |
+
if np.ndim(tensor) > 3:
|
| 16 |
+
assert tensor.shape[0] == 1
|
| 17 |
+
tensor = tensor[0]
|
| 18 |
+
return Image.fromarray(tensor)
|
| 19 |
+
|
| 20 |
+
# Function to separate foreground and background
|
| 21 |
+
def separate_foreground_background(image):
|
| 22 |
+
if isinstance(image, np.ndarray):
|
| 23 |
+
image = Image.fromarray(image)
|
| 24 |
+
|
| 25 |
+
output_image = remove(image)
|
| 26 |
+
input_rgb = np.array(image.convert('RGB'))
|
| 27 |
+
output_rgba = np.array(output_image)
|
| 28 |
+
|
| 29 |
+
alpha = output_rgba[:, :, 3]
|
| 30 |
+
alpha3 = np.dstack((alpha, alpha, alpha))
|
| 31 |
+
background_rgb = input_rgb.astype(np.float32) * (1 - alpha3.astype(np.float32) / 255)
|
| 32 |
+
background_rgb = background_rgb.astype(np.uint8)
|
| 33 |
+
|
| 34 |
+
foreground = Image.fromarray(output_rgba)
|
| 35 |
+
background = Image.fromarray(background_rgb)
|
| 36 |
+
return foreground, background
|
| 37 |
+
|
| 38 |
+
# Style transfer function
|
| 39 |
+
def apply_style_transfer(content_image, style_image, intensity=1.0):
|
| 40 |
+
content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.0
|
| 41 |
+
style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.0
|
| 42 |
+
|
| 43 |
+
style_image = style_image * intensity
|
| 44 |
+
outputs = model(tf.constant(content_image), tf.constant(style_image))
|
| 45 |
+
stylized_image = outputs[0]
|
| 46 |
+
|
| 47 |
+
return tensor_to_image(stylized_image)
|
| 48 |
+
|
| 49 |
+
# Function to enhance the image to make it glistening
|
| 50 |
+
def enhance_image(image):
|
| 51 |
+
# Convert to Image for processing
|
| 52 |
+
if isinstance(image, np.ndarray):
|
| 53 |
+
image = Image.fromarray(image)
|
| 54 |
+
|
| 55 |
+
# Enhance color
|
| 56 |
+
enhancer = ImageEnhance.Color(image)
|
| 57 |
+
image = enhancer.enhance(1.5) # Increase color saturation
|
| 58 |
+
|
| 59 |
+
# Enhance contrast
|
| 60 |
+
enhancer = ImageEnhance.Contrast(image)
|
| 61 |
+
image = enhancer.enhance(1.3) # Increase contrast
|
| 62 |
+
|
| 63 |
+
# Apply a slight blur to simulate glow
|
| 64 |
+
image = image.filter(ImageFilter.GaussianBlur(radius=2))
|
| 65 |
+
|
| 66 |
+
# Optionally add noise (uncomment to use)
|
| 67 |
+
# noise = np.random.normal(0, 25, (image.height, image.width, 3))
|
| 68 |
+
# noise_image = np.array(image) + noise
|
| 69 |
+
# image = Image.fromarray(np.clip(noise_image, 0, 255).astype(np.uint8))
|
| 70 |
+
|
| 71 |
+
return image
|
| 72 |
+
|
| 73 |
+
# Function to process image
|
| 74 |
+
def process_image(content_image, style_image):
|
| 75 |
+
# Ensure style_image is a PIL Image
|
| 76 |
+
if isinstance(style_image, np.ndarray):
|
| 77 |
+
style_image = Image.fromarray(style_image)
|
| 78 |
+
|
| 79 |
+
foreground, background = separate_foreground_background(content_image)
|
| 80 |
+
|
| 81 |
+
# Resize all images to the same size
|
| 82 |
+
target_size = (512, 512) # Example size, adjust as needed
|
| 83 |
+
foreground = foreground.resize(target_size, Image.LANCZOS)
|
| 84 |
+
background = background.resize(target_size, Image.LANCZOS)
|
| 85 |
+
style_image = style_image.resize(target_size, Image.LANCZOS)
|
| 86 |
+
|
| 87 |
+
# Convert to RGB format by removing the alpha channel
|
| 88 |
+
foreground_rgb = np.array(foreground.convert('RGB'))
|
| 89 |
+
background_rgb = np.array(background)
|
| 90 |
+
|
| 91 |
+
styled_foreground = apply_style_transfer(foreground_rgb, np.array(style_image.convert('RGB')), intensity=1.0)
|
| 92 |
+
styled_background = apply_style_transfer(background_rgb, np.array(style_image.convert('RGB')), intensity=0.3)
|
| 93 |
+
|
| 94 |
+
styled_foreground_np = np.array(styled_foreground)
|
| 95 |
+
styled_background_np = np.array(styled_background)
|
| 96 |
+
|
| 97 |
+
# Extract the alpha channel from the foreground
|
| 98 |
+
alpha = np.array(foreground)[:, :, 3] / 255.0
|
| 99 |
+
combined_image_np = (styled_foreground_np * alpha[..., np.newaxis] +
|
| 100 |
+
styled_background_np * (1 - alpha[..., np.newaxis]))
|
| 101 |
+
|
| 102 |
+
combined_image = Image.fromarray(np.clip(combined_image_np, 0, 255).astype(np.uint8))
|
| 103 |
+
|
| 104 |
+
# Apply enhancement to make it glistening
|
| 105 |
+
enhanced_image = enhance_image(combined_image)
|
| 106 |
+
|
| 107 |
+
return enhanced_image
|
| 108 |
+
|
| 109 |
+
# Gradio interface setup
|
| 110 |
+
image1 = gr.Image(label="Content Image")
|
| 111 |
+
image2 = gr.Image(label="Style Image")
|
| 112 |
+
stylizedimg = gr.Image(label="Result")
|
| 113 |
+
gr.Interface(
|
| 114 |
+
fn=process_image,
|
| 115 |
+
inputs=[image1, image2],
|
| 116 |
+
outputs=stylizedimg,
|
| 117 |
+
|
| 118 |
+
).launch()
|