vahidrezanezhad commited on
Commit
ab95daf
·
verified ·
1 Parent(s): e7c39e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -15
app.py CHANGED
@@ -180,9 +180,58 @@ def do_prediction(model_name, img):
180
  label_p_pred = model.predict(img_in, verbose=0)
181
  num_col = np.argmax(label_p_pred[0]) + 1
182
  return "Found {} columns".format(num_col), None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
 
184
  # bitmap output
185
- case "SBB/eynollah-binarization" | "SBB/eynollah-page-extraction" | "SBB/eynollah-textline" | "SBB/eynollah-textline_light" | "SBB/eynollah-enhancement" | "SBB/eynollah-tables" | "SBB/eynollah-main-regions" | "SBB/eynollah-main-regions-aug-rotation" | "SBB/eynollah-main-regions-aug-scaling" | "SBB/eynollah-main-regions-ensembled" | "SBB/eynollah-full-regions-1column" | "SBB/eynollah-full-regions-3pluscolumn":
186
 
187
  img_height_model=model.layers[len(model.layers)-1].output_shape[1]
188
  img_width_model=model.layers[len(model.layers)-1].output_shape[2]
@@ -304,20 +353,6 @@ def do_prediction(model_name, img):
304
 
305
  prediction_true = prediction_true.astype(np.uint8)
306
 
307
- '''
308
- img = img / float(255.0)
309
- image = resize_image(image, 224,448)
310
- prediction = model.predict(image.reshape(1,224,448,image.shape[2]))
311
- prediction = tf.squeeze(tf.round(prediction))
312
-
313
- prediction = np.argmax(prediction,axis=2)
314
-
315
- prediction = np.repeat(prediction[:, :, np.newaxis]*255, 3, axis=2)
316
- print(prediction.shape)
317
-
318
- '''
319
- #prediction_true = prediction_true * -1
320
- #prediction_true = prediction_true + 1
321
  return "No numerical output", visualize_model_output(prediction_true,img_org, model_name)
322
 
323
  # catch-all (we should not reach this)
 
180
  label_p_pred = model.predict(img_in, verbose=0)
181
  num_col = np.argmax(label_p_pred[0]) + 1
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
+ img = img / float(255.0)
192
+ img = resize_image(img, img_height_model, img_width_model)
193
+
194
+ label_p_pred = model.predict(img.reshape(1, img.shape[0], img.shape[1], img.shape[2]),
195
+ verbose=0)
196
+
197
+ seg = np.argmax(label_p_pred, axis=3)[0]
198
+ seg_color = np.repeat(seg[:, :, np.newaxis], 3, axis=2)
199
+ prediction_true = resize_image(seg_color, img_h_page, img_w_page)
200
+ prediction_true = prediction_true.astype(np.uint8)
201
+
202
+ imgray = cv2.cvtColor(prediction_true, cv2.COLOR_BGR2GRAY)
203
+ _, thresh = cv2.threshold(imgray, 0, 255, 0)
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)]
210
+ x, y, w, h = cv2.boundingRect(cnt)
211
+ if x <= 30:
212
+ w += x
213
+ x = 0
214
+ if (img.shape[1] - (x + w)) <= 30:
215
+ w = w + (img.shape[1] - (x + w))
216
+ if y <= 30:
217
+ h = h + y
218
+ y = 0
219
+ if (img.shape[0] - (y + h)) <= 30:
220
+ h = h + (img.shape[0] - (y + h))
221
+
222
+ box = [x, y, w, h]
223
+
224
+ img_border = np.zeros((prediction_true.shape[0],prediction_true.shape[1]))
225
+ img_border[y:y+h, x:x+w] = 1
226
+ else:
227
+ img_border = np.zeros((prediction_true.shape[0],prediction_true.shape[1]))
228
+ img_border[:, :] = 1
229
+
230
+ return "No numerical output", visualize_model_output(img_border,img, model_name)
231
+
232
 
233
  # bitmap output
234
+ case "SBB/eynollah-binarization" | "SBB/eynollah-textline" | "SBB/eynollah-textline_light" | "SBB/eynollah-enhancement" | "SBB/eynollah-tables" | "SBB/eynollah-main-regions" | "SBB/eynollah-main-regions-aug-rotation" | "SBB/eynollah-main-regions-aug-scaling" | "SBB/eynollah-main-regions-ensembled" | "SBB/eynollah-full-regions-1column" | "SBB/eynollah-full-regions-3pluscolumn":
235
 
236
  img_height_model=model.layers[len(model.layers)-1].output_shape[1]
237
  img_width_model=model.layers[len(model.layers)-1].output_shape[2]
 
353
 
354
  prediction_true = prediction_true.astype(np.uint8)
355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356
  return "No numerical output", visualize_model_output(prediction_true,img_org, model_name)
357
 
358
  # catch-all (we should not reach this)