File size: 2,408 Bytes
3a76a8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2899e27
3a76a8c
 
621c3a4
3a76a8c
 
 
915ddca
 
 
 
3a76a8c
 
915ddca
 
 
 
 
3a76a8c
 
 
 
 
 
 
 
 
915ddca
3a76a8c
 
 
 
 
2e77929
6733e56
d0d4c48
91671ee
3a76a8c
 
 
 
 
aa4e1bc
 
3a76a8c
 
 
 
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
import os
import tensorflow as tf
os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED'
import numpy as np
import PIL.Image
import gradio as gr
import tensorflow_hub as hub
import matplotlib.pyplot as plt

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

def tensor_to_image(tensor):
    tensor = tensor*255
    tensor = np.array(tensor, dtype=np.uint8)
    if np.ndim(tensor) > 3:
        assert tensor.shape[0] == 1
        tensor = tensor[0]
    return PIL.Image.fromarray(tensor)

content_image_input = gr.Image(label="Content Image", sources = ('upload', 'webcam'))
style_image_input = gr.Image(label="Style Image")

text = "#Developer: Aditya Jadhav   #College: Government Polytechnic Nagpur   #Branch: AIML (Artificial Intelligence and Machine Learning), 3rd year   #Date: 17th November 2023   #LinkedIn: https://www.linkedin.com/in/aditya-jadhav-27a790269   #GitHub: https://github.com/AdityaJ9801  #Introduction: The application focuses on neural style transfer, where the style from a style image is applied to a content image."


def perform_neural_transfer(content_image_input, style_image_input):
    if content_image_input is None:
        # Handle the case when content image is not provided
        return PIL.Image.fromarray(np.zeros((1, 1, 3), dtype=np.uint8))

    # Load content images
    content_image = content_image_input.astype(np.float32)[np.newaxis, ...] / 255.

    if style_image_input is None:
        # Handle the case when style image is not provided
        return tensor_to_image(content_image)

    style_image = style_image_input.astype(np.float32)[np.newaxis, ...] / 255.

    # Apply neural style transfer
    outputs = hub_module(tf.constant(content_image), tf.constant(style_image))
    stylized_image = outputs[0]

    return tensor_to_image(stylized_image)



app_interface = gr.Interface(
    fn=perform_neural_transfer,
    inputs=[content_image_input, style_image_input],
    outputs="image",
    title="Art Generation with Neural Style Transfer",
    description=text,
    concurrency_limit = 5,
    theme = gr.themes.Default(),
        examples=[
        ['content1.jpg','image1.jpg'],
            [None,'image2.jpg'],
            [None,'image4.jpg'],
        [None,'image5.jpg'],
        [None,'image6.jpg'],
        [None,'image7.jpg'],
        [None,'image8.jpg'],
        ]
)

app_interface.launch(debug =True)