Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,47 @@
|
|
|
|
|
|
|
|
| 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 |
-
import cv2
|
| 8 |
|
| 9 |
# Load the neural style transfer model from TensorFlow Hub
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# Function to convert tensor to image
|
| 13 |
def tensor_to_image(tensor):
|
| 14 |
tensor = tensor * 255
|
| 15 |
tensor = np.array(tensor, dtype=np.uint8)
|
|
@@ -18,19 +50,16 @@ def tensor_to_image(tensor):
|
|
| 18 |
tensor = tensor[0]
|
| 19 |
return Image.fromarray(tensor)
|
| 20 |
|
| 21 |
-
# Function to resize image while maintaining aspect ratio
|
| 22 |
def resize_image(image, target_size):
|
| 23 |
image.thumbnail(target_size, Image.LANCZOS)
|
| 24 |
return image
|
| 25 |
|
| 26 |
-
# Function to separate foreground and background
|
| 27 |
def separate_foreground_background(image):
|
| 28 |
-
# Ensure the image is a PIL Image
|
| 29 |
if isinstance(image, np.ndarray):
|
| 30 |
image = Image.fromarray(image)
|
| 31 |
|
| 32 |
output_image = remove(image)
|
| 33 |
-
input_rgb = np.array(image.convert('RGB'))
|
| 34 |
output_rgba = np.array(output_image)
|
| 35 |
|
| 36 |
alpha = output_rgba[:, :, 3]
|
|
@@ -42,67 +71,17 @@ def separate_foreground_background(image):
|
|
| 42 |
background = Image.fromarray(background_rgb)
|
| 43 |
return foreground, background
|
| 44 |
|
| 45 |
-
# Style transfer function
|
| 46 |
def apply_style_transfer(content_image, style_image, intensity=1.0):
|
| 47 |
content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.0
|
| 48 |
style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.0
|
| 49 |
|
| 50 |
-
# Adjust the style intensity
|
| 51 |
style_image = style_image * intensity
|
| 52 |
-
outputs =
|
| 53 |
stylized_image = outputs[0]
|
| 54 |
|
| 55 |
return tensor_to_image(stylized_image)
|
| 56 |
|
| 57 |
-
# Function to apply glossy effect
|
| 58 |
-
def apply_glossy_effect(image):
|
| 59 |
-
# Convert the input image to a PIL Image
|
| 60 |
-
image = Image.fromarray(image)
|
| 61 |
-
|
| 62 |
-
# Enhance color saturation
|
| 63 |
-
color_enhancer = ImageEnhance.Color(image)
|
| 64 |
-
vibrant_image = color_enhancer.enhance(1) # Increase saturation
|
| 65 |
-
|
| 66 |
-
# Enhance brightness
|
| 67 |
-
brightness_enhancer = ImageEnhance.Brightness(vibrant_image)
|
| 68 |
-
bright_image = brightness_enhancer.enhance(1) # Adjust brightness
|
| 69 |
-
|
| 70 |
-
# Apply a soft blur to create a subtle glow
|
| 71 |
-
glow_image = bright_image.filter(ImageFilter.GaussianBlur(radius=2))
|
| 72 |
-
|
| 73 |
-
# Blend the original and glow images to create the glossy effect
|
| 74 |
-
glossy_image = Image.blend(bright_image, glow_image, alpha=0.9)
|
| 75 |
-
|
| 76 |
-
# Enhance contrast
|
| 77 |
-
contrast_enhancer = ImageEnhance.Contrast(glossy_image)
|
| 78 |
-
glossy_image = contrast_enhancer.enhance(1.5) # Increase contrast
|
| 79 |
-
|
| 80 |
-
# Enhance sharpness
|
| 81 |
-
sharpness_enhancer = ImageEnhance.Sharpness(glossy_image)
|
| 82 |
-
glossy_image = sharpness_enhancer.enhance(2.8) # Increase sharpness
|
| 83 |
-
|
| 84 |
-
# Apply edge enhancement
|
| 85 |
-
edges = glossy_image.filter(ImageFilter.FIND_EDGES)
|
| 86 |
-
glossy_image = Image.blend(glossy_image, edges, alpha=0.1) # Subtle edge enhancement
|
| 87 |
-
|
| 88 |
-
# Apply vignette effect
|
| 89 |
-
np_img = np.array(glossy_image)
|
| 90 |
-
rows, cols, _ = np_img.shape
|
| 91 |
-
kernel_x = cv2.getGaussianKernel(cols, cols / 2)
|
| 92 |
-
kernel_y = cv2.getGaussianKernel(rows, rows / 2)
|
| 93 |
-
kernel = kernel_y @ kernel_x.T
|
| 94 |
-
mask = 255 * kernel / np.max(kernel)
|
| 95 |
-
vignette = np.zeros_like(np_img, dtype=np.uint8)
|
| 96 |
-
for i in range(3): # Apply vignette on each channel
|
| 97 |
-
vignette[..., i] = np_img[..., i] * mask.astype(np.float32) / 255
|
| 98 |
-
glossy_image = Image.fromarray(np.clip(vignette, 0, 255).astype(np.uint8))
|
| 99 |
-
|
| 100 |
-
# Convert the glossy image back to a NumPy array
|
| 101 |
-
return np.array(glossy_image)
|
| 102 |
-
|
| 103 |
-
# Function to process image
|
| 104 |
def process_image(content_image, style_image):
|
| 105 |
-
# Ensure content_image and style_image are PIL Images
|
| 106 |
if isinstance(content_image, np.ndarray):
|
| 107 |
content_image = Image.fromarray(content_image)
|
| 108 |
if isinstance(style_image, np.ndarray):
|
|
@@ -110,29 +89,23 @@ def process_image(content_image, style_image):
|
|
| 110 |
|
| 111 |
foreground, background = separate_foreground_background(content_image)
|
| 112 |
|
| 113 |
-
# Define the target size based on the content image
|
| 114 |
target_size = content_image.size
|
| 115 |
|
| 116 |
-
# Resize the foreground and background to match the target size
|
| 117 |
foreground = resize_image(foreground, target_size)
|
| 118 |
background = resize_image(background, target_size)
|
| 119 |
|
| 120 |
-
# Convert to RGB format by removing the alpha channel
|
| 121 |
foreground_rgb = np.array(foreground.convert('RGB'))
|
| 122 |
background_rgb = np.array(background)
|
| 123 |
|
| 124 |
-
# Apply style transfer
|
| 125 |
styled_foreground = apply_style_transfer(foreground_rgb, np.array(style_image), intensity=1.0)
|
| 126 |
styled_background = apply_style_transfer(background_rgb, np.array(style_image), intensity=0.3)
|
| 127 |
|
| 128 |
-
# Ensure both styled images have the same size
|
| 129 |
styled_foreground = styled_foreground.resize(target_size, Image.LANCZOS)
|
| 130 |
styled_background = styled_background.resize(target_size, Image.LANCZOS)
|
| 131 |
|
| 132 |
styled_foreground_np = np.array(styled_foreground)
|
| 133 |
styled_background_np = np.array(styled_background)
|
| 134 |
|
| 135 |
-
# Extract the alpha channel from the foreground
|
| 136 |
alpha = np.array(foreground)[:, :, 3] / 255.0
|
| 137 |
alpha_resized = np.array(foreground.resize(target_size))[:, :, 3] / 255.0
|
| 138 |
|
|
@@ -141,8 +114,8 @@ def process_image(content_image, style_image):
|
|
| 141 |
|
| 142 |
combined_image = Image.fromarray(np.clip(combined_image_np, 0, 255).astype(np.uint8))
|
| 143 |
|
| 144 |
-
# Apply
|
| 145 |
-
final_image =
|
| 146 |
|
| 147 |
return final_image
|
| 148 |
|
|
@@ -154,5 +127,5 @@ gr.Interface(
|
|
| 154 |
fn=process_image,
|
| 155 |
inputs=[image1, image2],
|
| 156 |
outputs=stylizedimg,
|
| 157 |
-
|
| 158 |
).launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
+
import onnxruntime as ort
|
| 6 |
+
from PIL import Image
|
| 7 |
import tensorflow as tf
|
| 8 |
import tensorflow_hub as hub
|
|
|
|
|
|
|
| 9 |
from rembg import remove
|
|
|
|
| 10 |
|
| 11 |
# Load the neural style transfer model from TensorFlow Hub
|
| 12 |
+
style_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
|
| 13 |
+
|
| 14 |
+
# Set up ONNX runtime for cartoonizer model
|
| 15 |
+
_sess_options = ort.SessionOptions()
|
| 16 |
+
_sess_options.intra_op_num_threads = os.cpu_count()
|
| 17 |
+
MODEL_SESS = ort.InferenceSession(
|
| 18 |
+
"cartoonizer.onnx", _sess_options, providers=["CPUExecutionProvider"]
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
def preprocess_image(image: Image) -> np.ndarray:
|
| 22 |
+
image = np.array(image)
|
| 23 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 24 |
+
|
| 25 |
+
h, w, c = np.shape(image)
|
| 26 |
+
if min(h, w) > 720:
|
| 27 |
+
if h > w:
|
| 28 |
+
h, w = int(720 * h / w), 720
|
| 29 |
+
else:
|
| 30 |
+
h, w = 720, int(720 * w / h)
|
| 31 |
+
image = cv2.resize(image, (w, h), interpolation=cv2.INTER_AREA)
|
| 32 |
+
h, w = (h // 8) * 8, (w // 8) * 8
|
| 33 |
+
image = image[:h, :w, :]
|
| 34 |
+
image = image.astype(np.float32) / 127.5 - 1
|
| 35 |
+
return np.expand_dims(image, axis=0)
|
| 36 |
+
|
| 37 |
+
def cartoonize(image: np.ndarray) -> Image:
|
| 38 |
+
image = preprocess_image(image)
|
| 39 |
+
results = MODEL_SESS.run(None, {"input_photo:0": image})
|
| 40 |
+
output = (np.squeeze(results[0]) + 1.0) * 127.5
|
| 41 |
+
output = np.clip(output, 0, 255).astype(np.uint8)
|
| 42 |
+
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
|
| 43 |
+
return Image.fromarray(output)
|
| 44 |
|
|
|
|
| 45 |
def tensor_to_image(tensor):
|
| 46 |
tensor = tensor * 255
|
| 47 |
tensor = np.array(tensor, dtype=np.uint8)
|
|
|
|
| 50 |
tensor = tensor[0]
|
| 51 |
return Image.fromarray(tensor)
|
| 52 |
|
|
|
|
| 53 |
def resize_image(image, target_size):
|
| 54 |
image.thumbnail(target_size, Image.LANCZOS)
|
| 55 |
return image
|
| 56 |
|
|
|
|
| 57 |
def separate_foreground_background(image):
|
|
|
|
| 58 |
if isinstance(image, np.ndarray):
|
| 59 |
image = Image.fromarray(image)
|
| 60 |
|
| 61 |
output_image = remove(image)
|
| 62 |
+
input_rgb = np.array(image.convert('RGB'))
|
| 63 |
output_rgba = np.array(output_image)
|
| 64 |
|
| 65 |
alpha = output_rgba[:, :, 3]
|
|
|
|
| 71 |
background = Image.fromarray(background_rgb)
|
| 72 |
return foreground, background
|
| 73 |
|
|
|
|
| 74 |
def apply_style_transfer(content_image, style_image, intensity=1.0):
|
| 75 |
content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.0
|
| 76 |
style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.0
|
| 77 |
|
|
|
|
| 78 |
style_image = style_image * intensity
|
| 79 |
+
outputs = style_model(tf.constant(content_image), tf.constant(style_image))
|
| 80 |
stylized_image = outputs[0]
|
| 81 |
|
| 82 |
return tensor_to_image(stylized_image)
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
def process_image(content_image, style_image):
|
|
|
|
| 85 |
if isinstance(content_image, np.ndarray):
|
| 86 |
content_image = Image.fromarray(content_image)
|
| 87 |
if isinstance(style_image, np.ndarray):
|
|
|
|
| 89 |
|
| 90 |
foreground, background = separate_foreground_background(content_image)
|
| 91 |
|
|
|
|
| 92 |
target_size = content_image.size
|
| 93 |
|
|
|
|
| 94 |
foreground = resize_image(foreground, target_size)
|
| 95 |
background = resize_image(background, target_size)
|
| 96 |
|
|
|
|
| 97 |
foreground_rgb = np.array(foreground.convert('RGB'))
|
| 98 |
background_rgb = np.array(background)
|
| 99 |
|
|
|
|
| 100 |
styled_foreground = apply_style_transfer(foreground_rgb, np.array(style_image), intensity=1.0)
|
| 101 |
styled_background = apply_style_transfer(background_rgb, np.array(style_image), intensity=0.3)
|
| 102 |
|
|
|
|
| 103 |
styled_foreground = styled_foreground.resize(target_size, Image.LANCZOS)
|
| 104 |
styled_background = styled_background.resize(target_size, Image.LANCZOS)
|
| 105 |
|
| 106 |
styled_foreground_np = np.array(styled_foreground)
|
| 107 |
styled_background_np = np.array(styled_background)
|
| 108 |
|
|
|
|
| 109 |
alpha = np.array(foreground)[:, :, 3] / 255.0
|
| 110 |
alpha_resized = np.array(foreground.resize(target_size))[:, :, 3] / 255.0
|
| 111 |
|
|
|
|
| 114 |
|
| 115 |
combined_image = Image.fromarray(np.clip(combined_image_np, 0, 255).astype(np.uint8))
|
| 116 |
|
| 117 |
+
# Apply cartoonization to the final combined image
|
| 118 |
+
final_image = cartoonize(np.array(combined_image))
|
| 119 |
|
| 120 |
return final_image
|
| 121 |
|
|
|
|
| 127 |
fn=process_image,
|
| 128 |
inputs=[image1, image2],
|
| 129 |
outputs=stylizedimg,
|
| 130 |
+
title='Stylized Foreground and Background Combination with Cartoon Effect',
|
| 131 |
).launch()
|