Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -182,14 +182,12 @@ def do_prediction(model_name, img):
|
|
| 182 |
return "Found {} columns".format(num_col), None
|
| 183 |
|
| 184 |
case "SBB/eynollah-page-extraction":
|
| 185 |
-
|
| 186 |
img_height_model = model.layers[len(model.layers) - 1].output_shape[1]
|
| 187 |
img_width_model = model.layers[len(model.layers) - 1].output_shape[2]
|
| 188 |
|
| 189 |
img_h_page = img.shape[0]
|
| 190 |
img_w_page = img.shape[1]
|
| 191 |
|
| 192 |
-
print(img.shape, img_h_page,img_w_page)
|
| 193 |
img = img / float(255.0)
|
| 194 |
img = resize_image(img, img_height_model, img_width_model)
|
| 195 |
|
|
@@ -206,7 +204,6 @@ def do_prediction(model_name, img):
|
|
| 206 |
#thresh = cv2.dilate(thresh, KERNEL, iterations=3)
|
| 207 |
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
| 208 |
|
| 209 |
-
print(len(contours),'ggg')
|
| 210 |
if len(contours)>0:
|
| 211 |
cnt_size = np.array([cv2.contourArea(contours[j]) for j in range(len(contours))])
|
| 212 |
cnt = contours[np.argmax(cnt_size)]
|
|
@@ -224,7 +221,6 @@ def do_prediction(model_name, img):
|
|
| 224 |
|
| 225 |
box = [x, y, w, h]
|
| 226 |
|
| 227 |
-
print(box)
|
| 228 |
|
| 229 |
img_border = np.zeros((prediction_true.shape[0],prediction_true.shape[1]))
|
| 230 |
img_border[y:y+h, x:x+w] = 1
|
|
@@ -308,9 +304,12 @@ def do_prediction(model_name, img):
|
|
| 308 |
label_p_pred = model.predict(img_patch.reshape(1, img_patch.shape[0], img_patch.shape[1], img_patch.shape[2]),
|
| 309 |
verbose=0)
|
| 310 |
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
|
|
|
|
|
|
|
|
|
| 314 |
|
| 315 |
if i == 0 and j == 0:
|
| 316 |
seg_color = seg_color[0 : seg_color.shape[0] - margin, 0 : seg_color.shape[1] - margin, :]
|
|
@@ -358,9 +357,14 @@ def do_prediction(model_name, img):
|
|
| 358 |
#mask_true[index_y_d + margin : index_y_u - margin, index_x_d + margin : index_x_u - margin] = seg
|
| 359 |
prediction_true[index_y_d + margin : index_y_u - margin, index_x_d + margin : index_x_u - margin, :] = seg_color
|
| 360 |
|
| 361 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
|
| 363 |
-
|
| 364 |
|
| 365 |
# catch-all (we should not reach this)
|
| 366 |
case _:
|
|
|
|
| 182 |
return "Found {} columns".format(num_col), None
|
| 183 |
|
| 184 |
case "SBB/eynollah-page-extraction":
|
|
|
|
| 185 |
img_height_model = model.layers[len(model.layers) - 1].output_shape[1]
|
| 186 |
img_width_model = model.layers[len(model.layers) - 1].output_shape[2]
|
| 187 |
|
| 188 |
img_h_page = img.shape[0]
|
| 189 |
img_w_page = img.shape[1]
|
| 190 |
|
|
|
|
| 191 |
img = img / float(255.0)
|
| 192 |
img = resize_image(img, img_height_model, img_width_model)
|
| 193 |
|
|
|
|
| 204 |
#thresh = cv2.dilate(thresh, KERNEL, iterations=3)
|
| 205 |
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
| 206 |
|
|
|
|
| 207 |
if len(contours)>0:
|
| 208 |
cnt_size = np.array([cv2.contourArea(contours[j]) for j in range(len(contours))])
|
| 209 |
cnt = contours[np.argmax(cnt_size)]
|
|
|
|
| 221 |
|
| 222 |
box = [x, y, w, h]
|
| 223 |
|
|
|
|
| 224 |
|
| 225 |
img_border = np.zeros((prediction_true.shape[0],prediction_true.shape[1]))
|
| 226 |
img_border[y:y+h, x:x+w] = 1
|
|
|
|
| 304 |
label_p_pred = model.predict(img_patch.reshape(1, img_patch.shape[0], img_patch.shape[1], img_patch.shape[2]),
|
| 305 |
verbose=0)
|
| 306 |
|
| 307 |
+
if model_name == "SBB/eynollah-enhancement":
|
| 308 |
+
seg_color = label_p_pred[0, :, :, :]
|
| 309 |
+
seg_color = seg_color * 255
|
| 310 |
+
else:
|
| 311 |
+
seg = np.argmax(label_p_pred, axis=3)[0]
|
| 312 |
+
seg_color = np.repeat(seg[:, :, np.newaxis], 3, axis=2)
|
| 313 |
|
| 314 |
if i == 0 and j == 0:
|
| 315 |
seg_color = seg_color[0 : seg_color.shape[0] - margin, 0 : seg_color.shape[1] - margin, :]
|
|
|
|
| 357 |
#mask_true[index_y_d + margin : index_y_u - margin, index_x_d + margin : index_x_u - margin] = seg
|
| 358 |
prediction_true[index_y_d + margin : index_y_u - margin, index_x_d + margin : index_x_u - margin, :] = seg_color
|
| 359 |
|
| 360 |
+
if model_name == "SBB/eynollah-enhancement":
|
| 361 |
+
prediction_true = prediction_true.astype(int)
|
| 362 |
+
return "No numerical output", prediction_true
|
| 363 |
+
else:
|
| 364 |
+
prediction_true = prediction_true.astype(np.uint8)
|
| 365 |
+
return "No numerical output", visualize_model_output(prediction_true,img_org, model_name)
|
| 366 |
|
| 367 |
+
|
| 368 |
|
| 369 |
# catch-all (we should not reach this)
|
| 370 |
case _:
|