sbb_binarization/sbb_binarize/sbb_binarize.py

226 lines
9.6 KiB
Python
Raw Normal View History

2020-10-15 15:33:30 +02:00
"""
Tool to load model and binarize a given image.
"""
2019-08-23 15:51:10 +02:00
2020-10-15 15:33:30 +02:00
from os import listdir
from os.path import join
from warnings import catch_warnings, simplefilter
2019-08-23 15:51:10 +02:00
import numpy as np
import cv2
from keras.models import load_model
import tensorflow as tf
2019-12-10 12:48:08 +01:00
2020-10-15 15:33:30 +02:00
# XXX better to set env var before tensorflow import to suppress those specific warnings
with catch_warnings():
simplefilter("ignore")
2019-08-23 15:51:10 +02:00
def resize_image(img_in, input_height, input_width):
return cv2.resize(img_in, (input_width, input_height), interpolation=cv2.INTER_NEAREST)
2020-10-15 15:38:49 +02:00
class SbbBinarizer:
2020-10-15 15:33:30 +02:00
# TODO use True/False for patches
def __init__(self, model, image=None, image_path=None, patches='false', save=None):
if not(image or image_path) or (image and image_path):
raise ValueError("Must pass either a PIL image or an image_path")
if image:
self.image = image
else:
self.image = cv2.imread(self.image)
2020-10-15 15:33:30 +02:00
self.patches = patches
self.save = save
self.model_dir = model
2019-08-23 15:51:10 +02:00
def start_new_session_and_model(self):
config = tf.ConfigProto()
2020-10-15 15:33:30 +02:00
config.gpu_options.allow_growth = True
self.session = tf.Session(config=config) # tf.InteractiveSession()
def load_model(self, model_name):
2019-08-23 15:51:10 +02:00
2020-10-15 15:33:30 +02:00
self.model = load_model(join(self.model_dir, model_name), compile=False)
2019-08-23 15:51:10 +02:00
2020-10-15 15:33:30 +02:00
self.img_height = self.model.layers[len(self.model.layers)-1].output_shape[1]
self.img_width = self.model.layers[len(self.model.layers)-1].output_shape[2]
self.n_classes = self.model.layers[len(self.model.layers)-1].output_shape[3]
2019-08-23 15:51:10 +02:00
2020-10-15 15:33:30 +02:00
def end_session(self):
self.session.close()
2019-08-23 15:51:10 +02:00
del self.model
del self.session
2020-10-15 15:33:30 +02:00
2019-08-23 15:51:10 +02:00
def predict(self,model_name):
self.load_model(model_name)
img = self.image
2020-10-15 15:33:30 +02:00
img_width_model = self.img_width
img_height_model = self.img_height
2019-08-23 15:51:10 +02:00
if self.patches in ('true', 'True'):
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
margin = int(0.1 * img_width_model)
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
width_mid = img_width_model - 2 * margin
height_mid = img_height_model - 2 * margin
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
img = img / float(255.0)
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
img_h = img.shape[0]
img_w = img.shape[1]
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
prediction_true = np.zeros((img_h, img_w, 3))
mask_true = np.zeros((img_h, img_w))
nxf = img_w / float(width_mid)
nyf = img_h / float(height_mid)
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
if nxf > int(nxf):
nxf = int(nxf) + 1
else:
nxf = int(nxf)
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
if nyf > int(nyf):
nyf = int(nyf) + 1
else:
nyf = int(nyf)
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
for i in range(nxf):
for j in range(nyf):
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
if i == 0:
index_x_d = i * width_mid
index_x_u = index_x_d + img_width_model
elif i > 0:
index_x_d = i * width_mid
index_x_u = index_x_d + img_width_model
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
if j == 0:
index_y_d = j * height_mid
index_y_u = index_y_d + img_height_model
elif j > 0:
index_y_d = j * height_mid
index_y_u = index_y_d + img_height_model
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
if index_x_u > img_w:
index_x_u = img_w
index_x_d = img_w - img_width_model
if index_y_u > img_h:
index_y_u = img_h
index_y_d = img_h - img_height_model
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
img_patch = img[index_y_d:index_y_u, index_x_d:index_x_u, :]
2019-08-23 15:51:10 +02:00
2020-10-15 15:33:30 +02:00
label_p_pred = self.model.predict(img_patch.reshape(1, img_patch.shape[0], img_patch.shape[1], img_patch.shape[2]))
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
seg = np.argmax(label_p_pred, axis=3)[0]
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
seg_color = np.repeat(seg[:, :, np.newaxis], 3, axis=2)
2019-08-23 15:51:10 +02:00
2020-10-15 15:33:30 +02:00
if i == 0 and j == 0:
2019-12-10 12:48:08 +01:00
seg_color = seg_color[0:seg_color.shape[0] - margin, 0:seg_color.shape[1] - margin, :]
seg = seg[0:seg.shape[0] - margin, 0:seg.shape[1] - margin]
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + 0:index_x_u - margin] = seg
2020-10-15 15:33:30 +02:00
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + 0:index_x_u - margin, :] = seg_color
elif i == nxf-1 and j == nyf-1:
2019-12-10 12:48:08 +01:00
seg_color = seg_color[margin:seg_color.shape[0] - 0, margin:seg_color.shape[1] - 0, :]
seg = seg[margin:seg.shape[0] - 0, margin:seg.shape[1] - 0]
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - 0] = seg
2020-10-15 15:33:30 +02:00
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - 0, :] = seg_color
elif i == 0 and j == nyf-1:
2019-12-10 12:48:08 +01:00
seg_color = seg_color[margin:seg_color.shape[0] - 0, 0:seg_color.shape[1] - margin, :]
seg = seg[margin:seg.shape[0] - 0, 0:seg.shape[1] - margin]
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + 0:index_x_u - margin] = seg
2020-10-15 15:33:30 +02:00
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + 0:index_x_u - margin, :] = seg_color
elif i == nxf-1 and j == 0:
2019-12-10 12:48:08 +01:00
seg_color = seg_color[0:seg_color.shape[0] - margin, margin:seg_color.shape[1] - 0, :]
seg = seg[0:seg.shape[0] - margin, margin:seg.shape[1] - 0]
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - 0] = seg
2020-10-15 15:33:30 +02:00
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - 0, :] = seg_color
elif i == 0 and j != 0 and j != nyf-1:
2019-12-10 12:48:08 +01:00
seg_color = seg_color[margin:seg_color.shape[0] - margin, 0:seg_color.shape[1] - margin, :]
seg = seg[margin:seg.shape[0] - margin, 0:seg.shape[1] - margin]
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + 0:index_x_u - margin] = seg
2020-10-15 15:33:30 +02:00
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + 0:index_x_u - margin, :] = seg_color
elif i == nxf-1 and j != 0 and j != nyf-1:
2019-12-10 12:48:08 +01:00
seg_color = seg_color[margin:seg_color.shape[0] - margin, margin:seg_color.shape[1] - 0, :]
seg = seg[margin:seg.shape[0] - margin, margin:seg.shape[1] - 0]
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - 0] = seg
2020-10-15 15:33:30 +02:00
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - 0, :] = seg_color
elif i != 0 and i != nxf-1 and j == 0:
2019-12-10 12:48:08 +01:00
seg_color = seg_color[0:seg_color.shape[0] - margin, margin:seg_color.shape[1] - margin, :]
seg = seg[0:seg.shape[0] - margin, margin:seg.shape[1] - margin]
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - margin] = seg
2020-10-15 15:33:30 +02:00
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - margin, :] = seg_color
elif i != 0 and i != nxf-1 and j == nyf-1:
2019-12-10 12:48:08 +01:00
seg_color = seg_color[margin:seg_color.shape[0] - 0, margin:seg_color.shape[1] - margin, :]
seg = seg[margin:seg.shape[0] - 0, margin:seg.shape[1] - margin]
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - margin] = seg
2020-10-15 15:33:30 +02:00
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - margin, :] = seg_color
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
else:
seg_color = seg_color[margin:seg_color.shape[0] - margin, margin:seg_color.shape[1] - margin, :]
seg = seg[margin:seg.shape[0] - margin, margin:seg.shape[1] - margin]
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - margin] = seg
2020-10-15 15:33:30 +02:00
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - margin, :] = seg_color
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
prediction_true = prediction_true.astype(np.uint8)
2020-10-15 15:33:30 +02:00
2019-12-10 12:48:08 +01:00
else:
2020-10-15 15:33:30 +02:00
img_h_page = img.shape[0]
img_w_page = img.shape[1]
img = img / float(255.0)
img = resize_image(img, img_height_model, img_width_model)
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
label_p_pred = self.model.predict(
img.reshape(1, img.shape[0], img.shape[1], img.shape[2]))
2019-08-23 15:51:10 +02:00
2019-12-10 12:48:08 +01:00
seg = np.argmax(label_p_pred, axis=3)[0]
2020-10-15 15:33:30 +02:00
seg_color = np.repeat(seg[:, :, np.newaxis], 3, axis=2)
prediction_true = resize_image(seg_color, img_h_page, img_w_page)
2019-12-10 12:48:08 +01:00
prediction_true = prediction_true.astype(np.uint8)
return prediction_true[:,:,0]
2019-08-23 15:51:10 +02:00
def run(self):
self.start_new_session_and_model()
2020-10-15 15:33:30 +02:00
models_n = listdir(self.model_dir)
img_last = 0
2019-08-23 15:51:10 +02:00
for model_in in models_n:
2020-10-15 15:33:30 +02:00
res = self.predict(model_in)
img_fin = np.zeros((res.shape[0], res.shape[1], 3))
res[:, :][res[:, :] == 0] = 2
res = res-1
res = res*255
img_fin[:, :, 0] = res
img_fin[:, :, 1] = res
img_fin[:, :, 2] = res
img_fin = img_fin.astype(np.uint8)
img_fin = (res[:, :] == 0)*255
img_last = img_last+img_fin
kernel = np.ones((5, 5), np.uint8)
img_last[:, :][img_last[:, :] > 0] = 255
img_last = (img_last[:, :] == 0)*255
if self.save:
cv2.imwrite(self.save, img_last)
return img_last