mirror of
https://github.com/qurator-spk/eynollah.git
synced 2025-06-08 11:49:55 +02:00
pep8: whitespaces around operators
This commit is contained in:
parent
fa7bb63481
commit
87ae6d11a9
13 changed files with 3141 additions and 2917 deletions
|
@ -17,21 +17,18 @@ def main():
|
||||||
help="directory of GT page-xml files",
|
help="directory of GT page-xml files",
|
||||||
type=click.Path(exists=True, file_okay=False),
|
type=click.Path(exists=True, file_okay=False),
|
||||||
)
|
)
|
||||||
|
|
||||||
@click.option(
|
@click.option(
|
||||||
"--dir_out_modal_image",
|
"--dir_out_modal_image",
|
||||||
"-domi",
|
"-domi",
|
||||||
help="directory where ground truth images would be written",
|
help="directory where ground truth images would be written",
|
||||||
type=click.Path(exists=True, file_okay=False),
|
type=click.Path(exists=True, file_okay=False),
|
||||||
)
|
)
|
||||||
|
|
||||||
@click.option(
|
@click.option(
|
||||||
"--dir_out_classes",
|
"--dir_out_classes",
|
||||||
"-docl",
|
"-docl",
|
||||||
help="directory where ground truth classes would be written",
|
help="directory where ground truth classes would be written",
|
||||||
type=click.Path(exists=True, file_okay=False),
|
type=click.Path(exists=True, file_okay=False),
|
||||||
)
|
)
|
||||||
|
|
||||||
@click.option(
|
@click.option(
|
||||||
"--input_height",
|
"--input_height",
|
||||||
"-ih",
|
"-ih",
|
||||||
|
@ -47,18 +44,17 @@ def main():
|
||||||
"-min",
|
"-min",
|
||||||
help="min area size of regions considered for reading order training.",
|
help="min area size of regions considered for reading order training.",
|
||||||
)
|
)
|
||||||
|
def machine_based_reading_order(dir_xml, dir_out_modal_image, dir_out_classes, input_height, input_width,
|
||||||
def machine_based_reading_order(dir_xml, dir_out_modal_image, dir_out_classes, input_height, input_width, min_area_size):
|
min_area_size):
|
||||||
xml_files_ind = os.listdir(dir_xml)
|
xml_files_ind = os.listdir(dir_xml)
|
||||||
|
|
||||||
|
|
||||||
@main.command()
|
@main.command()
|
||||||
@click.option('--patches/--no-patches', default=True, help='by enabling this parameter you let the model to see the image in patches.')
|
@click.option('--patches/--no-patches', default=True,
|
||||||
|
help='by enabling this parameter you let the model to see the image in patches.')
|
||||||
@click.option('--model_dir', '-m', type=click.Path(exists=True, file_okay=False), required=True, help='directory containing models for prediction')
|
@click.option('--model_dir', '-m', type=click.Path(exists=True, file_okay=False), required=True,
|
||||||
|
help='directory containing models for prediction')
|
||||||
@click.argument('input_image')
|
@click.argument('input_image')
|
||||||
|
|
||||||
@click.argument('output_image')
|
@click.argument('output_image')
|
||||||
@click.option(
|
@click.option(
|
||||||
"--dir_in",
|
"--dir_in",
|
||||||
|
@ -72,7 +68,6 @@ def machine_based_reading_order(dir_xml, dir_out_modal_image, dir_out_classes, i
|
||||||
help="directory where the binarized images will be written",
|
help="directory where the binarized images will be written",
|
||||||
type=click.Path(exists=True, file_okay=False),
|
type=click.Path(exists=True, file_okay=False),
|
||||||
)
|
)
|
||||||
|
|
||||||
def binarization(patches, model_dir, input_image, output_image, dir_in, dir_out):
|
def binarization(patches, model_dir, input_image, output_image, dir_in, dir_out):
|
||||||
if not dir_out and dir_in:
|
if not dir_out and dir_in:
|
||||||
print("Error: You used -di but did not set -do")
|
print("Error: You used -di but did not set -do")
|
||||||
|
@ -80,9 +75,8 @@ def binarization(patches, model_dir, input_image, output_image, dir_in, dir_out)
|
||||||
elif dir_out and not dir_in:
|
elif dir_out and not dir_in:
|
||||||
print("Error: You used -do to write out binarized images but have not set -di")
|
print("Error: You used -do to write out binarized images but have not set -di")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
SbbBinarizer(model_dir).run(image_path=input_image, use_patches=patches, save=output_image, dir_in=dir_in, dir_out=dir_out)
|
SbbBinarizer(model_dir).run(image_path=input_image, use_patches=patches, save=output_image, dir_in=dir_in,
|
||||||
|
dir_out=dir_out)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@main.command()
|
@main.command()
|
||||||
|
@ -92,7 +86,6 @@ def binarization(patches, model_dir, input_image, output_image, dir_in, dir_out)
|
||||||
help="image filename",
|
help="image filename",
|
||||||
type=click.Path(exists=True, dir_okay=False),
|
type=click.Path(exists=True, dir_okay=False),
|
||||||
)
|
)
|
||||||
|
|
||||||
@click.option(
|
@click.option(
|
||||||
"--out",
|
"--out",
|
||||||
"-o",
|
"-o",
|
||||||
|
@ -261,15 +254,19 @@ def binarization(patches, model_dir, input_image, output_image, dir_in, dir_out)
|
||||||
type=click.Choice(['OFF', 'DEBUG', 'INFO', 'WARN', 'ERROR']),
|
type=click.Choice(['OFF', 'DEBUG', 'INFO', 'WARN', 'ERROR']),
|
||||||
help="Override log level globally to this",
|
help="Override log level globally to this",
|
||||||
)
|
)
|
||||||
|
def layout(image, out, overwrite, dir_in, model, save_images, save_layout, save_deskewed, save_all, extract_only_images,
|
||||||
def layout(image, out, overwrite, dir_in, model, save_images, save_layout, save_deskewed, save_all, extract_only_images, save_page, enable_plotting, allow_enhancement, curved_line, textline_light, full_layout, tables, right2left, input_binary, allow_scaling, headers_off, light_version, reading_order_machine_based, do_ocr, num_col_upper, num_col_lower, skip_layout_and_reading_order, ignore_page_extraction, log_level):
|
save_page, enable_plotting, allow_enhancement, curved_line, textline_light, full_layout, tables, right2left,
|
||||||
|
input_binary, allow_scaling, headers_off, light_version, reading_order_machine_based, do_ocr, num_col_upper,
|
||||||
|
num_col_lower, skip_layout_and_reading_order, ignore_page_extraction, log_level):
|
||||||
initLogging()
|
initLogging()
|
||||||
if log_level:
|
if log_level:
|
||||||
getLogger('eynollah').setLevel(getLevelName(log_level))
|
getLogger('eynollah').setLevel(getLevelName(log_level))
|
||||||
if not enable_plotting and (save_layout or save_deskewed or save_all or save_page or save_images or allow_enhancement):
|
if not enable_plotting and (
|
||||||
|
save_layout or save_deskewed or save_all or save_page or save_images or allow_enhancement):
|
||||||
print("Error: You used one of -sl, -sd, -sa, -sp, -si or -ae but did not enable plotting with -ep")
|
print("Error: You used one of -sl, -sd, -sa, -sp, -si or -ae but did not enable plotting with -ep")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
elif enable_plotting and not (save_layout or save_deskewed or save_all or save_page or save_images or allow_enhancement):
|
elif enable_plotting and not (
|
||||||
|
save_layout or save_deskewed or save_all or save_page or save_images or allow_enhancement):
|
||||||
print("Error: You used -ep to enable plotting but set none of -sl, -sd, -sa, -sp, -si or -ae")
|
print("Error: You used -ep to enable plotting but set none of -sl, -sd, -sa, -sp, -si or -ae")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
if textline_light and not light_version:
|
if textline_light and not light_version:
|
||||||
|
@ -277,8 +274,10 @@ def layout(image, out, overwrite, dir_in, model, save_images, save_layout, save_
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
if light_version and not textline_light:
|
if light_version and not textline_light:
|
||||||
print('Error: You used -light without -tll. Light version need light textline to be enabled.')
|
print('Error: You used -light without -tll. Light version need light textline to be enabled.')
|
||||||
if extract_only_images and (allow_enhancement or allow_scaling or light_version or curved_line or textline_light or full_layout or tables or right2left or headers_off) :
|
if extract_only_images and (
|
||||||
print('Error: You used -eoi which can not be enabled alongside light_version -light or allow_scaling -as or allow_enhancement -ae or curved_line -cl or textline_light -tll or full_layout -fl or tables -tab or right2left -r2l or headers_off -ho')
|
allow_enhancement or allow_scaling or light_version or curved_line or textline_light or full_layout or tables or right2left or headers_off):
|
||||||
|
print(
|
||||||
|
'Error: You used -eoi which can not be enabled alongside light_version -light or allow_scaling -as or allow_enhancement -ae or curved_line -cl or textline_light -tll or full_layout -fl or tables -tab or right2left -r2l or headers_off -ho')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
eynollah = Eynollah(
|
eynollah = Eynollah(
|
||||||
image_filename=image,
|
image_filename=image,
|
||||||
|
@ -368,8 +367,8 @@ def layout(image, out, overwrite, dir_in, model, save_images, save_layout, save_
|
||||||
type=click.Choice(['OFF', 'DEBUG', 'INFO', 'WARN', 'ERROR']),
|
type=click.Choice(['OFF', 'DEBUG', 'INFO', 'WARN', 'ERROR']),
|
||||||
help="Override log level globally to this",
|
help="Override log level globally to this",
|
||||||
)
|
)
|
||||||
|
def ocr(dir_in, out, dir_xmls, model, tr_ocr, export_textline_images_and_text, do_not_mask_with_textline_contour,
|
||||||
def ocr(dir_in, out, dir_xmls, model, tr_ocr, export_textline_images_and_text, do_not_mask_with_textline_contour, log_level):
|
log_level):
|
||||||
if log_level:
|
if log_level:
|
||||||
setOverrideLogLevel(log_level)
|
setOverrideLogLevel(log_level)
|
||||||
initLogging()
|
initLogging()
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -115,14 +115,16 @@ class SbbBinarizeProcessor(Processor):
|
||||||
file_id + '.IMG-BIN',
|
file_id + '.IMG-BIN',
|
||||||
page_id=input_file.pageId,
|
page_id=input_file.pageId,
|
||||||
file_grp=self.output_file_grp)
|
file_grp=self.output_file_grp)
|
||||||
page.add_AlternativeImage(AlternativeImageType(filename=bin_image_path, comments='%s,binarized' % page_xywh['features']))
|
page.add_AlternativeImage(
|
||||||
|
AlternativeImageType(filename=bin_image_path, comments='%s,binarized' % page_xywh['features']))
|
||||||
|
|
||||||
elif oplevel == 'region':
|
elif oplevel == 'region':
|
||||||
regions = page.get_AllRegions(['Text', 'Table'], depth=1)
|
regions = page.get_AllRegions(['Text', 'Table'], depth=1)
|
||||||
if not regions:
|
if not regions:
|
||||||
LOG.warning("Page '%s' contains no text/table regions", page_id)
|
LOG.warning("Page '%s' contains no text/table regions", page_id)
|
||||||
for region in regions:
|
for region in regions:
|
||||||
region_image, region_xywh = self.workspace.image_from_segment(region, page_image, page_xywh, feature_filter='binarized')
|
region_image, region_xywh = self.workspace.image_from_segment(region, page_image, page_xywh,
|
||||||
|
feature_filter='binarized')
|
||||||
region_image_bin = cv2pil(binarizer.run(image=pil2cv(region_image), use_patches=True))
|
region_image_bin = cv2pil(binarizer.run(image=pil2cv(region_image), use_patches=True))
|
||||||
region_image_bin_path = self.workspace.save_image_file(
|
region_image_bin_path = self.workspace.save_image_file(
|
||||||
region_image_bin,
|
region_image_bin,
|
||||||
|
@ -130,14 +132,16 @@ class SbbBinarizeProcessor(Processor):
|
||||||
page_id=input_file.pageId,
|
page_id=input_file.pageId,
|
||||||
file_grp=self.output_file_grp)
|
file_grp=self.output_file_grp)
|
||||||
region.add_AlternativeImage(
|
region.add_AlternativeImage(
|
||||||
AlternativeImageType(filename=region_image_bin_path, comments='%s,binarized' % region_xywh['features']))
|
AlternativeImageType(filename=region_image_bin_path,
|
||||||
|
comments='%s,binarized' % region_xywh['features']))
|
||||||
|
|
||||||
elif oplevel == 'line':
|
elif oplevel == 'line':
|
||||||
region_line_tuples = [(r.id, r.get_TextLine()) for r in page.get_AllRegions(['Text'], depth=0)]
|
region_line_tuples = [(r.id, r.get_TextLine()) for r in page.get_AllRegions(['Text'], depth=0)]
|
||||||
if not region_line_tuples:
|
if not region_line_tuples:
|
||||||
LOG.warning("Page '%s' contains no text lines", page_id)
|
LOG.warning("Page '%s' contains no text lines", page_id)
|
||||||
for region_id, line in region_line_tuples:
|
for region_id, line in region_line_tuples:
|
||||||
line_image, line_xywh = self.workspace.image_from_segment(line, page_image, page_xywh, feature_filter='binarized')
|
line_image, line_xywh = self.workspace.image_from_segment(line, page_image, page_xywh,
|
||||||
|
feature_filter='binarized')
|
||||||
line_image_bin = cv2pil(binarizer.run(image=pil2cv(line_image), use_patches=True))
|
line_image_bin = cv2pil(binarizer.run(image=pil2cv(line_image), use_patches=True))
|
||||||
line_image_bin_path = self.workspace.save_image_file(
|
line_image_bin_path = self.workspace.save_image_file(
|
||||||
line_image_bin,
|
line_image_bin,
|
||||||
|
@ -145,7 +149,8 @@ class SbbBinarizeProcessor(Processor):
|
||||||
page_id=input_file.pageId,
|
page_id=input_file.pageId,
|
||||||
file_grp=self.output_file_grp)
|
file_grp=self.output_file_grp)
|
||||||
line.add_AlternativeImage(
|
line.add_AlternativeImage(
|
||||||
AlternativeImageType(filename=line_image_bin_path, comments='%s,binarized' % line_xywh['features']))
|
AlternativeImageType(filename=line_image_bin_path,
|
||||||
|
comments='%s,binarized' % line_xywh['features']))
|
||||||
|
|
||||||
self.workspace.add_file(
|
self.workspace.add_file(
|
||||||
ID=file_id,
|
ID=file_id,
|
||||||
|
|
|
@ -45,22 +45,23 @@ class EynollahPlotter:
|
||||||
if self.dir_of_layout is not None:
|
if self.dir_of_layout is not None:
|
||||||
values = np.unique(text_regions_p[:, :])
|
values = np.unique(text_regions_p[:, :])
|
||||||
# pixels=['Background' , 'Main text' , 'Heading' , 'Marginalia' ,'Drop capitals' , 'Images' , 'Seperators' , 'Tables', 'Graphics']
|
# 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]
|
values_indexes = [0, 1, 2, 3, 4]
|
||||||
plt.figure(figsize=(40, 40))
|
plt.figure(figsize=(40, 40))
|
||||||
plt.rcParams["font.size"] = "40"
|
plt.rcParams["font.size"] = "40"
|
||||||
im = plt.imshow(text_regions_p[:, :])
|
im = plt.imshow(text_regions_p[:, :])
|
||||||
colors = [im.cmap(im.norm(value)) for value in values]
|
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.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"))
|
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):
|
def save_plot_of_layout_main_all(self, text_regions_p, image_page):
|
||||||
if self.dir_of_all is not None:
|
if self.dir_of_all is not None:
|
||||||
values = np.unique(text_regions_p[:, :])
|
values = np.unique(text_regions_p[:, :])
|
||||||
# pixels=['Background' , 'Main text' , 'Heading' , 'Marginalia' ,'Drop capitals' , 'Images' , 'Seperators' , 'Tables', 'Graphics']
|
# 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]
|
values_indexes = [0, 1, 2, 3, 4]
|
||||||
plt.figure(figsize=(80, 40))
|
plt.figure(figsize=(80, 40))
|
||||||
plt.rcParams["font.size"] = "40"
|
plt.rcParams["font.size"] = "40"
|
||||||
|
@ -69,7 +70,9 @@ class EynollahPlotter:
|
||||||
plt.subplot(1, 2, 2)
|
plt.subplot(1, 2, 2)
|
||||||
im = plt.imshow(text_regions_p[:, :])
|
im = plt.imshow(text_regions_p[:, :])
|
||||||
colors = [im.cmap(im.norm(value)) for value in values]
|
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.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"))
|
plt.savefig(os.path.join(self.dir_of_all, self.image_filename_stem + "_layout_main_and_page.png"))
|
||||||
|
|
||||||
|
@ -83,7 +86,9 @@ class EynollahPlotter:
|
||||||
plt.rcParams["font.size"] = "40"
|
plt.rcParams["font.size"] = "40"
|
||||||
im = plt.imshow(text_regions_p[:, :])
|
im = plt.imshow(text_regions_p[:, :])
|
||||||
colors = [im.cmap(im.norm(value)) for value in values]
|
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.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"))
|
plt.savefig(os.path.join(self.dir_of_layout, self.image_filename_stem + "_layout.png"))
|
||||||
|
|
||||||
|
@ -100,7 +105,9 @@ class EynollahPlotter:
|
||||||
plt.subplot(1, 2, 2)
|
plt.subplot(1, 2, 2)
|
||||||
im = plt.imshow(text_regions_p[:, :])
|
im = plt.imshow(text_regions_p[:, :])
|
||||||
colors = [im.cmap(im.norm(value)) for value in values]
|
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.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"))
|
plt.savefig(os.path.join(self.dir_of_all, self.image_filename_stem + "_layout_and_page.png"))
|
||||||
|
|
||||||
|
@ -116,7 +123,9 @@ class EynollahPlotter:
|
||||||
plt.subplot(1, 2, 2)
|
plt.subplot(1, 2, 2)
|
||||||
im = plt.imshow(textline_mask_tot_ea[:, :])
|
im = plt.imshow(textline_mask_tot_ea[:, :])
|
||||||
colors = [im.cmap(im.norm(value)) for value in values]
|
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.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"))
|
plt.savefig(os.path.join(self.dir_of_all, self.image_filename_stem + "_textline_and_page.png"))
|
||||||
|
|
||||||
|
@ -132,33 +141,36 @@ class EynollahPlotter:
|
||||||
cv2.imwrite(os.path.join(self.dir_of_all, self.image_filename_stem + "_page.png"), 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:
|
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)
|
cv2.imwrite(os.path.join(self.dir_save_page, self.image_filename_stem + "_page.png"), image_page)
|
||||||
|
|
||||||
def save_enhanced_image(self, img_res):
|
def save_enhanced_image(self, img_res):
|
||||||
cv2.imwrite(os.path.join(self.dir_out, self.image_filename_stem + "_enhanced.png"), 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):
|
def save_plot_of_textline_density(self, img_patch_org):
|
||||||
if self.dir_of_all is not None:
|
if self.dir_of_all is not None:
|
||||||
plt.figure(figsize=(80,40))
|
plt.figure(figsize=(80, 40))
|
||||||
plt.rcParams['font.size']='50'
|
plt.rcParams['font.size'] = '50'
|
||||||
plt.subplot(1,2,1)
|
plt.subplot(1, 2, 1)
|
||||||
plt.imshow(img_patch_org)
|
plt.imshow(img_patch_org)
|
||||||
plt.subplot(1,2,2)
|
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.plot(gaussian_filter1d(img_patch_org.sum(axis=1), 3),
|
||||||
plt.xlabel('Density of textline prediction in direction of X axis',fontsize=60)
|
np.array(range(len(gaussian_filter1d(img_patch_org.sum(axis=1), 3)))), linewidth=8)
|
||||||
plt.ylabel('Height',fontsize=60)
|
plt.xlabel('Density of textline prediction in direction of X axis', fontsize=60)
|
||||||
plt.yticks([0,len(gaussian_filter1d(img_patch_org.sum(axis=1), 3))])
|
plt.ylabel('Height', fontsize=60)
|
||||||
|
plt.yticks([0, len(gaussian_filter1d(img_patch_org.sum(axis=1), 3))])
|
||||||
plt.gca().invert_yaxis()
|
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):
|
def save_plot_of_rotation_angle(self, angels, var_res):
|
||||||
if self.dir_of_all is not None:
|
if self.dir_of_all is not None:
|
||||||
plt.figure(figsize=(60,30))
|
plt.figure(figsize=(60, 30))
|
||||||
plt.rcParams['font.size']='50'
|
plt.rcParams['font.size'] = '50'
|
||||||
plt.plot(angels,np.array(var_res),'-o',markersize=25,linewidth=4)
|
plt.plot(angels, np.array(var_res), '-o', markersize=25, linewidth=4)
|
||||||
plt.xlabel('angle',fontsize=50)
|
plt.xlabel('angle', fontsize=50)
|
||||||
plt.ylabel('variance of sum of rotated textline in direction of x axis',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.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.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):
|
def write_images_into_directory(self, img_contours, image_page):
|
||||||
if self.dir_of_cropped_images is not None:
|
if self.dir_of_cropped_images is not None:
|
||||||
|
@ -168,9 +180,9 @@ class EynollahPlotter:
|
||||||
box = [x, y, w, h]
|
box = [x, y, w, h]
|
||||||
croped_page, page_coord = crop_image_inside_box(box, image_page)
|
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")
|
path = os.path.join(self.dir_of_cropped_images, self.image_filename_stem + "_" + str(index) + ".jpg")
|
||||||
cv2.imwrite(path, croped_page)
|
cv2.imwrite(path, croped_page)
|
||||||
index += 1
|
index += 1
|
||||||
|
|
||||||
|
|
|
@ -12,14 +12,15 @@ import os
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import cv2
|
import cv2
|
||||||
|
|
||||||
environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
||||||
stderr = sys.stderr
|
stderr = sys.stderr
|
||||||
sys.stderr = open(devnull, 'w')
|
sys.stderr = open(devnull, 'w')
|
||||||
import tensorflow as tf
|
import tensorflow as tf
|
||||||
from tensorflow.keras.models import load_model
|
from tensorflow.keras.models import load_model
|
||||||
from tensorflow.python.keras import backend as tensorflow_backend
|
from tensorflow.python.keras import backend as tensorflow_backend
|
||||||
sys.stderr = stderr
|
|
||||||
|
|
||||||
|
sys.stderr = stderr
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
@ -36,7 +37,7 @@ class SbbBinarizer:
|
||||||
|
|
||||||
self.start_new_session()
|
self.start_new_session()
|
||||||
|
|
||||||
self.model_files = glob(self.model_dir+"/*/", recursive = True)
|
self.model_files = glob(self.model_dir + "/*/", recursive=True)
|
||||||
|
|
||||||
self.models = []
|
self.models = []
|
||||||
for model_file in self.model_files:
|
for model_file in self.model_files:
|
||||||
|
@ -56,9 +57,9 @@ class SbbBinarizer:
|
||||||
|
|
||||||
def load_model(self, model_name):
|
def load_model(self, model_name):
|
||||||
model = load_model(join(self.model_dir, model_name), compile=False)
|
model = load_model(join(self.model_dir, model_name), compile=False)
|
||||||
model_height = model.layers[len(model.layers)-1].output_shape[1]
|
model_height = model.layers[len(model.layers) - 1].output_shape[1]
|
||||||
model_width = model.layers[len(model.layers)-1].output_shape[2]
|
model_width = model.layers[len(model.layers) - 1].output_shape[2]
|
||||||
n_classes = model.layers[len(model.layers)-1].output_shape[3]
|
n_classes = model.layers[len(model.layers) - 1].output_shape[3]
|
||||||
return model, model_height, model_width, n_classes
|
return model, model_height, model_width, n_classes
|
||||||
|
|
||||||
def predict(self, model_in, img, use_patches, n_batch_inference=5):
|
def predict(self, model_in, img, use_patches, n_batch_inference=5):
|
||||||
|
@ -69,40 +70,37 @@ class SbbBinarizer:
|
||||||
img_org_w = img.shape[1]
|
img_org_w = img.shape[1]
|
||||||
|
|
||||||
if img.shape[0] < model_height and img.shape[1] >= model_width:
|
if img.shape[0] < model_height and img.shape[1] >= model_width:
|
||||||
img_padded = np.zeros(( model_height, img.shape[1], img.shape[2] ))
|
img_padded = np.zeros((model_height, img.shape[1], img.shape[2]))
|
||||||
|
|
||||||
index_start_h = int( abs( img.shape[0] - model_height) /2.)
|
index_start_h = int(abs(img.shape[0] - model_height) / 2.)
|
||||||
index_start_w = 0
|
index_start_w = 0
|
||||||
|
|
||||||
img_padded [ index_start_h: index_start_h+img.shape[0], :, : ] = img[:,:,:]
|
img_padded[index_start_h: index_start_h + img.shape[0], :, :] = img[:, :, :]
|
||||||
|
|
||||||
elif img.shape[0] >= model_height and img.shape[1] < model_width:
|
elif img.shape[0] >= model_height and img.shape[1] < model_width:
|
||||||
img_padded = np.zeros(( img.shape[0], model_width, img.shape[2] ))
|
img_padded = np.zeros((img.shape[0], model_width, img.shape[2]))
|
||||||
|
|
||||||
index_start_h = 0
|
index_start_h = 0
|
||||||
index_start_w = int( abs( img.shape[1] - model_width) /2.)
|
index_start_w = int(abs(img.shape[1] - model_width) / 2.)
|
||||||
|
|
||||||
img_padded [ :, index_start_w: index_start_w+img.shape[1], : ] = img[:,:,:]
|
|
||||||
|
|
||||||
|
img_padded[:, index_start_w: index_start_w + img.shape[1], :] = img[:, :, :]
|
||||||
|
|
||||||
elif img.shape[0] < model_height and img.shape[1] < model_width:
|
elif img.shape[0] < model_height and img.shape[1] < model_width:
|
||||||
img_padded = np.zeros(( model_height, model_width, img.shape[2] ))
|
img_padded = np.zeros((model_height, model_width, img.shape[2]))
|
||||||
|
|
||||||
index_start_h = int( abs( img.shape[0] - model_height) /2.)
|
index_start_h = int(abs(img.shape[0] - model_height) / 2.)
|
||||||
index_start_w = int( abs( img.shape[1] - model_width) /2.)
|
index_start_w = int(abs(img.shape[1] - model_width) / 2.)
|
||||||
|
|
||||||
img_padded [ index_start_h: index_start_h+img.shape[0], index_start_w: index_start_w+img.shape[1], : ] = img[:,:,:]
|
img_padded[index_start_h: index_start_h + img.shape[0], index_start_w: index_start_w + img.shape[1],
|
||||||
|
:] = img[:, :, :]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
index_start_h = 0
|
index_start_h = 0
|
||||||
index_start_w = 0
|
index_start_w = 0
|
||||||
img_padded = np.copy(img)
|
img_padded = np.copy(img)
|
||||||
|
|
||||||
|
|
||||||
img = np.copy(img_padded)
|
img = np.copy(img_padded)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if use_patches:
|
if use_patches:
|
||||||
|
|
||||||
margin = int(0.1 * model_width)
|
margin = int(0.1 * model_width)
|
||||||
|
@ -110,7 +108,6 @@ class SbbBinarizer:
|
||||||
width_mid = model_width - 2 * margin
|
width_mid = model_width - 2 * margin
|
||||||
height_mid = model_height - 2 * margin
|
height_mid = model_height - 2 * margin
|
||||||
|
|
||||||
|
|
||||||
img = img / float(255.0)
|
img = img / float(255.0)
|
||||||
|
|
||||||
img_h = img.shape[0]
|
img_h = img.shape[0]
|
||||||
|
@ -131,7 +128,6 @@ class SbbBinarizer:
|
||||||
else:
|
else:
|
||||||
nyf = int(nyf)
|
nyf = int(nyf)
|
||||||
|
|
||||||
|
|
||||||
list_i_s = []
|
list_i_s = []
|
||||||
list_j_s = []
|
list_j_s = []
|
||||||
list_x_u = []
|
list_x_u = []
|
||||||
|
@ -141,7 +137,7 @@ class SbbBinarizer:
|
||||||
|
|
||||||
batch_indexer = 0
|
batch_indexer = 0
|
||||||
|
|
||||||
img_patch = np.zeros((n_batch_inference, model_height, model_width,3))
|
img_patch = np.zeros((n_batch_inference, model_height, model_width, 3))
|
||||||
|
|
||||||
for i in range(nxf):
|
for i in range(nxf):
|
||||||
for j in range(nyf):
|
for j in range(nyf):
|
||||||
|
@ -167,7 +163,6 @@ class SbbBinarizer:
|
||||||
index_y_u = img_h
|
index_y_u = img_h
|
||||||
index_y_d = img_h - model_height
|
index_y_d = img_h - model_height
|
||||||
|
|
||||||
|
|
||||||
list_i_s.append(i)
|
list_i_s.append(i)
|
||||||
list_j_s.append(j)
|
list_j_s.append(j)
|
||||||
list_x_u.append(index_x_u)
|
list_x_u.append(index_x_u)
|
||||||
|
@ -175,16 +170,13 @@ class SbbBinarizer:
|
||||||
list_y_d.append(index_y_d)
|
list_y_d.append(index_y_d)
|
||||||
list_y_u.append(index_y_u)
|
list_y_u.append(index_y_u)
|
||||||
|
|
||||||
|
img_patch[batch_indexer, :, :, :] = img[index_y_d:index_y_u, index_x_d:index_x_u, :]
|
||||||
img_patch[batch_indexer,:,:,:] = img[index_y_d:index_y_u, index_x_d:index_x_u, :]
|
|
||||||
|
|
||||||
batch_indexer = batch_indexer + 1
|
batch_indexer = batch_indexer + 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if batch_indexer == n_batch_inference:
|
if batch_indexer == n_batch_inference:
|
||||||
|
|
||||||
label_p_pred = model.predict(img_patch,verbose=0)
|
label_p_pred = model.predict(img_patch, verbose=0)
|
||||||
|
|
||||||
seg = np.argmax(label_p_pred, axis=3)
|
seg = np.argmax(label_p_pred, axis=3)
|
||||||
|
|
||||||
|
@ -192,7 +184,7 @@ class SbbBinarizer:
|
||||||
|
|
||||||
indexer_inside_batch = 0
|
indexer_inside_batch = 0
|
||||||
for i_batch, j_batch in zip(list_i_s, list_j_s):
|
for i_batch, j_batch in zip(list_i_s, list_j_s):
|
||||||
seg_in = seg[indexer_inside_batch,:,:]
|
seg_in = seg[indexer_inside_batch, :, :]
|
||||||
seg_color = np.repeat(seg_in[:, :, np.newaxis], 3, axis=2)
|
seg_color = np.repeat(seg_in[:, :, np.newaxis], 3, axis=2)
|
||||||
|
|
||||||
index_y_u_in = list_y_u[indexer_inside_batch]
|
index_y_u_in = list_y_u[indexer_inside_batch]
|
||||||
|
@ -202,35 +194,48 @@ class SbbBinarizer:
|
||||||
index_x_d_in = list_x_d[indexer_inside_batch]
|
index_x_d_in = list_x_d[indexer_inside_batch]
|
||||||
|
|
||||||
if i_batch == 0 and j_batch == 0:
|
if i_batch == 0 and j_batch == 0:
|
||||||
seg_color = seg_color[0 : seg_color.shape[0] - margin, 0 : seg_color.shape[1] - margin, :]
|
seg_color = seg_color[0: seg_color.shape[0] - margin, 0: seg_color.shape[1] - margin, :]
|
||||||
prediction_true[index_y_d_in + 0 : index_y_u_in - margin, index_x_d_in + 0 : index_x_u_in - margin, :] = seg_color
|
prediction_true[index_y_d_in + 0: index_y_u_in - margin,
|
||||||
|
index_x_d_in + 0: index_x_u_in - margin, :] = seg_color
|
||||||
elif i_batch == nxf - 1 and j_batch == nyf - 1:
|
elif i_batch == nxf - 1 and j_batch == 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, :]
|
||||||
prediction_true[index_y_d_in + margin : index_y_u_in - 0, index_x_d_in + margin : index_x_u_in - 0, :] = seg_color
|
prediction_true[index_y_d_in + margin: index_y_u_in - 0,
|
||||||
|
index_x_d_in + margin: index_x_u_in - 0, :] = seg_color
|
||||||
elif i_batch == 0 and j_batch == nyf - 1:
|
elif i_batch == 0 and j_batch == 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, :]
|
||||||
prediction_true[index_y_d_in + margin : index_y_u_in - 0, index_x_d_in + 0 : index_x_u_in - margin, :] = seg_color
|
prediction_true[index_y_d_in + margin: index_y_u_in - 0,
|
||||||
|
index_x_d_in + 0: index_x_u_in - margin, :] = seg_color
|
||||||
elif i_batch == nxf - 1 and j_batch == 0:
|
elif i_batch == nxf - 1 and j_batch == 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, :]
|
||||||
prediction_true[index_y_d_in + 0 : index_y_u_in - margin, index_x_d_in + margin : index_x_u_in - 0, :] = seg_color
|
prediction_true[index_y_d_in + 0: index_y_u_in - margin,
|
||||||
|
index_x_d_in + margin: index_x_u_in - 0, :] = seg_color
|
||||||
elif i_batch == 0 and j_batch != 0 and j_batch != nyf - 1:
|
elif i_batch == 0 and j_batch != 0 and j_batch != 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,
|
||||||
prediction_true[index_y_d_in + margin : index_y_u_in - margin, index_x_d_in + 0 : index_x_u_in - margin, :] = seg_color
|
0: seg_color.shape[1] - margin, :]
|
||||||
|
prediction_true[index_y_d_in + margin: index_y_u_in - margin,
|
||||||
|
index_x_d_in + 0: index_x_u_in - margin, :] = seg_color
|
||||||
elif i_batch == nxf - 1 and j_batch != 0 and j_batch != nyf - 1:
|
elif i_batch == nxf - 1 and j_batch != 0 and j_batch != 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,
|
||||||
prediction_true[index_y_d_in + margin : index_y_u_in - margin, index_x_d_in + margin : index_x_u_in - 0, :] = seg_color
|
margin: seg_color.shape[1] - 0, :]
|
||||||
|
prediction_true[index_y_d_in + margin: index_y_u_in - margin,
|
||||||
|
index_x_d_in + margin: index_x_u_in - 0, :] = seg_color
|
||||||
elif i_batch != 0 and i_batch != nxf - 1 and j_batch == 0:
|
elif i_batch != 0 and i_batch != nxf - 1 and j_batch == 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,
|
||||||
prediction_true[index_y_d_in + 0 : index_y_u_in - margin, index_x_d_in + margin : index_x_u_in - margin, :] = seg_color
|
margin: seg_color.shape[1] - margin, :]
|
||||||
|
prediction_true[index_y_d_in + 0: index_y_u_in - margin,
|
||||||
|
index_x_d_in + margin: index_x_u_in - margin, :] = seg_color
|
||||||
elif i_batch != 0 and i_batch != nxf - 1 and j_batch == nyf - 1:
|
elif i_batch != 0 and i_batch != nxf - 1 and j_batch == 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,
|
||||||
prediction_true[index_y_d_in + margin : index_y_u_in - 0, index_x_d_in + margin : index_x_u_in - margin, :] = seg_color
|
margin: seg_color.shape[1] - margin, :]
|
||||||
|
prediction_true[index_y_d_in + margin: index_y_u_in - 0,
|
||||||
|
index_x_d_in + margin: index_x_u_in - margin, :] = 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,
|
||||||
prediction_true[index_y_d_in + margin : index_y_u_in - margin, index_x_d_in + margin : index_x_u_in - margin, :] = seg_color
|
margin: seg_color.shape[1] - margin, :]
|
||||||
|
prediction_true[index_y_d_in + margin: index_y_u_in - margin,
|
||||||
indexer_inside_batch = indexer_inside_batch +1
|
index_x_d_in + margin: index_x_u_in - margin, :] = seg_color
|
||||||
|
|
||||||
|
indexer_inside_batch = indexer_inside_batch + 1
|
||||||
|
|
||||||
list_i_s = []
|
list_i_s = []
|
||||||
list_j_s = []
|
list_j_s = []
|
||||||
|
@ -241,10 +246,10 @@ class SbbBinarizer:
|
||||||
|
|
||||||
batch_indexer = 0
|
batch_indexer = 0
|
||||||
|
|
||||||
img_patch = np.zeros((n_batch_inference, model_height, model_width,3))
|
img_patch = np.zeros((n_batch_inference, model_height, model_width, 3))
|
||||||
|
|
||||||
elif i==(nxf-1) and j==(nyf-1):
|
elif i == (nxf - 1) and j == (nyf - 1):
|
||||||
label_p_pred = model.predict(img_patch,verbose=0)
|
label_p_pred = model.predict(img_patch, verbose=0)
|
||||||
|
|
||||||
seg = np.argmax(label_p_pred, axis=3)
|
seg = np.argmax(label_p_pred, axis=3)
|
||||||
|
|
||||||
|
@ -252,7 +257,7 @@ class SbbBinarizer:
|
||||||
|
|
||||||
indexer_inside_batch = 0
|
indexer_inside_batch = 0
|
||||||
for i_batch, j_batch in zip(list_i_s, list_j_s):
|
for i_batch, j_batch in zip(list_i_s, list_j_s):
|
||||||
seg_in = seg[indexer_inside_batch,:,:]
|
seg_in = seg[indexer_inside_batch, :, :]
|
||||||
seg_color = np.repeat(seg_in[:, :, np.newaxis], 3, axis=2)
|
seg_color = np.repeat(seg_in[:, :, np.newaxis], 3, axis=2)
|
||||||
|
|
||||||
index_y_u_in = list_y_u[indexer_inside_batch]
|
index_y_u_in = list_y_u[indexer_inside_batch]
|
||||||
|
@ -262,35 +267,48 @@ class SbbBinarizer:
|
||||||
index_x_d_in = list_x_d[indexer_inside_batch]
|
index_x_d_in = list_x_d[indexer_inside_batch]
|
||||||
|
|
||||||
if i_batch == 0 and j_batch == 0:
|
if i_batch == 0 and j_batch == 0:
|
||||||
seg_color = seg_color[0 : seg_color.shape[0] - margin, 0 : seg_color.shape[1] - margin, :]
|
seg_color = seg_color[0: seg_color.shape[0] - margin, 0: seg_color.shape[1] - margin, :]
|
||||||
prediction_true[index_y_d_in + 0 : index_y_u_in - margin, index_x_d_in + 0 : index_x_u_in - margin, :] = seg_color
|
prediction_true[index_y_d_in + 0: index_y_u_in - margin,
|
||||||
|
index_x_d_in + 0: index_x_u_in - margin, :] = seg_color
|
||||||
elif i_batch == nxf - 1 and j_batch == nyf - 1:
|
elif i_batch == nxf - 1 and j_batch == 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, :]
|
||||||
prediction_true[index_y_d_in + margin : index_y_u_in - 0, index_x_d_in + margin : index_x_u_in - 0, :] = seg_color
|
prediction_true[index_y_d_in + margin: index_y_u_in - 0,
|
||||||
|
index_x_d_in + margin: index_x_u_in - 0, :] = seg_color
|
||||||
elif i_batch == 0 and j_batch == nyf - 1:
|
elif i_batch == 0 and j_batch == 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, :]
|
||||||
prediction_true[index_y_d_in + margin : index_y_u_in - 0, index_x_d_in + 0 : index_x_u_in - margin, :] = seg_color
|
prediction_true[index_y_d_in + margin: index_y_u_in - 0,
|
||||||
|
index_x_d_in + 0: index_x_u_in - margin, :] = seg_color
|
||||||
elif i_batch == nxf - 1 and j_batch == 0:
|
elif i_batch == nxf - 1 and j_batch == 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, :]
|
||||||
prediction_true[index_y_d_in + 0 : index_y_u_in - margin, index_x_d_in + margin : index_x_u_in - 0, :] = seg_color
|
prediction_true[index_y_d_in + 0: index_y_u_in - margin,
|
||||||
|
index_x_d_in + margin: index_x_u_in - 0, :] = seg_color
|
||||||
elif i_batch == 0 and j_batch != 0 and j_batch != nyf - 1:
|
elif i_batch == 0 and j_batch != 0 and j_batch != 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,
|
||||||
prediction_true[index_y_d_in + margin : index_y_u_in - margin, index_x_d_in + 0 : index_x_u_in - margin, :] = seg_color
|
0: seg_color.shape[1] - margin, :]
|
||||||
|
prediction_true[index_y_d_in + margin: index_y_u_in - margin,
|
||||||
|
index_x_d_in + 0: index_x_u_in - margin, :] = seg_color
|
||||||
elif i_batch == nxf - 1 and j_batch != 0 and j_batch != nyf - 1:
|
elif i_batch == nxf - 1 and j_batch != 0 and j_batch != 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,
|
||||||
prediction_true[index_y_d_in + margin : index_y_u_in - margin, index_x_d_in + margin : index_x_u_in - 0, :] = seg_color
|
margin: seg_color.shape[1] - 0, :]
|
||||||
|
prediction_true[index_y_d_in + margin: index_y_u_in - margin,
|
||||||
|
index_x_d_in + margin: index_x_u_in - 0, :] = seg_color
|
||||||
elif i_batch != 0 and i_batch != nxf - 1 and j_batch == 0:
|
elif i_batch != 0 and i_batch != nxf - 1 and j_batch == 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,
|
||||||
prediction_true[index_y_d_in + 0 : index_y_u_in - margin, index_x_d_in + margin : index_x_u_in - margin, :] = seg_color
|
margin: seg_color.shape[1] - margin, :]
|
||||||
|
prediction_true[index_y_d_in + 0: index_y_u_in - margin,
|
||||||
|
index_x_d_in + margin: index_x_u_in - margin, :] = seg_color
|
||||||
elif i_batch != 0 and i_batch != nxf - 1 and j_batch == nyf - 1:
|
elif i_batch != 0 and i_batch != nxf - 1 and j_batch == 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,
|
||||||
prediction_true[index_y_d_in + margin : index_y_u_in - 0, index_x_d_in + margin : index_x_u_in - margin, :] = seg_color
|
margin: seg_color.shape[1] - margin, :]
|
||||||
|
prediction_true[index_y_d_in + margin: index_y_u_in - 0,
|
||||||
|
index_x_d_in + margin: index_x_u_in - margin, :] = 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,
|
||||||
prediction_true[index_y_d_in + margin : index_y_u_in - margin, index_x_d_in + margin : index_x_u_in - margin, :] = seg_color
|
margin: seg_color.shape[1] - margin, :]
|
||||||
|
prediction_true[index_y_d_in + margin: index_y_u_in - margin,
|
||||||
indexer_inside_batch = indexer_inside_batch +1
|
index_x_d_in + margin: index_x_u_in - margin, :] = seg_color
|
||||||
|
|
||||||
|
indexer_inside_batch = indexer_inside_batch + 1
|
||||||
|
|
||||||
list_i_s = []
|
list_i_s = []
|
||||||
list_j_s = []
|
list_j_s = []
|
||||||
|
@ -301,11 +319,10 @@ class SbbBinarizer:
|
||||||
|
|
||||||
batch_indexer = 0
|
batch_indexer = 0
|
||||||
|
|
||||||
img_patch = np.zeros((n_batch_inference, model_height, model_width,3))
|
img_patch = np.zeros((n_batch_inference, model_height, model_width, 3))
|
||||||
|
|
||||||
|
prediction_true = prediction_true[index_start_h: index_start_h + img_org_h,
|
||||||
|
index_start_w: index_start_w + img_org_w, :]
|
||||||
prediction_true = prediction_true[index_start_h: index_start_h+img_org_h, index_start_w: index_start_w+img_org_w,:]
|
|
||||||
prediction_true = prediction_true.astype(np.uint8)
|
prediction_true = prediction_true.astype(np.uint8)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
@ -320,10 +337,10 @@ class SbbBinarizer:
|
||||||
seg_color = np.repeat(seg[:, :, np.newaxis], 3, axis=2)
|
seg_color = np.repeat(seg[:, :, np.newaxis], 3, axis=2)
|
||||||
prediction_true = resize_image(seg_color, img_h_page, img_w_page)
|
prediction_true = resize_image(seg_color, img_h_page, img_w_page)
|
||||||
prediction_true = prediction_true.astype(np.uint8)
|
prediction_true = prediction_true.astype(np.uint8)
|
||||||
return prediction_true[:,:,0]
|
return prediction_true[:, :, 0]
|
||||||
|
|
||||||
def run(self, image=None, image_path=None, save=None, use_patches=False, dir_in=None, dir_out=None):
|
def run(self, image=None, image_path=None, save=None, use_patches=False, dir_in=None, dir_out=None):
|
||||||
print(dir_in,'dir_in')
|
print(dir_in, 'dir_in')
|
||||||
if not dir_in:
|
if not dir_in:
|
||||||
if (image is not None and image_path is not None) or \
|
if (image is not None and image_path is not None) or \
|
||||||
(image is None and image_path is None):
|
(image is None and image_path is None):
|
||||||
|
@ -358,8 +375,8 @@ class SbbBinarizer:
|
||||||
ls_imgs = os.listdir(dir_in)
|
ls_imgs = os.listdir(dir_in)
|
||||||
for image_name in ls_imgs:
|
for image_name in ls_imgs:
|
||||||
image_stem = image_name.split('.')[0]
|
image_stem = image_name.split('.')[0]
|
||||||
print(image_name,'image_name')
|
print(image_name, 'image_name')
|
||||||
image = cv2.imread(os.path.join(dir_in,image_name) )
|
image = cv2.imread(os.path.join(dir_in, image_name))
|
||||||
img_last = 0
|
img_last = 0
|
||||||
for n, (model, model_file) in enumerate(zip(self.models, self.model_files)):
|
for n, (model, model_file) in enumerate(zip(self.models, self.model_files)):
|
||||||
self.log.info('Predicting with model %s [%s/%s]' % (model_file, n + 1, len(self.model_files)))
|
self.log.info('Predicting with model %s [%s/%s]' % (model_file, n + 1, len(self.model_files)))
|
||||||
|
@ -382,4 +399,4 @@ class SbbBinarizer:
|
||||||
img_last[:, :][img_last[:, :] > 0] = 255
|
img_last[:, :][img_last[:, :] > 0] = 255
|
||||||
img_last = (img_last[:, :] == 0) * 255
|
img_last = (img_last[:, :] == 0) * 255
|
||||||
|
|
||||||
cv2.imwrite(os.path.join(dir_out,image_stem+'.png'), img_last)
|
cv2.imwrite(os.path.join(dir_out, image_stem + '.png'), img_last)
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -43,7 +43,7 @@ def get_text_region_boxes_by_given_contours(contours):
|
||||||
|
|
||||||
def filter_contours_area_of_image(image, contours, hierarchy, max_area, min_area):
|
def filter_contours_area_of_image(image, contours, hierarchy, max_area, min_area):
|
||||||
found_polygons_early = []
|
found_polygons_early = []
|
||||||
for jv,c in enumerate(contours):
|
for jv, c in enumerate(contours):
|
||||||
if len(c) < 3: # A polygon cannot have less than 3 points
|
if len(c) < 3: # A polygon cannot have less than 3 points
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ def filter_contours_area_of_image(image, contours, hierarchy, max_area, min_area
|
||||||
|
|
||||||
def filter_contours_area_of_image_tables(image, contours, hierarchy, max_area, min_area):
|
def filter_contours_area_of_image_tables(image, contours, hierarchy, max_area, min_area):
|
||||||
found_polygons_early = []
|
found_polygons_early = []
|
||||||
for jv,c in enumerate(contours):
|
for jv, c in enumerate(contours):
|
||||||
if len(c) < 3: # A polygon cannot have less than 3 points
|
if len(c) < 3: # A polygon cannot have less than 3 points
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -122,15 +122,15 @@ def find_new_features_of_contours(contours_main):
|
||||||
|
|
||||||
|
|
||||||
def find_features_of_contours(contours_main):
|
def find_features_of_contours(contours_main):
|
||||||
areas_main=np.array([cv2.contourArea(contours_main[j]) for j in range(len(contours_main))])
|
areas_main = np.array([cv2.contourArea(contours_main[j]) for j in range(len(contours_main))])
|
||||||
M_main=[cv2.moments(contours_main[j]) for j in range(len(contours_main))]
|
M_main = [cv2.moments(contours_main[j]) for j in range(len(contours_main))]
|
||||||
cx_main=[(M_main[j]['m10']/(M_main[j]['m00']+1e-32)) for j in range(len(M_main))]
|
cx_main = [(M_main[j]['m10'] / (M_main[j]['m00'] + 1e-32)) for j in range(len(M_main))]
|
||||||
cy_main=[(M_main[j]['m01']/(M_main[j]['m00']+1e-32)) for j in range(len(M_main))]
|
cy_main = [(M_main[j]['m01'] / (M_main[j]['m00'] + 1e-32)) for j in range(len(M_main))]
|
||||||
x_min_main=np.array([np.min(contours_main[j][:,0,0]) for j in range(len(contours_main))])
|
x_min_main = np.array([np.min(contours_main[j][:, 0, 0]) for j in range(len(contours_main))])
|
||||||
x_max_main=np.array([np.max(contours_main[j][:,0,0]) for j in range(len(contours_main))])
|
x_max_main = np.array([np.max(contours_main[j][:, 0, 0]) for j in range(len(contours_main))])
|
||||||
|
|
||||||
y_min_main=np.array([np.min(contours_main[j][:,0,1]) for j in range(len(contours_main))])
|
y_min_main = np.array([np.min(contours_main[j][:, 0, 1]) for j in range(len(contours_main))])
|
||||||
y_max_main=np.array([np.max(contours_main[j][:,0,1]) for j in range(len(contours_main))])
|
y_max_main = np.array([np.max(contours_main[j][:, 0, 1]) for j in range(len(contours_main))])
|
||||||
|
|
||||||
return y_min_main, y_max_main
|
return y_min_main, y_max_main
|
||||||
|
|
||||||
|
@ -257,17 +257,17 @@ def do_back_rotation_and_get_cnt_back(contour_par, index_r_con, img, slope_first
|
||||||
def get_textregion_contours_in_org_image_light(cnts, img, slope_first, map=map):
|
def get_textregion_contours_in_org_image_light(cnts, img, slope_first, map=map):
|
||||||
if not len(cnts):
|
if not len(cnts):
|
||||||
return []
|
return []
|
||||||
img = cv2.resize(img, (int(img.shape[1]/6), int(img.shape[0]/6)), interpolation=cv2.INTER_NEAREST)
|
img = cv2.resize(img, (int(img.shape[1] / 6), int(img.shape[0] / 6)), interpolation=cv2.INTER_NEAREST)
|
||||||
##cnts = list( (np.array(cnts)/2).astype(np.int16) )
|
##cnts = list( (np.array(cnts)/2).astype(np.int16) )
|
||||||
#cnts = cnts/2
|
#cnts = cnts/2
|
||||||
cnts = [(i/6).astype(np.int) for i in cnts]
|
cnts = [(i / 6).astype(np.int) for i in cnts]
|
||||||
results = map(partial(do_back_rotation_and_get_cnt_back,
|
results = map(partial(do_back_rotation_and_get_cnt_back,
|
||||||
img=img,
|
img=img,
|
||||||
slope_first=slope_first,
|
slope_first=slope_first,
|
||||||
),
|
),
|
||||||
cnts, range(len(cnts)))
|
cnts, range(len(cnts)))
|
||||||
contours, indexes = tuple(zip(*results))
|
contours, indexes = tuple(zip(*results))
|
||||||
return [i*6 for i in contours]
|
return [i * 6 for i in contours]
|
||||||
|
|
||||||
|
|
||||||
def return_contours_of_interested_textline(region_pre_p, pixel):
|
def return_contours_of_interested_textline(region_pre_p, pixel):
|
||||||
|
@ -339,4 +339,3 @@ def return_contours_of_interested_region_by_size(region_pre_p, pixel, min_area,
|
||||||
img_ret = cv2.fillPoly(img_ret, pts=contours_imgs, color=(1, 1, 1))
|
img_ret = cv2.fillPoly(img_ret, pts=contours_imgs, color=(1, 1, 1))
|
||||||
|
|
||||||
return img_ret[:, :, 0]
|
return img_ret[:, :, 0]
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,8 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
|
|
||||||
img_con_all = np.zeros((text_regions_p.shape[0], text_regions_p.shape[1], 3))
|
img_con_all = np.zeros((text_regions_p.shape[0], text_regions_p.shape[1], 3))
|
||||||
for j_cont in range(len(contours_only_text_parent)):
|
for j_cont in range(len(contours_only_text_parent)):
|
||||||
img_con_all[all_box_coord[j_cont][0] : all_box_coord[j_cont][1], all_box_coord[j_cont][2] : all_box_coord[j_cont][3], 0] = (j_cont + 1) * 3
|
img_con_all[all_box_coord[j_cont][0]: all_box_coord[j_cont][1],
|
||||||
|
all_box_coord[j_cont][2]: all_box_coord[j_cont][3], 0] = (j_cont + 1) * 3
|
||||||
# img_con_all=cv2.fillPoly(img_con_all,pts=[contours_only_text_parent[j_cont]],color=((j_cont+1)*3,(j_cont+1)*3,(j_cont+1)*3))
|
# img_con_all=cv2.fillPoly(img_con_all,pts=[contours_only_text_parent[j_cont]],color=((j_cont+1)*3,(j_cont+1)*3,(j_cont+1)*3))
|
||||||
|
|
||||||
# plt.imshow(img_con_all[:,:,0])
|
# plt.imshow(img_con_all[:,:,0])
|
||||||
|
@ -85,14 +86,16 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
sum_pixels_of_intersection = []
|
sum_pixels_of_intersection = []
|
||||||
for i in range(len(region_with_intersected_drop)):
|
for i in range(len(region_with_intersected_drop)):
|
||||||
# print((region_with_intersected_drop[i]*3+1))
|
# print((region_with_intersected_drop[i]*3+1))
|
||||||
sum_pixels_of_intersection.append(((img_con_all_copy[:, :, 0] == (region_with_intersected_drop[i] * 3 + 1)) * 1).sum())
|
sum_pixels_of_intersection.append(
|
||||||
|
((img_con_all_copy[:, :, 0] == (region_with_intersected_drop[i] * 3 + 1)) * 1).sum())
|
||||||
# print(sum_pixels_of_intersection)
|
# print(sum_pixels_of_intersection)
|
||||||
region_final = region_with_intersected_drop[np.argmax(sum_pixels_of_intersection)] - 1
|
region_final = region_with_intersected_drop[np.argmax(sum_pixels_of_intersection)] - 1
|
||||||
|
|
||||||
# print(region_final,'region_final')
|
# print(region_final,'region_final')
|
||||||
# cx_t,cy_t ,_, _, _ ,_,_= find_new_features_of_contours(all_found_textline_polygons[int(region_final)])
|
# cx_t,cy_t ,_, _, _ ,_,_= find_new_features_of_contours(all_found_textline_polygons[int(region_final)])
|
||||||
try:
|
try:
|
||||||
cx_t, cy_t, _, _, _, _, _ = find_new_features_of_contours(all_found_textline_polygons[int(region_final)])
|
cx_t, cy_t, _, _, _, _, _ = find_new_features_of_contours(
|
||||||
|
all_found_textline_polygons[int(region_final)])
|
||||||
# print(all_box_coord[j_cont])
|
# print(all_box_coord[j_cont])
|
||||||
# print(cx_t)
|
# print(cx_t)
|
||||||
# print(cy_t)
|
# print(cy_t)
|
||||||
|
@ -109,12 +112,15 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
# print(arg_min)
|
# print(arg_min)
|
||||||
|
|
||||||
cnt_nearest = np.copy(all_found_textline_polygons[int(region_final)][arg_min])
|
cnt_nearest = np.copy(all_found_textline_polygons[int(region_final)][arg_min])
|
||||||
cnt_nearest[:, 0, 0] = all_found_textline_polygons[int(region_final)][arg_min][:, 0, 0] # +all_box_coord[int(region_final)][2]
|
cnt_nearest[:, 0, 0] = all_found_textline_polygons[int(region_final)][arg_min][:, 0,
|
||||||
cnt_nearest[:, 0, 1] = all_found_textline_polygons[int(region_final)][arg_min][:, 0, 1] # +all_box_coord[int(region_final)][0]
|
0] # +all_box_coord[int(region_final)][2]
|
||||||
|
cnt_nearest[:, 0, 1] = all_found_textline_polygons[int(region_final)][arg_min][:, 0,
|
||||||
|
1] # +all_box_coord[int(region_final)][0]
|
||||||
|
|
||||||
img_textlines = np.zeros((text_regions_p.shape[0], text_regions_p.shape[1], 3))
|
img_textlines = np.zeros((text_regions_p.shape[0], text_regions_p.shape[1], 3))
|
||||||
img_textlines = cv2.fillPoly(img_textlines, pts=[cnt_nearest], color=(255, 255, 255))
|
img_textlines = cv2.fillPoly(img_textlines, pts=[cnt_nearest], color=(255, 255, 255))
|
||||||
img_textlines = cv2.fillPoly(img_textlines, pts=[polygons_of_drop_capitals[i_drop]], color=(255, 255, 255))
|
img_textlines = cv2.fillPoly(img_textlines, pts=[polygons_of_drop_capitals[i_drop]],
|
||||||
|
color=(255, 255, 255))
|
||||||
|
|
||||||
img_textlines = img_textlines.astype(np.uint8)
|
img_textlines = img_textlines.astype(np.uint8)
|
||||||
|
|
||||||
|
@ -128,7 +134,8 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
|
|
||||||
#contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
#contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||||
|
|
||||||
areas_cnt_text = np.array([cv2.contourArea(contours_combined[j]) for j in range(len(contours_combined))])
|
areas_cnt_text = np.array(
|
||||||
|
[cv2.contourArea(contours_combined[j]) for j in range(len(contours_combined))])
|
||||||
|
|
||||||
contours_biggest = contours_combined[np.argmax(areas_cnt_text)]
|
contours_biggest = contours_combined[np.argmax(areas_cnt_text)]
|
||||||
|
|
||||||
|
@ -139,10 +146,11 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
|
|
||||||
# contours_biggest=contours_biggest.reshape(np.shape(contours_biggest)[0],np.shape(contours_biggest)[2])
|
# contours_biggest=contours_biggest.reshape(np.shape(contours_biggest)[0],np.shape(contours_biggest)[2])
|
||||||
|
|
||||||
if len(contours_combined)==1:
|
if len(contours_combined) == 1:
|
||||||
all_found_textline_polygons[int(region_final)][arg_min] = contours_biggest
|
all_found_textline_polygons[int(region_final)][arg_min] = contours_biggest
|
||||||
elif len(contours_combined)==2:
|
elif len(contours_combined) == 2:
|
||||||
all_found_textline_polygons[int(region_final)].insert(arg_min, polygons_of_drop_capitals[i_drop] )
|
all_found_textline_polygons[int(region_final)].insert(arg_min,
|
||||||
|
polygons_of_drop_capitals[i_drop])
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -156,7 +164,8 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
|
|
||||||
# cx_t,cy_t ,_, _, _ ,_,_= find_new_features_of_contours(all_found_textline_polygons[int(region_final)])
|
# cx_t,cy_t ,_, _, _ ,_,_= find_new_features_of_contours(all_found_textline_polygons[int(region_final)])
|
||||||
try:
|
try:
|
||||||
cx_t, cy_t, _, _, _, _, _ = find_new_features_of_contours(all_found_textline_polygons[int(region_final)])
|
cx_t, cy_t, _, _, _, _, _ = find_new_features_of_contours(
|
||||||
|
all_found_textline_polygons[int(region_final)])
|
||||||
# print(all_box_coord[j_cont])
|
# print(all_box_coord[j_cont])
|
||||||
# print(cx_t)
|
# print(cx_t)
|
||||||
# print(cy_t)
|
# print(cy_t)
|
||||||
|
@ -171,23 +180,26 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
# print(arg_min)
|
# print(arg_min)
|
||||||
|
|
||||||
cnt_nearest = np.copy(all_found_textline_polygons[int(region_final)][arg_min])
|
cnt_nearest = np.copy(all_found_textline_polygons[int(region_final)][arg_min])
|
||||||
cnt_nearest[:, 0, 0] = all_found_textline_polygons[int(region_final)][arg_min][:, 0, 0] # +all_box_coord[int(region_final)][2]
|
cnt_nearest[:, 0, 0] = all_found_textline_polygons[int(region_final)][arg_min][:, 0,
|
||||||
cnt_nearest[:, 0, 1] = all_found_textline_polygons[int(region_final)][arg_min][:, 0, 1] # +all_box_coord[int(region_final)][0]
|
0] # +all_box_coord[int(region_final)][2]
|
||||||
|
cnt_nearest[:, 0, 1] = all_found_textline_polygons[int(region_final)][arg_min][:, 0,
|
||||||
|
1] # +all_box_coord[int(region_final)][0]
|
||||||
|
|
||||||
img_textlines = np.zeros((text_regions_p.shape[0], text_regions_p.shape[1], 3))
|
img_textlines = np.zeros((text_regions_p.shape[0], text_regions_p.shape[1], 3))
|
||||||
img_textlines = cv2.fillPoly(img_textlines, pts=[cnt_nearest], color=(255, 255, 255))
|
img_textlines = cv2.fillPoly(img_textlines, pts=[cnt_nearest], color=(255, 255, 255))
|
||||||
img_textlines = cv2.fillPoly(img_textlines, pts=[polygons_of_drop_capitals[i_drop]], color=(255, 255, 255))
|
img_textlines = cv2.fillPoly(img_textlines, pts=[polygons_of_drop_capitals[i_drop]],
|
||||||
|
color=(255, 255, 255))
|
||||||
|
|
||||||
img_textlines = img_textlines.astype(np.uint8)
|
img_textlines = img_textlines.astype(np.uint8)
|
||||||
|
|
||||||
|
|
||||||
contours_combined = return_contours_of_interested_region(img_textlines, 255, 0)
|
contours_combined = return_contours_of_interested_region(img_textlines, 255, 0)
|
||||||
##imgray = cv2.cvtColor(img_textlines, cv2.COLOR_BGR2GRAY)
|
##imgray = cv2.cvtColor(img_textlines, cv2.COLOR_BGR2GRAY)
|
||||||
##ret, thresh = cv2.threshold(imgray, 0, 255, 0)
|
##ret, thresh = cv2.threshold(imgray, 0, 255, 0)
|
||||||
|
|
||||||
##contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
##contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||||
|
|
||||||
areas_cnt_text = np.array([cv2.contourArea(contours_combined[j]) for j in range(len(contours_combined))])
|
areas_cnt_text = np.array(
|
||||||
|
[cv2.contourArea(contours_combined[j]) for j in range(len(contours_combined))])
|
||||||
|
|
||||||
contours_biggest = contours_combined[np.argmax(areas_cnt_text)]
|
contours_biggest = contours_combined[np.argmax(areas_cnt_text)]
|
||||||
|
|
||||||
|
@ -198,10 +210,11 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
# print(np.shape(contours_biggest),'contours_biggest')
|
# print(np.shape(contours_biggest),'contours_biggest')
|
||||||
# print(np.shape(all_found_textline_polygons[int(region_final)][arg_min]))
|
# print(np.shape(all_found_textline_polygons[int(region_final)][arg_min]))
|
||||||
##contours_biggest=contours_biggest.reshape(np.shape(contours_biggest)[0],np.shape(contours_biggest)[2])
|
##contours_biggest=contours_biggest.reshape(np.shape(contours_biggest)[0],np.shape(contours_biggest)[2])
|
||||||
if len(contours_combined)==1:
|
if len(contours_combined) == 1:
|
||||||
all_found_textline_polygons[int(region_final)][arg_min] = contours_biggest
|
all_found_textline_polygons[int(region_final)][arg_min] = contours_biggest
|
||||||
elif len(contours_combined)==2:
|
elif len(contours_combined) == 2:
|
||||||
all_found_textline_polygons[int(region_final)].insert(arg_min, polygons_of_drop_capitals[i_drop] )
|
all_found_textline_polygons[int(region_final)].insert(arg_min,
|
||||||
|
polygons_of_drop_capitals[i_drop])
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
except:
|
except:
|
||||||
|
@ -209,7 +222,8 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# print(all_found_textline_polygons[j_cont][0])
|
# print(all_found_textline_polygons[j_cont][0])
|
||||||
cx_t, cy_t, _, _, _, _, _ = find_new_features_of_contours(all_found_textline_polygons[int(region_final)])
|
cx_t, cy_t, _, _, _, _, _ = find_new_features_of_contours(
|
||||||
|
all_found_textline_polygons[int(region_final)])
|
||||||
# print(all_box_coord[j_cont])
|
# print(all_box_coord[j_cont])
|
||||||
# print(cx_t)
|
# print(cx_t)
|
||||||
# print(cy_t)
|
# print(cy_t)
|
||||||
|
@ -224,12 +238,15 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
# print(arg_min)
|
# print(arg_min)
|
||||||
|
|
||||||
cnt_nearest = np.copy(all_found_textline_polygons[int(region_final)][arg_min])
|
cnt_nearest = np.copy(all_found_textline_polygons[int(region_final)][arg_min])
|
||||||
cnt_nearest[:, 0, 0] = all_found_textline_polygons[int(region_final)][arg_min][:, 0, 0] # +all_box_coord[int(region_final)][2]
|
cnt_nearest[:, 0, 0] = all_found_textline_polygons[int(region_final)][arg_min][:, 0,
|
||||||
cnt_nearest[:, 0, 1] = all_found_textline_polygons[int(region_final)][arg_min][:, 0, 1] # +all_box_coord[int(region_final)][0]
|
0] # +all_box_coord[int(region_final)][2]
|
||||||
|
cnt_nearest[:, 0, 1] = all_found_textline_polygons[int(region_final)][arg_min][:, 0,
|
||||||
|
1] # +all_box_coord[int(region_final)][0]
|
||||||
|
|
||||||
img_textlines = np.zeros((text_regions_p.shape[0], text_regions_p.shape[1], 3))
|
img_textlines = np.zeros((text_regions_p.shape[0], text_regions_p.shape[1], 3))
|
||||||
img_textlines = cv2.fillPoly(img_textlines, pts=[cnt_nearest], color=(255, 255, 255))
|
img_textlines = cv2.fillPoly(img_textlines, pts=[cnt_nearest], color=(255, 255, 255))
|
||||||
img_textlines = cv2.fillPoly(img_textlines, pts=[polygons_of_drop_capitals[i_drop]], color=(255, 255, 255))
|
img_textlines = cv2.fillPoly(img_textlines, pts=[polygons_of_drop_capitals[i_drop]],
|
||||||
|
color=(255, 255, 255))
|
||||||
|
|
||||||
img_textlines = img_textlines.astype(np.uint8)
|
img_textlines = img_textlines.astype(np.uint8)
|
||||||
contours_combined = return_contours_of_interested_region(img_textlines, 255, 0)
|
contours_combined = return_contours_of_interested_region(img_textlines, 255, 0)
|
||||||
|
@ -239,7 +256,8 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
#contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
#contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||||
|
|
||||||
# print(len(contours_combined),'len textlines mixed')
|
# print(len(contours_combined),'len textlines mixed')
|
||||||
areas_cnt_text = np.array([cv2.contourArea(contours_combined[j]) for j in range(len(contours_combined))])
|
areas_cnt_text = np.array(
|
||||||
|
[cv2.contourArea(contours_combined[j]) for j in range(len(contours_combined))])
|
||||||
|
|
||||||
contours_biggest = contours_combined[np.argmax(areas_cnt_text)]
|
contours_biggest = contours_combined[np.argmax(areas_cnt_text)]
|
||||||
|
|
||||||
|
@ -249,10 +267,11 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
contours_biggest[:, 0, 1] = contours_biggest[:, 0, 1] # -all_box_coord[int(region_final)][0]
|
contours_biggest[:, 0, 1] = contours_biggest[:, 0, 1] # -all_box_coord[int(region_final)][0]
|
||||||
|
|
||||||
##contours_biggest=contours_biggest.reshape(np.shape(contours_biggest)[0],np.shape(contours_biggest)[2])
|
##contours_biggest=contours_biggest.reshape(np.shape(contours_biggest)[0],np.shape(contours_biggest)[2])
|
||||||
if len(contours_combined)==1:
|
if len(contours_combined) == 1:
|
||||||
all_found_textline_polygons[int(region_final)][arg_min] = contours_biggest
|
all_found_textline_polygons[int(region_final)][arg_min] = contours_biggest
|
||||||
elif len(contours_combined)==2:
|
elif len(contours_combined) == 2:
|
||||||
all_found_textline_polygons[int(region_final)].insert(arg_min, polygons_of_drop_capitals[i_drop] )
|
all_found_textline_polygons[int(region_final)].insert(arg_min,
|
||||||
|
polygons_of_drop_capitals[i_drop])
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
# all_found_textline_polygons[int(region_final)][arg_min]=contours_biggest
|
# all_found_textline_polygons[int(region_final)][arg_min]=contours_biggest
|
||||||
|
@ -311,14 +330,16 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
sum_pixels_of_intersection = []
|
sum_pixels_of_intersection = []
|
||||||
for i in range(len(region_with_intersected_drop)):
|
for i in range(len(region_with_intersected_drop)):
|
||||||
# print((region_with_intersected_drop[i]*3+1))
|
# print((region_with_intersected_drop[i]*3+1))
|
||||||
sum_pixels_of_intersection.append(((img_con_all_copy[:, :, 0] == (region_with_intersected_drop[i] * 3 + 1)) * 1).sum())
|
sum_pixels_of_intersection.append(
|
||||||
|
((img_con_all_copy[:, :, 0] == (region_with_intersected_drop[i] * 3 + 1)) * 1).sum())
|
||||||
# print(sum_pixels_of_intersection)
|
# print(sum_pixels_of_intersection)
|
||||||
region_final = region_with_intersected_drop[np.argmax(sum_pixels_of_intersection)] - 1
|
region_final = region_with_intersected_drop[np.argmax(sum_pixels_of_intersection)] - 1
|
||||||
|
|
||||||
# print(region_final,'region_final')
|
# print(region_final,'region_final')
|
||||||
# cx_t,cy_t ,_, _, _ ,_,_= find_new_features_of_contours(all_found_textline_polygons[int(region_final)])
|
# cx_t,cy_t ,_, _, _ ,_,_= find_new_features_of_contours(all_found_textline_polygons[int(region_final)])
|
||||||
try:
|
try:
|
||||||
cx_t, cy_t, _, _, _, _, _ = find_new_features_of_contours(all_found_textline_polygons[int(region_final)])
|
cx_t, cy_t, _, _, _, _, _ = find_new_features_of_contours(
|
||||||
|
all_found_textline_polygons[int(region_final)])
|
||||||
# print(all_box_coord[j_cont])
|
# print(all_box_coord[j_cont])
|
||||||
# print(cx_t)
|
# print(cx_t)
|
||||||
# print(cy_t)
|
# print(cy_t)
|
||||||
|
@ -335,12 +356,15 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
# print(arg_min)
|
# print(arg_min)
|
||||||
|
|
||||||
cnt_nearest = np.copy(all_found_textline_polygons[int(region_final)][arg_min])
|
cnt_nearest = np.copy(all_found_textline_polygons[int(region_final)][arg_min])
|
||||||
cnt_nearest[:, 0] = all_found_textline_polygons[int(region_final)][arg_min][:, 0] + all_box_coord[int(region_final)][2]
|
cnt_nearest[:, 0] = all_found_textline_polygons[int(region_final)][arg_min][:, 0] + \
|
||||||
cnt_nearest[:, 1] = all_found_textline_polygons[int(region_final)][arg_min][:, 1] + all_box_coord[int(region_final)][0]
|
all_box_coord[int(region_final)][2]
|
||||||
|
cnt_nearest[:, 1] = all_found_textline_polygons[int(region_final)][arg_min][:, 1] + \
|
||||||
|
all_box_coord[int(region_final)][0]
|
||||||
|
|
||||||
img_textlines = np.zeros((text_regions_p.shape[0], text_regions_p.shape[1], 3))
|
img_textlines = np.zeros((text_regions_p.shape[0], text_regions_p.shape[1], 3))
|
||||||
img_textlines = cv2.fillPoly(img_textlines, pts=[cnt_nearest], color=(255, 255, 255))
|
img_textlines = cv2.fillPoly(img_textlines, pts=[cnt_nearest], color=(255, 255, 255))
|
||||||
img_textlines = cv2.fillPoly(img_textlines, pts=[polygons_of_drop_capitals[i_drop]], color=(255, 255, 255))
|
img_textlines = cv2.fillPoly(img_textlines, pts=[polygons_of_drop_capitals[i_drop]],
|
||||||
|
color=(255, 255, 255))
|
||||||
|
|
||||||
img_textlines = img_textlines.astype(np.uint8)
|
img_textlines = img_textlines.astype(np.uint8)
|
||||||
contours_combined = return_contours_of_interested_region(img_textlines, 255, 0)
|
contours_combined = return_contours_of_interested_region(img_textlines, 255, 0)
|
||||||
|
@ -351,7 +375,8 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
#contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
#contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||||
|
|
||||||
# print(len(contours_combined),'len textlines mixed')
|
# print(len(contours_combined),'len textlines mixed')
|
||||||
areas_cnt_text = np.array([cv2.contourArea(contours_combined[j]) for j in range(len(contours_combined))])
|
areas_cnt_text = np.array(
|
||||||
|
[cv2.contourArea(contours_combined[j]) for j in range(len(contours_combined))])
|
||||||
|
|
||||||
contours_biggest = contours_combined[np.argmax(areas_cnt_text)]
|
contours_biggest = contours_combined[np.argmax(areas_cnt_text)]
|
||||||
|
|
||||||
|
@ -360,11 +385,13 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
contours_biggest[:, 0, 0] = contours_biggest[:, 0, 0] - all_box_coord[int(region_final)][2]
|
contours_biggest[:, 0, 0] = contours_biggest[:, 0, 0] - all_box_coord[int(region_final)][2]
|
||||||
contours_biggest[:, 0, 1] = contours_biggest[:, 0, 1] - all_box_coord[int(region_final)][0]
|
contours_biggest[:, 0, 1] = contours_biggest[:, 0, 1] - all_box_coord[int(region_final)][0]
|
||||||
|
|
||||||
contours_biggest = contours_biggest.reshape(np.shape(contours_biggest)[0], np.shape(contours_biggest)[2])
|
contours_biggest = contours_biggest.reshape(np.shape(contours_biggest)[0],
|
||||||
if len(contours_combined)==1:
|
np.shape(contours_biggest)[2])
|
||||||
|
if len(contours_combined) == 1:
|
||||||
all_found_textline_polygons[int(region_final)][arg_min] = contours_biggest
|
all_found_textline_polygons[int(region_final)][arg_min] = contours_biggest
|
||||||
elif len(contours_combined)==2:
|
elif len(contours_combined) == 2:
|
||||||
all_found_textline_polygons[int(region_final)].insert(arg_min, polygons_of_drop_capitals[i_drop] )
|
all_found_textline_polygons[int(region_final)].insert(arg_min,
|
||||||
|
polygons_of_drop_capitals[i_drop])
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -381,7 +408,8 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
# print(cx_t,'print')
|
# print(cx_t,'print')
|
||||||
try:
|
try:
|
||||||
# print(all_found_textline_polygons[j_cont][0])
|
# print(all_found_textline_polygons[j_cont][0])
|
||||||
cx_t, cy_t, _, _, _, _, _ = find_new_features_of_contours(all_found_textline_polygons[int(region_final)])
|
cx_t, cy_t, _, _, _, _, _ = find_new_features_of_contours(
|
||||||
|
all_found_textline_polygons[int(region_final)])
|
||||||
# print(all_box_coord[j_cont])
|
# print(all_box_coord[j_cont])
|
||||||
# print(cx_t)
|
# print(cx_t)
|
||||||
# print(cy_t)
|
# print(cy_t)
|
||||||
|
@ -396,12 +424,15 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
# print(arg_min)
|
# print(arg_min)
|
||||||
|
|
||||||
cnt_nearest = np.copy(all_found_textline_polygons[int(region_final)][arg_min])
|
cnt_nearest = np.copy(all_found_textline_polygons[int(region_final)][arg_min])
|
||||||
cnt_nearest[:, 0] = all_found_textline_polygons[int(region_final)][arg_min][:, 0] + all_box_coord[int(region_final)][2]
|
cnt_nearest[:, 0] = all_found_textline_polygons[int(region_final)][arg_min][:, 0] + \
|
||||||
cnt_nearest[:, 1] = all_found_textline_polygons[int(region_final)][arg_min][:, 1] + all_box_coord[int(region_final)][0]
|
all_box_coord[int(region_final)][2]
|
||||||
|
cnt_nearest[:, 1] = all_found_textline_polygons[int(region_final)][arg_min][:, 1] + \
|
||||||
|
all_box_coord[int(region_final)][0]
|
||||||
|
|
||||||
img_textlines = np.zeros((text_regions_p.shape[0], text_regions_p.shape[1], 3))
|
img_textlines = np.zeros((text_regions_p.shape[0], text_regions_p.shape[1], 3))
|
||||||
img_textlines = cv2.fillPoly(img_textlines, pts=[cnt_nearest], color=(255, 255, 255))
|
img_textlines = cv2.fillPoly(img_textlines, pts=[cnt_nearest], color=(255, 255, 255))
|
||||||
img_textlines = cv2.fillPoly(img_textlines, pts=[polygons_of_drop_capitals[i_drop]], color=(255, 255, 255))
|
img_textlines = cv2.fillPoly(img_textlines, pts=[polygons_of_drop_capitals[i_drop]],
|
||||||
|
color=(255, 255, 255))
|
||||||
|
|
||||||
img_textlines = img_textlines.astype(np.uint8)
|
img_textlines = img_textlines.astype(np.uint8)
|
||||||
contours_combined = return_contours_of_interested_region(img_textlines, 255, 0)
|
contours_combined = return_contours_of_interested_region(img_textlines, 255, 0)
|
||||||
|
@ -412,7 +443,8 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
#contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
#contours_combined, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
||||||
|
|
||||||
# print(len(contours_combined),'len textlines mixed')
|
# print(len(contours_combined),'len textlines mixed')
|
||||||
areas_cnt_text = np.array([cv2.contourArea(contours_combined[j]) for j in range(len(contours_combined))])
|
areas_cnt_text = np.array(
|
||||||
|
[cv2.contourArea(contours_combined[j]) for j in range(len(contours_combined))])
|
||||||
|
|
||||||
contours_biggest = contours_combined[np.argmax(areas_cnt_text)]
|
contours_biggest = contours_combined[np.argmax(areas_cnt_text)]
|
||||||
|
|
||||||
|
@ -421,11 +453,13 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
contours_biggest[:, 0, 0] = contours_biggest[:, 0, 0] - all_box_coord[int(region_final)][2]
|
contours_biggest[:, 0, 0] = contours_biggest[:, 0, 0] - all_box_coord[int(region_final)][2]
|
||||||
contours_biggest[:, 0, 1] = contours_biggest[:, 0, 1] - all_box_coord[int(region_final)][0]
|
contours_biggest[:, 0, 1] = contours_biggest[:, 0, 1] - all_box_coord[int(region_final)][0]
|
||||||
|
|
||||||
contours_biggest = contours_biggest.reshape(np.shape(contours_biggest)[0], np.shape(contours_biggest)[2])
|
contours_biggest = contours_biggest.reshape(np.shape(contours_biggest)[0],
|
||||||
if len(contours_combined)==1:
|
np.shape(contours_biggest)[2])
|
||||||
|
if len(contours_combined) == 1:
|
||||||
all_found_textline_polygons[int(region_final)][arg_min] = contours_biggest
|
all_found_textline_polygons[int(region_final)][arg_min] = contours_biggest
|
||||||
elif len(contours_combined)==2:
|
elif len(contours_combined) == 2:
|
||||||
all_found_textline_polygons[int(region_final)].insert(arg_min, polygons_of_drop_capitals[i_drop] )
|
all_found_textline_polygons[int(region_final)].insert(arg_min,
|
||||||
|
polygons_of_drop_capitals[i_drop])
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
# all_found_textline_polygons[int(region_final)][arg_min]=contours_biggest
|
# all_found_textline_polygons[int(region_final)][arg_min]=contours_biggest
|
||||||
|
@ -502,7 +536,6 @@ def adhere_drop_capital_region_into_corresponding_textline(
|
||||||
|
|
||||||
|
|
||||||
def filter_small_drop_capitals_from_no_patch_layout(layout_no_patch, layout1):
|
def filter_small_drop_capitals_from_no_patch_layout(layout_no_patch, layout1):
|
||||||
|
|
||||||
drop_only = (layout_no_patch[:, :, 0] == 4) * 1
|
drop_only = (layout_no_patch[:, :, 0] == 4) * 1
|
||||||
contours_drop, hir_on_drop = return_contours_of_image(drop_only)
|
contours_drop, hir_on_drop = return_contours_of_image(drop_only)
|
||||||
contours_drop_parent = return_parent_contours(contours_drop, hir_on_drop)
|
contours_drop_parent = return_parent_contours(contours_drop, hir_on_drop)
|
||||||
|
@ -510,7 +543,8 @@ def filter_small_drop_capitals_from_no_patch_layout(layout_no_patch, layout1):
|
||||||
areas_cnt_text = np.array([cv2.contourArea(contours_drop_parent[j]) for j in range(len(contours_drop_parent))])
|
areas_cnt_text = np.array([cv2.contourArea(contours_drop_parent[j]) for j in range(len(contours_drop_parent))])
|
||||||
areas_cnt_text = areas_cnt_text / float(drop_only.shape[0] * drop_only.shape[1])
|
areas_cnt_text = areas_cnt_text / float(drop_only.shape[0] * drop_only.shape[1])
|
||||||
|
|
||||||
contours_drop_parent = [contours_drop_parent[jz] for jz in range(len(contours_drop_parent)) if areas_cnt_text[jz] > 0.001]
|
contours_drop_parent = [contours_drop_parent[jz] for jz in range(len(contours_drop_parent)) if
|
||||||
|
areas_cnt_text[jz] > 0.001]
|
||||||
|
|
||||||
areas_cnt_text = [areas_cnt_text[jz] for jz in range(len(areas_cnt_text)) if areas_cnt_text[jz] > 0.001]
|
areas_cnt_text = [areas_cnt_text[jz] for jz in range(len(areas_cnt_text)) if areas_cnt_text[jz] > 0.001]
|
||||||
|
|
||||||
|
@ -520,15 +554,17 @@ def filter_small_drop_capitals_from_no_patch_layout(layout_no_patch, layout1):
|
||||||
x, y, w, h = cv2.boundingRect(contours_drop_parent[jj])
|
x, y, w, h = cv2.boundingRect(contours_drop_parent[jj])
|
||||||
# boxes.append([int(x), int(y), int(w), int(h)])
|
# boxes.append([int(x), int(y), int(w), int(h)])
|
||||||
|
|
||||||
iou_of_box_and_contoure = float(drop_only.shape[0] * drop_only.shape[1]) * areas_cnt_text[jj] / float(w * h) * 100
|
iou_of_box_and_contoure = float(drop_only.shape[0] * drop_only.shape[1]) * areas_cnt_text[jj] / float(
|
||||||
|
w * h) * 100
|
||||||
height_to_weight_ratio = h / float(w)
|
height_to_weight_ratio = h / float(w)
|
||||||
weigh_to_height_ratio = w / float(h)
|
weigh_to_height_ratio = w / float(h)
|
||||||
|
|
||||||
if iou_of_box_and_contoure > 60 and weigh_to_height_ratio < 1.2 and height_to_weight_ratio < 2:
|
if iou_of_box_and_contoure > 60 and weigh_to_height_ratio < 1.2 and height_to_weight_ratio < 2:
|
||||||
map_of_drop_contour_bb = np.zeros((layout1.shape[0], layout1.shape[1]))
|
map_of_drop_contour_bb = np.zeros((layout1.shape[0], layout1.shape[1]))
|
||||||
map_of_drop_contour_bb[y : y + h, x : x + w] = layout1[y : y + h, x : x + w]
|
map_of_drop_contour_bb[y: y + h, x: x + w] = layout1[y: y + h, x: x + w]
|
||||||
|
|
||||||
if (((map_of_drop_contour_bb == 1) * 1).sum() / float(((map_of_drop_contour_bb == 5) * 1).sum()) * 100) >= 15:
|
if (((map_of_drop_contour_bb == 1) * 1).sum() / float(
|
||||||
|
((map_of_drop_contour_bb == 5) * 1).sum()) * 100) >= 15:
|
||||||
contours_drop_parent_final.append(contours_drop_parent[jj])
|
contours_drop_parent_final.append(contours_drop_parent[jj])
|
||||||
|
|
||||||
layout_no_patch[:, :, 0][layout_no_patch[:, :, 0] == 4] = 0
|
layout_no_patch[:, :, 0][layout_no_patch[:, :, 0] == 4] = 0
|
||||||
|
@ -536,4 +572,3 @@ def filter_small_drop_capitals_from_no_patch_layout(layout_no_patch, layout1):
|
||||||
layout_no_patch = cv2.fillPoly(layout_no_patch, pts=contours_drop_parent_final, color=(4, 4, 4))
|
layout_no_patch = cv2.fillPoly(layout_no_patch, pts=contours_drop_parent_final, color=(4, 4, 4))
|
||||||
|
|
||||||
return layout_no_patch
|
return layout_no_patch
|
||||||
|
|
||||||
|
|
|
@ -8,206 +8,196 @@ from .rotate import rotate_image
|
||||||
|
|
||||||
|
|
||||||
def get_marginals(text_with_lines, text_regions, num_col, slope_deskew, light_version=False, kernel=None):
|
def get_marginals(text_with_lines, text_regions, num_col, slope_deskew, light_version=False, kernel=None):
|
||||||
mask_marginals=np.zeros((text_with_lines.shape[0],text_with_lines.shape[1]))
|
mask_marginals = np.zeros((text_with_lines.shape[0], text_with_lines.shape[1]))
|
||||||
mask_marginals=mask_marginals.astype(np.uint8)
|
mask_marginals = mask_marginals.astype(np.uint8)
|
||||||
|
|
||||||
|
text_with_lines = text_with_lines.astype(np.uint8)
|
||||||
text_with_lines=text_with_lines.astype(np.uint8)
|
|
||||||
##text_with_lines=cv2.erode(text_with_lines,self.kernel,iterations=3)
|
##text_with_lines=cv2.erode(text_with_lines,self.kernel,iterations=3)
|
||||||
|
|
||||||
text_with_lines_eroded=cv2.erode(text_with_lines,kernel,iterations=5)
|
text_with_lines_eroded = cv2.erode(text_with_lines, kernel, iterations=5)
|
||||||
|
|
||||||
if text_with_lines.shape[0]<=1500:
|
if text_with_lines.shape[0] <= 1500:
|
||||||
pass
|
pass
|
||||||
elif 1500 < text_with_lines.shape[0] <= 1800:
|
elif 1500 < text_with_lines.shape[0] <= 1800:
|
||||||
text_with_lines=resize_image(text_with_lines,int(text_with_lines.shape[0]*1.5),text_with_lines.shape[1])
|
text_with_lines = resize_image(text_with_lines, int(text_with_lines.shape[0] * 1.5), text_with_lines.shape[1])
|
||||||
text_with_lines=cv2.erode(text_with_lines,kernel,iterations=5)
|
text_with_lines = cv2.erode(text_with_lines, kernel, iterations=5)
|
||||||
text_with_lines=resize_image(text_with_lines,text_with_lines_eroded.shape[0],text_with_lines_eroded.shape[1])
|
text_with_lines = resize_image(text_with_lines, text_with_lines_eroded.shape[0],
|
||||||
|
text_with_lines_eroded.shape[1])
|
||||||
else:
|
else:
|
||||||
text_with_lines=resize_image(text_with_lines,int(text_with_lines.shape[0]*1.8),text_with_lines.shape[1])
|
text_with_lines = resize_image(text_with_lines, int(text_with_lines.shape[0] * 1.8), text_with_lines.shape[1])
|
||||||
text_with_lines=cv2.erode(text_with_lines,kernel,iterations=7)
|
text_with_lines = cv2.erode(text_with_lines, kernel, iterations=7)
|
||||||
text_with_lines=resize_image(text_with_lines,text_with_lines_eroded.shape[0],text_with_lines_eroded.shape[1])
|
text_with_lines = resize_image(text_with_lines, text_with_lines_eroded.shape[0],
|
||||||
|
text_with_lines_eroded.shape[1])
|
||||||
|
|
||||||
|
text_with_lines_y = text_with_lines.sum(axis=0)
|
||||||
|
text_with_lines_y_eroded = text_with_lines_eroded.sum(axis=0)
|
||||||
|
|
||||||
text_with_lines_y=text_with_lines.sum(axis=0)
|
thickness_along_y_percent = text_with_lines_y_eroded.max() / (float(text_with_lines.shape[0])) * 100
|
||||||
text_with_lines_y_eroded=text_with_lines_eroded.sum(axis=0)
|
|
||||||
|
|
||||||
thickness_along_y_percent=text_with_lines_y_eroded.max()/(float(text_with_lines.shape[0]))*100
|
|
||||||
|
|
||||||
#print(thickness_along_y_percent,'thickness_along_y_percent')
|
#print(thickness_along_y_percent,'thickness_along_y_percent')
|
||||||
|
|
||||||
if thickness_along_y_percent<30:
|
if thickness_along_y_percent < 30:
|
||||||
min_textline_thickness=8
|
min_textline_thickness = 8
|
||||||
elif 30 <= thickness_along_y_percent < 50:
|
elif 30 <= thickness_along_y_percent < 50:
|
||||||
min_textline_thickness=20
|
min_textline_thickness = 20
|
||||||
else:
|
else:
|
||||||
min_textline_thickness=40
|
min_textline_thickness = 40
|
||||||
|
|
||||||
|
if thickness_along_y_percent >= 14:
|
||||||
|
|
||||||
|
text_with_lines_y_rev = -1 * text_with_lines_y[:]
|
||||||
|
|
||||||
if thickness_along_y_percent>=14:
|
text_with_lines_y_rev = text_with_lines_y_rev - np.min(text_with_lines_y_rev)
|
||||||
|
|
||||||
text_with_lines_y_rev=-1*text_with_lines_y[:]
|
sigma_gaus = 1
|
||||||
|
region_sum_0 = gaussian_filter1d(text_with_lines_y, sigma_gaus)
|
||||||
|
|
||||||
text_with_lines_y_rev=text_with_lines_y_rev-np.min(text_with_lines_y_rev)
|
region_sum_0_rev = gaussian_filter1d(text_with_lines_y_rev, sigma_gaus)
|
||||||
|
|
||||||
sigma_gaus=1
|
region_sum_0_updown = region_sum_0[len(region_sum_0)::-1]
|
||||||
region_sum_0= gaussian_filter1d(text_with_lines_y, sigma_gaus)
|
|
||||||
|
|
||||||
region_sum_0_rev=gaussian_filter1d(text_with_lines_y_rev, sigma_gaus)
|
first_nonzero = (next((i for i, x in enumerate(region_sum_0) if x), None))
|
||||||
|
last_nonzero = (next((i for i, x in enumerate(region_sum_0_updown) if x), None))
|
||||||
|
|
||||||
region_sum_0_updown=region_sum_0[len(region_sum_0)::-1]
|
last_nonzero = len(region_sum_0) - last_nonzero
|
||||||
|
|
||||||
first_nonzero=(next((i for i, x in enumerate(region_sum_0) if x), None))
|
mid_point = (last_nonzero + first_nonzero) / 2.
|
||||||
last_nonzero=(next((i for i, x in enumerate(region_sum_0_updown) if x), None))
|
|
||||||
|
|
||||||
|
one_third_right = (last_nonzero - mid_point) / 3.0
|
||||||
last_nonzero=len(region_sum_0)-last_nonzero
|
one_third_left = (mid_point - first_nonzero) / 3.0
|
||||||
|
|
||||||
mid_point=(last_nonzero+first_nonzero)/2.
|
|
||||||
|
|
||||||
|
|
||||||
one_third_right=(last_nonzero-mid_point)/3.0
|
|
||||||
one_third_left=(mid_point-first_nonzero)/3.0
|
|
||||||
|
|
||||||
peaks, _ = find_peaks(text_with_lines_y_rev, height=0)
|
peaks, _ = find_peaks(text_with_lines_y_rev, height=0)
|
||||||
peaks=np.array(peaks)
|
peaks = np.array(peaks)
|
||||||
peaks=peaks[(peaks>first_nonzero) & (peaks < last_nonzero)]
|
peaks = peaks[(peaks > first_nonzero) & (peaks < last_nonzero)]
|
||||||
peaks=peaks[region_sum_0[peaks]<min_textline_thickness ]
|
peaks = peaks[region_sum_0[peaks] < min_textline_thickness]
|
||||||
|
|
||||||
|
|
||||||
if num_col==1:
|
|
||||||
peaks_right=peaks[peaks>mid_point]
|
|
||||||
peaks_left=peaks[peaks<mid_point]
|
|
||||||
if num_col==2:
|
|
||||||
peaks_right=peaks[peaks>(mid_point+one_third_right)]
|
|
||||||
peaks_left=peaks[peaks<(mid_point-one_third_left)]
|
|
||||||
|
|
||||||
|
if num_col == 1:
|
||||||
|
peaks_right = peaks[peaks > mid_point]
|
||||||
|
peaks_left = peaks[peaks < mid_point]
|
||||||
|
if num_col == 2:
|
||||||
|
peaks_right = peaks[peaks > (mid_point + one_third_right)]
|
||||||
|
peaks_left = peaks[peaks < (mid_point - one_third_left)]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
point_right=np.min(peaks_right)
|
point_right = np.min(peaks_right)
|
||||||
except:
|
except:
|
||||||
point_right=last_nonzero
|
point_right = last_nonzero
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
point_left=np.max(peaks_left)
|
point_left = np.max(peaks_left)
|
||||||
except:
|
except:
|
||||||
point_left=first_nonzero
|
point_left = first_nonzero
|
||||||
|
|
||||||
|
if point_right >= mask_marginals.shape[1]:
|
||||||
|
point_right = mask_marginals.shape[1] - 1
|
||||||
if point_right>=mask_marginals.shape[1]:
|
|
||||||
point_right=mask_marginals.shape[1]-1
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
mask_marginals[:,point_left:point_right]=1
|
mask_marginals[:, point_left:point_right] = 1
|
||||||
except:
|
except:
|
||||||
mask_marginals[:,:]=1
|
mask_marginals[:, :] = 1
|
||||||
|
|
||||||
mask_marginals_rotated=rotate_image(mask_marginals,-slope_deskew)
|
mask_marginals_rotated = rotate_image(mask_marginals, -slope_deskew)
|
||||||
|
|
||||||
mask_marginals_rotated_sum=mask_marginals_rotated.sum(axis=0)
|
mask_marginals_rotated_sum = mask_marginals_rotated.sum(axis=0)
|
||||||
|
|
||||||
mask_marginals_rotated_sum[mask_marginals_rotated_sum!=0]=1
|
mask_marginals_rotated_sum[mask_marginals_rotated_sum != 0] = 1
|
||||||
index_x=np.array(range(len(mask_marginals_rotated_sum)))+1
|
index_x = np.array(range(len(mask_marginals_rotated_sum))) + 1
|
||||||
|
|
||||||
index_x_interest=index_x[mask_marginals_rotated_sum==1]
|
index_x_interest = index_x[mask_marginals_rotated_sum == 1]
|
||||||
|
|
||||||
min_point_of_left_marginal=np.min(index_x_interest)-16
|
min_point_of_left_marginal = np.min(index_x_interest) - 16
|
||||||
max_point_of_right_marginal=np.max(index_x_interest)+16
|
max_point_of_right_marginal = np.max(index_x_interest) + 16
|
||||||
|
|
||||||
if min_point_of_left_marginal<0:
|
if min_point_of_left_marginal < 0:
|
||||||
min_point_of_left_marginal=0
|
min_point_of_left_marginal = 0
|
||||||
if max_point_of_right_marginal>=text_regions.shape[1]:
|
if max_point_of_right_marginal >= text_regions.shape[1]:
|
||||||
max_point_of_right_marginal=text_regions.shape[1]-1
|
max_point_of_right_marginal = text_regions.shape[1] - 1
|
||||||
|
|
||||||
if light_version:
|
if light_version:
|
||||||
text_regions_org = np.copy(text_regions)
|
text_regions_org = np.copy(text_regions)
|
||||||
text_regions[text_regions[:,:]==1]=4
|
text_regions[text_regions[:, :] == 1] = 4
|
||||||
|
|
||||||
pixel_img=4
|
pixel_img = 4
|
||||||
min_area_text=0.00001
|
min_area_text = 0.00001
|
||||||
|
|
||||||
polygon_mask_marginals_rotated = return_contours_of_interested_region(mask_marginals,1,min_area_text)
|
polygon_mask_marginals_rotated = return_contours_of_interested_region(mask_marginals, 1, min_area_text)
|
||||||
|
|
||||||
polygon_mask_marginals_rotated = polygon_mask_marginals_rotated[0]
|
polygon_mask_marginals_rotated = polygon_mask_marginals_rotated[0]
|
||||||
|
|
||||||
polygons_of_marginals=return_contours_of_interested_region(text_regions,pixel_img,min_area_text)
|
polygons_of_marginals = return_contours_of_interested_region(text_regions, pixel_img, min_area_text)
|
||||||
|
|
||||||
cx_text_only,cy_text_only ,x_min_text_only,x_max_text_only, y_min_text_only ,y_max_text_only,y_cor_x_min_main=find_new_features_of_contours(polygons_of_marginals)
|
cx_text_only, cy_text_only, x_min_text_only, x_max_text_only, y_min_text_only, y_max_text_only, y_cor_x_min_main = find_new_features_of_contours(
|
||||||
|
polygons_of_marginals)
|
||||||
|
|
||||||
text_regions[(text_regions[:,:]==4)]=1
|
text_regions[(text_regions[:, :] == 4)] = 1
|
||||||
|
|
||||||
marginlas_should_be_main_text=[]
|
marginlas_should_be_main_text = []
|
||||||
|
|
||||||
x_min_marginals_left=[]
|
x_min_marginals_left = []
|
||||||
x_min_marginals_right=[]
|
x_min_marginals_right = []
|
||||||
|
|
||||||
for i in range(len(cx_text_only)):
|
for i in range(len(cx_text_only)):
|
||||||
results = cv2.pointPolygonTest(polygon_mask_marginals_rotated, (cx_text_only[i], cy_text_only[i]), False)
|
results = cv2.pointPolygonTest(polygon_mask_marginals_rotated, (cx_text_only[i], cy_text_only[i]),
|
||||||
|
False)
|
||||||
|
|
||||||
if results == -1:
|
if results == -1:
|
||||||
marginlas_should_be_main_text.append(polygons_of_marginals[i])
|
marginlas_should_be_main_text.append(polygons_of_marginals[i])
|
||||||
|
|
||||||
|
text_regions_org = cv2.fillPoly(text_regions_org, pts=marginlas_should_be_main_text, color=(4, 4))
|
||||||
|
|
||||||
text_regions_org=cv2.fillPoly(text_regions_org, pts =marginlas_should_be_main_text, color=(4,4))
|
|
||||||
text_regions = np.copy(text_regions_org)
|
text_regions = np.copy(text_regions_org)
|
||||||
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
text_regions[(mask_marginals_rotated[:,:]!=1) & (text_regions[:,:]==1)]=4
|
text_regions[(mask_marginals_rotated[:, :] != 1) & (text_regions[:, :] == 1)] = 4
|
||||||
|
|
||||||
pixel_img=4
|
pixel_img = 4
|
||||||
min_area_text=0.00001
|
min_area_text = 0.00001
|
||||||
|
|
||||||
polygons_of_marginals=return_contours_of_interested_region(text_regions,pixel_img,min_area_text)
|
polygons_of_marginals = return_contours_of_interested_region(text_regions, pixel_img, min_area_text)
|
||||||
|
|
||||||
cx_text_only,cy_text_only ,x_min_text_only,x_max_text_only, y_min_text_only ,y_max_text_only,y_cor_x_min_main=find_new_features_of_contours(polygons_of_marginals)
|
cx_text_only, cy_text_only, x_min_text_only, x_max_text_only, y_min_text_only, y_max_text_only, y_cor_x_min_main = find_new_features_of_contours(
|
||||||
|
polygons_of_marginals)
|
||||||
|
|
||||||
text_regions[(text_regions[:,:]==4)]=1
|
text_regions[(text_regions[:, :] == 4)] = 1
|
||||||
|
|
||||||
marginlas_should_be_main_text=[]
|
marginlas_should_be_main_text = []
|
||||||
|
|
||||||
x_min_marginals_left=[]
|
x_min_marginals_left = []
|
||||||
x_min_marginals_right=[]
|
x_min_marginals_right = []
|
||||||
|
|
||||||
for i in range(len(cx_text_only)):
|
for i in range(len(cx_text_only)):
|
||||||
x_width_mar=abs(x_min_text_only[i]-x_max_text_only[i])
|
x_width_mar = abs(x_min_text_only[i] - x_max_text_only[i])
|
||||||
y_height_mar=abs(y_min_text_only[i]-y_max_text_only[i])
|
y_height_mar = abs(y_min_text_only[i] - y_max_text_only[i])
|
||||||
|
|
||||||
if x_width_mar>16 and y_height_mar/x_width_mar<18:
|
if x_width_mar > 16 and y_height_mar / x_width_mar < 18:
|
||||||
marginlas_should_be_main_text.append(polygons_of_marginals[i])
|
marginlas_should_be_main_text.append(polygons_of_marginals[i])
|
||||||
if x_min_text_only[i]<(mid_point-one_third_left):
|
if x_min_text_only[i] < (mid_point - one_third_left):
|
||||||
x_min_marginals_left_new=x_min_text_only[i]
|
x_min_marginals_left_new = x_min_text_only[i]
|
||||||
if len(x_min_marginals_left)==0:
|
if len(x_min_marginals_left) == 0:
|
||||||
x_min_marginals_left.append(x_min_marginals_left_new)
|
x_min_marginals_left.append(x_min_marginals_left_new)
|
||||||
else:
|
else:
|
||||||
x_min_marginals_left[0]=min(x_min_marginals_left[0],x_min_marginals_left_new)
|
x_min_marginals_left[0] = min(x_min_marginals_left[0], x_min_marginals_left_new)
|
||||||
else:
|
else:
|
||||||
x_min_marginals_right_new=x_min_text_only[i]
|
x_min_marginals_right_new = x_min_text_only[i]
|
||||||
if len(x_min_marginals_right)==0:
|
if len(x_min_marginals_right) == 0:
|
||||||
x_min_marginals_right.append(x_min_marginals_right_new)
|
x_min_marginals_right.append(x_min_marginals_right_new)
|
||||||
else:
|
else:
|
||||||
x_min_marginals_right[0]=min(x_min_marginals_right[0],x_min_marginals_right_new)
|
x_min_marginals_right[0] = min(x_min_marginals_right[0], x_min_marginals_right_new)
|
||||||
|
|
||||||
if len(x_min_marginals_left)==0:
|
if len(x_min_marginals_left) == 0:
|
||||||
x_min_marginals_left=[0]
|
x_min_marginals_left = [0]
|
||||||
if len(x_min_marginals_right)==0:
|
if len(x_min_marginals_right) == 0:
|
||||||
x_min_marginals_right=[text_regions.shape[1]-1]
|
x_min_marginals_right = [text_regions.shape[1] - 1]
|
||||||
|
|
||||||
|
|
||||||
text_regions=cv2.fillPoly(text_regions, pts =marginlas_should_be_main_text, color=(4,4))
|
|
||||||
|
|
||||||
|
text_regions = cv2.fillPoly(text_regions, pts=marginlas_should_be_main_text, color=(4, 4))
|
||||||
|
|
||||||
#text_regions[:,:int(x_min_marginals_left[0])][text_regions[:,:int(x_min_marginals_left[0])]==1]=0
|
#text_regions[:,:int(x_min_marginals_left[0])][text_regions[:,:int(x_min_marginals_left[0])]==1]=0
|
||||||
#text_regions[:,int(x_min_marginals_right[0]):][text_regions[:,int(x_min_marginals_right[0]):]==1]=0
|
#text_regions[:,int(x_min_marginals_right[0]):][text_regions[:,int(x_min_marginals_right[0]):]==1]=0
|
||||||
|
|
||||||
|
text_regions[:, :int(min_point_of_left_marginal)][
|
||||||
text_regions[:,:int(min_point_of_left_marginal)][text_regions[:,:int(min_point_of_left_marginal)]==1]=0
|
text_regions[:, :int(min_point_of_left_marginal)] == 1] = 0
|
||||||
text_regions[:,int(max_point_of_right_marginal):][text_regions[:,int(max_point_of_right_marginal):]==1]=0
|
text_regions[:, int(max_point_of_right_marginal):][
|
||||||
|
text_regions[:, int(max_point_of_right_marginal):] == 1] = 0
|
||||||
|
|
||||||
###text_regions[:,0:point_left][text_regions[:,0:point_left]==1]=4
|
###text_regions[:,0:point_left][text_regions[:,0:point_left]==1]=4
|
||||||
|
|
||||||
|
@ -216,7 +206,6 @@ def get_marginals(text_with_lines, text_regions, num_col, slope_deskew, light_ve
|
||||||
#plt.plot(peaks,region_sum_0[peaks],'*')
|
#plt.plot(peaks,region_sum_0[peaks],'*')
|
||||||
#plt.show()
|
#plt.show()
|
||||||
|
|
||||||
|
|
||||||
#plt.imshow(text_regions)
|
#plt.imshow(text_regions)
|
||||||
#plt.show()
|
#plt.show()
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ import numpy as np
|
||||||
from ocrd_models import OcrdExif
|
from ocrd_models import OcrdExif
|
||||||
from cv2 import COLOR_GRAY2BGR, COLOR_RGB2BGR, COLOR_BGR2RGB, cvtColor, imread
|
from cv2 import COLOR_GRAY2BGR, COLOR_RGB2BGR, COLOR_BGR2RGB, cvtColor, imread
|
||||||
|
|
||||||
|
|
||||||
# from sbb_binarization
|
# from sbb_binarization
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ def rotate_image(img_patch, slope):
|
||||||
return cv2.warpAffine(img_patch, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
|
return cv2.warpAffine(img_patch, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
|
||||||
|
|
||||||
|
|
||||||
def rotate_image_different( img, slope):
|
def rotate_image_different(img, slope):
|
||||||
# img = cv2.imread('images/input.jpg')
|
# img = cv2.imread('images/input.jpg')
|
||||||
num_rows, num_cols = img.shape[:2]
|
num_rows, num_cols = img.shape[:2]
|
||||||
|
|
||||||
|
@ -65,7 +65,8 @@ def rotate_max_area(image, rotated, rotated_textline, rotated_layout, rotated_ta
|
||||||
y2 = y1 + int(hr)
|
y2 = y1 + int(hr)
|
||||||
x1 = w // 2 - int(wr / 2)
|
x1 = w // 2 - int(wr / 2)
|
||||||
x2 = x1 + int(wr)
|
x2 = x1 + int(wr)
|
||||||
return rotated[y1:y2, x1:x2], rotated_textline[y1:y2, x1:x2], rotated_layout[y1:y2, x1:x2], rotated_table_prediction[y1:y2, x1:x2]
|
return rotated[y1:y2, x1:x2], rotated_textline[y1:y2, x1:x2], rotated_layout[y1:y2,
|
||||||
|
x1:x2], rotated_table_prediction[y1:y2, x1:x2]
|
||||||
|
|
||||||
|
|
||||||
def rotation_not_90_func(img, textline, text_regions_p_1, table_prediction, thetha):
|
def rotation_not_90_func(img, textline, text_regions_p_1, table_prediction, thetha):
|
||||||
|
@ -91,5 +92,5 @@ def rotate_max_area_full_layout(image, rotated, rotated_textline, rotated_layout
|
||||||
y2 = y1 + int(hr)
|
y2 = y1 + int(hr)
|
||||||
x1 = w // 2 - int(wr / 2)
|
x1 = w // 2 - int(wr / 2)
|
||||||
x2 = x1 + int(wr)
|
x2 = x1 + int(wr)
|
||||||
return rotated[y1:y2, x1:x2], rotated_textline[y1:y2, x1:x2], rotated_layout[y1:y2, x1:x2], rotated_layout_full[y1:y2, x1:x2]
|
return rotated[y1:y2, x1:x2], rotated_textline[y1:y2, x1:x2], rotated_layout[y1:y2, x1:x2], rotated_layout_full[
|
||||||
|
y1:y2, x1:x2]
|
||||||
|
|
|
@ -47,7 +47,7 @@ def dedup_separate_lines(img_patch, contour_text_interest, thetha, axis):
|
||||||
|
|
||||||
y = textline_patch_sum_along_width[:] # [first_nonzero:last_nonzero]
|
y = textline_patch_sum_along_width[:] # [first_nonzero:last_nonzero]
|
||||||
y_padded = np.zeros(len(y) + 40)
|
y_padded = np.zeros(len(y) + 40)
|
||||||
y_padded[20 : len(y) + 20] = y
|
y_padded[20: len(y) + 20] = y
|
||||||
x = np.array(range(len(y)))
|
x = np.array(range(len(y)))
|
||||||
|
|
||||||
peaks_real, _ = find_peaks(gaussian_filter1d(y, 3), height=0)
|
peaks_real, _ = find_peaks(gaussian_filter1d(y, 3), height=0)
|
||||||
|
@ -56,14 +56,15 @@ def dedup_separate_lines(img_patch, contour_text_interest, thetha, axis):
|
||||||
y_padded_smoothed_e = gaussian_filter1d(y_padded, 2)
|
y_padded_smoothed_e = gaussian_filter1d(y_padded, 2)
|
||||||
y_padded_up_to_down_e = -y_padded + np.max(y_padded)
|
y_padded_up_to_down_e = -y_padded + np.max(y_padded)
|
||||||
y_padded_up_to_down_padded_e = np.zeros(len(y_padded_up_to_down_e) + 40)
|
y_padded_up_to_down_padded_e = np.zeros(len(y_padded_up_to_down_e) + 40)
|
||||||
y_padded_up_to_down_padded_e[20 : len(y_padded_up_to_down_e) + 20] = y_padded_up_to_down_e
|
y_padded_up_to_down_padded_e[20: len(y_padded_up_to_down_e) + 20] = y_padded_up_to_down_e
|
||||||
y_padded_up_to_down_padded_e = gaussian_filter1d(y_padded_up_to_down_padded_e, 2)
|
y_padded_up_to_down_padded_e = gaussian_filter1d(y_padded_up_to_down_padded_e, 2)
|
||||||
|
|
||||||
peaks_e, _ = find_peaks(y_padded_smoothed_e, height=0)
|
peaks_e, _ = find_peaks(y_padded_smoothed_e, height=0)
|
||||||
peaks_neg_e, _ = find_peaks(y_padded_up_to_down_padded_e, height=0)
|
peaks_neg_e, _ = find_peaks(y_padded_up_to_down_padded_e, height=0)
|
||||||
neg_peaks_max = np.max(y_padded_up_to_down_padded_e[peaks_neg_e])
|
neg_peaks_max = np.max(y_padded_up_to_down_padded_e[peaks_neg_e])
|
||||||
|
|
||||||
arg_neg_must_be_deleted = np.arange(len(peaks_neg_e))[y_padded_up_to_down_padded_e[peaks_neg_e] / float(neg_peaks_max) < 0.3]
|
arg_neg_must_be_deleted = np.arange(len(peaks_neg_e))[
|
||||||
|
y_padded_up_to_down_padded_e[peaks_neg_e] / float(neg_peaks_max) < 0.3]
|
||||||
diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)
|
diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)
|
||||||
|
|
||||||
arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
|
arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
|
||||||
|
@ -74,11 +75,11 @@ def dedup_separate_lines(img_patch, contour_text_interest, thetha, axis):
|
||||||
|
|
||||||
clusters_to_be_deleted = []
|
clusters_to_be_deleted = []
|
||||||
if len(arg_diff_cluster) > 0:
|
if len(arg_diff_cluster) > 0:
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0 : arg_diff_cluster[0] + 1])
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0: arg_diff_cluster[0] + 1])
|
||||||
for i in range(len(arg_diff_cluster) - 1):
|
for i in range(len(arg_diff_cluster) - 1):
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1 :
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1:
|
||||||
arg_diff_cluster[i + 1] + 1])
|
arg_diff_cluster[i + 1] + 1])
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1 :])
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1:])
|
||||||
if len(clusters_to_be_deleted) > 0:
|
if len(clusters_to_be_deleted) > 0:
|
||||||
peaks_new_extra = []
|
peaks_new_extra = []
|
||||||
for m in range(len(clusters_to_be_deleted)):
|
for m in range(len(clusters_to_be_deleted)):
|
||||||
|
@ -115,7 +116,7 @@ def dedup_separate_lines(img_patch, contour_text_interest, thetha, axis):
|
||||||
y_padded_smoothed = gaussian_filter1d(y_padded, sigma_gaus)
|
y_padded_smoothed = gaussian_filter1d(y_padded, sigma_gaus)
|
||||||
y_padded_up_to_down = -y_padded + np.max(y_padded)
|
y_padded_up_to_down = -y_padded + np.max(y_padded)
|
||||||
y_padded_up_to_down_padded = np.zeros(len(y_padded_up_to_down) + 40)
|
y_padded_up_to_down_padded = np.zeros(len(y_padded_up_to_down) + 40)
|
||||||
y_padded_up_to_down_padded[20 : len(y_padded_up_to_down) + 20] = y_padded_up_to_down
|
y_padded_up_to_down_padded[20: len(y_padded_up_to_down) + 20] = y_padded_up_to_down
|
||||||
y_padded_up_to_down_padded = gaussian_filter1d(y_padded_up_to_down_padded, sigma_gaus)
|
y_padded_up_to_down_padded = gaussian_filter1d(y_padded_up_to_down_padded, sigma_gaus)
|
||||||
|
|
||||||
peaks, _ = find_peaks(y_padded_smoothed, height=0)
|
peaks, _ = find_peaks(y_padded_smoothed, height=0)
|
||||||
|
@ -164,118 +165,119 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
||||||
x = np.array(range(len(y)))
|
x = np.array(range(len(y)))
|
||||||
|
|
||||||
peaks_real, _ = find_peaks(gaussian_filter1d(y, 3), height=0)
|
peaks_real, _ = find_peaks(gaussian_filter1d(y, 3), height=0)
|
||||||
if 1>0:
|
if 1 > 0:
|
||||||
try:
|
try:
|
||||||
y_padded_smoothed_e= gaussian_filter1d(y_padded, 2)
|
y_padded_smoothed_e = gaussian_filter1d(y_padded, 2)
|
||||||
y_padded_up_to_down_e=-y_padded+np.max(y_padded)
|
y_padded_up_to_down_e = -y_padded + np.max(y_padded)
|
||||||
y_padded_up_to_down_padded_e=np.zeros(len(y_padded_up_to_down_e)+40)
|
y_padded_up_to_down_padded_e = np.zeros(len(y_padded_up_to_down_e) + 40)
|
||||||
y_padded_up_to_down_padded_e[20:len(y_padded_up_to_down_e)+20]=y_padded_up_to_down_e
|
y_padded_up_to_down_padded_e[20:len(y_padded_up_to_down_e) + 20] = y_padded_up_to_down_e
|
||||||
y_padded_up_to_down_padded_e= gaussian_filter1d(y_padded_up_to_down_padded_e, 2)
|
y_padded_up_to_down_padded_e = gaussian_filter1d(y_padded_up_to_down_padded_e, 2)
|
||||||
|
|
||||||
|
|
||||||
peaks_e, _ = find_peaks(y_padded_smoothed_e, height=0)
|
peaks_e, _ = find_peaks(y_padded_smoothed_e, height=0)
|
||||||
peaks_neg_e, _ = find_peaks(y_padded_up_to_down_padded_e, height=0)
|
peaks_neg_e, _ = find_peaks(y_padded_up_to_down_padded_e, height=0)
|
||||||
neg_peaks_max=np.max(y_padded_up_to_down_padded_e[peaks_neg_e])
|
neg_peaks_max = np.max(y_padded_up_to_down_padded_e[peaks_neg_e])
|
||||||
|
|
||||||
arg_neg_must_be_deleted= np.arange(len(peaks_neg_e))[y_padded_up_to_down_padded_e[peaks_neg_e]/float(neg_peaks_max)<0.3]
|
arg_neg_must_be_deleted = np.arange(len(peaks_neg_e))[
|
||||||
diff_arg_neg_must_be_deleted=np.diff(arg_neg_must_be_deleted)
|
y_padded_up_to_down_padded_e[peaks_neg_e] / float(neg_peaks_max) < 0.3]
|
||||||
|
diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)
|
||||||
|
|
||||||
arg_diff=np.array(range(len(diff_arg_neg_must_be_deleted)))
|
arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
|
||||||
arg_diff_cluster=arg_diff[diff_arg_neg_must_be_deleted>1]
|
arg_diff_cluster = arg_diff[diff_arg_neg_must_be_deleted > 1]
|
||||||
|
|
||||||
peaks_new=peaks_e[:]
|
peaks_new = peaks_e[:]
|
||||||
peaks_neg_new=peaks_neg_e[:]
|
peaks_neg_new = peaks_neg_e[:]
|
||||||
|
|
||||||
clusters_to_be_deleted=[]
|
clusters_to_be_deleted = []
|
||||||
if len(arg_diff_cluster)>0:
|
if len(arg_diff_cluster) > 0:
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0:arg_diff_cluster[0]+1])
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0:arg_diff_cluster[0] + 1])
|
||||||
for i in range(len(arg_diff_cluster)-1):
|
for i in range(len(arg_diff_cluster) - 1):
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i]+1:
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1:
|
||||||
arg_diff_cluster[i+1]+1])
|
arg_diff_cluster[i + 1] + 1])
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster)-1]+1:])
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1:])
|
||||||
if len(clusters_to_be_deleted)>0:
|
if len(clusters_to_be_deleted) > 0:
|
||||||
peaks_new_extra=[]
|
peaks_new_extra = []
|
||||||
for m in range(len(clusters_to_be_deleted)):
|
for m in range(len(clusters_to_be_deleted)):
|
||||||
min_cluster=np.min(peaks_e[clusters_to_be_deleted[m]])
|
min_cluster = np.min(peaks_e[clusters_to_be_deleted[m]])
|
||||||
max_cluster=np.max(peaks_e[clusters_to_be_deleted[m]])
|
max_cluster = np.max(peaks_e[clusters_to_be_deleted[m]])
|
||||||
peaks_new_extra.append( int( (min_cluster+max_cluster)/2.0) )
|
peaks_new_extra.append(int((min_cluster + max_cluster) / 2.0))
|
||||||
for m1 in range(len(clusters_to_be_deleted[m])):
|
for m1 in range(len(clusters_to_be_deleted[m])):
|
||||||
peaks_new=peaks_new[peaks_new!=peaks_e[clusters_to_be_deleted[m][m1]-1]]
|
peaks_new = peaks_new[peaks_new != peaks_e[clusters_to_be_deleted[m][m1] - 1]]
|
||||||
peaks_new=peaks_new[peaks_new!=peaks_e[clusters_to_be_deleted[m][m1]]]
|
peaks_new = peaks_new[peaks_new != peaks_e[clusters_to_be_deleted[m][m1]]]
|
||||||
peaks_neg_new=peaks_neg_new[peaks_neg_new!=peaks_neg_e[clusters_to_be_deleted[m][m1]]]
|
peaks_neg_new = peaks_neg_new[peaks_neg_new != peaks_neg_e[clusters_to_be_deleted[m][m1]]]
|
||||||
peaks_new_tot=[]
|
peaks_new_tot = []
|
||||||
for i1 in peaks_new:
|
for i1 in peaks_new:
|
||||||
peaks_new_tot.append(i1)
|
peaks_new_tot.append(i1)
|
||||||
for i1 in peaks_new_extra:
|
for i1 in peaks_new_extra:
|
||||||
peaks_new_tot.append(i1)
|
peaks_new_tot.append(i1)
|
||||||
peaks_new_tot=np.sort(peaks_new_tot)
|
peaks_new_tot = np.sort(peaks_new_tot)
|
||||||
else:
|
else:
|
||||||
peaks_new_tot=peaks_e[:]
|
peaks_new_tot = peaks_e[:]
|
||||||
|
|
||||||
textline_con,hierarchy=return_contours_of_image(img_patch)
|
textline_con, hierarchy = return_contours_of_image(img_patch)
|
||||||
textline_con_fil=filter_contours_area_of_image(img_patch,
|
textline_con_fil = filter_contours_area_of_image(img_patch,
|
||||||
textline_con, hierarchy,
|
textline_con, hierarchy,
|
||||||
max_area=1, min_area=0.0008)
|
max_area=1, min_area=0.0008)
|
||||||
y_diff_mean=np.mean(np.diff(peaks_new_tot))#self.find_contours_mean_y_diff(textline_con_fil)
|
y_diff_mean = np.mean(np.diff(peaks_new_tot)) #self.find_contours_mean_y_diff(textline_con_fil)
|
||||||
sigma_gaus=int( y_diff_mean * (7./40.0) )
|
sigma_gaus = int(y_diff_mean * (7. / 40.0))
|
||||||
#print(sigma_gaus,'sigma_gaus')
|
#print(sigma_gaus,'sigma_gaus')
|
||||||
except:
|
except:
|
||||||
sigma_gaus=12
|
sigma_gaus = 12
|
||||||
if sigma_gaus<3:
|
if sigma_gaus < 3:
|
||||||
sigma_gaus=3
|
sigma_gaus = 3
|
||||||
#print(sigma_gaus,'sigma')
|
#print(sigma_gaus,'sigma')
|
||||||
|
|
||||||
y_padded_smoothed= gaussian_filter1d(y_padded, sigma_gaus)
|
y_padded_smoothed = gaussian_filter1d(y_padded, sigma_gaus)
|
||||||
y_padded_up_to_down=-y_padded+np.max(y_padded)
|
y_padded_up_to_down = -y_padded + np.max(y_padded)
|
||||||
y_padded_up_to_down_padded=np.zeros(len(y_padded_up_to_down)+40)
|
y_padded_up_to_down_padded = np.zeros(len(y_padded_up_to_down) + 40)
|
||||||
y_padded_up_to_down_padded[20:len(y_padded_up_to_down)+20]=y_padded_up_to_down
|
y_padded_up_to_down_padded[20:len(y_padded_up_to_down) + 20] = y_padded_up_to_down
|
||||||
y_padded_up_to_down_padded= gaussian_filter1d(y_padded_up_to_down_padded, sigma_gaus)
|
y_padded_up_to_down_padded = gaussian_filter1d(y_padded_up_to_down_padded, sigma_gaus)
|
||||||
|
|
||||||
peaks, _ = find_peaks(y_padded_smoothed, height=0)
|
peaks, _ = find_peaks(y_padded_smoothed, height=0)
|
||||||
peaks_neg, _ = find_peaks(y_padded_up_to_down_padded, height=0)
|
peaks_neg, _ = find_peaks(y_padded_up_to_down_padded, height=0)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
neg_peaks_max=np.max(y_padded_smoothed[peaks])
|
neg_peaks_max = np.max(y_padded_smoothed[peaks])
|
||||||
arg_neg_must_be_deleted= np.arange(len(peaks_neg))[y_padded_up_to_down_padded[peaks_neg]/float(neg_peaks_max)<0.42]
|
arg_neg_must_be_deleted = np.arange(len(peaks_neg))[
|
||||||
diff_arg_neg_must_be_deleted=np.diff(arg_neg_must_be_deleted)
|
y_padded_up_to_down_padded[peaks_neg] / float(neg_peaks_max) < 0.42]
|
||||||
|
diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)
|
||||||
|
|
||||||
arg_diff=np.array(range(len(diff_arg_neg_must_be_deleted)))
|
arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
|
||||||
arg_diff_cluster=arg_diff[diff_arg_neg_must_be_deleted>1]
|
arg_diff_cluster = arg_diff[diff_arg_neg_must_be_deleted > 1]
|
||||||
except:
|
except:
|
||||||
arg_neg_must_be_deleted=[]
|
arg_neg_must_be_deleted = []
|
||||||
arg_diff_cluster=[]
|
arg_diff_cluster = []
|
||||||
try:
|
try:
|
||||||
peaks_new=peaks[:]
|
peaks_new = peaks[:]
|
||||||
peaks_neg_new=peaks_neg[:]
|
peaks_neg_new = peaks_neg[:]
|
||||||
clusters_to_be_deleted=[]
|
clusters_to_be_deleted = []
|
||||||
|
|
||||||
if len(arg_diff_cluster)>=2 and len(arg_diff_cluster)>0:
|
if len(arg_diff_cluster) >= 2 and len(arg_diff_cluster) > 0:
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0:arg_diff_cluster[0]+1])
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0:arg_diff_cluster[0] + 1])
|
||||||
for i in range(len(arg_diff_cluster)-1):
|
for i in range(len(arg_diff_cluster) - 1):
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i]+1:
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1:
|
||||||
arg_diff_cluster[i+1]+1])
|
arg_diff_cluster[i + 1] + 1])
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster)-1]+1:])
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1:])
|
||||||
elif len(arg_neg_must_be_deleted)>=2 and len(arg_diff_cluster)==0:
|
elif len(arg_neg_must_be_deleted) >= 2 and len(arg_diff_cluster) == 0:
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[:])
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[:])
|
||||||
|
|
||||||
if len(arg_neg_must_be_deleted)==1:
|
if len(arg_neg_must_be_deleted) == 1:
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted)
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted)
|
||||||
if len(clusters_to_be_deleted)>0:
|
if len(clusters_to_be_deleted) > 0:
|
||||||
peaks_new_extra=[]
|
peaks_new_extra = []
|
||||||
for m in range(len(clusters_to_be_deleted)):
|
for m in range(len(clusters_to_be_deleted)):
|
||||||
min_cluster=np.min(peaks[clusters_to_be_deleted[m]])
|
min_cluster = np.min(peaks[clusters_to_be_deleted[m]])
|
||||||
max_cluster=np.max(peaks[clusters_to_be_deleted[m]])
|
max_cluster = np.max(peaks[clusters_to_be_deleted[m]])
|
||||||
peaks_new_extra.append( int( (min_cluster+max_cluster)/2.0) )
|
peaks_new_extra.append(int((min_cluster + max_cluster) / 2.0))
|
||||||
for m1 in range(len(clusters_to_be_deleted[m])):
|
for m1 in range(len(clusters_to_be_deleted[m])):
|
||||||
peaks_new=peaks_new[peaks_new!=peaks[clusters_to_be_deleted[m][m1]-1]]
|
peaks_new = peaks_new[peaks_new != peaks[clusters_to_be_deleted[m][m1] - 1]]
|
||||||
peaks_new=peaks_new[peaks_new!=peaks[clusters_to_be_deleted[m][m1]]]
|
peaks_new = peaks_new[peaks_new != peaks[clusters_to_be_deleted[m][m1]]]
|
||||||
peaks_neg_new=peaks_neg_new[peaks_neg_new!=peaks_neg[clusters_to_be_deleted[m][m1]]]
|
peaks_neg_new = peaks_neg_new[peaks_neg_new != peaks_neg[clusters_to_be_deleted[m][m1]]]
|
||||||
peaks_new_tot=[]
|
peaks_new_tot = []
|
||||||
for i1 in peaks_new:
|
for i1 in peaks_new:
|
||||||
peaks_new_tot.append(i1)
|
peaks_new_tot.append(i1)
|
||||||
for i1 in peaks_new_extra:
|
for i1 in peaks_new_extra:
|
||||||
peaks_new_tot.append(i1)
|
peaks_new_tot.append(i1)
|
||||||
peaks_new_tot=np.sort(peaks_new_tot)
|
peaks_new_tot = np.sort(peaks_new_tot)
|
||||||
|
|
||||||
##plt.plot(y_padded_up_to_down_padded)
|
##plt.plot(y_padded_up_to_down_padded)
|
||||||
##plt.plot(peaks_neg,y_padded_up_to_down_padded[peaks_neg],'*')
|
##plt.plot(peaks_neg,y_padded_up_to_down_padded[peaks_neg],'*')
|
||||||
|
@ -292,18 +294,18 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
||||||
##plt.plot(y_padded_smoothed)
|
##plt.plot(y_padded_smoothed)
|
||||||
##plt.plot(peaks_new_tot,y_padded_smoothed[peaks_new_tot],'*')
|
##plt.plot(peaks_new_tot,y_padded_smoothed[peaks_new_tot],'*')
|
||||||
##plt.show()
|
##plt.show()
|
||||||
peaks=peaks_new_tot[:]
|
peaks = peaks_new_tot[:]
|
||||||
peaks_neg=peaks_neg_new[:]
|
peaks_neg = peaks_neg_new[:]
|
||||||
else:
|
else:
|
||||||
peaks_new_tot=peaks[:]
|
peaks_new_tot = peaks[:]
|
||||||
peaks=peaks_new_tot[:]
|
peaks = peaks_new_tot[:]
|
||||||
peaks_neg=peaks_neg_new[:]
|
peaks_neg = peaks_neg_new[:]
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
mean_value_of_peaks=np.mean(y_padded_smoothed[peaks])
|
mean_value_of_peaks = np.mean(y_padded_smoothed[peaks])
|
||||||
std_value_of_peaks=np.std(y_padded_smoothed[peaks])
|
std_value_of_peaks = np.std(y_padded_smoothed[peaks])
|
||||||
peaks_values=y_padded_smoothed[peaks]
|
peaks_values = y_padded_smoothed[peaks]
|
||||||
|
|
||||||
peaks_neg = peaks_neg - 20 - 20
|
peaks_neg = peaks_neg - 20 - 20
|
||||||
peaks = peaks - 20
|
peaks = peaks - 20
|
||||||
|
@ -320,16 +322,16 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
||||||
if len(peaks_neg) == len(peaks) + 1 and len(peaks) >= 3:
|
if len(peaks_neg) == len(peaks) + 1 and len(peaks) >= 3:
|
||||||
for jj in range(len(peaks)):
|
for jj in range(len(peaks)):
|
||||||
|
|
||||||
if jj==(len(peaks)-1):
|
if jj == (len(peaks) - 1):
|
||||||
dis_to_next_up = abs(peaks[jj] - peaks_neg[jj])
|
dis_to_next_up = abs(peaks[jj] - peaks_neg[jj])
|
||||||
dis_to_next_down = abs(peaks[jj] - peaks_neg[jj + 1])
|
dis_to_next_down = abs(peaks[jj] - peaks_neg[jj + 1])
|
||||||
|
|
||||||
if peaks_values[jj]>mean_value_of_peaks-std_value_of_peaks/2.:
|
if peaks_values[jj] > mean_value_of_peaks - std_value_of_peaks / 2.:
|
||||||
point_up = peaks[jj] + first_nonzero - int(1.3 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0)
|
point_up = peaks[jj] + first_nonzero - int(1.3 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0)
|
||||||
point_down =y_max_cont-1##peaks[jj] + first_nonzero + int(1.3 * dis_to_next_down) #point_up# np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0)
|
point_down = y_max_cont - 1 ##peaks[jj] + first_nonzero + int(1.3 * dis_to_next_down) #point_up# np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0)
|
||||||
else:
|
else:
|
||||||
point_up = peaks[jj] + first_nonzero - int(1.4 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0)
|
point_up = peaks[jj] + first_nonzero - int(1.4 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0)
|
||||||
point_down =y_max_cont-1##peaks[jj] + first_nonzero + int(1.6 * dis_to_next_down) #point_up# np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0)
|
point_down = y_max_cont - 1 ##peaks[jj] + first_nonzero + int(1.6 * dis_to_next_down) #point_up# np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0)
|
||||||
|
|
||||||
point_down_narrow = peaks[jj] + first_nonzero + int(
|
point_down_narrow = peaks[jj] + first_nonzero + int(
|
||||||
1.4 * dis_to_next_down) ###-int(dis_to_next_down*1./2)
|
1.4 * dis_to_next_down) ###-int(dis_to_next_down*1./2)
|
||||||
|
@ -337,22 +339,24 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
||||||
dis_to_next_up = abs(peaks[jj] - peaks_neg[jj])
|
dis_to_next_up = abs(peaks[jj] - peaks_neg[jj])
|
||||||
dis_to_next_down = abs(peaks[jj] - peaks_neg[jj + 1])
|
dis_to_next_down = abs(peaks[jj] - peaks_neg[jj + 1])
|
||||||
|
|
||||||
if peaks_values[jj]>mean_value_of_peaks-std_value_of_peaks/2.:
|
if peaks_values[jj] > mean_value_of_peaks - std_value_of_peaks / 2.:
|
||||||
point_up = peaks[jj] + first_nonzero - int(1.1 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0)
|
point_up = peaks[jj] + first_nonzero - int(1.1 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0)
|
||||||
point_down = peaks[jj] + first_nonzero + int(1.1 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0)
|
point_down = peaks[jj] + first_nonzero + int(
|
||||||
|
1.1 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0)
|
||||||
else:
|
else:
|
||||||
point_up = peaks[jj] + first_nonzero - int(1.23 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0)
|
point_up = peaks[jj] + first_nonzero - int(1.23 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0)
|
||||||
point_down = peaks[jj] + first_nonzero + int(1.33 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0)
|
point_down = peaks[jj] + first_nonzero + int(
|
||||||
|
1.33 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0)
|
||||||
|
|
||||||
point_down_narrow = peaks[jj] + first_nonzero + int(
|
point_down_narrow = peaks[jj] + first_nonzero + int(
|
||||||
1.1 * dis_to_next_down) ###-int(dis_to_next_down*1./2)
|
1.1 * dis_to_next_down) ###-int(dis_to_next_down*1./2)
|
||||||
|
|
||||||
|
|
||||||
if point_down_narrow >= img_patch.shape[0]:
|
if point_down_narrow >= img_patch.shape[0]:
|
||||||
point_down_narrow = img_patch.shape[0] - 2
|
point_down_narrow = img_patch.shape[0] - 2
|
||||||
|
|
||||||
|
distances = [cv2.pointPolygonTest(contour_text_interest_copy,
|
||||||
distances = [cv2.pointPolygonTest(contour_text_interest_copy, tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])), True)
|
tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])),
|
||||||
|
True)
|
||||||
for mj in range(len(xv))]
|
for mj in range(len(xv))]
|
||||||
distances = np.array(distances)
|
distances = np.array(distances)
|
||||||
|
|
||||||
|
@ -375,24 +379,24 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
||||||
x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
|
x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
|
||||||
x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d
|
x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d
|
||||||
|
|
||||||
if x_min_rot1<0:
|
if x_min_rot1 < 0:
|
||||||
x_min_rot1=0
|
x_min_rot1 = 0
|
||||||
if x_min_rot4<0:
|
if x_min_rot4 < 0:
|
||||||
x_min_rot4=0
|
x_min_rot4 = 0
|
||||||
if point_up_rot1<0:
|
if point_up_rot1 < 0:
|
||||||
point_up_rot1=0
|
point_up_rot1 = 0
|
||||||
if point_up_rot2<0:
|
if point_up_rot2 < 0:
|
||||||
point_up_rot2=0
|
point_up_rot2 = 0
|
||||||
|
|
||||||
x_min_rot1=x_min_rot1-x_help
|
x_min_rot1 = x_min_rot1 - x_help
|
||||||
x_max_rot2=x_max_rot2-x_help
|
x_max_rot2 = x_max_rot2 - x_help
|
||||||
x_max_rot3=x_max_rot3-x_help
|
x_max_rot3 = x_max_rot3 - x_help
|
||||||
x_min_rot4=x_min_rot4-x_help
|
x_min_rot4 = x_min_rot4 - x_help
|
||||||
|
|
||||||
point_up_rot1=point_up_rot1-y_help
|
point_up_rot1 = point_up_rot1 - y_help
|
||||||
point_up_rot2=point_up_rot2-y_help
|
point_up_rot2 = point_up_rot2 - y_help
|
||||||
point_down_rot3=point_down_rot3-y_help
|
point_down_rot3 = point_down_rot3 - y_help
|
||||||
point_down_rot4=point_down_rot4-y_help
|
point_down_rot4 = point_down_rot4 - y_help
|
||||||
|
|
||||||
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)],
|
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)],
|
||||||
[int(x_max_rot2), int(point_up_rot2)],
|
[int(x_max_rot2), int(point_up_rot2)],
|
||||||
|
@ -434,24 +438,24 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
||||||
x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
|
x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
|
||||||
x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d
|
x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d
|
||||||
|
|
||||||
if x_min_rot1<0:
|
if x_min_rot1 < 0:
|
||||||
x_min_rot1=0
|
x_min_rot1 = 0
|
||||||
if x_min_rot4<0:
|
if x_min_rot4 < 0:
|
||||||
x_min_rot4=0
|
x_min_rot4 = 0
|
||||||
if point_up_rot1<0:
|
if point_up_rot1 < 0:
|
||||||
point_up_rot1=0
|
point_up_rot1 = 0
|
||||||
if point_up_rot2<0:
|
if point_up_rot2 < 0:
|
||||||
point_up_rot2=0
|
point_up_rot2 = 0
|
||||||
|
|
||||||
x_min_rot1=x_min_rot1-x_help
|
x_min_rot1 = x_min_rot1 - x_help
|
||||||
x_max_rot2=x_max_rot2-x_help
|
x_max_rot2 = x_max_rot2 - x_help
|
||||||
x_max_rot3=x_max_rot3-x_help
|
x_max_rot3 = x_max_rot3 - x_help
|
||||||
x_min_rot4=x_min_rot4-x_help
|
x_min_rot4 = x_min_rot4 - x_help
|
||||||
|
|
||||||
point_up_rot1=point_up_rot1-y_help
|
point_up_rot1 = point_up_rot1 - y_help
|
||||||
point_up_rot2=point_up_rot2-y_help
|
point_up_rot2 = point_up_rot2 - y_help
|
||||||
point_down_rot3=point_down_rot3-y_help
|
point_down_rot3 = point_down_rot3 - y_help
|
||||||
point_down_rot4=point_down_rot4-y_help
|
point_down_rot4 = point_down_rot4 - y_help
|
||||||
|
|
||||||
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)],
|
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)],
|
||||||
[int(x_max_rot2), int(point_up_rot2)],
|
[int(x_max_rot2), int(point_up_rot2)],
|
||||||
|
@ -465,21 +469,22 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
||||||
dis_to_next = np.abs(peaks[1] - peaks[0])
|
dis_to_next = np.abs(peaks[1] - peaks[0])
|
||||||
for jj in range(len(peaks)):
|
for jj in range(len(peaks)):
|
||||||
if jj == 0:
|
if jj == 0:
|
||||||
point_up = 0#peaks[jj] + first_nonzero - int(1. / 1.7 * dis_to_next)
|
point_up = 0 #peaks[jj] + first_nonzero - int(1. / 1.7 * dis_to_next)
|
||||||
if point_up < 0:
|
if point_up < 0:
|
||||||
point_up = 1
|
point_up = 1
|
||||||
point_down = peaks_neg[1] + first_nonzero# peaks[jj] + first_nonzero + int(1. / 1.8 * dis_to_next)
|
point_down = peaks_neg[1] + first_nonzero # peaks[jj] + first_nonzero + int(1. / 1.8 * dis_to_next)
|
||||||
elif jj == 1:
|
elif jj == 1:
|
||||||
point_down =peaks_neg[1] + first_nonzero# peaks[jj] + first_nonzero + int(1. / 1.8 * dis_to_next)
|
point_down = peaks_neg[1] + first_nonzero # peaks[jj] + first_nonzero + int(1. / 1.8 * dis_to_next)
|
||||||
if point_down >= img_patch.shape[0]:
|
if point_down >= img_patch.shape[0]:
|
||||||
point_down = img_patch.shape[0] - 2
|
point_down = img_patch.shape[0] - 2
|
||||||
try:
|
try:
|
||||||
point_up = peaks_neg[2] + first_nonzero#peaks[jj] + first_nonzero - int(1. / 1.8 * dis_to_next)
|
point_up = peaks_neg[2] + first_nonzero #peaks[jj] + first_nonzero - int(1. / 1.8 * dis_to_next)
|
||||||
except:
|
except:
|
||||||
point_up =peaks[jj] + first_nonzero - int(1. / 1.8 * dis_to_next)
|
point_up = peaks[jj] + first_nonzero - int(1. / 1.8 * dis_to_next)
|
||||||
|
|
||||||
distances = [cv2.pointPolygonTest(contour_text_interest_copy,
|
distances = [cv2.pointPolygonTest(contour_text_interest_copy,
|
||||||
tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])), True)
|
tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])),
|
||||||
|
True)
|
||||||
for mj in range(len(xv))]
|
for mj in range(len(xv))]
|
||||||
distances = np.array(distances)
|
distances = np.array(distances)
|
||||||
|
|
||||||
|
@ -501,24 +506,24 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
||||||
x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
|
x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
|
||||||
x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d
|
x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d
|
||||||
|
|
||||||
if x_min_rot1<0:
|
if x_min_rot1 < 0:
|
||||||
x_min_rot1=0
|
x_min_rot1 = 0
|
||||||
if x_min_rot4<0:
|
if x_min_rot4 < 0:
|
||||||
x_min_rot4=0
|
x_min_rot4 = 0
|
||||||
if point_up_rot1<0:
|
if point_up_rot1 < 0:
|
||||||
point_up_rot1=0
|
point_up_rot1 = 0
|
||||||
if point_up_rot2<0:
|
if point_up_rot2 < 0:
|
||||||
point_up_rot2=0
|
point_up_rot2 = 0
|
||||||
|
|
||||||
x_min_rot1=x_min_rot1-x_help
|
x_min_rot1 = x_min_rot1 - x_help
|
||||||
x_max_rot2=x_max_rot2-x_help
|
x_max_rot2 = x_max_rot2 - x_help
|
||||||
x_max_rot3=x_max_rot3-x_help
|
x_max_rot3 = x_max_rot3 - x_help
|
||||||
x_min_rot4=x_min_rot4-x_help
|
x_min_rot4 = x_min_rot4 - x_help
|
||||||
|
|
||||||
point_up_rot1=point_up_rot1-y_help
|
point_up_rot1 = point_up_rot1 - y_help
|
||||||
point_up_rot2=point_up_rot2-y_help
|
point_up_rot2 = point_up_rot2 - y_help
|
||||||
point_down_rot3=point_down_rot3-y_help
|
point_down_rot3 = point_down_rot3 - y_help
|
||||||
point_down_rot4=point_down_rot4-y_help
|
point_down_rot4 = point_down_rot4 - y_help
|
||||||
|
|
||||||
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)],
|
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)],
|
||||||
[int(x_max_rot2), int(point_up_rot2)],
|
[int(x_max_rot2), int(point_up_rot2)],
|
||||||
|
@ -554,7 +559,8 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
||||||
point_down = peaks[jj] + first_nonzero + int(1. / 1.9 * dis_to_next_down)
|
point_down = peaks[jj] + first_nonzero + int(1. / 1.9 * dis_to_next_down)
|
||||||
|
|
||||||
distances = [cv2.pointPolygonTest(contour_text_interest_copy,
|
distances = [cv2.pointPolygonTest(contour_text_interest_copy,
|
||||||
tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])), True)
|
tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])),
|
||||||
|
True)
|
||||||
for mj in range(len(xv))]
|
for mj in range(len(xv))]
|
||||||
distances = np.array(distances)
|
distances = np.array(distances)
|
||||||
|
|
||||||
|
@ -576,24 +582,24 @@ def separate_lines(img_patch, contour_text_interest, thetha, x_help, y_help):
|
||||||
x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
|
x_max_rot3, point_down_rot3 = p3[0] + x_d, p3[1] + y_d
|
||||||
x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d
|
x_min_rot4, point_down_rot4 = p4[0] + x_d, p4[1] + y_d
|
||||||
|
|
||||||
if x_min_rot1<0:
|
if x_min_rot1 < 0:
|
||||||
x_min_rot1=0
|
x_min_rot1 = 0
|
||||||
if x_min_rot4<0:
|
if x_min_rot4 < 0:
|
||||||
x_min_rot4=0
|
x_min_rot4 = 0
|
||||||
if point_up_rot1<0:
|
if point_up_rot1 < 0:
|
||||||
point_up_rot1=0
|
point_up_rot1 = 0
|
||||||
if point_up_rot2<0:
|
if point_up_rot2 < 0:
|
||||||
point_up_rot2=0
|
point_up_rot2 = 0
|
||||||
|
|
||||||
x_min_rot1=x_min_rot1-x_help
|
x_min_rot1 = x_min_rot1 - x_help
|
||||||
x_max_rot2=x_max_rot2-x_help
|
x_max_rot2 = x_max_rot2 - x_help
|
||||||
x_max_rot3=x_max_rot3-x_help
|
x_max_rot3 = x_max_rot3 - x_help
|
||||||
x_min_rot4=x_min_rot4-x_help
|
x_min_rot4 = x_min_rot4 - x_help
|
||||||
|
|
||||||
point_up_rot1=point_up_rot1-y_help
|
point_up_rot1 = point_up_rot1 - y_help
|
||||||
point_up_rot2=point_up_rot2-y_help
|
point_up_rot2 = point_up_rot2 - y_help
|
||||||
point_down_rot3=point_down_rot3-y_help
|
point_down_rot3 = point_down_rot3 - y_help
|
||||||
point_down_rot4=point_down_rot4-y_help
|
point_down_rot4 = point_down_rot4 - y_help
|
||||||
|
|
||||||
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)],
|
textline_boxes_rot.append(np.array([[int(x_min_rot1), int(point_up_rot1)],
|
||||||
[int(x_max_rot2), int(point_up_rot2)],
|
[int(x_max_rot2), int(point_up_rot2)],
|
||||||
|
@ -626,7 +632,8 @@ def separate_lines_vertical(img_patch, contour_text_interest, thetha):
|
||||||
|
|
||||||
neg_peaks_max = np.max(y_padded_up_to_down_padded[peaks_neg])
|
neg_peaks_max = np.max(y_padded_up_to_down_padded[peaks_neg])
|
||||||
|
|
||||||
arg_neg_must_be_deleted = np.arange(len(peaks_neg))[y_padded_up_to_down_padded[peaks_neg] / float(neg_peaks_max) < 0.42]
|
arg_neg_must_be_deleted = np.arange(len(peaks_neg))[
|
||||||
|
y_padded_up_to_down_padded[peaks_neg] / float(neg_peaks_max) < 0.42]
|
||||||
diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)
|
diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)
|
||||||
|
|
||||||
arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
|
arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
|
||||||
|
@ -637,11 +644,11 @@ def separate_lines_vertical(img_patch, contour_text_interest, thetha):
|
||||||
clusters_to_be_deleted = []
|
clusters_to_be_deleted = []
|
||||||
|
|
||||||
if len(arg_diff_cluster) >= 2 and len(arg_diff_cluster) > 0:
|
if len(arg_diff_cluster) >= 2 and len(arg_diff_cluster) > 0:
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0 : arg_diff_cluster[0] + 1])
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0: arg_diff_cluster[0] + 1])
|
||||||
for i in range(len(arg_diff_cluster) - 1):
|
for i in range(len(arg_diff_cluster) - 1):
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1 :
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1:
|
||||||
arg_diff_cluster[i + 1] + 1])
|
arg_diff_cluster[i + 1] + 1])
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1 :])
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1:])
|
||||||
elif len(arg_neg_must_be_deleted) >= 2 and len(arg_diff_cluster) == 0:
|
elif len(arg_neg_must_be_deleted) >= 2 and len(arg_diff_cluster) == 0:
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[:])
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[:])
|
||||||
if len(arg_neg_must_be_deleted) == 1:
|
if len(arg_neg_must_be_deleted) == 1:
|
||||||
|
@ -704,24 +711,30 @@ def separate_lines_vertical(img_patch, contour_text_interest, thetha):
|
||||||
point_up = peaks[jj] + first_nonzero - int(1.4 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0)
|
point_up = peaks[jj] + first_nonzero - int(1.4 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0)
|
||||||
point_down = x_max_cont - 1 ##peaks[jj] + first_nonzero + int(1.6 * dis_to_next_down) #point_up# np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0)
|
point_down = x_max_cont - 1 ##peaks[jj] + first_nonzero + int(1.6 * dis_to_next_down) #point_up# np.max(y_cont)#peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0)
|
||||||
|
|
||||||
point_down_narrow = peaks[jj] + first_nonzero + int(1.4 * dis_to_next_down) ###-int(dis_to_next_down*1./2)
|
point_down_narrow = peaks[jj] + first_nonzero + int(
|
||||||
|
1.4 * dis_to_next_down) ###-int(dis_to_next_down*1./2)
|
||||||
else:
|
else:
|
||||||
dis_to_next_up = abs(peaks[jj] - peaks_neg[jj])
|
dis_to_next_up = abs(peaks[jj] - peaks_neg[jj])
|
||||||
dis_to_next_down = abs(peaks[jj] - peaks_neg[jj + 1])
|
dis_to_next_down = abs(peaks[jj] - peaks_neg[jj + 1])
|
||||||
|
|
||||||
if peaks_values[jj] > mean_value_of_peaks - std_value_of_peaks / 2.0:
|
if peaks_values[jj] > mean_value_of_peaks - std_value_of_peaks / 2.0:
|
||||||
point_up = peaks[jj] + first_nonzero - int(1.1 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0)
|
point_up = peaks[jj] + first_nonzero - int(1.1 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0)
|
||||||
point_down = peaks[jj] + first_nonzero + int(1.1 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0)
|
point_down = peaks[jj] + first_nonzero + int(
|
||||||
|
1.1 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0)
|
||||||
else:
|
else:
|
||||||
point_up = peaks[jj] + first_nonzero - int(1.23 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0)
|
point_up = peaks[jj] + first_nonzero - int(1.23 * dis_to_next_up) ##+int(dis_to_next_up*1./4.0)
|
||||||
point_down = peaks[jj] + first_nonzero + int(1.33 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0)
|
point_down = peaks[jj] + first_nonzero + int(
|
||||||
|
1.33 * dis_to_next_down) ###-int(dis_to_next_down*1./4.0)
|
||||||
|
|
||||||
point_down_narrow = peaks[jj] + first_nonzero + int(1.1 * dis_to_next_down) ###-int(dis_to_next_down*1./2)
|
point_down_narrow = peaks[jj] + first_nonzero + int(
|
||||||
|
1.1 * dis_to_next_down) ###-int(dis_to_next_down*1./2)
|
||||||
|
|
||||||
if point_down_narrow >= img_patch.shape[0]:
|
if point_down_narrow >= img_patch.shape[0]:
|
||||||
point_down_narrow = img_patch.shape[0] - 2
|
point_down_narrow = img_patch.shape[0] - 2
|
||||||
|
|
||||||
distances = [cv2.pointPolygonTest(contour_text_interest_copy, tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])), True) for mj in range(len(xv))]
|
distances = [cv2.pointPolygonTest(contour_text_interest_copy,
|
||||||
|
tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])),
|
||||||
|
True) for mj in range(len(xv))]
|
||||||
distances = np.array(distances)
|
distances = np.array(distances)
|
||||||
|
|
||||||
xvinside = xv[distances >= 0]
|
xvinside = xv[distances >= 0]
|
||||||
|
@ -810,7 +823,8 @@ def separate_lines_vertical(img_patch, contour_text_interest, thetha):
|
||||||
point_up = peaks[jj] + first_nonzero - int(1.0 / 1.8 * dis_to_next)
|
point_up = peaks[jj] + first_nonzero - int(1.0 / 1.8 * dis_to_next)
|
||||||
|
|
||||||
distances = [cv2.pointPolygonTest(contour_text_interest_copy,
|
distances = [cv2.pointPolygonTest(contour_text_interest_copy,
|
||||||
tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])), True)
|
tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])),
|
||||||
|
True)
|
||||||
for mj in range(len(xv))]
|
for mj in range(len(xv))]
|
||||||
distances = np.array(distances)
|
distances = np.array(distances)
|
||||||
|
|
||||||
|
@ -875,7 +889,8 @@ def separate_lines_vertical(img_patch, contour_text_interest, thetha):
|
||||||
point_down = peaks[jj] + first_nonzero + int(1.0 / 1.9 * dis_to_next_down)
|
point_down = peaks[jj] + first_nonzero + int(1.0 / 1.9 * dis_to_next_down)
|
||||||
|
|
||||||
distances = [cv2.pointPolygonTest(contour_text_interest_copy,
|
distances = [cv2.pointPolygonTest(contour_text_interest_copy,
|
||||||
tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])), True)
|
tuple(int(x) for x in np.array([xv[mj], peaks[jj] + first_nonzero])),
|
||||||
|
True)
|
||||||
for mj in range(len(xv))]
|
for mj in range(len(xv))]
|
||||||
distances = np.array(distances)
|
distances = np.array(distances)
|
||||||
|
|
||||||
|
@ -944,7 +959,7 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
||||||
|
|
||||||
y = textline_patch_sum_along_width[:] # [first_nonzero:last_nonzero]
|
y = textline_patch_sum_along_width[:] # [first_nonzero:last_nonzero]
|
||||||
y_padded = np.zeros(len(y) + 40)
|
y_padded = np.zeros(len(y) + 40)
|
||||||
y_padded[20 : len(y) + 20] = y
|
y_padded[20: len(y) + 20] = y
|
||||||
x = np.array(range(len(y)))
|
x = np.array(range(len(y)))
|
||||||
|
|
||||||
peaks_real, _ = find_peaks(gaussian_filter1d(y, 3), height=0)
|
peaks_real, _ = find_peaks(gaussian_filter1d(y, 3), height=0)
|
||||||
|
@ -953,14 +968,15 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
||||||
y_padded_smoothed_e = gaussian_filter1d(y_padded, 2)
|
y_padded_smoothed_e = gaussian_filter1d(y_padded, 2)
|
||||||
y_padded_up_to_down_e = -y_padded + np.max(y_padded)
|
y_padded_up_to_down_e = -y_padded + np.max(y_padded)
|
||||||
y_padded_up_to_down_padded_e = np.zeros(len(y_padded_up_to_down_e) + 40)
|
y_padded_up_to_down_padded_e = np.zeros(len(y_padded_up_to_down_e) + 40)
|
||||||
y_padded_up_to_down_padded_e[20 : len(y_padded_up_to_down_e) + 20] = y_padded_up_to_down_e
|
y_padded_up_to_down_padded_e[20: len(y_padded_up_to_down_e) + 20] = y_padded_up_to_down_e
|
||||||
y_padded_up_to_down_padded_e = gaussian_filter1d(y_padded_up_to_down_padded_e, 2)
|
y_padded_up_to_down_padded_e = gaussian_filter1d(y_padded_up_to_down_padded_e, 2)
|
||||||
|
|
||||||
peaks_e, _ = find_peaks(y_padded_smoothed_e, height=0)
|
peaks_e, _ = find_peaks(y_padded_smoothed_e, height=0)
|
||||||
peaks_neg_e, _ = find_peaks(y_padded_up_to_down_padded_e, height=0)
|
peaks_neg_e, _ = find_peaks(y_padded_up_to_down_padded_e, height=0)
|
||||||
neg_peaks_max = np.max(y_padded_up_to_down_padded_e[peaks_neg_e])
|
neg_peaks_max = np.max(y_padded_up_to_down_padded_e[peaks_neg_e])
|
||||||
|
|
||||||
arg_neg_must_be_deleted = np.arange(len(peaks_neg_e))[y_padded_up_to_down_padded_e[peaks_neg_e] / float(neg_peaks_max) < 0.3]
|
arg_neg_must_be_deleted = np.arange(len(peaks_neg_e))[
|
||||||
|
y_padded_up_to_down_padded_e[peaks_neg_e] / float(neg_peaks_max) < 0.3]
|
||||||
diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)
|
diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)
|
||||||
|
|
||||||
arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
|
arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
|
||||||
|
@ -971,10 +987,11 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
||||||
|
|
||||||
clusters_to_be_deleted = []
|
clusters_to_be_deleted = []
|
||||||
if len(arg_diff_cluster) > 0:
|
if len(arg_diff_cluster) > 0:
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0 : arg_diff_cluster[0] + 1])
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0: arg_diff_cluster[0] + 1])
|
||||||
for i in range(len(arg_diff_cluster) - 1):
|
for i in range(len(arg_diff_cluster) - 1):
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1 : arg_diff_cluster[i + 1] + 1])
|
clusters_to_be_deleted.append(
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1 :])
|
arg_neg_must_be_deleted[arg_diff_cluster[i] + 1: arg_diff_cluster[i + 1] + 1])
|
||||||
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1:])
|
||||||
if len(clusters_to_be_deleted) > 0:
|
if len(clusters_to_be_deleted) > 0:
|
||||||
peaks_new_extra = []
|
peaks_new_extra = []
|
||||||
for m in range(len(clusters_to_be_deleted)):
|
for m in range(len(clusters_to_be_deleted)):
|
||||||
|
@ -1011,7 +1028,7 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
||||||
y_padded_smoothed = gaussian_filter1d(y_padded, sigma_gaus)
|
y_padded_smoothed = gaussian_filter1d(y_padded, sigma_gaus)
|
||||||
y_padded_up_to_down = -y_padded + np.max(y_padded)
|
y_padded_up_to_down = -y_padded + np.max(y_padded)
|
||||||
y_padded_up_to_down_padded = np.zeros(len(y_padded_up_to_down) + 40)
|
y_padded_up_to_down_padded = np.zeros(len(y_padded_up_to_down) + 40)
|
||||||
y_padded_up_to_down_padded[20 : len(y_padded_up_to_down) + 20] = y_padded_up_to_down
|
y_padded_up_to_down_padded[20: len(y_padded_up_to_down) + 20] = y_padded_up_to_down
|
||||||
y_padded_up_to_down_padded = gaussian_filter1d(y_padded_up_to_down_padded, sigma_gaus)
|
y_padded_up_to_down_padded = gaussian_filter1d(y_padded_up_to_down_padded, sigma_gaus)
|
||||||
|
|
||||||
peaks, _ = find_peaks(y_padded_smoothed, height=0)
|
peaks, _ = find_peaks(y_padded_smoothed, height=0)
|
||||||
|
@ -1023,7 +1040,8 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
||||||
try:
|
try:
|
||||||
neg_peaks_max = np.max(y_padded_smoothed[peaks])
|
neg_peaks_max = np.max(y_padded_smoothed[peaks])
|
||||||
|
|
||||||
arg_neg_must_be_deleted = np.arange(len(peaks_neg))[y_padded_up_to_down_padded[peaks_neg] / float(neg_peaks_max) < 0.24]
|
arg_neg_must_be_deleted = np.arange(len(peaks_neg))[
|
||||||
|
y_padded_up_to_down_padded[peaks_neg] / float(neg_peaks_max) < 0.24]
|
||||||
diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)
|
diff_arg_neg_must_be_deleted = np.diff(arg_neg_must_be_deleted)
|
||||||
|
|
||||||
arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
|
arg_diff = np.array(range(len(diff_arg_neg_must_be_deleted)))
|
||||||
|
@ -1031,11 +1049,11 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
||||||
|
|
||||||
clusters_to_be_deleted = []
|
clusters_to_be_deleted = []
|
||||||
if len(arg_diff_cluster) >= 2 and len(arg_diff_cluster) > 0:
|
if len(arg_diff_cluster) >= 2 and len(arg_diff_cluster) > 0:
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0 : arg_diff_cluster[0] + 1])
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[0: arg_diff_cluster[0] + 1])
|
||||||
for i in range(len(arg_diff_cluster) - 1):
|
for i in range(len(arg_diff_cluster) - 1):
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1 :
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[i] + 1:
|
||||||
arg_diff_cluster[i + 1] + 1])
|
arg_diff_cluster[i + 1] + 1])
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1 :])
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[arg_diff_cluster[len(arg_diff_cluster) - 1] + 1:])
|
||||||
elif len(arg_neg_must_be_deleted) >= 2 and len(arg_diff_cluster) == 0:
|
elif len(arg_neg_must_be_deleted) >= 2 and len(arg_diff_cluster) == 0:
|
||||||
clusters_to_be_deleted.append(arg_neg_must_be_deleted[:])
|
clusters_to_be_deleted.append(arg_neg_must_be_deleted[:])
|
||||||
if len(arg_neg_must_be_deleted) == 1:
|
if len(arg_neg_must_be_deleted) == 1:
|
||||||
|
@ -1098,7 +1116,7 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
||||||
|
|
||||||
# print(peaks_neg_true)
|
# print(peaks_neg_true)
|
||||||
for i in range(len(peaks_neg_true)):
|
for i in range(len(peaks_neg_true)):
|
||||||
img_patch[peaks_neg_true[i] - 6 : peaks_neg_true[i] + 6, :] = 0
|
img_patch[peaks_neg_true[i] - 6: peaks_neg_true[i] + 6, :] = 0
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -1108,7 +1126,7 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
||||||
|
|
||||||
for i in range(len(peaks_pos_true)):
|
for i in range(len(peaks_pos_true)):
|
||||||
##img_patch[peaks_pos_true[i]-8:peaks_pos_true[i]+8,:]=1
|
##img_patch[peaks_pos_true[i]-8:peaks_pos_true[i]+8,:]=1
|
||||||
img_patch[peaks_pos_true[i] - 6 : peaks_pos_true[i] + 6, :] = 1
|
img_patch[peaks_pos_true[i] - 6: peaks_pos_true[i] + 6, :] = 1
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
kernel = np.ones((5, 5), np.uint8)
|
kernel = np.ones((5, 5), np.uint8)
|
||||||
|
@ -1118,6 +1136,7 @@ def separate_lines_new_inside_tiles2(img_patch, thetha):
|
||||||
img_patch = cv2.erode(img_patch, kernel, iterations=1)
|
img_patch = cv2.erode(img_patch, kernel, iterations=1)
|
||||||
return img_patch
|
return img_patch
|
||||||
|
|
||||||
|
|
||||||
def separate_lines_new_inside_tiles(img_path, thetha):
|
def separate_lines_new_inside_tiles(img_path, thetha):
|
||||||
(h, w) = img_path.shape[:2]
|
(h, w) = img_path.shape[:2]
|
||||||
center = (w // 2, h // 2)
|
center = (w // 2, h // 2)
|
||||||
|
@ -1144,7 +1163,7 @@ def separate_lines_new_inside_tiles(img_path, thetha):
|
||||||
|
|
||||||
y = mada_n[:] # [first_nonzero:last_nonzero]
|
y = mada_n[:] # [first_nonzero:last_nonzero]
|
||||||
y_help = np.zeros(len(y) + 40)
|
y_help = np.zeros(len(y) + 40)
|
||||||
y_help[20 : len(y) + 20] = y
|
y_help[20: len(y) + 20] = y
|
||||||
x = np.array(range(len(y)))
|
x = np.array(range(len(y)))
|
||||||
|
|
||||||
peaks_real, _ = find_peaks(gaussian_filter1d(y, 3), height=0)
|
peaks_real, _ = find_peaks(gaussian_filter1d(y, 3), height=0)
|
||||||
|
@ -1156,7 +1175,7 @@ def separate_lines_new_inside_tiles(img_path, thetha):
|
||||||
z = gaussian_filter1d(y_help, sigma_gaus)
|
z = gaussian_filter1d(y_help, sigma_gaus)
|
||||||
zneg_rev = -y_help + np.max(y_help)
|
zneg_rev = -y_help + np.max(y_help)
|
||||||
zneg = np.zeros(len(zneg_rev) + 40)
|
zneg = np.zeros(len(zneg_rev) + 40)
|
||||||
zneg[20 : len(zneg_rev) + 20] = zneg_rev
|
zneg[20: len(zneg_rev) + 20] = zneg_rev
|
||||||
zneg = gaussian_filter1d(zneg, sigma_gaus)
|
zneg = gaussian_filter1d(zneg, sigma_gaus)
|
||||||
|
|
||||||
peaks, _ = find_peaks(z, height=0)
|
peaks, _ = find_peaks(z, height=0)
|
||||||
|
@ -1240,7 +1259,7 @@ def separate_lines_new_inside_tiles(img_path, thetha):
|
||||||
|
|
||||||
# print(peaks_neg_true)
|
# print(peaks_neg_true)
|
||||||
for i in range(len(peaks_neg_true)):
|
for i in range(len(peaks_neg_true)):
|
||||||
img_path[peaks_neg_true[i] - 6 : peaks_neg_true[i] + 6, :] = 0
|
img_path[peaks_neg_true[i] - 6: peaks_neg_true[i] + 6, :] = 0
|
||||||
|
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
@ -1250,7 +1269,7 @@ def separate_lines_new_inside_tiles(img_path, thetha):
|
||||||
peaks_pos_true = peaks_pos_true - 20
|
peaks_pos_true = peaks_pos_true - 20
|
||||||
|
|
||||||
for i in range(len(peaks_pos_true)):
|
for i in range(len(peaks_pos_true)):
|
||||||
img_path[peaks_pos_true[i] - 8 : peaks_pos_true[i] + 8, :] = 1
|
img_path[peaks_pos_true[i] - 8: peaks_pos_true[i] + 8, :] = 1
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
kernel = np.ones((5, 5), np.uint8)
|
kernel = np.ones((5, 5), np.uint8)
|
||||||
|
@ -1305,7 +1324,8 @@ def separate_lines_vertical_cont(img_patch, contour_text_interest, thetha, box_i
|
||||||
return None, cont_final
|
return None, cont_final
|
||||||
|
|
||||||
|
|
||||||
def textline_contours_postprocessing(textline_mask, slope, contour_text_interest, box_ind, add_boxes_coor_into_textlines=False):
|
def textline_contours_postprocessing(textline_mask, slope, contour_text_interest, box_ind,
|
||||||
|
add_boxes_coor_into_textlines=False):
|
||||||
textline_mask = np.repeat(textline_mask[:, :, np.newaxis], 3, axis=2) * 255
|
textline_mask = np.repeat(textline_mask[:, :, np.newaxis], 3, axis=2) * 255
|
||||||
textline_mask = textline_mask.astype(np.uint8)
|
textline_mask = textline_mask.astype(np.uint8)
|
||||||
kernel = np.ones((5, 5), np.uint8)
|
kernel = np.ones((5, 5), np.uint8)
|
||||||
|
@ -1332,8 +1352,8 @@ def textline_contours_postprocessing(textline_mask, slope, contour_text_interest
|
||||||
|
|
||||||
textline_mask_help = np.zeros((textline_mask.shape[0] + int(2 * y_help),
|
textline_mask_help = np.zeros((textline_mask.shape[0] + int(2 * y_help),
|
||||||
textline_mask.shape[1] + int(2 * x_help), 3))
|
textline_mask.shape[1] + int(2 * x_help), 3))
|
||||||
textline_mask_help[y_help : y_help + textline_mask.shape[0],
|
textline_mask_help[y_help: y_help + textline_mask.shape[0],
|
||||||
x_help : x_help + textline_mask.shape[1], :] = np.copy(textline_mask[:, :, :])
|
x_help: x_help + textline_mask.shape[1], :] = np.copy(textline_mask[:, :, :])
|
||||||
|
|
||||||
dst = rotate_image(textline_mask_help, slope)
|
dst = rotate_image(textline_mask_help, slope)
|
||||||
dst = dst[:, :, 0]
|
dst = dst[:, :, 0]
|
||||||
|
@ -1356,8 +1376,8 @@ def textline_contours_postprocessing(textline_mask, slope, contour_text_interest
|
||||||
|
|
||||||
img_contour_help = np.zeros((img_contour.shape[0] + int(2 * y_help),
|
img_contour_help = np.zeros((img_contour.shape[0] + int(2 * y_help),
|
||||||
img_contour.shape[1] + int(2 * x_help), 3))
|
img_contour.shape[1] + int(2 * x_help), 3))
|
||||||
img_contour_help[y_help : y_help + img_contour.shape[0],
|
img_contour_help[y_help: y_help + img_contour.shape[0],
|
||||||
x_help : x_help + img_contour.shape[1], :] = np.copy(img_contour[:, :, :])
|
x_help: x_help + img_contour.shape[1], :] = np.copy(img_contour[:, :, :])
|
||||||
|
|
||||||
img_contour_rot = rotate_image(img_contour_help, slope)
|
img_contour_rot = rotate_image(img_contour_help, slope)
|
||||||
# plt.imshow(img_contour_rot_help)
|
# plt.imshow(img_contour_rot_help)
|
||||||
|
@ -1497,11 +1517,13 @@ def separate_lines_new2(img_path, thetha, num_col, slope_region, logger=None, pl
|
||||||
img_patch_separated_returned[:, :][img_patch_separated_returned[:, :] != 0] = 1
|
img_patch_separated_returned[:, :][img_patch_separated_returned[:, :] != 0] = 1
|
||||||
|
|
||||||
img_patch_separated_returned_true_size = img_patch_separated_returned[
|
img_patch_separated_returned_true_size = img_patch_separated_returned[
|
||||||
int(img_int.shape[0] * 0.1): int(img_int.shape[0] * 0.1) + img_int.shape[0],
|
int(img_int.shape[0] * 0.1): int(img_int.shape[0] * 0.1) +
|
||||||
int(img_int.shape[1] * 1.0): int(img_int.shape[1] * 1.0) + img_int.shape[1]]
|
img_int.shape[0],
|
||||||
|
int(img_int.shape[1] * 1.0): int(img_int.shape[1] * 1.0) +
|
||||||
|
img_int.shape[1]]
|
||||||
|
|
||||||
img_patch_separated_returned_true_size = img_patch_separated_returned_true_size[:, margin : length_x - margin]
|
img_patch_separated_returned_true_size = img_patch_separated_returned_true_size[:, margin: length_x - margin]
|
||||||
img_patch_ineterst_revised[:, index_x_d + margin : index_x_u - margin] = img_patch_separated_returned_true_size
|
img_patch_ineterst_revised[:, index_x_d + margin: index_x_u - margin] = img_patch_separated_returned_true_size
|
||||||
|
|
||||||
# plt.imshow(img_patch_ineterst_revised)
|
# plt.imshow(img_patch_ineterst_revised)
|
||||||
# plt.show()
|
# plt.show()
|
||||||
|
@ -1512,7 +1534,7 @@ def do_image_rotation(angle, img, sigma_des, logger=None):
|
||||||
if logger is None:
|
if logger is None:
|
||||||
logger = getLogger(__package__)
|
logger = getLogger(__package__)
|
||||||
img_rot = rotate_image(img, angle)
|
img_rot = rotate_image(img, angle)
|
||||||
img_rot[img_rot!=0] = 1
|
img_rot[img_rot != 0] = 1
|
||||||
try:
|
try:
|
||||||
var = find_num_col_deskew(img_rot, sigma_des, 20.3)
|
var = find_num_col_deskew(img_rot, sigma_des, 20.3)
|
||||||
except:
|
except:
|
||||||
|
@ -1521,23 +1543,23 @@ def do_image_rotation(angle, img, sigma_des, logger=None):
|
||||||
return var
|
return var
|
||||||
|
|
||||||
|
|
||||||
def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100,
|
def return_deskew_slop(img_patch_org, sigma_des, n_tot_angles=100,
|
||||||
main_page=False, logger=None, plotter=None, map=map):
|
main_page=False, logger=None, plotter=None, map=map):
|
||||||
if main_page and plotter:
|
if main_page and plotter:
|
||||||
plotter.save_plot_of_textline_density(img_patch_org)
|
plotter.save_plot_of_textline_density(img_patch_org)
|
||||||
|
|
||||||
img_int=np.zeros((img_patch_org.shape[0],img_patch_org.shape[1]))
|
img_int = np.zeros((img_patch_org.shape[0], img_patch_org.shape[1]))
|
||||||
img_int[:,:]=img_patch_org[:,:]#img_patch_org[:,:,0]
|
img_int[:, :] = img_patch_org[:, :] #img_patch_org[:,:,0]
|
||||||
|
|
||||||
max_shape=np.max(img_int.shape)
|
max_shape = np.max(img_int.shape)
|
||||||
img_resized=np.zeros((int(max_shape * 1.1) , int(max_shape * 1.1)))
|
img_resized = np.zeros((int(max_shape * 1.1), int(max_shape * 1.1)))
|
||||||
|
|
||||||
onset_x=int((img_resized.shape[1]-img_int.shape[1])/2.)
|
onset_x = int((img_resized.shape[1] - img_int.shape[1]) / 2.)
|
||||||
onset_y=int((img_resized.shape[0]-img_int.shape[0])/2.)
|
onset_y = int((img_resized.shape[0] - img_int.shape[0]) / 2.)
|
||||||
|
|
||||||
#img_resized=np.zeros((int( img_int.shape[0]*(1.8) ) , int( img_int.shape[1]*(2.6) ) ))
|
#img_resized=np.zeros((int( img_int.shape[0]*(1.8) ) , int( img_int.shape[1]*(2.6) ) ))
|
||||||
#img_resized[ int( img_int.shape[0]*(.4)):int( img_int.shape[0]*(.4))+img_int.shape[0] , int( img_int.shape[1]*(.8)):int( img_int.shape[1]*(.8))+img_int.shape[1] ]=img_int[:,:]
|
#img_resized[ int( img_int.shape[0]*(.4)):int( img_int.shape[0]*(.4))+img_int.shape[0] , int( img_int.shape[1]*(.8)):int( img_int.shape[1]*(.8))+img_int.shape[1] ]=img_int[:,:]
|
||||||
img_resized[ onset_y:onset_y+img_int.shape[0] , onset_x:onset_x+img_int.shape[1] ]=img_int[:,:]
|
img_resized[onset_y:onset_y + img_int.shape[0], onset_x:onset_x + img_int.shape[1]] = img_int[:, :]
|
||||||
|
|
||||||
#print(img_resized.shape,'img_resizedshape')
|
#print(img_resized.shape,'img_resizedshape')
|
||||||
#plt.imshow(img_resized)
|
#plt.imshow(img_resized)
|
||||||
|
@ -1545,7 +1567,7 @@ def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100,
|
||||||
if main_page and img_patch_org.shape[1] > img_patch_org.shape[0]:
|
if main_page and img_patch_org.shape[1] > img_patch_org.shape[0]:
|
||||||
#plt.imshow(img_resized)
|
#plt.imshow(img_resized)
|
||||||
#plt.show()
|
#plt.show()
|
||||||
angles = np.array([-45, 0, 45, 90,])
|
angles = np.array([-45, 0, 45, 90, ])
|
||||||
angle = get_smallest_skew(img_resized, sigma_des, angles, map=map, logger=logger, plotter=plotter)
|
angle = get_smallest_skew(img_resized, sigma_des, angles, map=map, logger=logger, plotter=plotter)
|
||||||
|
|
||||||
angles = np.linspace(angle - 22.5, angle + 22.5, n_tot_angles)
|
angles = np.linspace(angle - 22.5, angle + 22.5, n_tot_angles)
|
||||||
|
@ -1553,10 +1575,10 @@ def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100,
|
||||||
elif main_page:
|
elif main_page:
|
||||||
#plt.imshow(img_resized)
|
#plt.imshow(img_resized)
|
||||||
#plt.show()
|
#plt.show()
|
||||||
angles = np.linspace(-12, 12, n_tot_angles)#np.array([0 , 45 , 90 , -45])
|
angles = np.linspace(-12, 12, n_tot_angles) #np.array([0 , 45 , 90 , -45])
|
||||||
angle = get_smallest_skew(img_resized, sigma_des, angles, map=map, logger=logger, plotter=plotter)
|
angle = get_smallest_skew(img_resized, sigma_des, angles, map=map, logger=logger, plotter=plotter)
|
||||||
|
|
||||||
early_slope_edge=11
|
early_slope_edge = 11
|
||||||
if abs(angle) > early_slope_edge:
|
if abs(angle) > early_slope_edge:
|
||||||
if angle < 0:
|
if angle < 0:
|
||||||
angles = np.linspace(-90, -12, n_tot_angles)
|
angles = np.linspace(-90, -12, n_tot_angles)
|
||||||
|
@ -1567,7 +1589,7 @@ def return_deskew_slop(img_patch_org, sigma_des,n_tot_angles=100,
|
||||||
angles = np.linspace(-25, 25, int(0.5 * n_tot_angles) + 10)
|
angles = np.linspace(-25, 25, int(0.5 * n_tot_angles) + 10)
|
||||||
angle = get_smallest_skew(img_resized, sigma_des, angles, map=map, logger=logger, plotter=plotter)
|
angle = get_smallest_skew(img_resized, sigma_des, angles, map=map, logger=logger, plotter=plotter)
|
||||||
|
|
||||||
early_slope_edge=22
|
early_slope_edge = 22
|
||||||
if abs(angle) > early_slope_edge:
|
if abs(angle) > early_slope_edge:
|
||||||
if angle < 0:
|
if angle < 0:
|
||||||
angles = np.linspace(-90, -25, int(0.5 * n_tot_angles) + 10)
|
angles = np.linspace(-90, -25, int(0.5 * n_tot_angles) + 10)
|
||||||
|
@ -1608,13 +1630,13 @@ def do_work_of_slopes_new(
|
||||||
x, y, w, h = box_text
|
x, y, w, h = box_text
|
||||||
_, crop_coor = crop_image_inside_box(box_text, image_page_rotated)
|
_, crop_coor = crop_image_inside_box(box_text, image_page_rotated)
|
||||||
mask_textline = np.zeros(textline_mask_tot_ea.shape)
|
mask_textline = np.zeros(textline_mask_tot_ea.shape)
|
||||||
mask_textline = cv2.fillPoly(mask_textline, pts=[contour], color=(1,1,1))
|
mask_textline = cv2.fillPoly(mask_textline, pts=[contour], color=(1, 1, 1))
|
||||||
all_text_region_raw = textline_mask_tot_ea * mask_textline
|
all_text_region_raw = textline_mask_tot_ea * mask_textline
|
||||||
all_text_region_raw = all_text_region_raw[y: y + h, x: x + w].astype(np.uint8)
|
all_text_region_raw = all_text_region_raw[y: y + h, x: x + w].astype(np.uint8)
|
||||||
img_int_p = all_text_region_raw[:,:]
|
img_int_p = all_text_region_raw[:, :]
|
||||||
img_int_p = cv2.erode(img_int_p, KERNEL, iterations=2)
|
img_int_p = cv2.erode(img_int_p, KERNEL, iterations=2)
|
||||||
|
|
||||||
if img_int_p.shape[0] /img_int_p.shape[1] < 0.1:
|
if img_int_p.shape[0] / img_int_p.shape[1] < 0.1:
|
||||||
slope = 0
|
slope = 0
|
||||||
slope_for_all = slope_deskew
|
slope_for_all = slope_deskew
|
||||||
all_text_region_raw = textline_mask_tot_ea[y: y + h, x: x + w]
|
all_text_region_raw = textline_mask_tot_ea[y: y + h, x: x + w]
|
||||||
|
@ -1715,11 +1737,11 @@ def do_work_of_slopes_new_curved(
|
||||||
x, y, w, h = cv2.boundingRect(contour_par)
|
x, y, w, h = cv2.boundingRect(contour_par)
|
||||||
mask_biggest = np.zeros(mask_texts_only.shape)
|
mask_biggest = np.zeros(mask_texts_only.shape)
|
||||||
mask_biggest = cv2.fillPoly(mask_biggest, pts=[contour_par], color=(1, 1, 1))
|
mask_biggest = cv2.fillPoly(mask_biggest, pts=[contour_par], color=(1, 1, 1))
|
||||||
mask_region_in_patch_region = mask_biggest[y : y + h, x : x + w]
|
mask_region_in_patch_region = mask_biggest[y: y + h, x: x + w]
|
||||||
textline_biggest_region = mask_biggest * textline_mask_tot_ea
|
textline_biggest_region = mask_biggest * textline_mask_tot_ea
|
||||||
|
|
||||||
# print(slope_for_all,'slope_for_all')
|
# print(slope_for_all,'slope_for_all')
|
||||||
textline_rotated_separated = separate_lines_new2(textline_biggest_region[y: y+h, x: x+w], 0,
|
textline_rotated_separated = separate_lines_new2(textline_biggest_region[y: y + h, x: x + w], 0,
|
||||||
num_col, slope_for_all,
|
num_col, slope_for_all,
|
||||||
logger=logger, plotter=plotter)
|
logger=logger, plotter=plotter)
|
||||||
|
|
||||||
|
@ -1728,7 +1750,7 @@ def do_work_of_slopes_new_curved(
|
||||||
textline_rotated_separated[mask_region_in_patch_region[:, :] != 1] = 0
|
textline_rotated_separated[mask_region_in_patch_region[:, :] != 1] = 0
|
||||||
# till here
|
# till here
|
||||||
|
|
||||||
textline_region_in_image[y : y + h, x : x + w] = textline_rotated_separated
|
textline_region_in_image[y: y + h, x: x + w] = textline_rotated_separated
|
||||||
|
|
||||||
# plt.imshow(textline_region_in_image)
|
# plt.imshow(textline_region_in_image)
|
||||||
# plt.show()
|
# plt.show()
|
||||||
|
@ -1746,14 +1768,16 @@ def do_work_of_slopes_new_curved(
|
||||||
mask_biggest2 = cv2.dilate(mask_biggest2, KERNEL, iterations=4)
|
mask_biggest2 = cv2.dilate(mask_biggest2, KERNEL, iterations=4)
|
||||||
|
|
||||||
pixel_img = 1
|
pixel_img = 1
|
||||||
mask_biggest2 = resize_image(mask_biggest2, int(mask_biggest2.shape[0] * scale_par), int(mask_biggest2.shape[1] * scale_par))
|
mask_biggest2 = resize_image(mask_biggest2, int(mask_biggest2.shape[0] * scale_par),
|
||||||
|
int(mask_biggest2.shape[1] * scale_par))
|
||||||
cnt_textlines_in_image_ind = return_contours_of_interested_textline(mask_biggest2, pixel_img)
|
cnt_textlines_in_image_ind = return_contours_of_interested_textline(mask_biggest2, pixel_img)
|
||||||
try:
|
try:
|
||||||
textlines_cnt_per_region.append(cnt_textlines_in_image_ind[0])
|
textlines_cnt_per_region.append(cnt_textlines_in_image_ind[0])
|
||||||
except Exception as why:
|
except Exception as why:
|
||||||
logger.error(why)
|
logger.error(why)
|
||||||
else:
|
else:
|
||||||
textlines_cnt_per_region = textline_contours_postprocessing(all_text_region_raw, slope_for_all, contour_par, box_text, True)
|
textlines_cnt_per_region = textline_contours_postprocessing(all_text_region_raw, slope_for_all, contour_par,
|
||||||
|
box_text, True)
|
||||||
# print(np.shape(textlines_cnt_per_region),'textlines_cnt_per_region')
|
# print(np.shape(textlines_cnt_per_region),'textlines_cnt_per_region')
|
||||||
|
|
||||||
return textlines_cnt_per_region[::-1], box_text, contour, contour_par, crop_coor, index_r_con, slope
|
return textlines_cnt_per_region[::-1], box_text, contour, contour_par, crop_coor, index_r_con, slope
|
||||||
|
@ -1771,7 +1795,7 @@ def do_work_of_slopes_new_light(
|
||||||
x, y, w, h = box_text
|
x, y, w, h = box_text
|
||||||
_, crop_coor = crop_image_inside_box(box_text, image_page_rotated)
|
_, crop_coor = crop_image_inside_box(box_text, image_page_rotated)
|
||||||
mask_textline = np.zeros(textline_mask_tot_ea.shape)
|
mask_textline = np.zeros(textline_mask_tot_ea.shape)
|
||||||
mask_textline = cv2.fillPoly(mask_textline, pts=[contour], color=(1,1,1))
|
mask_textline = cv2.fillPoly(mask_textline, pts=[contour], color=(1, 1, 1))
|
||||||
all_text_region_raw = textline_mask_tot_ea * mask_textline
|
all_text_region_raw = textline_mask_tot_ea * mask_textline
|
||||||
all_text_region_raw = all_text_region_raw[y: y + h, x: x + w].astype(np.uint8)
|
all_text_region_raw = all_text_region_raw[y: y + h, x: x + w].astype(np.uint8)
|
||||||
|
|
||||||
|
|
|
@ -18,13 +18,13 @@ from ocrd_models.ocrd_page import (
|
||||||
TableRegionType,
|
TableRegionType,
|
||||||
SeparatorRegionType,
|
SeparatorRegionType,
|
||||||
to_xml
|
to_xml
|
||||||
)
|
)
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
class EynollahXmlWriter:
|
class EynollahXmlWriter:
|
||||||
|
|
||||||
def __init__(self, *, dir_out, image_filename, curved_line,textline_light, pcgts=None):
|
def __init__(self, *, dir_out, image_filename, curved_line, textline_light, pcgts=None):
|
||||||
self.logger = getLogger('eynollah.writer')
|
self.logger = getLogger('eynollah.writer')
|
||||||
self.counter = EynollahIdCounter()
|
self.counter = EynollahIdCounter()
|
||||||
self.dir_out = dir_out
|
self.dir_out = dir_out
|
||||||
|
@ -53,11 +53,12 @@ class EynollahXmlWriter:
|
||||||
else:
|
else:
|
||||||
points_page_print += str(int((contour[0][0]) / self.scale_x))
|
points_page_print += str(int((contour[0][0]) / self.scale_x))
|
||||||
points_page_print += ','
|
points_page_print += ','
|
||||||
points_page_print += str(int((contour[0][1] ) / self.scale_y))
|
points_page_print += str(int((contour[0][1]) / self.scale_y))
|
||||||
points_page_print = points_page_print + ' '
|
points_page_print = points_page_print + ' '
|
||||||
return points_page_print[:-1]
|
return points_page_print[:-1]
|
||||||
|
|
||||||
def serialize_lines_in_marginal(self, marginal_region, all_found_textline_polygons_marginals, marginal_idx, page_coord, all_box_coord_marginals, slopes_marginals, counter):
|
def serialize_lines_in_marginal(self, marginal_region, all_found_textline_polygons_marginals, marginal_idx,
|
||||||
|
page_coord, all_box_coord_marginals, slopes_marginals, counter):
|
||||||
for j in range(len(all_found_textline_polygons_marginals[marginal_idx])):
|
for j in range(len(all_found_textline_polygons_marginals[marginal_idx])):
|
||||||
coords = CoordsType()
|
coords = CoordsType()
|
||||||
textline = TextLineType(id=counter.next_line_id, Coords=coords)
|
textline = TextLineType(id=counter.next_line_id, Coords=coords)
|
||||||
|
@ -67,43 +68,60 @@ class EynollahXmlWriter:
|
||||||
for l in range(len(all_found_textline_polygons_marginals[marginal_idx][j])):
|
for l in range(len(all_found_textline_polygons_marginals[marginal_idx][j])):
|
||||||
if not (self.curved_line or self.textline_light):
|
if not (self.curved_line or self.textline_light):
|
||||||
if len(all_found_textline_polygons_marginals[marginal_idx][j][l]) == 2:
|
if len(all_found_textline_polygons_marginals[marginal_idx][j][l]) == 2:
|
||||||
textline_x_coord = max(0, int((all_found_textline_polygons_marginals[marginal_idx][j][l][0] + all_box_coord_marginals[marginal_idx][2] + page_coord[2]) / self.scale_x) )
|
textline_x_coord = max(0, int((all_found_textline_polygons_marginals[marginal_idx][j][l][0] +
|
||||||
textline_y_coord = max(0, int((all_found_textline_polygons_marginals[marginal_idx][j][l][1] + all_box_coord_marginals[marginal_idx][0] + page_coord[0]) / self.scale_y) )
|
all_box_coord_marginals[marginal_idx][2] + page_coord[
|
||||||
|
2]) / self.scale_x))
|
||||||
|
textline_y_coord = max(0, int((all_found_textline_polygons_marginals[marginal_idx][j][l][1] +
|
||||||
|
all_box_coord_marginals[marginal_idx][0] + page_coord[
|
||||||
|
0]) / self.scale_y))
|
||||||
else:
|
else:
|
||||||
textline_x_coord = max(0, int((all_found_textline_polygons_marginals[marginal_idx][j][l][0][0] + all_box_coord_marginals[marginal_idx][2] + page_coord[2]) / self.scale_x) )
|
textline_x_coord = max(0, int((all_found_textline_polygons_marginals[marginal_idx][j][l][0][0] +
|
||||||
textline_y_coord = max(0, int((all_found_textline_polygons_marginals[marginal_idx][j][l][0][1] + all_box_coord_marginals[marginal_idx][0] + page_coord[0]) / self.scale_y) )
|
all_box_coord_marginals[marginal_idx][2] + page_coord[
|
||||||
|
2]) / self.scale_x))
|
||||||
|
textline_y_coord = max(0, int((all_found_textline_polygons_marginals[marginal_idx][j][l][0][1] +
|
||||||
|
all_box_coord_marginals[marginal_idx][0] + page_coord[
|
||||||
|
0]) / self.scale_y))
|
||||||
points_co += str(textline_x_coord)
|
points_co += str(textline_x_coord)
|
||||||
points_co += ','
|
points_co += ','
|
||||||
points_co += str(textline_y_coord)
|
points_co += str(textline_y_coord)
|
||||||
if (self.curved_line or self.textline_light) and np.abs(slopes_marginals[marginal_idx]) <= 45:
|
if (self.curved_line or self.textline_light) and np.abs(slopes_marginals[marginal_idx]) <= 45:
|
||||||
if len(all_found_textline_polygons_marginals[marginal_idx][j][l]) == 2:
|
if len(all_found_textline_polygons_marginals[marginal_idx][j][l]) == 2:
|
||||||
points_co += str(int((all_found_textline_polygons_marginals[marginal_idx][j][l][0] + page_coord[2]) / self.scale_x))
|
points_co += str(int((all_found_textline_polygons_marginals[marginal_idx][j][l][0] + page_coord[
|
||||||
|
2]) / self.scale_x))
|
||||||
points_co += ','
|
points_co += ','
|
||||||
points_co += str(int((all_found_textline_polygons_marginals[marginal_idx][j][l][1] + page_coord[0]) / self.scale_y))
|
points_co += str(int((all_found_textline_polygons_marginals[marginal_idx][j][l][1] + page_coord[
|
||||||
|
0]) / self.scale_y))
|
||||||
else:
|
else:
|
||||||
points_co += str(int((all_found_textline_polygons_marginals[marginal_idx][j][l][0][0] + page_coord[2]) / self.scale_x))
|
points_co += str(int((all_found_textline_polygons_marginals[marginal_idx][j][l][0][0] +
|
||||||
|
page_coord[2]) / self.scale_x))
|
||||||
points_co += ','
|
points_co += ','
|
||||||
points_co += str(int((all_found_textline_polygons_marginals[marginal_idx][j][l][0][1] + page_coord[0]) / self.scale_y))
|
points_co += str(int((all_found_textline_polygons_marginals[marginal_idx][j][l][0][1] +
|
||||||
|
page_coord[0]) / self.scale_y))
|
||||||
|
|
||||||
elif (self.curved_line or self.textline_light) and np.abs(slopes_marginals[marginal_idx]) > 45:
|
elif (self.curved_line or self.textline_light) and np.abs(slopes_marginals[marginal_idx]) > 45:
|
||||||
if len(all_found_textline_polygons_marginals[marginal_idx][j][l]) == 2:
|
if len(all_found_textline_polygons_marginals[marginal_idx][j][l]) == 2:
|
||||||
points_co += str(int((all_found_textline_polygons_marginals[marginal_idx][j][l][0] + all_box_coord_marginals[marginal_idx][2] + page_coord[2]) / self.scale_x))
|
points_co += str(int((all_found_textline_polygons_marginals[marginal_idx][j][l][0] +
|
||||||
|
all_box_coord_marginals[marginal_idx][2] + page_coord[2]) / self.scale_x))
|
||||||
points_co += ','
|
points_co += ','
|
||||||
points_co += str(int((all_found_textline_polygons_marginals[marginal_idx][j][l][1] + all_box_coord_marginals[marginal_idx][0] + page_coord[0]) / self.scale_y))
|
points_co += str(int((all_found_textline_polygons_marginals[marginal_idx][j][l][1] +
|
||||||
|
all_box_coord_marginals[marginal_idx][0] + page_coord[0]) / self.scale_y))
|
||||||
else:
|
else:
|
||||||
points_co += str(int((all_found_textline_polygons_marginals[marginal_idx][j][l][0][0] + all_box_coord_marginals[marginal_idx][2] + page_coord[2]) / self.scale_x))
|
points_co += str(int((all_found_textline_polygons_marginals[marginal_idx][j][l][0][0] +
|
||||||
|
all_box_coord_marginals[marginal_idx][2] + page_coord[2]) / self.scale_x))
|
||||||
points_co += ','
|
points_co += ','
|
||||||
points_co += str(int((all_found_textline_polygons_marginals[marginal_idx][j][l][0][1] + all_box_coord_marginals[marginal_idx][0] + page_coord[0]) / self.scale_y))
|
points_co += str(int((all_found_textline_polygons_marginals[marginal_idx][j][l][0][1] +
|
||||||
|
all_box_coord_marginals[marginal_idx][0] + page_coord[0]) / self.scale_y))
|
||||||
points_co += ' '
|
points_co += ' '
|
||||||
coords.set_points(points_co[:-1])
|
coords.set_points(points_co[:-1])
|
||||||
|
|
||||||
def serialize_lines_in_region(self, text_region, all_found_textline_polygons, region_idx, page_coord, all_box_coord, slopes, counter, ocr_all_textlines_textregion):
|
def serialize_lines_in_region(self, text_region, all_found_textline_polygons, region_idx, page_coord, all_box_coord,
|
||||||
|
slopes, counter, ocr_all_textlines_textregion):
|
||||||
self.logger.debug('enter serialize_lines_in_region')
|
self.logger.debug('enter serialize_lines_in_region')
|
||||||
for j in range(len(all_found_textline_polygons[region_idx])):
|
for j in range(len(all_found_textline_polygons[region_idx])):
|
||||||
coords = CoordsType()
|
coords = CoordsType()
|
||||||
textline = TextLineType(id=counter.next_line_id, Coords=coords)
|
textline = TextLineType(id=counter.next_line_id, Coords=coords)
|
||||||
if ocr_all_textlines_textregion:
|
if ocr_all_textlines_textregion:
|
||||||
textline.set_TextEquiv( [ TextEquivType(Unicode=ocr_all_textlines_textregion[j]) ] )
|
textline.set_TextEquiv([TextEquivType(Unicode=ocr_all_textlines_textregion[j])])
|
||||||
text_region.add_TextLine(textline)
|
text_region.add_TextLine(textline)
|
||||||
text_region.set_orientation(-slopes[region_idx])
|
text_region.set_orientation(-slopes[region_idx])
|
||||||
region_bboxes = all_box_coord[region_idx]
|
region_bboxes = all_box_coord[region_idx]
|
||||||
|
@ -111,11 +129,15 @@ class EynollahXmlWriter:
|
||||||
for idx_contour_textline, contour_textline in enumerate(all_found_textline_polygons[region_idx][j]):
|
for idx_contour_textline, contour_textline in enumerate(all_found_textline_polygons[region_idx][j]):
|
||||||
if not (self.curved_line or self.textline_light):
|
if not (self.curved_line or self.textline_light):
|
||||||
if len(contour_textline) == 2:
|
if len(contour_textline) == 2:
|
||||||
textline_x_coord = max(0, int((contour_textline[0] + region_bboxes[2] + page_coord[2]) / self.scale_x))
|
textline_x_coord = max(0, int((contour_textline[0] + region_bboxes[2] + page_coord[
|
||||||
textline_y_coord = max(0, int((contour_textline[1] + region_bboxes[0] + page_coord[0]) / self.scale_y))
|
2]) / self.scale_x))
|
||||||
|
textline_y_coord = max(0, int((contour_textline[1] + region_bboxes[0] + page_coord[
|
||||||
|
0]) / self.scale_y))
|
||||||
else:
|
else:
|
||||||
textline_x_coord = max(0, int((contour_textline[0][0] + region_bboxes[2] + page_coord[2]) / self.scale_x))
|
textline_x_coord = max(0, int((contour_textline[0][0] + region_bboxes[2] + page_coord[
|
||||||
textline_y_coord = max(0, int((contour_textline[0][1] + region_bboxes[0] + page_coord[0]) / self.scale_y))
|
2]) / self.scale_x))
|
||||||
|
textline_y_coord = max(0, int((contour_textline[0][1] + region_bboxes[0] + page_coord[
|
||||||
|
0]) / self.scale_y))
|
||||||
points_co += str(textline_x_coord)
|
points_co += str(textline_x_coord)
|
||||||
points_co += ','
|
points_co += ','
|
||||||
points_co += str(textline_y_coord)
|
points_co += str(textline_y_coord)
|
||||||
|
@ -128,26 +150,29 @@ class EynollahXmlWriter:
|
||||||
else:
|
else:
|
||||||
points_co += str(int((contour_textline[0][0] + page_coord[2]) / self.scale_x))
|
points_co += str(int((contour_textline[0][0] + page_coord[2]) / self.scale_x))
|
||||||
points_co += ','
|
points_co += ','
|
||||||
points_co += str(int((contour_textline[0][1] + page_coord[0])/self.scale_y))
|
points_co += str(int((contour_textline[0][1] + page_coord[0]) / self.scale_y))
|
||||||
elif (self.curved_line or self.textline_light) and np.abs(slopes[region_idx]) > 45:
|
elif (self.curved_line or self.textline_light) and np.abs(slopes[region_idx]) > 45:
|
||||||
if len(contour_textline)==2:
|
if len(contour_textline) == 2:
|
||||||
points_co += str(int((contour_textline[0] + region_bboxes[2] + page_coord[2])/self.scale_x))
|
points_co += str(int((contour_textline[0] + region_bboxes[2] + page_coord[2]) / self.scale_x))
|
||||||
points_co += ','
|
points_co += ','
|
||||||
points_co += str(int((contour_textline[1] + region_bboxes[0] + page_coord[0])/self.scale_y))
|
points_co += str(int((contour_textline[1] + region_bboxes[0] + page_coord[0]) / self.scale_y))
|
||||||
else:
|
else:
|
||||||
points_co += str(int((contour_textline[0][0] + region_bboxes[2]+page_coord[2])/self.scale_x))
|
points_co += str(
|
||||||
|
int((contour_textline[0][0] + region_bboxes[2] + page_coord[2]) / self.scale_x))
|
||||||
points_co += ','
|
points_co += ','
|
||||||
points_co += str(int((contour_textline[0][1] + region_bboxes[0]+page_coord[0])/self.scale_y))
|
points_co += str(
|
||||||
|
int((contour_textline[0][1] + region_bboxes[0] + page_coord[0]) / self.scale_y))
|
||||||
points_co += ' '
|
points_co += ' '
|
||||||
coords.set_points(points_co[:-1])
|
coords.set_points(points_co[:-1])
|
||||||
|
|
||||||
def serialize_lines_in_dropcapital(self, text_region, all_found_textline_polygons, region_idx, page_coord, all_box_coord, slopes, counter, ocr_all_textlines_textregion):
|
def serialize_lines_in_dropcapital(self, text_region, all_found_textline_polygons, region_idx, page_coord,
|
||||||
|
all_box_coord, slopes, counter, ocr_all_textlines_textregion):
|
||||||
self.logger.debug('enter serialize_lines_in_region')
|
self.logger.debug('enter serialize_lines_in_region')
|
||||||
for j in range(1):
|
for j in range(1):
|
||||||
coords = CoordsType()
|
coords = CoordsType()
|
||||||
textline = TextLineType(id=counter.next_line_id, Coords=coords)
|
textline = TextLineType(id=counter.next_line_id, Coords=coords)
|
||||||
if ocr_all_textlines_textregion:
|
if ocr_all_textlines_textregion:
|
||||||
textline.set_TextEquiv( [ TextEquivType(Unicode=ocr_all_textlines_textregion[j]) ] )
|
textline.set_TextEquiv([TextEquivType(Unicode=ocr_all_textlines_textregion[j])])
|
||||||
text_region.add_TextLine(textline)
|
text_region.add_TextLine(textline)
|
||||||
#region_bboxes = all_box_coord[region_idx]
|
#region_bboxes = all_box_coord[region_idx]
|
||||||
points_co = ''
|
points_co = ''
|
||||||
|
@ -159,7 +184,7 @@ class EynollahXmlWriter:
|
||||||
else:
|
else:
|
||||||
points_co += str(int((contour_textline[0][0] + page_coord[2]) / self.scale_x))
|
points_co += str(int((contour_textline[0][0] + page_coord[2]) / self.scale_x))
|
||||||
points_co += ','
|
points_co += ','
|
||||||
points_co += str(int((contour_textline[0][1] + page_coord[0])/self.scale_y))
|
points_co += str(int((contour_textline[0][1] + page_coord[0]) / self.scale_y))
|
||||||
|
|
||||||
points_co += ' '
|
points_co += ' '
|
||||||
coords.set_points(points_co[:-1])
|
coords.set_points(points_co[:-1])
|
||||||
|
@ -169,7 +194,11 @@ class EynollahXmlWriter:
|
||||||
with open(self.output_filename, 'w') as f:
|
with open(self.output_filename, 'w') as f:
|
||||||
f.write(to_xml(pcgts))
|
f.write(to_xml(pcgts))
|
||||||
|
|
||||||
def build_pagexml_no_full_layout(self, found_polygons_text_region, page_coord, order_of_texts, id_of_texts, all_found_textline_polygons, all_box_coord, found_polygons_text_region_img, found_polygons_marginals, all_found_textline_polygons_marginals, all_box_coord_marginals, slopes, slopes_marginals, cont_page, polygons_lines_to_be_written_in_xml, found_polygons_tables, ocr_all_textlines):
|
def build_pagexml_no_full_layout(self, found_polygons_text_region, page_coord, order_of_texts, id_of_texts,
|
||||||
|
all_found_textline_polygons, all_box_coord, found_polygons_text_region_img,
|
||||||
|
found_polygons_marginals, all_found_textline_polygons_marginals,
|
||||||
|
all_box_coord_marginals, slopes, slopes_marginals, cont_page,
|
||||||
|
polygons_lines_to_be_written_in_xml, found_polygons_tables, ocr_all_textlines):
|
||||||
self.logger.debug('enter build_pagexml_no_full_layout')
|
self.logger.debug('enter build_pagexml_no_full_layout')
|
||||||
|
|
||||||
# create the file structure
|
# create the file structure
|
||||||
|
@ -185,20 +214,26 @@ class EynollahXmlWriter:
|
||||||
|
|
||||||
for mm in range(len(found_polygons_text_region)):
|
for mm in range(len(found_polygons_text_region)):
|
||||||
textregion = TextRegionType(id=counter.next_region_id, type_='paragraph',
|
textregion = TextRegionType(id=counter.next_region_id, type_='paragraph',
|
||||||
Coords=CoordsType(points=self.calculate_polygon_coords(found_polygons_text_region[mm], page_coord)),
|
Coords=CoordsType(
|
||||||
|
points=self.calculate_polygon_coords(found_polygons_text_region[mm],
|
||||||
|
page_coord)),
|
||||||
)
|
)
|
||||||
page.add_TextRegion(textregion)
|
page.add_TextRegion(textregion)
|
||||||
if ocr_all_textlines:
|
if ocr_all_textlines:
|
||||||
ocr_textlines = ocr_all_textlines[mm]
|
ocr_textlines = ocr_all_textlines[mm]
|
||||||
else:
|
else:
|
||||||
ocr_textlines = None
|
ocr_textlines = None
|
||||||
self.serialize_lines_in_region(textregion, all_found_textline_polygons, mm, page_coord, all_box_coord, slopes, counter, ocr_textlines)
|
self.serialize_lines_in_region(textregion, all_found_textline_polygons, mm, page_coord, all_box_coord,
|
||||||
|
slopes, counter, ocr_textlines)
|
||||||
|
|
||||||
for mm in range(len(found_polygons_marginals)):
|
for mm in range(len(found_polygons_marginals)):
|
||||||
marginal = TextRegionType(id=counter.next_region_id, type_='marginalia',
|
marginal = TextRegionType(id=counter.next_region_id, type_='marginalia',
|
||||||
Coords=CoordsType(points=self.calculate_polygon_coords(found_polygons_marginals[mm], page_coord)))
|
Coords=CoordsType(
|
||||||
|
points=self.calculate_polygon_coords(found_polygons_marginals[mm],
|
||||||
|
page_coord)))
|
||||||
page.add_TextRegion(marginal)
|
page.add_TextRegion(marginal)
|
||||||
self.serialize_lines_in_marginal(marginal, all_found_textline_polygons_marginals, mm, page_coord, all_box_coord_marginals, slopes_marginals, counter)
|
self.serialize_lines_in_marginal(marginal, all_found_textline_polygons_marginals, mm, page_coord,
|
||||||
|
all_box_coord_marginals, slopes_marginals, counter)
|
||||||
|
|
||||||
for mm in range(len(found_polygons_text_region_img)):
|
for mm in range(len(found_polygons_text_region_img)):
|
||||||
img_region = ImageRegionType(id=counter.next_region_id, Coords=CoordsType())
|
img_region = ImageRegionType(id=counter.next_region_id, Coords=CoordsType())
|
||||||
|
@ -206,15 +241,17 @@ class EynollahXmlWriter:
|
||||||
points_co = ''
|
points_co = ''
|
||||||
for lmm in range(len(found_polygons_text_region_img[mm])):
|
for lmm in range(len(found_polygons_text_region_img[mm])):
|
||||||
try:
|
try:
|
||||||
points_co += str(int((found_polygons_text_region_img[mm][lmm,0,0] + page_coord[2]) / self.scale_x))
|
points_co += str(
|
||||||
|
int((found_polygons_text_region_img[mm][lmm, 0, 0] + page_coord[2]) / self.scale_x))
|
||||||
points_co += ','
|
points_co += ','
|
||||||
points_co += str(int((found_polygons_text_region_img[mm][lmm,0,1] + page_coord[0]) / self.scale_y))
|
points_co += str(
|
||||||
|
int((found_polygons_text_region_img[mm][lmm, 0, 1] + page_coord[0]) / self.scale_y))
|
||||||
points_co += ' '
|
points_co += ' '
|
||||||
except:
|
except:
|
||||||
|
|
||||||
points_co += str(int((found_polygons_text_region_img[mm][lmm][0] + page_coord[2])/ self.scale_x ))
|
points_co += str(int((found_polygons_text_region_img[mm][lmm][0] + page_coord[2]) / self.scale_x))
|
||||||
points_co += ','
|
points_co += ','
|
||||||
points_co += str(int((found_polygons_text_region_img[mm][lmm][1] + page_coord[0])/ self.scale_y ))
|
points_co += str(int((found_polygons_text_region_img[mm][lmm][1] + page_coord[0]) / self.scale_y))
|
||||||
points_co += ' '
|
points_co += ' '
|
||||||
|
|
||||||
img_region.get_Coords().set_points(points_co[:-1])
|
img_region.get_Coords().set_points(points_co[:-1])
|
||||||
|
@ -224,9 +261,9 @@ class EynollahXmlWriter:
|
||||||
page.add_SeparatorRegion(sep_hor)
|
page.add_SeparatorRegion(sep_hor)
|
||||||
points_co = ''
|
points_co = ''
|
||||||
for lmm in range(len(polygons_lines_to_be_written_in_xml[mm])):
|
for lmm in range(len(polygons_lines_to_be_written_in_xml[mm])):
|
||||||
points_co += str(int((polygons_lines_to_be_written_in_xml[mm][lmm,0,0] ) / self.scale_x))
|
points_co += str(int((polygons_lines_to_be_written_in_xml[mm][lmm, 0, 0]) / self.scale_x))
|
||||||
points_co += ','
|
points_co += ','
|
||||||
points_co += str(int((polygons_lines_to_be_written_in_xml[mm][lmm,0,1] ) / self.scale_y))
|
points_co += str(int((polygons_lines_to_be_written_in_xml[mm][lmm, 0, 1]) / self.scale_y))
|
||||||
points_co += ' '
|
points_co += ' '
|
||||||
sep_hor.get_Coords().set_points(points_co[:-1])
|
sep_hor.get_Coords().set_points(points_co[:-1])
|
||||||
for mm in range(len(found_polygons_tables)):
|
for mm in range(len(found_polygons_tables)):
|
||||||
|
@ -234,15 +271,21 @@ class EynollahXmlWriter:
|
||||||
page.add_TableRegion(tab_region)
|
page.add_TableRegion(tab_region)
|
||||||
points_co = ''
|
points_co = ''
|
||||||
for lmm in range(len(found_polygons_tables[mm])):
|
for lmm in range(len(found_polygons_tables[mm])):
|
||||||
points_co += str(int((found_polygons_tables[mm][lmm,0,0] + page_coord[2]) / self.scale_x))
|
points_co += str(int((found_polygons_tables[mm][lmm, 0, 0] + page_coord[2]) / self.scale_x))
|
||||||
points_co += ','
|
points_co += ','
|
||||||
points_co += str(int((found_polygons_tables[mm][lmm,0,1] + page_coord[0]) / self.scale_y))
|
points_co += str(int((found_polygons_tables[mm][lmm, 0, 1] + page_coord[0]) / self.scale_y))
|
||||||
points_co += ' '
|
points_co += ' '
|
||||||
tab_region.get_Coords().set_points(points_co[:-1])
|
tab_region.get_Coords().set_points(points_co[:-1])
|
||||||
|
|
||||||
return pcgts
|
return pcgts
|
||||||
|
|
||||||
def build_pagexml_full_layout(self, found_polygons_text_region, found_polygons_text_region_h, page_coord, order_of_texts, id_of_texts, all_found_textline_polygons, all_found_textline_polygons_h, all_box_coord, all_box_coord_h, found_polygons_text_region_img, found_polygons_tables, found_polygons_drop_capitals, found_polygons_marginals, all_found_textline_polygons_marginals, all_box_coord_marginals, slopes, slopes_h, slopes_marginals, cont_page, polygons_lines_to_be_written_in_xml, ocr_all_textlines):
|
def build_pagexml_full_layout(self, found_polygons_text_region, found_polygons_text_region_h, page_coord,
|
||||||
|
order_of_texts, id_of_texts, all_found_textline_polygons,
|
||||||
|
all_found_textline_polygons_h, all_box_coord, all_box_coord_h,
|
||||||
|
found_polygons_text_region_img, found_polygons_tables, found_polygons_drop_capitals,
|
||||||
|
found_polygons_marginals, all_found_textline_polygons_marginals,
|
||||||
|
all_box_coord_marginals, slopes, slopes_h, slopes_marginals, cont_page,
|
||||||
|
polygons_lines_to_be_written_in_xml, ocr_all_textlines):
|
||||||
self.logger.debug('enter build_pagexml_full_layout')
|
self.logger.debug('enter build_pagexml_full_layout')
|
||||||
|
|
||||||
# create the file structure
|
# create the file structure
|
||||||
|
@ -257,49 +300,63 @@ class EynollahXmlWriter:
|
||||||
|
|
||||||
for mm in range(len(found_polygons_text_region)):
|
for mm in range(len(found_polygons_text_region)):
|
||||||
textregion = TextRegionType(id=counter.next_region_id, type_='paragraph',
|
textregion = TextRegionType(id=counter.next_region_id, type_='paragraph',
|
||||||
Coords=CoordsType(points=self.calculate_polygon_coords(found_polygons_text_region[mm], page_coord)))
|
Coords=CoordsType(
|
||||||
|
points=self.calculate_polygon_coords(found_polygons_text_region[mm],
|
||||||
|
page_coord)))
|
||||||
page.add_TextRegion(textregion)
|
page.add_TextRegion(textregion)
|
||||||
|
|
||||||
if ocr_all_textlines:
|
if ocr_all_textlines:
|
||||||
ocr_textlines = ocr_all_textlines[mm]
|
ocr_textlines = ocr_all_textlines[mm]
|
||||||
else:
|
else:
|
||||||
ocr_textlines = None
|
ocr_textlines = None
|
||||||
self.serialize_lines_in_region(textregion, all_found_textline_polygons, mm, page_coord, all_box_coord, slopes, counter, ocr_textlines)
|
self.serialize_lines_in_region(textregion, all_found_textline_polygons, mm, page_coord, all_box_coord,
|
||||||
|
slopes, counter, ocr_textlines)
|
||||||
|
|
||||||
self.logger.debug('len(found_polygons_text_region_h) %s', len(found_polygons_text_region_h))
|
self.logger.debug('len(found_polygons_text_region_h) %s', len(found_polygons_text_region_h))
|
||||||
for mm in range(len(found_polygons_text_region_h)):
|
for mm in range(len(found_polygons_text_region_h)):
|
||||||
textregion = TextRegionType(id=counter.next_region_id, type_='header',
|
textregion = TextRegionType(id=counter.next_region_id, type_='header',
|
||||||
Coords=CoordsType(points=self.calculate_polygon_coords(found_polygons_text_region_h[mm], page_coord)))
|
Coords=CoordsType(
|
||||||
|
points=self.calculate_polygon_coords(found_polygons_text_region_h[mm],
|
||||||
|
page_coord)))
|
||||||
page.add_TextRegion(textregion)
|
page.add_TextRegion(textregion)
|
||||||
|
|
||||||
if ocr_all_textlines:
|
if ocr_all_textlines:
|
||||||
ocr_textlines = ocr_all_textlines[mm]
|
ocr_textlines = ocr_all_textlines[mm]
|
||||||
else:
|
else:
|
||||||
ocr_textlines = None
|
ocr_textlines = None
|
||||||
self.serialize_lines_in_region(textregion, all_found_textline_polygons_h, mm, page_coord, all_box_coord_h, slopes_h, counter, ocr_textlines)
|
self.serialize_lines_in_region(textregion, all_found_textline_polygons_h, mm, page_coord, all_box_coord_h,
|
||||||
|
slopes_h, counter, ocr_textlines)
|
||||||
|
|
||||||
for mm in range(len(found_polygons_marginals)):
|
for mm in range(len(found_polygons_marginals)):
|
||||||
marginal = TextRegionType(id=counter.next_region_id, type_='marginalia',
|
marginal = TextRegionType(id=counter.next_region_id, type_='marginalia',
|
||||||
Coords=CoordsType(points=self.calculate_polygon_coords(found_polygons_marginals[mm], page_coord)))
|
Coords=CoordsType(
|
||||||
|
points=self.calculate_polygon_coords(found_polygons_marginals[mm],
|
||||||
|
page_coord)))
|
||||||
page.add_TextRegion(marginal)
|
page.add_TextRegion(marginal)
|
||||||
self.serialize_lines_in_marginal(marginal, all_found_textline_polygons_marginals, mm, page_coord, all_box_coord_marginals, slopes_marginals, counter)
|
self.serialize_lines_in_marginal(marginal, all_found_textline_polygons_marginals, mm, page_coord,
|
||||||
|
all_box_coord_marginals, slopes_marginals, counter)
|
||||||
|
|
||||||
for mm in range(len(found_polygons_drop_capitals)):
|
for mm in range(len(found_polygons_drop_capitals)):
|
||||||
dropcapital = TextRegionType(id=counter.next_region_id, type_='drop-capital',
|
dropcapital = TextRegionType(id=counter.next_region_id, type_='drop-capital',
|
||||||
Coords=CoordsType(points=self.calculate_polygon_coords(found_polygons_drop_capitals[mm], page_coord)))
|
Coords=CoordsType(
|
||||||
|
points=self.calculate_polygon_coords(found_polygons_drop_capitals[mm],
|
||||||
|
page_coord)))
|
||||||
page.add_TextRegion(dropcapital)
|
page.add_TextRegion(dropcapital)
|
||||||
###all_box_coord_drop = None
|
###all_box_coord_drop = None
|
||||||
###slopes_drop = None
|
###slopes_drop = None
|
||||||
###self.serialize_lines_in_dropcapital(dropcapital, [found_polygons_drop_capitals[mm]], mm, page_coord, all_box_coord_drop, slopes_drop, counter, ocr_all_textlines_textregion=None)
|
###self.serialize_lines_in_dropcapital(dropcapital, [found_polygons_drop_capitals[mm]], mm, page_coord, all_box_coord_drop, slopes_drop, counter, ocr_all_textlines_textregion=None)
|
||||||
|
|
||||||
for mm in range(len(found_polygons_text_region_img)):
|
for mm in range(len(found_polygons_text_region_img)):
|
||||||
page.add_ImageRegion(ImageRegionType(id=counter.next_region_id, Coords=CoordsType(points=self.calculate_polygon_coords(found_polygons_text_region_img[mm], page_coord))))
|
page.add_ImageRegion(ImageRegionType(id=counter.next_region_id, Coords=CoordsType(
|
||||||
|
points=self.calculate_polygon_coords(found_polygons_text_region_img[mm], page_coord))))
|
||||||
|
|
||||||
for mm in range(len(polygons_lines_to_be_written_in_xml)):
|
for mm in range(len(polygons_lines_to_be_written_in_xml)):
|
||||||
page.add_SeparatorRegion(ImageRegionType(id=counter.next_region_id, Coords=CoordsType(points=self.calculate_polygon_coords(polygons_lines_to_be_written_in_xml[mm], [0 , 0, 0, 0]))))
|
page.add_SeparatorRegion(ImageRegionType(id=counter.next_region_id, Coords=CoordsType(
|
||||||
|
points=self.calculate_polygon_coords(polygons_lines_to_be_written_in_xml[mm], [0, 0, 0, 0]))))
|
||||||
|
|
||||||
for mm in range(len(found_polygons_tables)):
|
for mm in range(len(found_polygons_tables)):
|
||||||
page.add_TableRegion(TableRegionType(id=counter.next_region_id, Coords=CoordsType(points=self.calculate_polygon_coords(found_polygons_tables[mm], page_coord))))
|
page.add_TableRegion(TableRegionType(id=counter.next_region_id, Coords=CoordsType(
|
||||||
|
points=self.calculate_polygon_coords(found_polygons_tables[mm], page_coord))))
|
||||||
|
|
||||||
return pcgts
|
return pcgts
|
||||||
|
|
||||||
|
@ -315,6 +372,5 @@ class EynollahXmlWriter:
|
||||||
coords += str(int((value_bbox[0][0] + page_coord[2]) / self.scale_x))
|
coords += str(int((value_bbox[0][0] + page_coord[2]) / self.scale_x))
|
||||||
coords += ','
|
coords += ','
|
||||||
coords += str(int((value_bbox[0][1] + page_coord[0]) / self.scale_y))
|
coords += str(int((value_bbox[0][1] + page_coord[0]) / self.scale_y))
|
||||||
coords=coords + ' '
|
coords = coords + ' '
|
||||||
return coords[:-1]
|
return coords[:-1]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue