mirror of
https://github.com/qurator-spk/sbb_binarization.git
synced 2025-06-09 20:29:57 +02:00
🎨 clean up code
This commit is contained in:
parent
150f03154f
commit
71d44408b3
1 changed files with 101 additions and 118 deletions
|
@ -1,27 +1,24 @@
|
||||||
#! /usr/bin/env python3
|
|
||||||
|
|
||||||
__version__= '1.0'
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
import sys
|
|
||||||
import os
|
|
||||||
import numpy as np
|
|
||||||
import warnings
|
|
||||||
import cv2
|
|
||||||
from keras.models import load_model
|
|
||||||
import tensorflow as tf
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
with warnings.catch_warnings():
|
|
||||||
warnings.simplefilter("ignore")
|
|
||||||
|
|
||||||
__doc__=\
|
|
||||||
"""
|
"""
|
||||||
Tool to load model and binarize a given image.
|
Tool to load model and binarize a given image.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
from os import listdir
|
||||||
|
from os.path import join
|
||||||
|
from warnings import catch_warnings, simplefilter
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import cv2
|
||||||
|
from keras.models import load_model
|
||||||
|
import tensorflow as tf
|
||||||
|
|
||||||
|
# XXX better to set env var before tensorflow import to suppress those specific warnings
|
||||||
|
with catch_warnings():
|
||||||
|
simplefilter("ignore")
|
||||||
|
|
||||||
class sbb_binarize:
|
class sbb_binarize:
|
||||||
|
|
||||||
|
# TODO use True/False for patches
|
||||||
def __init__(self, image, model, patches='false', save=None):
|
def __init__(self, image, model, patches='false', save=None):
|
||||||
self.image = image
|
self.image = image
|
||||||
self.patches = patches
|
self.patches = patches
|
||||||
|
@ -36,9 +33,10 @@ class sbb_binarize:
|
||||||
config.gpu_options.allow_growth = True
|
config.gpu_options.allow_growth = True
|
||||||
|
|
||||||
self.session = tf.Session(config=config) # tf.InteractiveSession()
|
self.session = tf.Session(config=config) # tf.InteractiveSession()
|
||||||
def load_model(self,model_name):
|
|
||||||
self.model = load_model(self.model_dir+'/'+model_name , compile=False)
|
|
||||||
|
|
||||||
|
def load_model(self,model_name):
|
||||||
|
|
||||||
|
self.model = load_model(join(self.model_dir, model_name), compile=False)
|
||||||
|
|
||||||
self.img_height = self.model.layers[len(self.model.layers)-1].output_shape[1]
|
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.img_width = self.model.layers[len(self.model.layers)-1].output_shape[2]
|
||||||
|
@ -46,10 +44,9 @@ class sbb_binarize:
|
||||||
|
|
||||||
def end_session(self):
|
def end_session(self):
|
||||||
self.session.close()
|
self.session.close()
|
||||||
|
|
||||||
|
|
||||||
del self.model
|
del self.model
|
||||||
del self.session
|
del self.session
|
||||||
|
|
||||||
def predict(self,model_name):
|
def predict(self,model_name):
|
||||||
self.load_model(model_name)
|
self.load_model(model_name)
|
||||||
img = cv2.imread(self.image)
|
img = cv2.imread(self.image)
|
||||||
|
@ -108,12 +105,9 @@ class sbb_binarize:
|
||||||
index_y_u = img_h
|
index_y_u = img_h
|
||||||
index_y_d = img_h - img_height_model
|
index_y_d = img_h - img_height_model
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
img_patch = img[index_y_d:index_y_u, index_x_d:index_x_u, :]
|
img_patch = img[index_y_d:index_y_u, index_x_d:index_x_u, :]
|
||||||
|
|
||||||
label_p_pred = self.model.predict(
|
label_p_pred = self.model.predict(img_patch.reshape(1, img_patch.shape[0], img_patch.shape[1], img_patch.shape[2]))
|
||||||
img_patch.reshape(1, img_patch.shape[0], img_patch.shape[1], img_patch.shape[2]))
|
|
||||||
|
|
||||||
seg = np.argmax(label_p_pred, axis=3)[0]
|
seg = np.argmax(label_p_pred, axis=3)[0]
|
||||||
|
|
||||||
|
@ -124,72 +118,63 @@ class sbb_binarize:
|
||||||
seg = seg[0:seg.shape[0] - margin, 0:seg.shape[1] - margin]
|
seg = seg[0:seg.shape[0] - margin, 0:seg.shape[1] - margin]
|
||||||
|
|
||||||
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + 0:index_x_u - margin] = seg
|
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + 0:index_x_u - margin] = seg
|
||||||
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + 0:index_x_u - margin,
|
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + 0:index_x_u - margin, :] = seg_color
|
||||||
:] = seg_color
|
|
||||||
|
|
||||||
elif i == nxf-1 and j == nyf-1:
|
elif i == nxf-1 and j == nyf-1:
|
||||||
seg_color = seg_color[margin:seg_color.shape[0] - 0, margin:seg_color.shape[1] - 0, :]
|
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]
|
seg = seg[margin:seg.shape[0] - 0, margin:seg.shape[1] - 0]
|
||||||
|
|
||||||
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - 0] = seg
|
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - 0] = seg
|
||||||
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - 0,
|
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - 0, :] = seg_color
|
||||||
:] = seg_color
|
|
||||||
|
|
||||||
elif i == 0 and j == nyf-1:
|
elif i == 0 and j == nyf-1:
|
||||||
seg_color = seg_color[margin:seg_color.shape[0] - 0, 0:seg_color.shape[1] - margin, :]
|
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]
|
seg = seg[margin:seg.shape[0] - 0, 0:seg.shape[1] - margin]
|
||||||
|
|
||||||
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + 0:index_x_u - margin] = seg
|
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + 0:index_x_u - margin] = seg
|
||||||
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + 0:index_x_u - margin,
|
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + 0:index_x_u - margin, :] = seg_color
|
||||||
:] = seg_color
|
|
||||||
|
|
||||||
elif i == nxf-1 and j == 0:
|
elif i == nxf-1 and j == 0:
|
||||||
seg_color = seg_color[0:seg_color.shape[0] - margin, margin:seg_color.shape[1] - 0, :]
|
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]
|
seg = seg[0:seg.shape[0] - margin, margin:seg.shape[1] - 0]
|
||||||
|
|
||||||
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - 0] = seg
|
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - 0] = seg
|
||||||
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - 0,
|
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - 0, :] = seg_color
|
||||||
:] = seg_color
|
|
||||||
|
|
||||||
elif i == 0 and j != 0 and j != nyf-1:
|
elif i == 0 and j != 0 and j != nyf-1:
|
||||||
seg_color = seg_color[margin:seg_color.shape[0] - margin, 0:seg_color.shape[1] - margin, :]
|
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]
|
seg = seg[margin:seg.shape[0] - margin, 0:seg.shape[1] - margin]
|
||||||
|
|
||||||
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + 0:index_x_u - margin] = seg
|
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + 0:index_x_u - margin] = seg
|
||||||
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + 0:index_x_u - margin,
|
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + 0:index_x_u - margin, :] = seg_color
|
||||||
:] = seg_color
|
|
||||||
|
|
||||||
elif i == nxf-1 and j != 0 and j != nyf-1:
|
elif i == nxf-1 and j != 0 and j != nyf-1:
|
||||||
seg_color = seg_color[margin:seg_color.shape[0] - margin, margin:seg_color.shape[1] - 0, :]
|
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]
|
seg = seg[margin:seg.shape[0] - margin, margin:seg.shape[1] - 0]
|
||||||
|
|
||||||
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - 0] = seg
|
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - 0] = seg
|
||||||
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - 0,
|
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - 0, :] = seg_color
|
||||||
:] = seg_color
|
|
||||||
|
|
||||||
elif i != 0 and i != nxf-1 and j == 0:
|
elif i != 0 and i != nxf-1 and j == 0:
|
||||||
seg_color = seg_color[0:seg_color.shape[0] - margin, margin:seg_color.shape[1] - margin, :]
|
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]
|
seg = seg[0:seg.shape[0] - margin, margin:seg.shape[1] - margin]
|
||||||
|
|
||||||
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - margin] = seg
|
mask_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - margin] = seg
|
||||||
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - margin,
|
prediction_true[index_y_d + 0:index_y_u - margin, index_x_d + margin:index_x_u - margin, :] = seg_color
|
||||||
:] = seg_color
|
|
||||||
|
|
||||||
elif i != 0 and i != nxf-1 and j == nyf-1:
|
elif i != 0 and i != nxf-1 and j == nyf-1:
|
||||||
seg_color = seg_color[margin:seg_color.shape[0] - 0, margin:seg_color.shape[1] - margin, :]
|
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]
|
seg = seg[margin:seg.shape[0] - 0, margin:seg.shape[1] - margin]
|
||||||
|
|
||||||
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - margin] = seg
|
mask_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - margin] = seg
|
||||||
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - margin,
|
prediction_true[index_y_d + margin:index_y_u - 0, index_x_d + margin:index_x_u - margin, :] = seg_color
|
||||||
:] = seg_color
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
seg_color = seg_color[margin:seg_color.shape[0] - margin, margin:seg_color.shape[1] - margin, :]
|
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]
|
seg = seg[margin:seg.shape[0] - margin, margin:seg.shape[1] - margin]
|
||||||
|
|
||||||
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - margin] = seg
|
mask_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - margin] = seg
|
||||||
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - margin,
|
prediction_true[index_y_d + margin:index_y_u - margin, index_x_d + margin:index_x_u - margin, :] = seg_color
|
||||||
:] = seg_color
|
|
||||||
|
|
||||||
prediction_true = prediction_true.astype(np.uint8)
|
prediction_true = prediction_true.astype(np.uint8)
|
||||||
|
|
||||||
|
@ -210,7 +195,7 @@ class sbb_binarize:
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.start_new_session_and_model()
|
self.start_new_session_and_model()
|
||||||
models_n=os.listdir(self.model_dir)
|
models_n = listdir(self.model_dir)
|
||||||
img_last = 0
|
img_last = 0
|
||||||
for model_in in models_n:
|
for model_in in models_n:
|
||||||
|
|
||||||
|
@ -227,13 +212,15 @@ class sbb_binarize:
|
||||||
img_fin = img_fin.astype(np.uint8)
|
img_fin = img_fin.astype(np.uint8)
|
||||||
img_fin = (res[:, :] == 0)*255
|
img_fin = (res[:, :] == 0)*255
|
||||||
img_last = img_last+img_fin
|
img_last = img_last+img_fin
|
||||||
|
|
||||||
kernel = np.ones((5, 5), np.uint8)
|
kernel = np.ones((5, 5), np.uint8)
|
||||||
img_last[:, :][img_last[:, :] > 0] = 255
|
img_last[:, :][img_last[:, :] > 0] = 255
|
||||||
img_last = (img_last[:, :] == 0)*255
|
img_last = (img_last[:, :] == 0)*255
|
||||||
if self.save is not None:
|
if self.save:
|
||||||
cv2.imwrite(self.save, img_last)
|
cv2.imwrite(self.save, img_last)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser=argparse.ArgumentParser()
|
parser = ArgumentParser()
|
||||||
|
|
||||||
parser.add_argument('-i', '--image', dest='inp1', default=None, help='image.')
|
parser.add_argument('-i', '--image', dest='inp1', default=None, help='image.')
|
||||||
parser.add_argument('-p', '--patches', dest='inp3', default=False, help='by setting this parameter to true you let the model to see the image in patches.')
|
parser.add_argument('-p', '--patches', dest='inp3', default=False, help='by setting this parameter to true you let the model to see the image in patches.')
|
||||||
|
@ -249,7 +236,3 @@ def main():
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue