Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,8 +9,160 @@ def resize_image(img_in,input_height,input_width):
|
|
9 |
return cv2.resize( img_in, ( input_width,input_height) ,interpolation=cv2.INTER_NEAREST)
|
10 |
|
11 |
|
12 |
-
def
|
13 |
model = from_pretrained_keras("vahidrezanezhad/sbb_binarization")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
image = resize_image(image, 224,448)
|
15 |
prediction = model.predict(image.reshape(1,224,448,image.shape[2]))
|
16 |
prediction = tf.squeeze(tf.round(prediction))
|
@@ -20,9 +172,8 @@ def greet(image):
|
|
20 |
prediction = np.repeat(prediction[:, :, np.newaxis]*255, 3, axis=2)
|
21 |
print(prediction.shape)
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
return prediction
|
26 |
|
27 |
-
iface = gr.Interface(fn=
|
28 |
iface.launch()
|
|
|
9 |
return cv2.resize( img_in, ( input_width,input_height) ,interpolation=cv2.INTER_NEAREST)
|
10 |
|
11 |
|
12 |
+
def do_prediction(image):
|
13 |
model = from_pretrained_keras("vahidrezanezhad/sbb_binarization")
|
14 |
+
|
15 |
+
img_height_model=model.layers[len(model.layers)-1].output_shape[1]
|
16 |
+
img_width_model=model.layers[len(model.layers)-1].output_shape[2]
|
17 |
+
n_classes=model.layers[len(model.layers)-1].output_shape[3]
|
18 |
+
|
19 |
+
kernel = np.ones((5,5),np.uint8)
|
20 |
+
margin = int(0.1 * img_width_model)
|
21 |
+
|
22 |
+
width_mid = img_width_model - 2 * margin
|
23 |
+
height_mid = img_height_model - 2 * margin
|
24 |
+
|
25 |
+
img = img / float(255.0)
|
26 |
+
|
27 |
+
img_h = img.shape[0]
|
28 |
+
img_w = img.shape[1]
|
29 |
+
|
30 |
+
prediction_true = np.zeros((img_h, img_w, 3))
|
31 |
+
mask_true = np.zeros((img_h, img_w))
|
32 |
+
nxf = img_w / float(width_mid)
|
33 |
+
nyf = img_h / float(height_mid)
|
34 |
+
|
35 |
+
if nxf > int(nxf):
|
36 |
+
nxf = int(nxf) + 1
|
37 |
+
else:
|
38 |
+
nxf = int(nxf)
|
39 |
+
|
40 |
+
if nyf > int(nyf):
|
41 |
+
nyf = int(nyf) + 1
|
42 |
+
else:
|
43 |
+
nyf = int(nyf)
|
44 |
+
|
45 |
+
for i in range(nxf):
|
46 |
+
for j in range(nyf):
|
47 |
+
|
48 |
+
if i == 0:
|
49 |
+
index_x_d = i * width_mid
|
50 |
+
index_x_u = index_x_d + img_width_model
|
51 |
+
elif i > 0:
|
52 |
+
index_x_d = i * width_mid
|
53 |
+
index_x_u = index_x_d + img_width_model
|
54 |
+
|
55 |
+
if j == 0:
|
56 |
+
index_y_d = j * height_mid
|
57 |
+
index_y_u = index_y_d + img_height_model
|
58 |
+
elif j > 0:
|
59 |
+
index_y_d = j * height_mid
|
60 |
+
index_y_u = index_y_d + img_height_model
|
61 |
+
|
62 |
+
if index_x_u > img_w:
|
63 |
+
index_x_u = img_w
|
64 |
+
index_x_d = img_w - img_width_model
|
65 |
+
if index_y_u > img_h:
|
66 |
+
index_y_u = img_h
|
67 |
+
index_y_d = img_h - img_height_model
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
img_patch = img[index_y_d:index_y_u, index_x_d:index_x_u, :]
|
72 |
+
|
73 |
+
label_p_pred = model.predict(
|
74 |
+
img_patch.reshape(1, img_patch.shape[0], img_patch.shape[1], img_patch.shape[2]), verbose=0)
|
75 |
+
|
76 |
+
seg = np.argmax(label_p_pred, axis=2)
|
77 |
+
|
78 |
+
|
79 |
+
seg_color = np.repeat(seg[:, :, np.newaxis], 3, axis=2)
|
80 |
+
|
81 |
+
if i==0 and j==0:
|
82 |
+
seg_color = seg_color[0:seg_color.shape[0] - margin, 0:seg_color.shape[1] - margin, :]
|
83 |
+
seg = seg[0:seg.shape[0] - margin, 0:seg.shape[1] - margin]
|
84 |
+
|
85 |
+
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + 0:index_x_u - margin] = seg
|
86 |
+
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + 0:index_x_u - margin,
|
87 |
+
:] = seg_color
|
88 |
+
|
89 |
+
elif i==nxf-1 and j==nyf-1:
|
90 |
+
seg_color = seg_color[margin:seg_color.shape[0] - 0, margin:seg_color.shape[1] - 0, :]
|
91 |
+
seg = seg[margin:seg.shape[0] - 0, margin:seg.shape[1] - 0]
|
92 |
+
|
93 |
+
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - 0] = seg
|
94 |
+
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - 0,
|
95 |
+
:] = seg_color
|
96 |
+
|
97 |
+
elif i==0 and j==nyf-1:
|
98 |
+
seg_color = seg_color[margin:seg_color.shape[0] - 0, 0:seg_color.shape[1] - margin, :]
|
99 |
+
seg = seg[margin:seg.shape[0] - 0, 0:seg.shape[1] - margin]
|
100 |
+
|
101 |
+
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + 0:index_x_u - margin] = seg
|
102 |
+
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + 0:index_x_u - margin,
|
103 |
+
:] = seg_color
|
104 |
+
|
105 |
+
elif i==nxf-1 and j==0:
|
106 |
+
seg_color = seg_color[0:seg_color.shape[0] - margin, margin:seg_color.shape[1] - 0, :]
|
107 |
+
seg = seg[0:seg.shape[0] - margin, margin:seg.shape[1] - 0]
|
108 |
+
|
109 |
+
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - 0] = seg
|
110 |
+
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - 0,
|
111 |
+
:] = seg_color
|
112 |
+
|
113 |
+
elif i==0 and j!=0 and j!=nyf-1:
|
114 |
+
seg_color = seg_color[margin:seg_color.shape[0] - margin, 0:seg_color.shape[1] - margin, :]
|
115 |
+
seg = seg[margin:seg.shape[0] - margin, 0:seg.shape[1] - margin]
|
116 |
+
|
117 |
+
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + 0:index_x_u - margin] = seg
|
118 |
+
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + 0:index_x_u - margin,
|
119 |
+
:] = seg_color
|
120 |
+
|
121 |
+
elif i==nxf-1 and j!=0 and j!=nyf-1:
|
122 |
+
seg_color = seg_color[margin:seg_color.shape[0] - margin, margin:seg_color.shape[1] - 0, :]
|
123 |
+
seg = seg[margin:seg.shape[0] - margin, margin:seg.shape[1] - 0]
|
124 |
+
|
125 |
+
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - 0] = seg
|
126 |
+
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - 0,
|
127 |
+
:] = seg_color
|
128 |
+
|
129 |
+
elif i!=0 and i!=nxf-1 and j==0:
|
130 |
+
seg_color = seg_color[0:seg_color.shape[0] - margin, margin:seg_color.shape[1] - margin, :]
|
131 |
+
seg = seg[0:seg.shape[0] - margin, margin:seg.shape[1] - margin]
|
132 |
+
|
133 |
+
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - margin] = seg
|
134 |
+
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - margin,
|
135 |
+
:] = seg_color
|
136 |
+
|
137 |
+
elif i!=0 and i!=nxf-1 and j==nyf-1:
|
138 |
+
seg_color = seg_color[margin:seg_color.shape[0] - 0, margin:seg_color.shape[1] - margin, :]
|
139 |
+
seg = seg[margin:seg.shape[0] - 0, margin:seg.shape[1] - margin]
|
140 |
+
|
141 |
+
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - margin] = seg
|
142 |
+
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - margin,
|
143 |
+
:] = seg_color
|
144 |
+
|
145 |
+
else:
|
146 |
+
seg_color = seg_color[margin:seg_color.shape[0] - margin, margin:seg_color.shape[1] - margin, :]
|
147 |
+
seg = seg[margin:seg.shape[0] - margin, margin:seg.shape[1] - margin]
|
148 |
+
|
149 |
+
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - margin] = seg
|
150 |
+
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - margin,
|
151 |
+
:] = seg_color
|
152 |
+
|
153 |
+
prediction_true = prediction_true.astype(np.uint8)
|
154 |
+
|
155 |
+
y_predi=cv2.resize( y_predi, ( img.shape[1],img.shape[0]) ,interpolation=cv2.INTER_NEAREST)
|
156 |
+
#return y_predi
|
157 |
+
|
158 |
+
|
159 |
+
|
160 |
+
print(y_predi.shape, np.unique(y_predi))
|
161 |
+
|
162 |
+
|
163 |
+
|
164 |
+
'''
|
165 |
+
img = img / float(255.0)
|
166 |
image = resize_image(image, 224,448)
|
167 |
prediction = model.predict(image.reshape(1,224,448,image.shape[2]))
|
168 |
prediction = tf.squeeze(tf.round(prediction))
|
|
|
172 |
prediction = np.repeat(prediction[:, :, np.newaxis]*255, 3, axis=2)
|
173 |
print(prediction.shape)
|
174 |
|
175 |
+
'''
|
176 |
+
return y_predi
|
|
|
177 |
|
178 |
+
iface = gr.Interface(fn=do_prediction, inputs=gr.Image(), outputs=gr.Image())
|
179 |
iface.launch()
|