-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtruth_meicreate.py
261 lines (213 loc) · 8.6 KB
/
gtruth_meicreate.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
'''
For creation of mei file containing information provided by users of the Ground
Truth system for the barline finder.
Based on meicreate.py by Gregory Burlet.
by Nicholas Esterer, March 2013.
'''
from __future__ import division
import argparse
from pyparsing import nestedExpr
import os
import re
import sys
import datetime
from pymei import MeiDocument, MeiElement, XmlExport
from gtruthrect import *
class GroundTruthBarlineDataConverter:
'''
Convert the stored measures of the Ground Truth System to MEI.
TODO: Only considers barpanels, not staffpanels
'''
def __init__(self, staffbb, barbb, verbose=False):
'''
Constructor of converter.
staffbb are the boxes bouding the staves (or systems)
barbb are the boxes bounding the measures (or bars)
These are passed in as lists of Rects defined in gtruth-rect
'''
# Print errors / messages
self.verbose = verbose
# The coordinates of the staves stored in the format:
# [index, top-corner x, top-corner y, bottom-corner x, bottom-corner y]
# (note this is a python list)
# To get an idea of what the staves are, image a piano score where each
# system consists (usually) of a treble staff and bass staff. This
# system is divided into measures and you can divide the measure into
# two parts: the box encompassing the top staff within the measure and
# the box encompassing the bottom staff within the measure. These two
# boxes are what is meant by staff bounding boxes.
self.staffbb = staffbb
if self.verbose:
sys.stderr.write("Staff bounding boxes" + repr(self.staffbb))
# The coordinates of the bars stored in the format:
# (index, top-corner x, top-corner y, bottom-corner x, bottom-corner y)
# (note this is a python tuple)
# The differentiation is for compatibility with meicreate.py
# Bars are the measures of the system. That means for an orchestral
# score the bars can be very tall and thin.
self.barbb = barbb
if self.verbose:
sys.stderr.write("Bar bounding boxes" + repr(self.barbb))
self.meidoc = None;
def bardata_to_mei(self, imagepath, imagewidth, imageheight, imagedpi=72):
'''
Perform the data conversion to MEI
'''
self.meidoc = MeiDocument()
mei = MeiElement('mei')
self.meidoc.setRootElement(mei)
###########################
# MetaData #
###########################
mei_head = self._create_header()
mei.addChild(mei_head)
###########################
# Body #
###########################
music = MeiElement('music')
body = MeiElement('body')
mdiv = MeiElement('mdiv')
score = MeiElement('score')
score_def = MeiElement('scoreDef')
section = MeiElement('section')
# physical location data
facsimile = MeiElement('facsimile')
surface = MeiElement('surface')
graphic = self._create_graphic(imagepath, imagewidth, imageheight,\
imagedpi)
surface.addChild(graphic)
mei.addChild(music)
music.addChild(facsimile)
facsimile.addChild(surface)
music.addChild(body)
body.addChild(mdiv)
mdiv.addChild(score)
score.addChild(score_def)
score.addChild(section)
barmeasdict = dict()
# Only add the bar bounding boxes
for bar in self.barbb:
# Zone is the coordinates where the measure is found in the image
zone = self._create_zone(\
int(bar.pos[0]),int(bar.pos[1]),\
int(bar.pos[0])+int(bar.size[0]),\
int(bar.pos[1])+int(bar.size[1]));
# Zone is a child element of the surface
surface.addChild(zone)
# The measure is found in the zone
measure = self._create_measure(bar.number,zone);
section.addChild(measure);
# store which measure this bar corresponds to so we can add it as a
# child to the staff bounding boxes later TODO: We don't do this
# right now
barmeasdict[bar] = measure
# Add the staff bounding boxes
# We don't do this but when we do, the staff needs to look up its
# children in barmeasdict
def _create_header(self, rodan_version='0.1'):
'''
Create a meiHead element
'''
mei_head = MeiElement('meiHead')
today = datetime.date.today().isoformat()
app_name = 'gtruth_write_mei'
# file description
file_desc = MeiElement('fileDesc')
title_stmt = MeiElement('titleStmt')
title = MeiElement('title')
resp_stmt = MeiElement('respStmt')
corp_name = MeiElement('corpName')
corp_name.setValue('Distributed Digital Music Archives and Libraries Lab (DDMAL)')
title_stmt.addChild(title)
title_stmt.addChild(resp_stmt)
resp_stmt.addChild(corp_name)
pub_stmt = MeiElement('pubStmt')
resp_stmt = MeiElement('respStmt')
corp_name = MeiElement('corpName')
corp_name.setValue('Distributed Digital Music Archives and Libraries Lab (DDMAL)')
pub_stmt.addChild(resp_stmt)
resp_stmt.addChild(corp_name)
mei_head.addChild(file_desc)
file_desc.addChild(title_stmt)
file_desc.addChild(pub_stmt)
# encoding description
encoding_desc = MeiElement('encodingDesc')
app_info = MeiElement('appInfo')
application = MeiElement('application')
application.addAttribute('version', rodan_version)
name = MeiElement('name')
name.setValue(app_name)
ptr = MeiElement('ptr')
ptr.addAttribute('target', 'https://github.com/DDMAL/barlineFinder')
mei_head.addChild(encoding_desc)
encoding_desc.addChild(app_info)
app_info.addChild(application)
application.addChild(name)
application.addChild(ptr)
# revision description
revision_desc = MeiElement('revisionDesc')
change = MeiElement('change')
change.addAttribute('n', '1')
resp_stmt = MeiElement('respStmt')
corp_name = MeiElement('corpName')
corp_name.setValue('Distributed Digital Music Archives and Libraries Lab (DDMAL)')
change_desc = MeiElement('changeDesc')
ref = MeiElement('ref')
ref.addAttribute('target', '#'+application.getId())
ref.setValue(app_name)
ref.setTail('.')
p = MeiElement('p')
p.addChild(ref)
p.setValue('Encoded using ')
date = MeiElement('date')
date.setValue(today)
mei_head.addChild(revision_desc)
revision_desc.addChild(change)
change.addChild(resp_stmt)
resp_stmt.addChild(corp_name)
change.addChild(change_desc)
change_desc.addChild(p)
change.addChild(date)
return mei_head
def _create_graphic(self, image_path, image_width, image_height, image_dpi):
'''
Create a graphic element.
'''
graphic = MeiElement('graphic')
graphic.addAttribute('height', str(image_height))
graphic.addAttribute('width', str(image_width))
graphic.addAttribute('target', image_path)
graphic.addAttribute('resolution', str(image_dpi))
graphic.addAttribute('unit', 'px')
return graphic
def _create_measure(self, n, zone = None):
'''
Create a measure element and attach a zone reference to it.
The zone element is optional, since the zone of the measure is
calculated once all of the staves within a measure have been added
to the MEI.
'''
measure = MeiElement('measure')
measure.addAttribute('n', str(n))
if zone is not None:
measure.addAttribute('facs', '#'+zone.getId())
return measure
def _create_zone(self, ulx, uly, lrx, lry):
'''
Create a zone element
'''
zone = MeiElement('zone')
zone.addAttribute('ulx', str(ulx))
zone.addAttribute('uly', str(uly))
zone.addAttribute('lrx', str(lrx))
zone.addAttribute('lry', str(lry))
return zone
def output_mei(self, output_path):
'''
Write the generated mei to disk
'''
# output mei file
if self.meidoc == None:
raise Warning('The MEI document has not yet been created');
return
XmlExport.meiDocumentToFile(self.meidoc, output_path)