Skip to content

Commit

Permalink
more code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
cneud committed Feb 23, 2024
1 parent 713b90e commit 1a95bca
Show file tree
Hide file tree
Showing 24 changed files with 1,418 additions and 1,329 deletions.
5 changes: 3 additions & 2 deletions eynollah/eynollah/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ def main(
ignore_page_extraction=ignore_page_extraction,
)
eynollah.run()
#pcgts = eynollah.run()
##eynollah.writer.write_pagexml(pcgts)
# pcgts = eynollah.run()
# eynollah.writer.write_pagexml(pcgts)


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions eynollah/eynollah/ocrd_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from click import command
from ocrd.decorators import ocrd_cli_options, ocrd_cli_wrap_processor


@command()
@ocrd_cli_options
def main(*args, **kwargs):
return ocrd_cli_wrap_processor(EynollahProcessor, *args, **kwargs)


if __name__ == '__main__':
main()
91 changes: 52 additions & 39 deletions eynollah/eynollah/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,25 @@
from .utils.rotate import rotate_image_different
from .utils.resize import resize_image


class EynollahPlotter():
"""
Class collecting all the plotting and image writing methods
"""

def __init__(
self,
*,
dir_out,
dir_of_all,
dir_save_page,
dir_of_deskewed,
dir_of_layout,
dir_of_cropped_images,
image_filename_stem,
image_org=None,
scale_x=1,
scale_y=1,
self,
*,
dir_out,
dir_of_all,
dir_save_page,
dir_of_deskewed,
dir_of_layout,
dir_of_cropped_images,
image_filename_stem,
image_org=None,
scale_x=1,
scale_y=1,
):
self.dir_out = dir_out
self.dir_of_all = dir_of_all
Expand All @@ -44,22 +45,23 @@ def save_plot_of_layout_main(self, text_regions_p, image_page):
if self.dir_of_layout is not None:
values = np.unique(text_regions_p[:, :])
# pixels=['Background' , 'Main text' , 'Heading' , 'Marginalia' ,'Drop capitals' , 'Images' , 'Seperators' , 'Tables', 'Graphics']
pixels=['Background' , 'Main text' , 'Image' , 'Separator','Marginalia']
pixels = ['Background', 'Main text', 'Image', 'Separator', 'Marginalia']
values_indexes = [0, 1, 2, 3, 4]
plt.figure(figsize=(40, 40))
plt.rcParams["font.size"] = "40"
im = plt.imshow(text_regions_p[:, :])
colors = [im.cmap(im.norm(value)) for value in values]
patches = [mpatches.Patch(color=colors[np.where(values == i)[0][0]], label="{l}".format(l=pixels[int(np.where(values_indexes == i)[0][0])])) for i in values]
patches = [mpatches.Patch(color=colors[np.where(values == i)[0][0]],
label="{l}".format(l=pixels[int(np.where(values_indexes == i)[0][0])])) for i in
values]
plt.legend(handles=patches, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0, fontsize=40)
plt.savefig(os.path.join(self.dir_of_layout, self.image_filename_stem + "_layout_main.png"))


def save_plot_of_layout_main_all(self, text_regions_p, image_page):
if self.dir_of_all is not None:
values = np.unique(text_regions_p[:, :])
# pixels=['Background' , 'Main text' , 'Heading' , 'Marginalia' ,'Drop capitals' , 'Images' , 'Seperators' , 'Tables', 'Graphics']
pixels=['Background' , 'Main text' , 'Image' , 'Separator','Marginalia']
pixels = ['Background', 'Main text', 'Image', 'Separator', 'Marginalia']
values_indexes = [0, 1, 2, 3, 4]
plt.figure(figsize=(80, 40))
plt.rcParams["font.size"] = "40"
Expand All @@ -68,7 +70,9 @@ def save_plot_of_layout_main_all(self, text_regions_p, image_page):
plt.subplot(1, 2, 2)
im = plt.imshow(text_regions_p[:, :])
colors = [im.cmap(im.norm(value)) for value in values]
patches = [mpatches.Patch(color=colors[np.where(values == i)[0][0]], label="{l}".format(l=pixels[int(np.where(values_indexes == i)[0][0])])) for i in values]
patches = [mpatches.Patch(color=colors[np.where(values == i)[0][0]],
label="{l}".format(l=pixels[int(np.where(values_indexes == i)[0][0])])) for i in
values]
plt.legend(handles=patches, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0, fontsize=60)
plt.savefig(os.path.join(self.dir_of_all, self.image_filename_stem + "_layout_main_and_page.png"))

Expand All @@ -82,7 +86,9 @@ def save_plot_of_layout(self, text_regions_p, image_page):
plt.rcParams["font.size"] = "40"
im = plt.imshow(text_regions_p[:, :])
colors = [im.cmap(im.norm(value)) for value in values]
patches = [mpatches.Patch(color=colors[np.where(values == i)[0][0]], label="{l}".format(l=pixels[int(np.where(values_indexes == i)[0][0])])) for i in values]
patches = [mpatches.Patch(color=colors[np.where(values == i)[0][0]],
label="{l}".format(l=pixels[int(np.where(values_indexes == i)[0][0])])) for i in
values]
plt.legend(handles=patches, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0, fontsize=40)
plt.savefig(os.path.join(self.dir_of_layout, self.image_filename_stem + "_layout.png"))

Expand All @@ -99,7 +105,9 @@ def save_plot_of_layout_all(self, text_regions_p, image_page):
plt.subplot(1, 2, 2)
im = plt.imshow(text_regions_p[:, :])
colors = [im.cmap(im.norm(value)) for value in values]
patches = [mpatches.Patch(color=colors[np.where(values == i)[0][0]], label="{l}".format(l=pixels[int(np.where(values_indexes == i)[0][0])])) for i in values]
patches = [mpatches.Patch(color=colors[np.where(values == i)[0][0]],
label="{l}".format(l=pixels[int(np.where(values_indexes == i)[0][0])])) for i in
values]
plt.legend(handles=patches, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0, fontsize=60)
plt.savefig(os.path.join(self.dir_of_all, self.image_filename_stem + "_layout_and_page.png"))

Expand All @@ -115,7 +123,9 @@ def save_plot_of_textlines(self, textline_mask_tot_ea, image_page):
plt.subplot(1, 2, 2)
im = plt.imshow(textline_mask_tot_ea[:, :])
colors = [im.cmap(im.norm(value)) for value in values]
patches = [mpatches.Patch(color=colors[np.where(values == i)[0][0]], label="{l}".format(l=pixels[int(np.where(values_indexes == i)[0][0])])) for i in values]
patches = [mpatches.Patch(color=colors[np.where(values == i)[0][0]],
label="{l}".format(l=pixels[int(np.where(values_indexes == i)[0][0])])) for i in
values]
plt.legend(handles=patches, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0, fontsize=60)
plt.savefig(os.path.join(self.dir_of_all, self.image_filename_stem + "_textline_and_page.png"))

Expand All @@ -131,33 +141,36 @@ def save_page_image(self, image_page):
cv2.imwrite(os.path.join(self.dir_of_all, self.image_filename_stem + "_page.png"), image_page)
if self.dir_save_page is not None:
cv2.imwrite(os.path.join(self.dir_save_page, self.image_filename_stem + "_page.png"), image_page)

def save_enhanced_image(self, img_res):
cv2.imwrite(os.path.join(self.dir_out, self.image_filename_stem + "_enhanced.png"), img_res)

def save_plot_of_textline_density(self, img_patch_org):
if self.dir_of_all is not None:
plt.figure(figsize=(80,40))
plt.rcParams['font.size']='50'
plt.subplot(1,2,1)
plt.figure(figsize=(80, 40))
plt.rcParams['font.size'] = '50'
plt.subplot(1, 2, 1)
plt.imshow(img_patch_org)
plt.subplot(1,2,2)
plt.plot(gaussian_filter1d(img_patch_org.sum(axis=1), 3),np.array(range(len(gaussian_filter1d(img_patch_org.sum(axis=1), 3)))),linewidth=8)
plt.xlabel('Density of textline prediction in direction of X axis',fontsize=60)
plt.ylabel('Height',fontsize=60)
plt.yticks([0,len(gaussian_filter1d(img_patch_org.sum(axis=1), 3))])
plt.subplot(1, 2, 2)
plt.plot(gaussian_filter1d(img_patch_org.sum(axis=1), 3),
np.array(range(len(gaussian_filter1d(img_patch_org.sum(axis=1), 3)))), linewidth=8)
plt.xlabel('Density of textline prediction in direction of X axis', fontsize=60)
plt.ylabel('Height', fontsize=60)
plt.yticks([0, len(gaussian_filter1d(img_patch_org.sum(axis=1), 3))])
plt.gca().invert_yaxis()
plt.savefig(os.path.join(self.dir_of_all, self.image_filename_stem+'_density_of_textline.png'))
plt.savefig(os.path.join(self.dir_of_all, self.image_filename_stem + '_density_of_textline.png'))

def save_plot_of_rotation_angle(self, angels, var_res):
if self.dir_of_all is not None:
plt.figure(figsize=(60,30))
plt.rcParams['font.size']='50'
plt.plot(angels,np.array(var_res),'-o',markersize=25,linewidth=4)
plt.xlabel('angle',fontsize=50)
plt.ylabel('variance of sum of rotated textline in direction of x axis',fontsize=50)
plt.plot(angels[np.argmax(var_res)],var_res[np.argmax(np.array(var_res))] ,'*',markersize=50,label='Angle of deskewing=' +str("{:.2f}".format(angels[np.argmax(var_res)]))+r'$\degree$')
plt.figure(figsize=(60, 30))
plt.rcParams['font.size'] = '50'
plt.plot(angels, np.array(var_res), '-o', markersize=25, linewidth=4)
plt.xlabel('angle', fontsize=50)
plt.ylabel('variance of sum of rotated textline in direction of x axis', fontsize=50)
plt.plot(angels[np.argmax(var_res)], var_res[np.argmax(np.array(var_res))], '*', markersize=50,
label='Angle of deskewing=' + str("{:.2f}".format(angels[np.argmax(var_res)])) + r'$\degree$')
plt.legend(loc='best')
plt.savefig(os.path.join(self.dir_of_all, self.image_filename_stem+'_rotation_angle.png'))
plt.savefig(os.path.join(self.dir_of_all, self.image_filename_stem + '_rotation_angle.png'))

def write_images_into_directory(self, img_contours, image_page):
if self.dir_of_cropped_images is not None:
Expand All @@ -167,9 +180,9 @@ def write_images_into_directory(self, img_contours, image_page):
box = [x, y, w, h]
croped_page, page_coord = crop_image_inside_box(box, image_page)

croped_page = resize_image(croped_page, int(croped_page.shape[0] / self.scale_y), int(croped_page.shape[1] / self.scale_x))
croped_page = resize_image(croped_page, int(croped_page.shape[0] / self.scale_y),
int(croped_page.shape[1] / self.scale_x))

path = os.path.join(self.dir_of_cropped_images, self.image_filename_stem + "_" + str(index) + ".jpg")
cv2.imwrite(path, croped_page)
index += 1

1 change: 1 addition & 0 deletions eynollah/eynollah/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

OCRD_TOOL = loads(resource_string(__name__, 'ocrd-tool.json').decode('utf8'))


class EynollahProcessor(Processor):

def __init__(self, *args, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
import os
import sys
import tensorflow as tf
import keras , warnings
import keras, warnings
from keras.optimizers import *
from sacred import Experiment
from models import *
from utils import *
from metrics import *




def configuration():
gpu_options = tf.compat.v1.GPUOptions(allow_growth=True)
session = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options))


if __name__=='__main__':
if __name__ == '__main__':
n_classes = 2
input_height = 224
input_width = 448
weight_decay = 1e-6
pretraining = False
dir_of_weights = 'model_bin_sbb_ens.h5'
#configuration()
model = resnet50_unet(n_classes, input_height, input_width,weight_decay,pretraining)

# configuration()

model = resnet50_unet(n_classes, input_height, input_width, weight_decay, pretraining)
model.load_weights(dir_of_weights)
model.save('./name_in_another_python_version.h5')


Loading

0 comments on commit 1a95bca

Please sign in to comment.