File size: 1,994 Bytes
eab0c71
 
 
1ffcb74
47cc597
1ffcb74
91c5d6f
40bdd20
 
91c5d6f
eab0c71
 
 
91c5d6f
40bdd20
91c5d6f
eab0c71
 
 
 
 
509507f
 
 
 
 
 
47cc597
ece08bc
91c5d6f
ece08bc
 
47cc597
91c5d6f
eab0c71
 
 
91c5d6f
47cc597
 
 
91c5d6f
47cc597
 
91c5d6f
47cc597
 
 
 
 
eab0c71
91c5d6f
eab0c71
 
91c5d6f
eab0c71
 
 
 
 
47cc597
91c5d6f
 
47cc597
 
 
 
eab0c71
1ffcb74
47cc597
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import gradio as gr
import cv2


IMAGE_SIZE = (256, 256)


style_transfer_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')

def load_image(image):
    
    image = cv2.resize(image, IMAGE_SIZE, interpolation=cv2.INTER_AREA)
    
    image = image.astype(np.float32)[np.newaxis, ...] / 255.
    if image.shape[-1] == 4:
        image = image[..., :3]
    return image

def apply_sharpness(image, intensity):
    kernel = np.array([[0, -intensity, 0],
                       [-intensity, 1 + 4 * intensity, -intensity],
                       [0, -intensity, 0]])
    sharp_image = cv2.filter2D(image, -1, kernel)
    return np.clip(sharp_image, 0, 255)

def interpolate_images(baseline, target, alpha):
    
    return baseline + alpha * (target - baseline)

def style_transfer(content_image, style_image, style_density, content_sharpness):
    #
    content_image = load_image(content_image)
    style_image = load_image(style_image)

    
    content_image_sharp = apply_sharpness(content_image[0], intensity=content_sharpness)
    content_image_sharp = content_image_sharp[np.newaxis, ...]

    
    stylized_image = style_transfer_model(tf.constant(content_image_sharp), tf.constant(style_image))[0]

    
    stylized_image = interpolate_images(
        baseline=content_image[0],
        target=stylized_image.numpy(),
        alpha=style_density
    )

    
    stylized_image = np.array(stylized_image * 255, np.uint8)

    
    stylized_image = np.squeeze(stylized_image)
    return stylized_image

iface = gr.Interface(
    fn=style_transfer,
    inputs=[
        gr.Image(label="Content Image"),  
        gr.Image(label="Style Image"),    
        gr.Slider(minimum=0, maximum=1, value=0.5, label="Adjust Style Density"),
        gr.Slider(minimum=0, maximum=1, value=0.5, label="Content Sharpness")
    ],
    outputs=gr.Image(label="Stylized Image")
)

iface.launch()