Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
r = requests.get(
|
11 |
+
'https://api.nasa.gov/planetary/apod?api_key=0eyGPKWmJmE5Z0Ijx25oG56ydbTKWE2H75xuEefx')
|
12 |
+
result = r.json()
|
13 |
+
receive = requests.get(result['url'])
|
14 |
+
img = Image.open(BytesIO(receive.content)).convert('RGB')
|
15 |
+
|
16 |
+
model = from_pretrained_keras("GIanlucaRub/autoencoder_model_d_0")
|
17 |
+
|
18 |
+
|
19 |
+
def double_res(input_image):
|
20 |
+
input_height = input_image.shape[0]
|
21 |
+
input_width = input_image.shape[1]
|
22 |
+
height = ceil(input_height/128)
|
23 |
+
width = ceil(input_width/128)
|
24 |
+
expanded_input_image = np.zeros((128*height, 128*width, 3), dtype=np.uint8)
|
25 |
+
np.copyto(expanded_input_image[0:input_height, 0:input_width], input_image)
|
26 |
+
|
27 |
+
output_image = np.zeros((128*height*2, 128*width*2, 3), dtype=np.float32)
|
28 |
+
|
29 |
+
for i in range(height):
|
30 |
+
for j in range(width):
|
31 |
+
temp_slice = expanded_input_image[i *
|
32 |
+
128:(i+1)*128, j*128:(j+1)*128]/255
|
33 |
+
upsampled_slice = model.predict(temp_slice[np.newaxis, ...])
|
34 |
+
np.copyto(output_image[i*256:(i+1)*256, j *
|
35 |
+
256:(j+1)*256], upsampled_slice[0])
|
36 |
+
if i != 0 and j != 0 and i != height-1 and j != width-1:
|
37 |
+
# removing inner borders
|
38 |
+
right_slice = expanded_input_image[i *
|
39 |
+
128:(i+1)*128, (j+1)*128-64:(j+1)*128+64]/255
|
40 |
+
right_upsampled_slice = model.predict(
|
41 |
+
right_slice[np.newaxis, ...])
|
42 |
+
resized_right_slice = right_upsampled_slice[0][64:192, 64:192]
|
43 |
+
np.copyto(output_image[i*256+64:(i+1)*256-64,
|
44 |
+
(j+1)*256-64:(j+1)*256+64], resized_right_slice)
|
45 |
+
|
46 |
+
left_slice = expanded_input_image[i *
|
47 |
+
128:(i+1)*128, j*128-64:(j)*128+64]/255
|
48 |
+
left_upsampled_slice = model.predict(
|
49 |
+
left_slice[np.newaxis, ...])
|
50 |
+
resized_left_slice = left_upsampled_slice[0][64:192, 64:192]
|
51 |
+
np.copyto(output_image[i*256+64:(i+1)*256-64,
|
52 |
+
j*256-64:j*256+64], resized_left_slice)
|
53 |
+
|
54 |
+
upper_slice = expanded_input_image[(
|
55 |
+
i+1)*128-64:(i+1)*128+64, j*128:(j+1)*128]/255
|
56 |
+
upper_upsampled_slice = model.predict(
|
57 |
+
upper_slice[np.newaxis, ...])
|
58 |
+
resized_upper_slice = upper_upsampled_slice[0][64:192, 64:192]
|
59 |
+
np.copyto(output_image[(i+1)*256-64:(i+1)*256+64,
|
60 |
+
j*256+64:(j+1)*256-64], resized_upper_slice)
|
61 |
+
|
62 |
+
lower_slice = expanded_input_image[i *
|
63 |
+
128-64:i*128+64, j*128:(j+1)*128]/255
|
64 |
+
lower_upsampled_slice = model.predict(
|
65 |
+
lower_slice[np.newaxis, ...])
|
66 |
+
resized_lower_slice = lower_upsampled_slice[0][64:192, 64:192]
|
67 |
+
np.copyto(output_image[i*256-64:i*256+64,
|
68 |
+
j*256+64:(j+1)*256-64], resized_lower_slice)
|
69 |
+
|
70 |
+
|
71 |
+
# removing angles
|
72 |
+
lower_right_slice = expanded_input_image[i *
|
73 |
+
128-64:i*128+64, (j+1)*128-64:(j+1)*128+64]/255
|
74 |
+
lower_right_upsampled_slice = model.predict(
|
75 |
+
lower_right_slice[np.newaxis, ...])
|
76 |
+
resized_lower_right_slice = lower_right_upsampled_slice[0][64:192, 64:192]
|
77 |
+
np.copyto(output_image[i*256-64:i*256+64, (j+1)
|
78 |
+
* 256-64:(j+1)*256+64], resized_lower_right_slice)
|
79 |
+
|
80 |
+
lower_left_slice = expanded_input_image[i *
|
81 |
+
128-64:i*128+64, j*128-64:j*128+64]/255
|
82 |
+
lower_left_upsampled_slice = model.predict(
|
83 |
+
lower_left_slice[np.newaxis, ...])
|
84 |
+
resized_lower_left_slice = lower_left_upsampled_slice[0][64:192, 64:192]
|
85 |
+
np.copyto(
|
86 |
+
output_image[i*256-64:i*256+64, j*256-64:j*256+64], resized_lower_left_slice)
|
87 |
+
|
88 |
+
resized_output_image = output_image[0:input_height*2, 0:input_width*2]
|
89 |
+
return resized_output_image
|
90 |
+
|
91 |
+
|
92 |
+
with gr.Blocks() as demo:
|
93 |
+
with gr.Row():
|
94 |
+
with gr.Column():
|
95 |
+
gr.Label("Original image")
|
96 |
+
input_img = gr.Image(img)
|
97 |
+
with gr.Column():
|
98 |
+
gr.Label("Image with resolution doubled")
|
99 |
+
numpydata = asarray(img)
|
100 |
+
output = double_res(numpydata) # numpy.ndarray
|
101 |
+
input_img = gr.Image(output)
|
102 |
+
demo.launch()
|