-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.py
77 lines (61 loc) · 2.04 KB
/
4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
# local/process_data.py data/local
import argparse
import os
import xml.dom.minidom as minidom
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
import numpy as np
parser = argparse.ArgumentParser(description="""Creates text, utt2spk
and images.scp files.""")
parser.add_argument('database_path', type=str,
help='Path to the downloaded (and extracted) IAM data')
args = parser.parse_args()
xml_file_loc = os.path.join(args.database_path,
'madcat_sample')
im = Image.open('/Users/ashisharora/madcat_sample/CNS_CMN_20070126.0303_002_LDC0523.tif')
# Create figure and axes
fig,ax = plt.subplots(1)
ax.imshow(im)
print("Processing data...")
xml_path = os.path.join(xml_file_loc , 'CNS_CMN_20070126.0303_002_LDC0523.gedi' + '.xml')
doc = minidom.parse(xml_path)
DL_ZONE = doc.getElementsByTagName('DL_ZONE')
previous_id="-1"
start="true"
region_list = list()
for node in DL_ZONE:
id=node.getAttribute('lineID')
if id=="":
continue
if previous_id != id and start=="false":
print(previous_id, id)
break
col=node.getAttribute('col')
row=node.getAttribute('row')
width=node.getAttribute('width')
height=node.getAttribute('height')
col_down = int(col)+int(width)
row_right = int(row)+int(height)
box = (int(col), int(row), col_down, row_right)
region = im.crop(box) # cropping a box
rect1 = patches.Rectangle((col, row), width, height, linewidth=1, edgecolor='w', facecolor='none')
ax.add_patch(rect1)
region_list.append(region)
previous_id = id
start="false"
result_width=0
result_height=-1
for x in region_list:
(width, height) = x.size
result_width += width
result_height = max(result_height, height)
result = Image.new('RGB', (result_width, result_height))
box_width=0
for x in region_list:
result.paste(im=x, box=(box_width,0))
(width, height) = x.size
box_width = box_width + width
result.show()
plt.show()