Spaces:
Runtime error
Runtime error
Commit
·
f283201
1
Parent(s):
90a3f92
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,58 +5,116 @@ from huggingface_hub import from_pretrained_keras
|
|
| 5 |
|
| 6 |
model = from_pretrained_keras("GIanlucaRub/doubleResFinal")
|
| 7 |
def double_res(input_image):
|
| 8 |
-
input_height = input_image.shape[0]
|
| 9 |
-
input_width = input_image.shape[1]
|
| 10 |
height = ceil(input_height/128)
|
| 11 |
width = ceil(input_width/128)
|
| 12 |
-
expanded_input_image = np.zeros((128*height, 128*width,3), dtype=np.uint8)
|
| 13 |
np.copyto(expanded_input_image[0:input_height, 0:input_width], input_image)
|
| 14 |
-
|
| 15 |
-
output_image = np.zeros((128*height*2, 128*width*2,3), dtype=np.float32)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
for i in range(height):
|
| 18 |
for j in range(width):
|
| 19 |
-
temp_slice = expanded_input_image[i
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
# removing inner borders
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
lower_slice = expanded_input_image[i*128-64:i*128+64, j*128:(j+1)*128]/255
|
| 40 |
-
lower_upsampled_slice = model.predict(lower_slice[np.newaxis, ...])
|
| 41 |
-
resized_lower_slice = lower_upsampled_slice[0][64:192,64:192]
|
| 42 |
-
np.copyto(output_image[i*256-64:i*256+64, j*256+64:(j+1)*256-64], resized_lower_slice)
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
|
|
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
return resized_output_image
|
| 61 |
|
| 62 |
demo = gr.Interface(
|
|
|
|
| 5 |
|
| 6 |
model = from_pretrained_keras("GIanlucaRub/doubleResFinal")
|
| 7 |
def double_res(input_image):
|
| 8 |
+
input_height = input_image.shape[0]
|
| 9 |
+
input_width = input_image.shape[1]
|
| 10 |
height = ceil(input_height/128)
|
| 11 |
width = ceil(input_width/128)
|
| 12 |
+
expanded_input_image = np.zeros((128*height, 128*width, 3), dtype=np.uint8)
|
| 13 |
np.copyto(expanded_input_image[0:input_height, 0:input_width], input_image)
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
output_image = np.zeros((128*height*2, 128*width*2, 3), dtype=np.float32)
|
| 16 |
+
|
| 17 |
+
to_predict = []
|
| 18 |
for i in range(height):
|
| 19 |
for j in range(width):
|
| 20 |
+
temp_slice = expanded_input_image[i *
|
| 21 |
+
128:(i+1)*128, j*128:(j+1)*128]/255
|
| 22 |
+
to_predict.append(temp_slice)
|
| 23 |
+
|
| 24 |
# removing inner borders
|
| 25 |
+
|
| 26 |
+
for i in range(height):
|
| 27 |
+
for j in range(width):
|
| 28 |
+
if i != 0 and j != 0 and i != height-1 and j != width-1:
|
| 29 |
+
right_slice = expanded_input_image[i *
|
| 30 |
+
128:(i+1)*128, (j+1)*128-64:(j+1)*128+64]/255
|
| 31 |
+
to_predict.append(right_slice)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
left_slice = expanded_input_image[i *
|
| 35 |
+
128:(i+1)*128, j*128-64:(j)*128+64]/255
|
| 36 |
+
to_predict.append(left_slice)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
upper_slice = expanded_input_image[(
|
| 40 |
+
i+1)*128-64:(i+1)*128+64, j*128:(j+1)*128]/255
|
| 41 |
+
to_predict.append(upper_slice)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
lower_slice = expanded_input_image[i *
|
| 45 |
+
128-64:i*128+64, j*128:(j+1)*128]/255
|
| 46 |
+
to_predict.append(lower_slice)
|
| 47 |
+
# removing angles
|
| 48 |
+
|
| 49 |
+
lower_right_slice = expanded_input_image[i *
|
| 50 |
+
128-64:i*128+64, (j+1)*128-64:(j+1)*128+64]/255
|
| 51 |
+
to_predict.append(lower_right_slice)
|
| 52 |
|
| 53 |
+
lower_left_slice = expanded_input_image[i *
|
| 54 |
+
128-64:i*128+64, j*128-64:j*128+64]/255
|
| 55 |
+
to_predict.append(lower_left_slice)
|
| 56 |
+
|
| 57 |
+
# predicting all images at once
|
| 58 |
+
predicted = model.predict(np.array(to_predict))
|
| 59 |
+
counter = 0
|
| 60 |
+
for i in range(height):
|
| 61 |
+
for j in range(width):
|
| 62 |
+
np.copyto(output_image[i*256:(i+1)*256, j *
|
| 63 |
+
256:(j+1)*256], predicted[counter])
|
| 64 |
+
counter+=1
|
| 65 |
+
|
| 66 |
|
| 67 |
+
|
| 68 |
+
for i in range(height):
|
| 69 |
+
for j in range(width):
|
| 70 |
+
if i != 0 and j != 0 and i != height-1 and j != width-1:
|
| 71 |
+
right_upsampled_slice = predicted[counter]
|
| 72 |
+
counter+=1
|
| 73 |
+
resized_right_slice = right_upsampled_slice[64:192, 64:192]
|
| 74 |
+
np.copyto(output_image[i*256+64:(i+1)*256-64,
|
| 75 |
+
(j+1)*256-64:(j+1)*256+64], resized_right_slice)
|
| 76 |
+
|
| 77 |
|
| 78 |
+
|
| 79 |
|
| 80 |
+
left_upsampled_slice = predicted[counter]
|
| 81 |
+
counter+=1
|
| 82 |
+
resized_left_slice = left_upsampled_slice[64:192, 64:192]
|
| 83 |
+
np.copyto(output_image[i*256+64:(i+1)*256-64,
|
| 84 |
+
j*256-64:j*256+64], resized_left_slice)
|
| 85 |
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
upper_upsampled_slice = predicted[counter]
|
| 89 |
+
counter+=1
|
| 90 |
+
resized_upper_slice = upper_upsampled_slice[64:192, 64:192]
|
| 91 |
+
np.copyto(output_image[(i+1)*256-64:(i+1)*256+64,
|
| 92 |
+
j*256+64:(j+1)*256-64], resized_upper_slice)
|
| 93 |
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
lower_upsampled_slice = predicted[counter]
|
| 97 |
+
counter+=1
|
| 98 |
+
resized_lower_slice = lower_upsampled_slice[64:192, 64:192]
|
| 99 |
+
np.copyto(output_image[i*256-64:i*256+64,
|
| 100 |
+
j*256+64:(j+1)*256-64], resized_lower_slice)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
lower_right_upsampled_slice = predicted[counter]
|
| 105 |
+
counter+=1
|
| 106 |
+
resized_lower_right_slice = lower_right_upsampled_slice[64:192, 64:192]
|
| 107 |
+
np.copyto(output_image[i*256-64:i*256+64, (j+1)
|
| 108 |
+
* 256-64:(j+1)*256+64], resized_lower_right_slice)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
lower_left_upsampled_slice = predicted[counter]
|
| 112 |
+
counter+=1
|
| 113 |
+
resized_lower_left_slice = lower_left_upsampled_slice[64:192, 64:192]
|
| 114 |
+
np.copyto(
|
| 115 |
+
output_image[i*256-64:i*256+64, j*256-64:j*256+64], resized_lower_left_slice)
|
| 116 |
+
|
| 117 |
+
resized_output_image = output_image[0:input_height*2, 0:input_width*2]
|
| 118 |
return resized_output_image
|
| 119 |
|
| 120 |
demo = gr.Interface(
|