@ -1,27 +1,24 @@
#! /usr/bin/env python3
"""
Tool to load model and binarize a given image .
"""
__version__ = ' 1.0 '
from argparse import ArgumentParser
from os import listdir
from os . path import join
from warnings import catch_warnings , simplefilter
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 .
"""
# XXX better to set env var before tensorflow import to suppress those specific warnings
with catch_warnings ( ) :
simplefilter ( " ignore " )
class sbb_binarize :
# TODO use True/False for patches
def __init__ ( self , image , model , patches = ' false ' , save = None ) :
self . image = image
self . patches = patches
@ -36,9 +33,10 @@ class sbb_binarize:
config . gpu_options . allow_growth = True
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 )
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_width = self . model . layers [ len ( self . model . layers ) - 1 ] . output_shape [ 2 ]
@ -46,10 +44,9 @@ class sbb_binarize:
def end_session ( self ) :
self . session . close ( )
del self . model
del self . session
def predict ( self , model_name ) :
self . load_model ( model_name )
img = cv2 . imread ( self . image )
@ -108,12 +105,9 @@ class sbb_binarize:
index_y_u = img_h
index_y_d = img_h - img_height_model
img_patch = img [ index_y_d : index_y_u , index_x_d : index_x_u , : ]
label_p_pred = self . model . predict (
img_patch . reshape ( 1 , img_patch . shape [ 0 ] , img_patch . shape [ 1 ] , img_patch . shape [ 2 ] ) )
label_p_pred = self . model . predict ( 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 ]
@ -124,72 +118,63 @@ class sbb_binarize:
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
prediction_true [ index_y_d + 0 : index_y_u - margin , index_x_d + 0 : index_x_u - margin ,
: ] = seg_color
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 :
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 ]
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 ,
: ] = seg_color
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 :
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 ]
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 ,
: ] = seg_color
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 :
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 ]
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 ,
: ] = seg_color
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 :
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 ]
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 ,
: ] = seg_color
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 :
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 ]
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 ,
: ] = seg_color
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 :
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 ]
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 ,
: ] = seg_color
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 :
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 ]
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 ,
: ] = seg_color
prediction_true [ index_y_d + margin : index_y_u - 0 , index_x_d + margin : index_x_u - margin , : ] = seg_color
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 ]
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 ,
: ] = seg_color
prediction_true [ index_y_d + margin : index_y_u - margin , index_x_d + margin : index_x_u - margin , : ] = seg_color
prediction_true = prediction_true . astype ( np . uint8 )
@ -210,7 +195,7 @@ class sbb_binarize:
def run ( self ) :
self . start_new_session_and_model ( )
models_n = os . listdir ( self . model_dir )
models_n = listdir ( self . model_dir )
img_last = 0
for model_in in models_n :
@ -227,13 +212,15 @@ class sbb_binarize:
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 is not None :
if self . save :
cv2 . imwrite ( self . save , img_last )
def main ( ) :
parser = argparse . ArgumentParser ( )
parser = ArgumentParser ( )
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. ' )
@ -249,7 +236,3 @@ def main():
if __name__ == " __main__ " :
main ( )