training: simplify image preprocessing…

- `utils.provide_patches`: split up loop into
  * `utils.preprocess_img` (single img function)
  * `utils.preprocess_imgs` (top-level loop)
- capture exceptions for all cases (not just some)
  at top level and with informative logging
- avoid repeating / delegating config keys in several
  places: only as kwargs to `preprocess_img()`
- read files into memory only once, then re-use
- improve readability (avoiding long lines, repeated code)
This commit is contained in:
Robert Sachunsky 2026-01-28 13:53:11 +01:00
parent e69b35b49c
commit 29a0f19cee
2 changed files with 510 additions and 370 deletions

View file

@ -26,7 +26,7 @@ from eynollah.training.utils import (
generate_data_from_folder_evaluation,
generate_data_from_folder_training,
get_one_hot,
provide_patches,
preprocess_imgs,
return_number_of_total_training_data
)
@ -240,8 +240,8 @@ def run(_config,
os.mkdir(dir_flow_eval_imgs)
os.mkdir(dir_flow_eval_labels)
# set the gpu configuration
configuration()
dir_img, dir_seg = get_dirs_or_files(dir_train)
dir_img_val, dir_seg_val = get_dirs_or_files(dir_eval)
imgs_list=np.array(os.listdir(dir_img))
segs_list=np.array(os.listdir(dir_seg))
@ -250,50 +250,21 @@ def run(_config,
segs_list_test=np.array(os.listdir(dir_seg_val))
# writing patches into a sub-folder in order to be flowed from directory.
common_args = [input_height, input_width,
blur_k, blur_aug,
padding_white, padding_black,
flip_aug, binarization,
adding_rgb_background,
adding_rgb_foreground,
add_red_textlines,
channels_shuffling,
scaling, shifting, degrading, brightening,
scales, degrade_scales, brightness,
flip_index, shuffle_indexes,
scaling_bluring, scaling_brightness, scaling_binarization,
rotation, rotation_not_90, thetha,
scaling_flip, task,
]
common_kwargs = dict(patches=
patches,
dir_img_bin=
dir_img_bin,
number_of_backgrounds_per_image=
number_of_backgrounds_per_image,
list_all_possible_background_images=
list_all_possible_background_images,
dir_rgb_backgrounds=
dir_rgb_backgrounds,
dir_rgb_foregrounds=
dir_rgb_foregrounds,
list_all_possible_foreground_rgbs=
list_all_possible_foreground_rgbs,
)
provide_patches(imgs_list, segs_list,
dir_img, dir_seg,
preprocess_imgs(_config,
imgs_list,
segs_list,
dir_img,
dir_seg,
dir_flow_train_imgs,
dir_flow_train_labels,
*common_args,
augmentation=augmentation,
**common_kwargs)
provide_patches(imgs_list_test, segs_list_test,
dir_img_val, dir_seg_val,
dir_flow_train_labels)
preprocess_imgs(_config,
imgs_list_test,
segs_list_test,
dir_img_val,
dir_seg_val,
dir_flow_eval_imgs,
dir_flow_eval_labels,
*common_args,
augmentation=False,
**common_kwargs)
augmentation=False)
if weighted_loss:
weights = np.zeros(n_classes)
@ -307,8 +278,8 @@ def run(_config,
label_obj = cv2.imread(label_file)
label_obj_one_hot = get_one_hot(label_obj, label_obj.shape[0], label_obj.shape[1], n_classes)
weights += (label_obj_one_hot.sum(axis=0)).sum(axis=0)
except Exception as e:
print("error reading data file '%s': %s" % (label_file, e), file=sys.stderr)
except Exception:
_log.exception("error reading data file '%s'", label_file)
weights = 1.00 / weights
weights = weights / float(np.sum(weights))
@ -340,7 +311,6 @@ def run(_config,
custom_objects = {"PatchEncoder": PatchEncoder,
"Patches": Patches})
else:
index_start = 0
if backbone_type == 'nontransformer':
model = resnet50_unet(n_classes,
input_height,
@ -391,7 +361,7 @@ def run(_config,
pretraining)
#if you want to see the model structure just uncomment model summary.
model.summary()
#model.summary()
if task in ["segmentation", "binarization"]:
if is_loss_soft_dice:
@ -424,6 +394,11 @@ def run(_config,
if save_interval:
callbacks.append(SaveWeightsAfterSteps(save_interval, dir_output, _config))
_log.info("training on %d batches in %d epochs",
len(os.listdir(dir_flow_train_imgs)) // n_batch - 1,
n_epochs)
_log.info("validating on %d batches",
len(os.listdir(dir_flow_eval_imgs)) // n_batch - 1)
model.fit(
train_gen,
steps_per_epoch=len(os.listdir(dir_flow_train_imgs)) // n_batch - 1,
@ -439,7 +414,6 @@ def run(_config,
#model.save(dir_output+'/'+'model'+'.h5')
elif task=='classification':
configuration()
model = resnet50_classifier(n_classes,
input_height,
input_width,
@ -474,7 +448,7 @@ def run(_config,
usable_checkpoints = np.flatnonzero(np.array(history['val_f1']) > f1_threshold_classification)
if len(usable_checkpoints) >= 1:
print("averaging over usable checkpoints", usable_checkpoints)
_log.info("averaging over usable checkpoints: %s", str(usable_checkpoints))
all_weights = []
for epoch in usable_checkpoints:
cp_path = os.path.join(dir_output, 'model_{epoch:02d}'.format(epoch=epoch))
@ -495,10 +469,9 @@ def run(_config,
model.save(cp_path)
with open(os.path.join(cp_path, "config.json"), "w") as fp:
json.dump(_config, fp) # encode dict into JSON
print("ensemble model saved under", cp_path)
_log.info("ensemble model saved under '%s'", cp_path)
elif task=='reading_order':
configuration()
model = machine_based_reading_order_model(
n_classes, input_height, input_width, weight_decay, pretraining)

View file

@ -1,6 +1,7 @@
import os
import math
import random
from logging import getLogger
import cv2
import numpy as np
@ -266,8 +267,9 @@ def generate_data_from_folder_training(path_classes, batchsize, height, width, n
ret_y= np.zeros((batchsize, n_classes)).astype(np.int16)
batchcount = 0
def do_brightening(img_in_dir, factor):
im = Image.open(img_in_dir)
def do_brightening(img, factor):
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
im = Image.fromarray(img_rgb)
enhancer = ImageEnhance.Brightness(im)
out_img = enhancer.enhance(factor)
out_img = out_img.convert('RGB')
@ -737,321 +739,486 @@ def get_patches_num_scale_new(dir_img_f, dir_seg_f, img, label, height, width, i
return indexer
def provide_patches(imgs_list_train, segs_list_train, dir_img, dir_seg, dir_flow_train_imgs,
dir_flow_train_labels, input_height, input_width, blur_k, blur_aug,
padding_white, padding_black, flip_aug, binarization, adding_rgb_background, adding_rgb_foreground, add_red_textlines, channels_shuffling, scaling, shifting, degrading,
brightening, scales, degrade_scales, brightness, flip_index, shuffle_indexes,
scaling_bluring, scaling_brightness, scaling_binarization, rotation,
rotation_not_90, thetha, scaling_flip, task, augmentation=False, patches=False, dir_img_bin=None,number_of_backgrounds_per_image=None,list_all_possible_background_images=None, dir_rgb_backgrounds=None, dir_rgb_foregrounds=None, list_all_possible_foreground_rgbs=None):
def preprocess_imgs(config,
imgs_list,
segs_list,
dir_img,
dir_seg,
dir_flow_imgs,
dir_flow_labels,
logger=None,
**kwargs,
):
if logger is None:
logger = getLogger('')
# make a copy for this run
config = dict(config)
# add derived keys not part of config
if config.get('dir_rgb_backgrounds', None):
config['list_all_possible_background_images'] = \
os.listdir(config['dir_rgb_backgrounds'])
if config.get('dir_rgb_foregrounds', None):
config['list_all_possible_foreground_rgbs'] = \
os.listdir(config['dir_rgb_foregrounds'])
# override keys from call
config.update(kwargs)
indexer = 0
for im, seg_i in tqdm(zip(imgs_list_train, segs_list_train)):
for im, seg_i in tqdm(zip(imgs_list, segs_list)):
img = cv2.imread(os.path.join(dir_img, im))
img_name = os.path.splitext(im)[0]
if task == "segmentation" or task == "binarization":
dir_of_label_file = os.path.join(dir_seg, img_name + '.png')
elif task=="enhancement":
dir_of_label_file = os.path.join(dir_seg, im)
if config['task'] in ["segmentation", "binarization"]:
lab = cv2.imread(os.path.join(dir_seg, img_name + '.png'))
elif config['task'] == "enhancement":
lab = cv2.imread(os.path.join(dir_seg, im))
else:
lab = None
try:
indexer = preprocess_img(indexer, img, img_name, lab,
dir_flow_imgs,
dir_flow_labels,
**config)
except:
logger.exception("skipping image %s", img_name)
def preprocess_img(indexer,
img,
img_name,
lab,
dir_flow_train_imgs,
dir_flow_train_labels,
input_height=None,
input_width=None,
augmentation=False,
flip_aug=False,
flip_index=None,
blur_aug=False,
blur_k=None,
padding_white=False,
padding_black=False,
scaling=False,
scaling_bluring=False,
scaling_brightness=False,
scaling_binarization=False,
scaling_flip=False,
scales=None,
shifting=False,
degrading=False,
degrade_scales=None,
brightening=False,
brightness=None,
binarization=False,
dir_img_bin=None,
add_red_textlines=False,
adding_rgb_background=False,
dir_rgb_backgrounds=None,
adding_rgb_foreground=False,
dir_rgb_foregrounds=None,
number_of_backgrounds_per_image=None,
channels_shuffling=False,
shuffle_indexes=None,
rotation=False,
rotation_not_90=False,
thetha=None,
patches=False,
list_all_possible_background_images=None,
list_all_possible_foreground_rgbs=None,
**kwargs,
):
if not patches:
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png', resize_image(cv2.imread(dir_img + '/' + im), input_height, input_width))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png', resize_image(cv2.imread(dir_of_label_file), input_height, input_width))
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png',
resize_image(img,
input_height,
input_width))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png',
resize_image(lab,
input_height,
input_width))
indexer += 1
if augmentation:
if flip_aug:
for f_i in flip_index:
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png',
resize_image(cv2.flip(cv2.imread(dir_img+'/'+im),f_i),input_height,input_width) )
resize_image(cv2.flip(img, f_i),
input_height,
input_width))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png',
resize_image(cv2.flip(cv2.imread(dir_of_label_file), f_i), input_height, input_width))
resize_image(cv2.flip(lab, f_i),
input_height,
input_width))
indexer += 1
if blur_aug:
for blur_i in blur_k:
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png',
(resize_image(bluring(cv2.imread(dir_img + '/' + im), blur_i), input_height, input_width)))
(resize_image(bluring(img, blur_i),
input_height,
input_width)))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png',
resize_image(cv2.imread(dir_of_label_file), input_height, input_width))
resize_image(lab,
input_height,
input_width))
indexer += 1
if brightening:
for factor in brightness:
try:
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png',
(resize_image(do_brightening(dir_img + '/' +im, factor), input_height, input_width)))
(resize_image(do_brightening(img, factor),
input_height,
input_width)))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png',
resize_image(cv2.imread(dir_of_label_file), input_height, input_width))
resize_image(lab,
input_height,
input_width))
indexer += 1
except:
pass
if binarization:
if dir_img_bin:
img_bin_corr = cv2.imread(dir_img_bin + '/' + img_name+'.png')
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png',
resize_image(img_bin_corr, input_height, input_width))
resize_image(img_bin_corr,
input_height,
input_width))
else:
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png',
resize_image(otsu_copy(cv2.imread(dir_img + '/' + im)), input_height, input_width))
resize_image(otsu_copy(img),
input_height,
input_width))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png',
resize_image(cv2.imread(dir_of_label_file), input_height, input_width))
resize_image(lab,
input_height,
input_width))
indexer += 1
if degrading:
for degrade_scale_ind in degrade_scales:
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png',
(resize_image(do_degrading(cv2.imread(dir_img + '/' + im), degrade_scale_ind), input_height, input_width)))
(resize_image(do_degrading(img, degrade_scale_ind),
input_height,
input_width)))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png',
resize_image(cv2.imread(dir_of_label_file), input_height, input_width))
resize_image(lab,
input_height,
input_width))
indexer += 1
if rotation_not_90:
for thetha_i in thetha:
img_max_rotated, label_max_rotated = rotation_not_90_func(cv2.imread(dir_img + '/'+im),
cv2.imread(dir_of_label_file), thetha_i)
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png', resize_image(img_max_rotated, input_height, input_width))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png', resize_image(label_max_rotated, input_height, input_width))
img_max_rotated, label_max_rotated = \
rotation_not_90_func(img, lab, thetha_i)
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png',
resize_image(img_max_rotated,
input_height,
input_width))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png',
resize_image(label_max_rotated,
input_height,
input_width))
indexer += 1
if channels_shuffling:
for shuffle_index in shuffle_indexes:
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png',
(resize_image(return_shuffled_channels(cv2.imread(dir_img + '/' + im), shuffle_index), input_height, input_width)))
(resize_image(return_shuffled_channels(img, shuffle_index),
input_height,
input_width)))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png',
resize_image(cv2.imread(dir_of_label_file), input_height, input_width))
resize_image(lab,
input_height,
input_width))
indexer += 1
if scaling:
for sc_ind in scales:
img_scaled, label_scaled = scale_image_for_no_patch(cv2.imread(dir_img + '/'+im),
cv2.imread(dir_of_label_file), sc_ind)
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png', resize_image(img_scaled, input_height, input_width))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png', resize_image(label_scaled, input_height, input_width))
img_scaled, label_scaled = \
scale_image_for_no_patch(img, lab, sc_ind)
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png',
resize_image(img_scaled,
input_height,
input_width))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png',
resize_image(label_scaled,
input_height,
input_width))
indexer += 1
if shifting:
shift_types = ['xpos', 'xmin', 'ypos', 'ymin', 'xypos', 'xymin']
for st_ind in shift_types:
img_shifted, label_shifted = shift_image_and_label(cv2.imread(dir_img + '/'+im),
cv2.imread(dir_of_label_file), st_ind)
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png', resize_image(img_shifted, input_height, input_width))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png', resize_image(label_shifted, input_height, input_width))
img_shifted, label_shifted = \
shift_image_and_label(img, lab, st_ind)
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png',
resize_image(img_shifted,
input_height,
input_width))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png',
resize_image(label_shifted,
input_height,
input_width))
indexer += 1
if adding_rgb_background:
img_bin_corr = cv2.imread(dir_img_bin + '/' + img_name+'.png')
for i_n in range(number_of_backgrounds_per_image):
background_image_chosen_name = random.choice(list_all_possible_background_images)
img_rgb_background_chosen = cv2.imread(dir_rgb_backgrounds + '/' + background_image_chosen_name)
img_with_overlayed_background = return_binary_image_with_given_rgb_background(img_bin_corr, img_rgb_background_chosen)
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png', resize_image(img_with_overlayed_background, input_height, input_width))
img_rgb_background_chosen = \
cv2.imread(dir_rgb_backgrounds + '/' + background_image_chosen_name)
img_with_overlayed_background = \
return_binary_image_with_given_rgb_background(
img_bin_corr, img_rgb_background_chosen)
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png',
resize_image(img_with_overlayed_background,
input_height,
input_width))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png',
resize_image(cv2.imread(dir_of_label_file), input_height, input_width))
resize_image(lab,
input_height,
input_width))
indexer += 1
if adding_rgb_foreground:
img_bin_corr = cv2.imread(dir_img_bin + '/' + img_name+'.png')
for i_n in range(number_of_backgrounds_per_image):
background_image_chosen_name = random.choice(list_all_possible_background_images)
foreground_rgb_chosen_name = random.choice(list_all_possible_foreground_rgbs)
img_rgb_background_chosen = cv2.imread(dir_rgb_backgrounds + '/' + background_image_chosen_name)
foreground_rgb_chosen = np.load(dir_rgb_foregrounds + '/' + foreground_rgb_chosen_name)
img_with_overlayed_background = return_binary_image_with_given_rgb_background_and_given_foreground_rgb(img_bin_corr, img_rgb_background_chosen, foreground_rgb_chosen)
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png', resize_image(img_with_overlayed_background, input_height, input_width))
img_rgb_background_chosen = \
cv2.imread(dir_rgb_backgrounds + '/' + background_image_chosen_name)
foreground_rgb_chosen = \
np.load(dir_rgb_foregrounds + '/' + foreground_rgb_chosen_name)
img_with_overlayed_background = \
return_binary_image_with_given_rgb_background_and_given_foreground_rgb(
img_bin_corr, img_rgb_background_chosen, foreground_rgb_chosen)
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png',
resize_image(img_with_overlayed_background,
input_height,
input_width))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png',
resize_image(cv2.imread(dir_of_label_file), input_height, input_width))
resize_image(lab,
input_height,
input_width))
indexer += 1
if add_red_textlines:
img_bin_corr = cv2.imread(dir_img_bin + '/' + img_name+'.png')
img_red_context = return_image_with_red_elements(cv2.imread(dir_img + '/'+im), img_bin_corr)
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png', resize_image(img_red_context, input_height, input_width))
img_red_context = \
return_image_with_red_elements(img, img_bin_corr)
cv2.imwrite(dir_flow_train_imgs + '/img_' + str(indexer) + '.png',
resize_image(img_red_context,
input_height,
input_width))
cv2.imwrite(dir_flow_train_labels + '/img_' + str(indexer) + '.png',
resize_image(cv2.imread(dir_of_label_file), input_height, input_width))
resize_image(lab,
input_height,
input_width))
indexer += 1
if patches:
indexer = get_patches(dir_flow_train_imgs, dir_flow_train_labels,
cv2.imread(dir_img + '/' + im), cv2.imread(dir_of_label_file),
input_height, input_width, indexer=indexer)
else:
indexer = get_patches(dir_flow_train_imgs,
dir_flow_train_labels,
img,
lab,
input_height,
input_width,
indexer=indexer)
if augmentation:
if rotation:
indexer = get_patches(dir_flow_train_imgs, dir_flow_train_labels,
rotation_90(cv2.imread(dir_img + '/' + im)),
rotation_90(cv2.imread(dir_of_label_file)),
input_height, input_width, indexer=indexer)
indexer = get_patches(dir_flow_train_imgs,
dir_flow_train_labels,
rotation_90(img),
rotation_90(lab),
input_height,
input_width,
indexer=indexer)
if rotation_not_90:
for thetha_i in thetha:
img_max_rotated, label_max_rotated = rotation_not_90_func(cv2.imread(dir_img + '/'+im),
cv2.imread(dir_of_label_file), thetha_i)
indexer = get_patches(dir_flow_train_imgs, dir_flow_train_labels,
img_max_rotated, label_max_rotated = \
rotation_not_90_func(img, lab, thetha_i)
indexer = get_patches(dir_flow_train_imgs,
dir_flow_train_labels,
img_max_rotated,
label_max_rotated,
input_height, input_width, indexer=indexer)
input_height,
input_width,
indexer=indexer)
if channels_shuffling:
for shuffle_index in shuffle_indexes:
indexer = get_patches(dir_flow_train_imgs, dir_flow_train_labels,
return_shuffled_channels(cv2.imread(dir_img + '/' + im), shuffle_index),
cv2.imread(dir_of_label_file),
input_height, input_width, indexer=indexer)
img_shuffled = \
return_shuffled_channels(img, shuffle_index),
indexer = get_patches(dir_flow_train_imgs,
dir_flow_train_labels,
img_shuffled,
lab,
input_height,
input_width,
indexer=indexer)
if adding_rgb_background:
img_bin_corr = cv2.imread(dir_img_bin + '/' + img_name+'.png')
for i_n in range(number_of_backgrounds_per_image):
background_image_chosen_name = random.choice(list_all_possible_background_images)
img_rgb_background_chosen = cv2.imread(dir_rgb_backgrounds + '/' + background_image_chosen_name)
img_with_overlayed_background = return_binary_image_with_given_rgb_background(img_bin_corr, img_rgb_background_chosen)
indexer = get_patches(dir_flow_train_imgs, dir_flow_train_labels,
img_rgb_background_chosen = \
cv2.imread(dir_rgb_backgrounds + '/' + background_image_chosen_name)
img_with_overlayed_background = \
return_binary_image_with_given_rgb_background(
img_bin_corr, img_rgb_background_chosen)
indexer = get_patches(dir_flow_train_imgs,
dir_flow_train_labels,
img_with_overlayed_background,
cv2.imread(dir_of_label_file),
input_height, input_width, indexer=indexer)
lab,
input_height,
input_width,
indexer=indexer)
if adding_rgb_foreground:
img_bin_corr = cv2.imread(dir_img_bin + '/' + img_name+'.png')
for i_n in range(number_of_backgrounds_per_image):
background_image_chosen_name = random.choice(list_all_possible_background_images)
foreground_rgb_chosen_name = random.choice(list_all_possible_foreground_rgbs)
img_rgb_background_chosen = cv2.imread(dir_rgb_backgrounds + '/' + background_image_chosen_name)
foreground_rgb_chosen = np.load(dir_rgb_foregrounds + '/' + foreground_rgb_chosen_name)
img_with_overlayed_background = return_binary_image_with_given_rgb_background_and_given_foreground_rgb(img_bin_corr, img_rgb_background_chosen, foreground_rgb_chosen)
indexer = get_patches(dir_flow_train_imgs, dir_flow_train_labels,
img_rgb_background_chosen = \
cv2.imread(dir_rgb_backgrounds + '/' + background_image_chosen_name)
foreground_rgb_chosen = \
np.load(dir_rgb_foregrounds + '/' + foreground_rgb_chosen_name)
img_with_overlayed_background = \
return_binary_image_with_given_rgb_background_and_given_foreground_rgb(
img_bin_corr, img_rgb_background_chosen, foreground_rgb_chosen)
indexer = get_patches(dir_flow_train_imgs,
dir_flow_train_labels,
img_with_overlayed_background,
cv2.imread(dir_of_label_file),
input_height, input_width, indexer=indexer)
lab,
input_height,
input_width,
indexer=indexer)
if add_red_textlines:
img_bin_corr = cv2.imread(dir_img_bin + '/' + img_name+'.png')
img_red_context = return_image_with_red_elements(cv2.imread(dir_img + '/'+im), img_bin_corr)
indexer = get_patches(dir_flow_train_imgs, dir_flow_train_labels,
img_red_context = \
return_image_with_red_elements(img, img_bin_corr)
indexer = get_patches(dir_flow_train_imgs,
dir_flow_train_labels,
img_red_context,
cv2.imread(dir_of_label_file),
input_height, input_width, indexer=indexer)
lab,
input_height,
input_width,
indexer=indexer)
if flip_aug:
for f_i in flip_index:
indexer = get_patches(dir_flow_train_imgs, dir_flow_train_labels,
cv2.flip(cv2.imread(dir_img + '/' + im), f_i),
cv2.flip(cv2.imread(dir_of_label_file), f_i),
input_height, input_width, indexer=indexer)
indexer = get_patches(dir_flow_train_imgs,
dir_flow_train_labels,
cv2.flip(img, f_i),
cv2.flip(lab, f_i),
input_height,
input_width,
indexer=indexer)
if blur_aug:
for blur_i in blur_k:
indexer = get_patches(dir_flow_train_imgs, dir_flow_train_labels,
bluring(cv2.imread(dir_img + '/' + im), blur_i),
cv2.imread(dir_of_label_file),
input_height, input_width, indexer=indexer)
indexer = get_patches(dir_flow_train_imgs,
dir_flow_train_labels,
bluring(img, blur_i),
lab,
input_height,
input_width,
indexer=indexer)
if padding_black:
indexer = get_patches(dir_flow_train_imgs, dir_flow_train_labels,
do_padding_black(cv2.imread(dir_img + '/' + im)),
do_padding_label(cv2.imread(dir_of_label_file)),
input_height, input_width, indexer=indexer)
indexer = get_patches(dir_flow_train_imgs,
dir_flow_train_labels,
do_padding_black(img),
do_padding_label(lab),
input_height,
input_width,
indexer=indexer)
if padding_white:
indexer = get_patches(dir_flow_train_imgs, dir_flow_train_labels,
do_padding_white(cv2.imread(dir_img + '/'+im)),
do_padding_label(cv2.imread(dir_of_label_file)),
input_height, input_width, indexer=indexer)
indexer = get_patches(dir_flow_train_imgs,
dir_flow_train_labels,
do_padding_white(img),
do_padding_label(lab),
input_height,
input_width,
indexer=indexer)
if brightening:
for factor in brightness:
try:
indexer = get_patches(dir_flow_train_imgs, dir_flow_train_labels,
do_brightening(dir_img + '/' +im, factor),
cv2.imread(dir_of_label_file),
input_height, input_width, indexer=indexer)
except:
pass
indexer = get_patches(dir_flow_train_imgs,
dir_flow_train_labels,
do_brightening(img, factor),
lab,
input_height,
input_width,
indexer=indexer)
if scaling:
for sc_ind in scales:
indexer = get_patches_num_scale_new(dir_flow_train_imgs, dir_flow_train_labels,
cv2.imread(dir_img + '/' + im) ,
cv2.imread(dir_of_label_file),
input_height, input_width, indexer=indexer, scaler=sc_ind)
indexer = get_patches_num_scale_new(
dir_flow_train_imgs,
dir_flow_train_labels,
img ,
lab,
input_height,
input_width,
indexer=indexer,
scaler=sc_ind)
if degrading:
for degrade_scale_ind in degrade_scales:
indexer = get_patches(dir_flow_train_imgs, dir_flow_train_labels,
do_degrading(cv2.imread(dir_img + '/' + im), degrade_scale_ind),
cv2.imread(dir_of_label_file),
input_height, input_width, indexer=indexer)
img_deg = \
do_degrading(img, degrade_scale_ind),
indexer = get_patches(dir_flow_train_imgs,
dir_flow_train_labels,
img_deg,
lab,
input_height,
input_width,
indexer=indexer)
if binarization:
if dir_img_bin:
img_bin_corr = cv2.imread(dir_img_bin + '/' + img_name+'.png')
indexer = get_patches(dir_flow_train_imgs, dir_flow_train_labels,
indexer = get_patches(dir_flow_train_imgs,
dir_flow_train_labels,
img_bin_corr,
cv2.imread(dir_of_label_file),
input_height, input_width, indexer=indexer)
lab,
input_height,
input_width,
indexer=indexer)
else:
indexer = get_patches(dir_flow_train_imgs, dir_flow_train_labels,
otsu_copy(cv2.imread(dir_img + '/' + im)),
cv2.imread(dir_of_label_file),
input_height, input_width, indexer=indexer)
indexer = get_patches(dir_flow_train_imgs,
dir_flow_train_labels,
otsu_copy(img),
lab,
input_height,
input_width,
indexer=indexer)
if scaling_brightness:
for sc_ind in scales:
for factor in brightness:
try:
indexer = get_patches_num_scale_new(dir_flow_train_imgs,
img_bright = do_brightening(img, factor)
indexer = get_patches_num_scale_new(
dir_flow_train_imgs,
dir_flow_train_labels,
do_brightening(dir_img + '/' + im, factor)
,cv2.imread(dir_of_label_file)
,input_height, input_width, indexer=indexer, scaler=sc_ind)
except:
pass
img_bright,
lab,
input_height,
input_width,
indexer=indexer,
scaler=sc_ind)
if scaling_bluring:
for sc_ind in scales:
for blur_i in blur_k:
indexer = get_patches_num_scale_new(dir_flow_train_imgs, dir_flow_train_labels,
bluring(cv2.imread(dir_img + '/' + im), blur_i),
cv2.imread(dir_of_label_file),
input_height, input_width, indexer=indexer, scaler=sc_ind)
img_blur = bluring(img, blur_i),
indexer = get_patches_num_scale_new(
dir_flow_train_imgs,
dir_flow_train_labels,
img_blur,
lab,
input_height,
input_width,
indexer=indexer,
scaler=sc_ind)
if scaling_binarization:
for sc_ind in scales:
indexer = get_patches_num_scale_new(dir_flow_train_imgs, dir_flow_train_labels,
otsu_copy(cv2.imread(dir_img + '/' + im)),
cv2.imread(dir_of_label_file),
input_height, input_width, indexer=indexer, scaler=sc_ind)
img_bin = otsu_copy(img),
indexer = get_patches_num_scale_new(
dir_flow_train_imgs,
dir_flow_train_labels,
img_bin,
lab,
input_height,
input_width,
indexer=indexer,
scaler=sc_ind)
if scaling_flip:
for sc_ind in scales:
for f_i in flip_index:
indexer = get_patches_num_scale_new(dir_flow_train_imgs, dir_flow_train_labels,
cv2.flip( cv2.imread(dir_img + '/' + im), f_i),
cv2.flip(cv2.imread(dir_of_label_file), f_i),
input_height, input_width, indexer=indexer, scaler=sc_ind)
indexer = get_patches_num_scale_new(
dir_flow_train_imgs,
dir_flow_train_labels,
cv2.flip(img, f_i),
cv2.flip(lab, f_i),
input_height,
input_width,
indexer=indexer,
scaler=sc_ind)
return indexer