Skip to content

Commit

Permalink
Merge pull request kaldi-asr#15 from aarora8/madcat_ar_2
Browse files Browse the repository at this point in the history
Madcat ar 2
  • Loading branch information
aarora8 authored May 15, 2018
2 parents cda2394 + 40617b7 commit 32c5a2c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 35 deletions.
40 changes: 8 additions & 32 deletions egs/madcat_ar/v1/local/create_line_image_from_page_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from math import atan2, cos, sin, pi, degrees, sqrt
from collections import namedtuple

from scipy.spatial import ConvexHull
from PIL import Image
from scipy.misc import toimage
import logging
Expand Down Expand Up @@ -262,7 +263,10 @@ def minimum_bounding_box(points):
"""

if len(points) <= 2: raise ValueError('More than two points required.')
hull_ordered = compute_hull(points)

hull_ordered = [points[index] for index in ConvexHull(points).vertices]
hull_ordered.append(hull_ordered[0])
#hull_ordered = compute_hull(points)
hull_ordered = tuple(hull_ordered)

min_rectangle = bounding_area(0, hull_ordered)
Expand Down Expand Up @@ -406,24 +410,9 @@ def set_line_image_data(image, line_id, image_file_name, image_fh):
image_fh.write(image_path + '\n')


def paste_line_image_on_blank_image(im, line_image, height_offset, width_offset):
""" Given line image and its location on page image, return a page image
with only line image in ti.
It is done to remove noise that can occur due to MAR.
:return:
--------
image: page image containing only line image
"""
image_width= im.size[0]
image_height = im.size[1]
stitched_image = Image.new('L', (image_width, image_height), "white")
stitched_image.paste(im=line_image, box=(width_offset, height_offset))
return stitched_image


def get_line_images_from_page_image(image_file_name, madcat_file_path, image_fh):
""" Given a page image, extracts the line images from it.
Input
Inout
-----
image_file_name (string): complete path and name of the page image.
madcat_file_path (string): complete path and name of the madcat xml file
Expand All @@ -433,22 +422,8 @@ def get_line_images_from_page_image(image_file_name, madcat_file_path, image_fh)
im = pad_image(im_wo_pad)
doc = minidom.parse(madcat_file_path)
zone = doc.getElementsByTagName('zone')
offset = int(args.padding // 2)
for node in zone:
id = node.getAttribute('id')
line_point = node.getElementsByTagName('point')
col, row = [], []
max_col, max_row, min_col, min_row = 0, 0, 0, 0
for line_node in line_point:
col.append(int(line_node.getAttribute('x')) + offset)
row.append(int(line_node.getAttribute('y')) + offset)
max_col, max_row = max(col), max(row)
min_col, min_row = min(col), min(row)

box = (min_col, min_row, max_col, max_row)
region = im.crop(box)
image = paste_line_image_on_blank_image(im, region, min_row, min_col)

token_image = node.getElementsByTagName('token-image')
minimum_bounding_box_input = []
for token_node in token_image:
Expand All @@ -469,7 +444,7 @@ def get_line_images_from_page_image(image_file_name, madcat_file_path, image_fh)
max_x = int(max(x1, x2, x3, x4))
max_y = int(max(y1, y2, y3, y4))
box = (min_x, min_y, max_x, max_y)
region_initial = image.crop(box)
region_initial = im.crop(box)
rot_points = []
p1_new = (x1 - min_x, y1 - min_y)
p2_new = (x2 - min_x, y2 - min_y)
Expand Down Expand Up @@ -603,3 +578,4 @@ def main():

if __name__ == '__main__':
main()

2 changes: 1 addition & 1 deletion egs/madcat_ar/v1/local/extract_features.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ utils/split_scp.pl $scp $split_scps || exit 1;

$cmd JOB=1:$nj $logdir/extract_features.JOB.log \
local/make_features.py $logdir/images.JOB.scp \
--allowed_len_file_path data/train \
--allowed_len_file_path $data/allowed_lengths.txt \
--feat-dim $feat_dim \| \
copy-feats --compress=true --compression-method=7 \
ark:- ark,scp:$featdir/images.JOB.ark,$featdir/images.JOB.scp
Expand Down
2 changes: 1 addition & 1 deletion egs/madcat_ar/v1/local/make_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def horizontal_pad(im, allowed_lengths = None):
out_fh = open(args.out_ark,'wb')

allowed_lengths = None
allowed_len_handle = '/'.join(filter(None, (args.allowed_len_file_path, 'allowed_lengths.txt')))
allowed_len_handle = args.allowed_len_file_path
if os.path.isfile(allowed_len_handle):
print("Found 'allowed_lengths.txt' file...", file=sys.stderr)
allowed_lengths = []
Expand Down
2 changes: 1 addition & 1 deletion egs/madcat_ar/v1/local/wer_output_filter
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ binmode STDERR, ":utf8";
# Arabic-specific normalization
while (<>) {
@F = split " ";
print $F[0] . " ";
print $F[0] . " ";
foreach $s (@F[1..$#F]) {
$s =~ s/\x{0623}/\x{0627}/g;
$s =~ s/\x{0625}/\x{0627}/g;
Expand Down

0 comments on commit 32c5a2c

Please sign in to comment.