Update app.py
Browse files
app.py
CHANGED
|
@@ -6,10 +6,7 @@ from PIL import Image
|
|
| 6 |
from rembg import remove
|
| 7 |
|
| 8 |
# Load the neural style transfer model from TensorFlow Hub
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Load a super-resolution model from TensorFlow Hub
|
| 12 |
-
super_res_model = hub.load('https://tfhub.dev/captain-pool/esrgan-tf2/1')
|
| 13 |
|
| 14 |
# Function to convert tensor to image
|
| 15 |
def tensor_to_image(tensor):
|
|
@@ -22,11 +19,12 @@ def tensor_to_image(tensor):
|
|
| 22 |
|
| 23 |
# Function to separate foreground and background
|
| 24 |
def separate_foreground_background(image):
|
|
|
|
| 25 |
if isinstance(image, np.ndarray):
|
| 26 |
image = Image.fromarray(image)
|
| 27 |
-
|
| 28 |
output_image = remove(image)
|
| 29 |
-
input_rgb = np.array(image.convert('RGB'))
|
| 30 |
output_rgba = np.array(output_image)
|
| 31 |
|
| 32 |
alpha = output_rgba[:, :, 3]
|
|
@@ -43,54 +41,31 @@ def apply_style_transfer(content_image, style_image, intensity=1.0):
|
|
| 43 |
content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.0
|
| 44 |
style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.0
|
| 45 |
|
|
|
|
| 46 |
style_image = style_image * intensity
|
| 47 |
-
outputs =
|
| 48 |
stylized_image = outputs[0]
|
| 49 |
|
| 50 |
return tensor_to_image(stylized_image)
|
| 51 |
|
| 52 |
-
# Super-resolution function
|
| 53 |
-
def apply_super_resolution(image):
|
| 54 |
-
image = np.array(image).astype(np.float32) / 255.0
|
| 55 |
-
sr_image = super_res_model(tf.constant(image[np.newaxis, ...]))
|
| 56 |
-
sr_image = sr_image[0]
|
| 57 |
-
return tensor_to_image(sr_image)
|
| 58 |
-
|
| 59 |
# Function to process image
|
| 60 |
def process_image(content_image, style_image):
|
| 61 |
-
# Ensure style_image is a PIL Image
|
| 62 |
-
if isinstance(style_image, np.ndarray):
|
| 63 |
-
style_image = Image.fromarray(style_image)
|
| 64 |
-
|
| 65 |
foreground, background = separate_foreground_background(content_image)
|
| 66 |
|
| 67 |
# Convert to RGB format by removing the alpha channel
|
| 68 |
foreground_rgb = np.array(foreground.convert('RGB'))
|
| 69 |
background_rgb = np.array(background)
|
| 70 |
|
| 71 |
-
styled_foreground = apply_style_transfer(foreground_rgb,
|
| 72 |
-
styled_background = apply_style_transfer(background_rgb,
|
| 73 |
-
|
| 74 |
-
# Resize the styled images to match each other using a fixed target size
|
| 75 |
-
target_size = (512, 512)
|
| 76 |
-
styled_foreground = styled_foreground.resize(target_size, Image.LANCZOS)
|
| 77 |
-
styled_background = styled_background.resize(target_size, Image.LANCZOS)
|
| 78 |
-
|
| 79 |
-
# Apply super-resolution to enhance quality
|
| 80 |
-
enhanced_foreground = apply_super_resolution(styled_foreground)
|
| 81 |
-
enhanced_background = apply_super_resolution(styled_background)
|
| 82 |
-
|
| 83 |
-
enhanced_foreground_np = np.array(enhanced_foreground)
|
| 84 |
-
enhanced_background_np = np.array(enhanced_background)
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
alpha = Image.fromarray((alpha * 255).astype(np.uint8)).resize(enhanced_foreground_np.shape[1::-1], Image.LANCZOS)
|
| 89 |
-
alpha = np.array(alpha) / 255.0
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
|
| 93 |
-
|
|
|
|
| 94 |
|
| 95 |
combined_image = Image.fromarray(np.clip(combined_image_np, 0, 255).astype(np.uint8))
|
| 96 |
|
|
@@ -104,5 +79,6 @@ gr.Interface(
|
|
| 104 |
fn=process_image,
|
| 105 |
inputs=[image1, image2],
|
| 106 |
outputs=stylizedimg,
|
| 107 |
-
title='Stylized Foreground and Background Combination
|
|
|
|
| 108 |
).launch()
|
|
|
|
| 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):
|
|
|
|
| 19 |
|
| 20 |
# Function to separate foreground and background
|
| 21 |
def separate_foreground_background(image):
|
| 22 |
+
# Ensure the image is a PIL Image
|
| 23 |
if isinstance(image, np.ndarray):
|
| 24 |
image = Image.fromarray(image)
|
| 25 |
+
|
| 26 |
output_image = remove(image)
|
| 27 |
+
input_rgb = np.array(image.convert('RGB')) # Ensure RGB format
|
| 28 |
output_rgba = np.array(output_image)
|
| 29 |
|
| 30 |
alpha = output_rgba[:, :, 3]
|
|
|
|
| 41 |
content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.0
|
| 42 |
style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.0
|
| 43 |
|
| 44 |
+
# Adjust the style intensity
|
| 45 |
style_image = style_image * intensity
|
| 46 |
+
outputs = model(tf.constant(content_image), tf.constant(style_image))
|
| 47 |
stylized_image = outputs[0]
|
| 48 |
|
| 49 |
return tensor_to_image(stylized_image)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# Function to process image
|
| 52 |
def process_image(content_image, style_image):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
foreground, background = separate_foreground_background(content_image)
|
| 54 |
|
| 55 |
# Convert to RGB format by removing the alpha channel
|
| 56 |
foreground_rgb = np.array(foreground.convert('RGB'))
|
| 57 |
background_rgb = np.array(background)
|
| 58 |
|
| 59 |
+
styled_foreground = apply_style_transfer(foreground_rgb, style_image, intensity=1.0)
|
| 60 |
+
styled_background = apply_style_transfer(background_rgb, style_image, intensity=0.3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
styled_foreground_np = np.array(styled_foreground)
|
| 63 |
+
styled_background_np = np.array(styled_background)
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# Extract the alpha channel from the foreground
|
| 66 |
+
alpha = np.array(foreground)[:, :, 3] / 255.0
|
| 67 |
+
combined_image_np = (styled_foreground_np * alpha[..., np.newaxis] +
|
| 68 |
+
styled_background_np * (1 - alpha[..., np.newaxis]))
|
| 69 |
|
| 70 |
combined_image = Image.fromarray(np.clip(combined_image_np, 0, 255).astype(np.uint8))
|
| 71 |
|
|
|
|
| 79 |
fn=process_image,
|
| 80 |
inputs=[image1, image2],
|
| 81 |
outputs=stylizedimg,
|
| 82 |
+
title='Stylized Foreground and Background Combination',
|
| 83 |
+
|
| 84 |
).launch()
|