File size: 1,620 Bytes
201e368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr

from diffusers import StableDiffusionUpscalePipeline
from PIL import Image
from io import BytesIO
import torch



model_id = "stabilityai/stable-diffusion-x4-upscaler"
pipeline = StableDiffusionUpscalePipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipeline = pipeline.to("cuda")


# def predict(input_img):
#     predictions = predictor(input_img)
#     return input_img, {p["label"]: p["score"] for p in predictions} 


def upscalex4(input_img):
    low_res_img = resize_on_scale(input_img)
    prompt = "highly detailed"
    upscaled_img = pipeline(prompt=prompt, image=low_res_img).images[0]
    print("Low resolution image size = ", low_res_img.size)
    print("Upscaled image size = ", upscaled_img.size)
    return upscaled_img


def resize_on_scale(input_img):
    low_res_img = input_img.convert("RGB")
    wsize = 300
    wpercent = (wsize / float(input_img.size[0]))
    hsize = int((float(input_img.size[1]) * float(wpercent)))
    low_res_img = low_res_img.resize((wsize, hsize))
    return low_res_img


gradio_app = gr.Interface(
    upscalex4, 
    inputs=gr.Image(label="Select a blurry image", sources=['upload', 'webcam', 'clipboard'], type="pil"),
    outputs=gr.Image(label="Processed Image"), 
    title="Image Upscaler",
)

if __name__ == "__main__":
    gradio_app.launch()








# img = Image.open(requests.get("https://pbs.twimg.com/profile_images/1600764211378139137/HirERJI5_400x400.jpg", stream=True).raw)
# img = img.resize((64, 64))

# print("Original image size = ", img.size)
# print("Upscaled image size = ", upscaled_img.size)
# upscaled_img.show()