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