Mercurial > hg > multiomr
comparison Automatism/BatchOMR.py @ 2:46fb79167a61 tip
Main Code
author | Victor Padilla <victor.padilla.mc@gmail.com> |
---|---|
date | Mon, 04 May 2015 22:56:18 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1:0f7f611deca4 | 2:46fb79167a61 |
---|---|
1 ''' | |
2 Created on 10/11/2014 | |
3 | |
4 @organization: Lancaster University & University of Leeds | |
5 @version: 1.0 | |
6 Created on 11/12/2014 | |
7 | |
8 @author: Victor Padilla | |
9 @contact: v.padilla@lancaster.ac.uk | |
10 | |
11 Functions related to SIKULI automatism | |
12 ''' | |
13 import logging | |
14 import os | |
15 import sys | |
16 from subprocess import Popen | |
17 import shutil | |
18 from music21 import converter | |
19 from Functions import FilesFunctions | |
20 | |
21 | |
22 | |
23 class BatchOMR: | |
24 ''' | |
25 Class for the SIKULI process | |
26 ''' | |
27 ################################################## | |
28 # @param idOMR It can be "SE" "SS" "CP" "PS" "ALL" | |
29 # @param dirName folder where the images are and .xml will be | |
30 # @param files array with tiff files | |
31 def Batch(self,idOMR,dirName,files): | |
32 ''' | |
33 Runs the SIKULI process. | |
34 - idOMR: SE, SS, CP, PS | |
35 - if exists, skip | |
36 - it takes all the .tif files and convert them to SE.xml, SS.xml, CP.xml or PS.xml | |
37 - it waits until the process is finished | |
38 | |
39 usage: | |
40 ff=FilesFunctions() | |
41 dirName="C:\\Users\\victor\\Desktop\\data" | |
42 files= ff.getAllImgFiles(dirName) | |
43 batchOMR=BatchOMR() | |
44 batchOMR.Batch("PS",dirName,files) | |
45 ''' | |
46 | |
47 print "Processing...",dirName+"/"+idOMR+".xml" | |
48 if os.path.exists(dirName+"/"+idOMR+".xml"): | |
49 print "....Completed ",dirName+"/"+idOMR+".xml" | |
50 return | |
51 rootApp=os.path.dirname(sys.argv[0]) | |
52 rootApp=rootApp.replace("\\","/") | |
53 print rootApp | |
54 strFiles="" | |
55 for f in files: | |
56 strFiles=strFiles+" \""+f+"\"" | |
57 print dirName,strFiles | |
58 | |
59 dirName=dirName.replace("\\","/") | |
60 dirName="\""+dirName+"/\"" | |
61 | |
62 cwd=rootApp+"/sikuli1.0.1/runScript.cmd -r "+rootApp+"/sikuli/"+idOMR+".sikuli --args " | |
63 cwd=cwd+dirName+strFiles | |
64 print cwd | |
65 p = Popen(cwd) | |
66 p.wait() | |
67 p.communicate() | |
68 print "....Completed ",dirName+"/"+idOMR+".xml" | |
69 | |
70 | |
71 ############################################### | |
72 # @param rootDir root process folder | |
73 def cleanXMLFiles(self,rootDir): | |
74 ''' | |
75 Removes all the .xml files and .mro files in the tree folder | |
76 It is used for testing mainly | |
77 | |
78 usage: | |
79 | |
80 batchOMR=BatchOMR() | |
81 batchOMR.cleanXMLFiles("C:\\Users\\victor\\Desktop\\data") | |
82 ''' | |
83 for outWalk in os.walk(rootDir): | |
84 dirName=outWalk[0] | |
85 fileList=outWalk[2] | |
86 for myfile in fileList: | |
87 if myfile.endswith('.xml'): | |
88 os.remove(dirName+"/"+myfile) | |
89 print myfile | |
90 if myfile.endswith('.mro'): | |
91 os.remove(dirName+"/"+myfile) | |
92 print myfile | |
93 | |
94 | |
95 ############################################### | |
96 # @param rootDir root process folder | |
97 # @param idOMR can be "PS" "CP" "SE" "SS" "ALL" | |
98 def processAllTiffFiles(self,rootDir,idOMR): | |
99 ''' | |
100 Runs the complete process for the tree folder | |
101 idOMR can be: | |
102 PS | |
103 CP | |
104 SE | |
105 SS | |
106 ALL (run all the OMRs). Capella is removed for many problems associated | |
107 | |
108 usage: | |
109 | |
110 batchOMR=BatchOMR() | |
111 batchOMR.processAllTiffFiles("C:\\Users\\victor\\Desktop\\data","SE") | |
112 ''' | |
113 print rootDir | |
114 logging.basicConfig(filename=rootDir+'\\BigDataLogs.log',format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') | |
115 for outWalk in os.walk(rootDir): | |
116 dirName=outWalk[0] | |
117 print('Found directory: %s' % dirName) | |
118 ff=FilesFunctions() | |
119 files= ff.getAllImgFiles(dirName) | |
120 if len(files)>0: | |
121 logging.warning(idOMR+" "+dirName) | |
122 if idOMR=="PS": | |
123 self.Batch("PS",dirName,files) | |
124 if idOMR=="SE": | |
125 self.Batch("SE",dirName,files) | |
126 if idOMR=="SS": | |
127 self.Batch("SS",dirName,files) | |
128 if idOMR=="CP": | |
129 self.Batch("CP",dirName,files) | |
130 if idOMR=="ALL": | |
131 # logging.warning("CP: "+dirName) | |
132 # Batch("CP",dirName,files) | |
133 logging.warning("PS: "+dirName) | |
134 self.Batch("PS",dirName,files) | |
135 logging.warning("SE: "+dirName) | |
136 self.Batch("SE",dirName,files) | |
137 logging.warning("SS: "+dirName) | |
138 self.Batch("SS",dirName,files) | |
139 | |
140 | |
141 ############################################### | |
142 # @param rootDir root process folder | |
143 def setupApp(self,rootDir): | |
144 ''' | |
145 Configures the main folder to process the data | |
146 Creates the "Process" close to "OMRS" folder and copy the .xml files. | |
147 - the parts files in "part/XML" folder with the structure: | |
148 - idpart.version.omrId.xml | |
149 - the full scores in fullScore/XML" folder with the structure: | |
150 - FS.version.omrId.xml | |
151 usage: | |
152 batchOMR=BatchOMR() | |
153 batchOMR.setupApp("C:\\Users\\victor\\Desktop\\data") | |
154 ''' | |
155 ff=FilesFunctions() | |
156 ################ | |
157 # FOR PARTS #### | |
158 ################ | |
159 for outWalk in os.walk(rootDir): | |
160 dirName=outWalk[0] | |
161 subdirList=outWalk[1] | |
162 if "OMRS" in subdirList: | |
163 rootScore=dirName | |
164 if(dirName.upper().find("\\OMRS\\")!=-1 and dirName.upper().find("\\XML")!=-1 and dirName.upper().find("\\PARTS")!=-1): | |
165 arrDir=dirName.split("\\") | |
166 idpart=arrDir[len(arrDir)-2] | |
167 version=arrDir[len(arrDir)-3] | |
168 idmovement=arrDir[len(arrDir)-5] | |
169 files= ff.getAllXMLFiles(dirName) | |
170 myfolder=rootScore+"\\Process\\"+idmovement+"\\parts\\"+idpart+"\\XML\\" | |
171 if not os.path.exists(myfolder): | |
172 os.makedirs(myfolder) | |
173 for f in files: | |
174 if not os.path.isfile(myfolder+idpart+"."+version+"."+f): | |
175 shutil.copy2(dirName+"\\"+f, myfolder+idpart+"."+version+"."+f) | |
176 | |
177 ########################### | |
178 # FOR FULL SCORE ########## | |
179 ########################### | |
180 for outWalk in os.walk(rootDir): | |
181 dirName=outWalk[0] | |
182 subdirList=outWalk[1] | |
183 if "OMRS" in subdirList: | |
184 rootScore=dirName | |
185 if(dirName.upper().find("\\OMRS\\")!=-1 and dirName.upper().find("\\XML")!=-1 and dirName.upper().find("\\FULLSCORE")!=-1): | |
186 arrDir=dirName.split("\\") | |
187 version=arrDir[len(arrDir)-2] | |
188 idmovement=arrDir[len(arrDir)-4] | |
189 files= ff.getAllXMLFiles(dirName) | |
190 myfolder=rootScore+"\\Process\\"+idmovement+"\\fullScore\\XML\\" | |
191 for f in files: | |
192 if not os.path.exists(myfolder): | |
193 os.makedirs(myfolder) | |
194 if not os.path.isfile(myfolder+"FS."+version+"."+f): | |
195 shutil.copy2(dirName+"\\"+f, myfolder+"FS."+version+"."+f) | |
196 | |
197 | |
198 | |
199 | |
200 | |
201 #################################### | |
202 # @param rootDir root process folder | |
203 def setGround(self,rootDir): | |
204 ''' | |
205 Process kern files in the OMRS folder (.krn) and set them as a "ground.xml" | |
206 in "Process" folder | |
207 | |
208 usage: | |
209 batchOMR=BatchOMR() | |
210 batchOMR.setGround("C:\\Users\\victor\\Desktop\\data") | |
211 ''' | |
212 for outWalk in os.walk(rootDir): | |
213 dirName=outWalk[0] | |
214 ff=FilesFunctions() | |
215 kernFile=ff.getKernFile(dirName) | |
216 newpath=dirName.replace("\\OMRS\\","\\Process\\") | |
217 | |
218 if not os.path.isfile(newpath+'\\ground.xml'): | |
219 if kernFile!=None: | |
220 | |
221 print dirName+"\\"+kernFile | |
222 xmlFile=ff.getXMLFile(dirName) | |
223 | |
224 #if we have a .xml file, we copy it | |
225 if xmlFile!=None: | |
226 shutil.copyfile(dirName+"\\"+xmlFile,newpath+"\\ground.xml") | |
227 else: | |
228 omr=converter.parse(dirName+"\\"+kernFile) | |
229 omr.write("musicxml", newpath+'\\ground.xml') | |
230 |