# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="J0Dlgnkbqgpk"
# #NEURAL STYLE TRANSFER

# + id="8e-6IH1HRNnD"
# %%capture
# !pip install gradio

# + [markdown] id="uAHb2bq7qZrD"
# ##Importing Libraries

# + id="haTY0gGEPdOu"
import gradio as gr

# + id="gC7xVOx7zE9D"
import tensorflow_hub as hub
import tensorflow as tf
import matplotlib.pyplot as plt
import IPython.display as display
import matplotlib as mpl
import os
import time
from PIL import Image
import numpy as np
import PIL.Image
import time
import functools
import cv2

# + colab={"base_uri": "https://localhost:8080/"} id="Kg4mzkNruD8L" outputId="680e7797-ccb9-4d30-f4f3-25ef1e448c52"
# !mkdir nstmodel 
# !wget -c https://storage.googleapis.com/tfhub-modules/google/magenta/arbitrary-image-stylization-v1-256/2.tar.gz -O - | tar -xz -C /content/nstmodel


# + id="XsxV3105pGyz"
import tensorflow.keras
from PIL import Image, ImageOps
import numpy as np


MODEL_PATH='/content/nstmodel'

# Disable scientific notation for clarity
np.set_printoptions(suppress=True)

# Load the model
model = tensorflow.keras.models.load_model(MODEL_PATH)


# + id="Or4H3jtCc3Yq"
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)



# + [markdown] id="k28bGvpF0bgp"
# ##Saving unscaled Tensor images.

# + id="3HgBL3bCJwEZ"
def save_image(image, filename):
  """
    Saves unscaled Tensor Images.
    Args:
      image: 3D image tensor. [height, width, channels]
      filename: Name of the file to save to.
  """
  if not isinstance(image, Image.Image):
    image = tf.clip_by_value(image, 0, 255)
    image = Image.fromarray(tf.cast(image, tf.uint8).numpy())
  image.save("%s.jpg" % filename)
  print("Saved as %s.jpg" % filename)



# + [markdown] id="v1GRqqoq0LNp"
# ##Grayscaling image for testing purpose to check if we could get better results.

# + id="kHIud3iSa11t"
import cv2

def gray_scaled(inp_img):
  gray = cv2.cvtColor(inp_img, cv2.COLOR_BGR2GRAY)
  gray_img = np.zeros_like(inp_img)
  gray_img[:,:,0] = gray
  gray_img[:,:,1] = gray
  gray_img[:,:,2] = gray
  return gray_img
  

# + id="SBcz5IelMLRc"


def transform_mymodel(content_image,style_image):
  # Convert to float32 numpy array, add batch dimension, and normalize to range [0, 1]
  content_image=gray_scaled(content_image)
  content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.0
  style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.0
 
  #Resizing image
  style_image = tf.image.resize(style_image, (256, 256))

  # Stylize image
  outputs = model(tf.constant(content_image), tf.constant(style_image))
  stylized_image = outputs[0]

  # stylized = tf.image.resize(stylized_image, (356, 356))
  stylized_image =tensor_to_image(stylized_image)
  save_image(stylized_image,'stylized')
  return stylized_image



# + id="L7OaaI836mme"
def gradio_intrface(mymodel):
# Initializing the input component 
  image1 = gr.inputs.Image() #CONTENT IMAGE
  image2 = gr.inputs.Image() #STYLE IMAGE
  stylizedimg=gr.outputs.Image() 
  gr.Interface(fn=mymodel, inputs= [image1,image2] , outputs= stylizedimg,title='Style Transfer').launch(share=False,)


# + [markdown] id="XLPR91kkz_Jy"
# ##The function will be launched both  inline and outline  where u need to add a content and style image.
#

# + id="ZalVEN2YWvHv" colab={"base_uri": "https://localhost:8080/", "height": 590} outputId="fb4c7cad-a7d2-49b3-b41b-25f9dd12c39d"
gradio_intrface(transform_mymodel)

# + id="YCF7bN6VzULi"


# + id="Cd7GfC2SzUI6"


StarcoderdataJupyterScriptsDedupFiltered