Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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/autoencoder_model_d_0")
|
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 |
+
|
96 |
+
with gr.Blocks() as demo:
|
97 |
+
with gr.Row():
|
98 |
+
with gr.Column():
|
99 |
+
gr.Label("Original image")
|
100 |
+
input_img = gr.Image(getRequest())
|
101 |
+
with gr.Column():
|
102 |
+
gr.Label("Image with resolution doubled")
|
103 |
+
numpydata = asarray(getRequest())
|
104 |
+
output = double_res(numpydata) # numpy.ndarray
|
105 |
+
input_img = gr.Image(output)
|
106 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
107 |
+
btn_get = gr.Button("Get the new daily-image")
|
108 |
+
# Event
|
109 |
+
btn_get.click(demo.launch())
|
110 |
+
demo.launch()
|