Spaces:
Runtime error
Runtime error
GIanlucaRub
commited on
Commit
•
bf2031a
1
Parent(s):
4a36ad4
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from PIL import Image
|
3 |
+
from io import BytesIO
|
4 |
+
from numpy import asarray
|
5 |
+
import gradio as gr
|
6 |
+
import numpy as np
|
7 |
+
from math import ceil
|
8 |
+
from huggingface_hub import from_pretrained_keras
|
9 |
+
|
10 |
+
|
11 |
+
def getRequest():
|
12 |
+
r = requests.get(
|
13 |
+
'https://api.nasa.gov/planetary/apod?api_key=0eyGPKWmJmE5Z0Ijx25oG56ydbTKWE2H75xuEefx')
|
14 |
+
result = r.json()
|
15 |
+
receive = requests.get(result['url'])
|
16 |
+
img = Image.open(BytesIO(receive.content)).convert('RGB')
|
17 |
+
return img
|
18 |
+
|
19 |
+
|
20 |
+
model = from_pretrained_keras("GIanlucaRub/doubleResFinal")
|
21 |
+
|
22 |
+
|
23 |
+
def double_res(input_image):
|
24 |
+
input_height = input_image.shape[0]
|
25 |
+
input_width = input_image.shape[1]
|
26 |
+
height = ceil(input_height/128)
|
27 |
+
width = ceil(input_width/128)
|
28 |
+
expanded_input_image = np.zeros((128*height, 128*width, 3), dtype=np.uint8)
|
29 |
+
np.copyto(expanded_input_image[0:input_height, 0:input_width], input_image)
|
30 |
+
|
31 |
+
output_image = np.zeros((128*height*2, 128*width*2, 3), dtype=np.float32)
|
32 |
+
|
33 |
+
for i in range(height):
|
34 |
+
for j in range(width):
|
35 |
+
temp_slice = expanded_input_image[i *
|
36 |
+
128:(i+1)*128, j*128:(j+1)*128]/255
|
37 |
+
upsampled_slice = model.predict(temp_slice[np.newaxis, ...])
|
38 |
+
np.copyto(output_image[i*256:(i+1)*256, j *
|
39 |
+
256:(j+1)*256], upsampled_slice[0])
|
40 |
+
if i != 0 and j != 0 and i != height-1 and j != width-1:
|
41 |
+
# removing inner borders
|
42 |
+
right_slice = expanded_input_image[i *
|
43 |
+
128:(i+1)*128, (j+1)*128-64:(j+1)*128+64]/255
|
44 |
+
right_upsampled_slice = model.predict(
|
45 |
+
right_slice[np.newaxis, ...])
|
46 |
+
resized_right_slice = right_upsampled_slice[0][64:192, 64:192]
|
47 |
+
np.copyto(output_image[i*256+64:(i+1)*256-64,
|
48 |
+
(j+1)*256-64:(j+1)*256+64], resized_right_slice)
|
49 |
+
|
50 |
+
left_slice = expanded_input_image[i *
|
51 |
+
128:(i+1)*128, j*128-64:(j)*128+64]/255
|
52 |
+
left_upsampled_slice = model.predict(
|
53 |
+
left_slice[np.newaxis, ...])
|
54 |
+
resized_left_slice = left_upsampled_slice[0][64:192, 64:192]
|
55 |
+
np.copyto(output_image[i*256+64:(i+1)*256-64,
|
56 |
+
j*256-64:j*256+64], resized_left_slice)
|
57 |
+
|
58 |
+
upper_slice = expanded_input_image[(
|
59 |
+
i+1)*128-64:(i+1)*128+64, j*128:(j+1)*128]/255
|
60 |
+
upper_upsampled_slice = model.predict(
|
61 |
+
upper_slice[np.newaxis, ...])
|
62 |
+
resized_upper_slice = upper_upsampled_slice[0][64:192, 64:192]
|
63 |
+
np.copyto(output_image[(i+1)*256-64:(i+1)*256+64,
|
64 |
+
j*256+64:(j+1)*256-64], resized_upper_slice)
|
65 |
+
|
66 |
+
lower_slice = expanded_input_image[i *
|
67 |
+
128-64:i*128+64, j*128:(j+1)*128]/255
|
68 |
+
lower_upsampled_slice = model.predict(
|
69 |
+
lower_slice[np.newaxis, ...])
|
70 |
+
resized_lower_slice = lower_upsampled_slice[0][64:192, 64:192]
|
71 |
+
np.copyto(output_image[i*256-64:i*256+64,
|
72 |
+
j*256+64:(j+1)*256-64], resized_lower_slice)
|
73 |
+
|
74 |
+
|
75 |
+
# removing angles
|
76 |
+
lower_right_slice = expanded_input_image[i *
|
77 |
+
128-64:i*128+64, (j+1)*128-64:(j+1)*128+64]/255
|
78 |
+
lower_right_upsampled_slice = model.predict(
|
79 |
+
lower_right_slice[np.newaxis, ...])
|
80 |
+
resized_lower_right_slice = lower_right_upsampled_slice[0][64:192, 64:192]
|
81 |
+
np.copyto(output_image[i*256-64:i*256+64, (j+1)
|
82 |
+
* 256-64:(j+1)*256+64], resized_lower_right_slice)
|
83 |
+
|
84 |
+
lower_left_slice = expanded_input_image[i *
|
85 |
+
128-64:i*128+64, j*128-64:j*128+64]/255
|
86 |
+
lower_left_upsampled_slice = model.predict(
|
87 |
+
lower_left_slice[np.newaxis, ...])
|
88 |
+
resized_lower_left_slice = lower_left_upsampled_slice[0][64:192, 64:192]
|
89 |
+
np.copyto(
|
90 |
+
output_image[i*256-64:i*256+64, j*256-64:j*256+64], resized_lower_left_slice)
|
91 |
+
|
92 |
+
resized_output_image = output_image[0:input_height*2, 0:input_width*2]
|
93 |
+
return resized_output_image
|
94 |
+
|
95 |
+
def get_new_img():
|
96 |
+
original_img = getRequest()
|
97 |
+
numpydata = asarray(original_img)
|
98 |
+
doubled_img = double_res(numpydata) # numpy.ndarray
|
99 |
+
return original_img,doubled_img
|
100 |
+
|
101 |
+
original_img, doubled_img = get_new_img()
|
102 |
+
|
103 |
+
with gr.Blocks() as demo:
|
104 |
+
with gr.Row():
|
105 |
+
with gr.Column():
|
106 |
+
gr.Label("Original image")
|
107 |
+
original = gr.Image(original_img)
|
108 |
+
with gr.Column():
|
109 |
+
gr.Label("Image with resolution doubled")
|
110 |
+
doubled = gr.Image(doubled_img)
|
111 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
112 |
+
btn_get = gr.Button("Get the new daily image")
|
113 |
+
# Event
|
114 |
+
btn_get.click(get_new_img, inputs=None, outputs = [original,doubled])
|
115 |
+
demo.launch()
|