Akbartus commited on
Commit
ba951fc
·
1 Parent(s): 9dee66a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -18
app.py CHANGED
@@ -2,37 +2,63 @@
2
 
3
  # gr.Interface.load("models/Akbartus/Lora360").launch(show_api=True)
4
 
5
- import gradio as gr
6
- import torch
 
7
  from PIL import Image
8
- from torchvision.transforms import ToTensor, ToPILImage
9
- from transformers import AutoModel
 
10
 
11
  # Load the original model
12
- original_model = gr.Interface.load("models/Akbartus/Lora360")
13
 
14
  # Load the super-resolution model from Hugging Face's model hub
15
- super_resolution_model = AutoModel.from_pretrained('keras-io/super-resolution')
16
 
17
  def process_image(input_image):
18
  # Run the original model
19
  output_image = original_model(input_image)
20
 
21
- # Transform the output image to tensor
22
- output_image = ToTensor()(output_image).unsqueeze(0)
 
 
 
 
 
 
23
 
24
- # Ensure the super-resolution model is in eval mode and perform super-resolution
25
- super_resolution_model.eval()
26
- with torch.no_grad():
27
- super_res_image = super_resolution_model(output_image)
28
 
29
- # Convert the output tensor to an image
30
- super_res_image = ToPILImage()(super_res_image[0])
31
- return super_res_image
32
 
33
- # Define the Gradio interface
34
- iface = gr.Interface(fn=process_image, inputs="image", outputs="image")
35
- iface.launch()
 
 
 
 
 
 
36
 
 
 
37
 
 
38
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  # gr.Interface.load("models/Akbartus/Lora360").launch(show_api=True)
4
 
5
+ import tensorflow as tf
6
+ import math
7
+ import numpy as np
8
  from PIL import Image
9
+ from tensorflow.keras.preprocessing.image import img_to_array
10
+ from huggingface_hub import from_pretrained_keras
11
+ import gradio as gr
12
 
13
  # Load the original model
14
+ original_model = gr.Interface.load("models/Tester/Lora360")
15
 
16
  # Load the super-resolution model from Hugging Face's model hub
17
+ super_resolution_model = from_pretrained_keras("keras-io/super-resolution")
18
 
19
  def process_image(input_image):
20
  # Run the original model
21
  output_image = original_model(input_image)
22
 
23
+ # Resize the image to 100x100
24
+ output_image = output_image.resize((100,100))
25
+
26
+ # Convert the image to YCbCr color space
27
+ ycbcr = output_image.convert("YCbCr")
28
+ y, cb, cr = ycbcr.split()
29
+ y = img_to_array(y)
30
+ y = y.astype("float32") / 255.0
31
 
32
+ # Expand the dimensions of the image for model input
33
+ input = np.expand_dims(y, axis=0)
34
+ out = super_resolution_model.predict(input)
 
35
 
36
+ # Process the output
37
+ out_img_y = out[0]
38
+ out_img_y *= 255.0
39
 
40
+ # Restore the image in RGB color space.
41
+ out_img_y = out_img_y.clip(0, 255)
42
+ out_img_y = out_img_y.reshape((np.shape(out_img_y)[0], np.shape(out_img_y)[1]))
43
+ out_img_y = Image.fromarray(np.uint8(out_img_y), mode="L")
44
+ out_img_cb = cb.resize(out_img_y.size, Image.BICUBIC)
45
+ out_img_cr = cr.resize(out_img_y.size, Image.BICUBIC)
46
+ out_img = Image.merge("YCbCr", (out_img_y, out_img_cb, out_img_cr)).convert(
47
+ "RGB"
48
+ )
49
 
50
+ # Return the original resized image and the super-resolved image
51
+ return (output_image, out_img)
52
 
53
+ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1609.05158' target='_blank'>Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network</a></p><center> <a href='https://keras.io/examples/vision/super_resolution_sub_pixel/' target='_blank'>Image Super-Resolution using an Efficient Sub-Pixel CNN</a></p> <center>Contributors: <a href='https://twitter.com/Cr0wley_zz'>Devjyoti Chakraborty</a>|<a href='https://twitter.com/ritwik_raha'>Ritwik Raha</a>|<a href='https://twitter.com/ariG23498'>Aritra Roy Gosthipaty</a></center>"
54
 
55
+ iface = gr.Interface(
56
+ fn=process_image,
57
+ title = " Image Super-resolution",
58
+ description = "This space is a demo of the keras tutorial 'Image Super-Resolution using an Efficient Sub-Pixel CNN' based on the paper 'Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network' 👀",
59
+ article = article,
60
+ inputs=gr.inputs.Image(label="Input Image"),
61
+ outputs=[gr.outputs.Image(label="Resized 100x100 image"),
62
+ gr.outputs.Image(label="Super-resolution 300x300 image")
63
+ ],
64
+ ).launch()