annotate MainMultiOMR.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
rev   line source
victor@2 1 '''
victor@2 2 @organization: Lancaster University & University of Leeds
victor@2 3 @version: 1.0
victor@2 4 Created on 11/12/2014
victor@2 5
victor@2 6 @author: Victor Padilla
victor@2 7 @contact: v.padilla@lancaster.ac.uk
victor@2 8
victor@2 9 The application can be run through command line (without windows interface) using
victor@2 10 MainMultiOMR library
victor@2 11
victor@2 12 '''
victor@2 13 import logging
victor@2 14 import time
victor@2 15 import os
victor@2 16 import gc
victor@2 17 import sys
victor@2 18 from subprocess import Popen
victor@2 19 from music21 import stream
victor@2 20 from music21 import converter
victor@2 21 from Process import PipelineAlignment
victor@2 22 from Process import Voting
victor@2 23 from Process import SymbolConversion
victor@2 24 from Process import FullScoreAlignment
victor@2 25 from Automatism import BatchOMR
victor@2 26 from Result import ProcessGroundS2
victor@2 27 from Result import ExcellData
victor@2 28 from Functions import FilesFunctions
victor@2 29 from Functions import MeasureFunctions
victor@2 30 from Alignment import NWunsch
victor@2 31 import shutil
victor@2 32
victor@2 33 from Preprocessing import ossia
victor@2 34 from Preprocessing import movements
victor@2 35 from Preprocessing import preomr
victor@2 36 import PythonMagick
victor@2 37 import PyPDF2
victor@2 38
victor@2 39 class MainMultiOMR:
victor@2 40 '''
victor@2 41 main functions to run the Big Data Process.
victor@2 42 They can be called without interface, from another program, for example
victor@2 43 ....
victor@2 44 # instance
victor@2 45 mmOMR=MainMultiOMR()
victor@2 46
victor@2 47 # run the Big Data process once
victor@2 48 mmOMR.runBigData("C:\\Users\\victor\\Desktop\\data")
victor@2 49
victor@2 50 # run the Big Data waiting for changes
victor@2 51 mmOMR.runDummyBigData("C:\\Users\\victor\\Desktop\\data")
victor@2 52
victor@2 53 # run the Big Data Ground process once to get the results
victor@2 54 mmOMR.runBigDataGroung("C:\\Users\\victor\\Desktop\\data")
victor@2 55
victor@2 56 # run the Big Data Ground waiting for changes
victor@2 57 mmOMR.runDummyBigData("C:\\Users\\victor\\Desktop\\data")
victor@2 58 '''
victor@2 59
victor@2 60 def _loadNWunsch(self):
victor@2 61 '''
victor@2 62 Load Needlemann-Wunsch algorith library by default
victor@2 63 '''
victor@2 64 a=["foo00"]
victor@2 65 b=["foo000"]
victor@2 66 NWunsch.NWunsch_getSimilarity(a,b)
victor@2 67 #print "Default NWunsch......"
victor@2 68
victor@2 69
victor@2 70 def processPDF2TIFF(self,filename):
victor@2 71 pdf_im = PyPDF2.PdfFileReader(file(filename, "rb"))
victor@2 72 npage = pdf_im.getNumPages()
victor@2 73 print('Converting %d pages.' % npage)
victor@2 74 intMovement=0
victor@2 75 for p in range(npage):
victor@2 76 im = PythonMagick.Image()
victor@2 77 im.density('300')
victor@2 78 print filename
victor@2 79 im.read(str(filename)+'[' + str(p) +']')
victor@2 80 strOut=str(filename) + str(p)
victor@2 81 im.write(strOut+ '.tif')
victor@2 82
victor@2 83 infile=strOut+ '.tif'
victor@2 84 po = preomr.PreOMR(infile)
victor@2 85 rv = po.split_movements(strOut+'_A.tif', strOut+'_B.tif')
victor@2 86 if rv != None:
victor@2 87 print("new movement was detected at system %d" % (rv,))
victor@2 88
victor@2 89 arrFile=filename.split("\\")
victor@2 90 myFile=arrFile.pop()
victor@2 91 path='\\'.join(arrFile)
victor@2 92 print "**********************",path
victor@2 93 if rv==0:
victor@2 94 intMovement+=1
victor@2 95 dirMovement=path+"\\m"+str(intMovement)
victor@2 96 os.makedirs(dirMovement)
victor@2 97 shutil.copy(strOut+ '.tif', dirMovement+"\\page_"+str(p)+".tif")
victor@2 98 else:
victor@2 99 dirMovement=path+"\\m"+str(intMovement)
victor@2 100 shutil.copy(strOut+ '_A.tif', dirMovement+"\\page_"+str(p)+".tif")
victor@2 101 intMovement+=1
victor@2 102 dirMovement=path+"\\m"+str(intMovement)
victor@2 103 os.makedirs(dirMovement)
victor@2 104 shutil.copy(strOut+ '_B.tif', dirMovement+"\\page_"+str(p)+".tif")
victor@2 105
victor@2 106 else:
victor@2 107 shutil.copy(strOut+ '.tif', dirMovement+"\\page_"+str(p)+".tif")
victor@2 108
victor@2 109
victor@2 110
victor@2 111
victor@2 112 def processOssia(self,rootDir):
victor@2 113 ###############################################
victor@2 114 # @param dirGeneral root movement folder
victor@2 115 for dirname, dirnames, filenames in os.walk(rootDir):
victor@2 116 for f in filenames:
victor@2 117 fname_path = os.path.join(dirname, f)
victor@2 118 if f.endswith(".tif"):
victor@2 119 print fname_path
victor@2 120
victor@2 121 ossia.process(fname_path, dirname+"\\_"+f)
victor@2 122 # rootApp=os.path.dirname(sys.argv[0])
victor@2 123 # rootApp=rootApp.replace("\\","/")
victor@2 124 # cwd="python.exe "+rootApp+"/Preprocessing/ossia.py "+fname_path+" "+fname_path
victor@2 125 #
victor@2 126 # print cwd
victor@2 127 # p = Popen(cwd)
victor@2 128 # p.wait()
victor@2 129 # p.communicate()
victor@2 130 #
victor@2 131
victor@2 132
victor@2 133 def processMovement(self,dirGeneral):
victor@2 134 '''
victor@2 135 Process just one movement. The structure should be
victor@2 136 ....
victor@2 137 \\movementName\\fullScore\\XML\\(the .xml files)
victor@2 138 \\parts\\0\\
victor@2 139 \\1\\
victor@2 140 \\2\\XML\\(the .xml files)
victor@2 141
victor@2 142 the result is \\movementName\\finalScore.xml
victor@2 143
victor@2 144 usage:
victor@2 145 mmOMR=MainMultiOMR()
victor@2 146 mmOMR.processMovement("C:\Users\victor\Desktop\data\k458\Process\m2")
victor@2 147 '''
victor@2 148
victor@2 149
victor@2 150 ff=FilesFunctions()
victor@2 151 subdirnameParts=ff.SubDirPath(dirGeneral+"\\Parts\\")
victor@2 152 fsOMRs_files=ff.getFiles(dirGeneral+"\\fullScore\\XML\\")
victor@2 153 for dirname in subdirnameParts:
victor@2 154 d=dirname+"/XML/"
victor@2 155 urlSplit=dirname.split("\\")
victor@2 156 part=int(urlSplit[-1])
victor@2 157 partOMRs_files=ff.getFiles(d)
victor@2 158 print "---------S2------------"
victor@2 159 logging.warning("Part:"+str(part)+" "+d)
victor@2 160 self.setResultS2(d,part,fsOMRs_files,partOMRs_files)
victor@2 161
victor@2 162 print "---------Synchronising scores and parts------------"
victor@2 163 logging.warning("---------Synchronising scores and parts------------:"+dirGeneral)
victor@2 164 #fsOMRs=ff.getOMRs(dirGeneral+"\\fullScore\\XML\\")
victor@2 165 self.__runSynchroScoresAndParts(dirGeneral,fsOMRs_files)
victor@2 166
victor@2 167 ###############################################
victor@2 168 # @param path folder where the .xml files are
victor@2 169 # @param idPart part number
victor@2 170 # @param fsOMRs array with the full score files processed by music21
victor@2 171 # @param partOMRs array with the part files processed by music21
victor@2 172 def setResultS2(self,path,idPart,fsOMRs,partOMRs):
victor@2 173 '''
victor@2 174 Takes the fullScores (processing the idPart) and parts.
victor@2 175 It writes the result (result.S2.xml) in the dirname
victor@2 176
victor@2 177 This function process each part independently
victor@2 178
victor@2 179 usage:
victor@2 180
victor@2 181 mmOMR=MainMultiOMR()
victor@2 182 ff=FilesFunctions()
victor@2 183 fsOMRs=ff.getOMRs("C:\\Users\\victor\\Desktop\\data\\k458_test\\Process\\m2\\fullScore\\XML")
victor@2 184 partOMRs=ff.getOMRs("C:\\Users\\victor\\Desktop\\data\\k458_test\\Process\\m2\\parts\\0\\XML")
victor@2 185 d="C:\\Users\\victor\\Desktop\\data\\k458_test\\Process\\m2\\parts\\0\\XML"
victor@2 186 mmOMR.setResultS2(d,0,fsOMRs,partOMRs)
victor@2 187 '''
victor@2 188
victor@2 189
victor@2 190
victor@2 191 pa=PipelineAlignment()
victor@2 192 vote=Voting()
victor@2 193 sc=SymbolConversion()
victor@2 194 ff=FilesFunctions()
victor@2 195 omr_symbolsAlign,betterOmrIds=pa.alignNJ_files(idPart,fsOMRs,partOMRs)
victor@2 196 #The .txt with the OMRs involved (tree)
victor@2 197 ff.writeText(path,betterOmrIds)
victor@2 198 #voting
victor@2 199
victor@2 200 outVote=vote.vote(omr_symbolsAlign)
victor@2 201 #apply voices if it is needed
victor@2 202
victor@2 203 outVote=sc.setVoices(outVote)
victor@2 204 #convert to music21
victor@2 205 resultS2=sc.convertM21(outVote)
victor@2 206 mf=MeasureFunctions()
victor@2 207 #remove blank measures due to the alignment
victor@2 208 resultS2_clean=mf.filterExtraMeasures(resultS2)
victor@2 209 resultS2_clean.write("musicxml", path+'/result.S2.xml')
victor@2 210
victor@2 211
victor@2 212 ###############################################
victor@2 213 # @param dirGeneral folder where is the root of the movement
victor@2 214 # @param fsOMRs array with the full score files processed by music21
victor@2 215 def __runSynchroScoresAndParts(self,dirGeneral,fsOMRs_files):
victor@2 216 '''
victor@2 217 Takes the fullScores and part files generated to align the final full score
victor@2 218 - dirGeneral is the movement URL
victor@2 219 - fsOMRs is an array with the full scores OMRs processed by music21
victor@2 220 '''
victor@2 221 ff=FilesFunctions()
victor@2 222 subdirnameParts=ff.SubDirPath(dirGeneral+"\\parts\\")
victor@2 223 partsNumber=len(subdirnameParts)
victor@2 224 fsa=FullScoreAlignment()
victor@2 225 idCompleteScoreBetter=fsa.getIdBetterOMRFullScore(fsOMRs_files,partsNumber)
victor@2 226 betterFsOMR=ff.getOMR(fsOMRs_files[idCompleteScoreBetter])
victor@2 227 finalScore=fsa.runSynchronisingMeasuresNJ(subdirnameParts,betterFsOMR)
victor@2 228 betterFsOMR=None
victor@2 229 finalScore.write("musicxml", dirGeneral+'/finalScore.xml')
victor@2 230 finalScore=None
victor@2 231 gc.collect()
victor@2 232 print "----"
victor@2 233
victor@2 234
victor@2 235
victor@2 236 ###############################################
victor@2 237 # @param dirGeneral root movement folder
victor@2 238 def processMovementGround(self,dirGeneral):
victor@2 239 '''
victor@2 240 Process the result. Just one movement. It takes each OMR file and compare against the ground.
victor@2 241 The differences are written in .XML and .xls
victor@2 242
victor@2 243 ....
victor@2 244 \\movementName\\fullScore\\XML\\(the .xml files)
victor@2 245 \\parts\\0\\
victor@2 246 \\1\\
victor@2 247 \\2\\XML\\(the .xml files)
victor@2 248
victor@2 249 the file finalScore.xml should be in \\movementName\\finalScore.xml
victor@2 250 the file ground.xml should be in \\movementName\\ground.xml
victor@2 251
victor@2 252 The final result is written in
victor@2 253 \\movementName\\parts\\resultGeneral.xlsx
victor@2 254 \\fullScore_errors.xml
victor@2 255
victor@2 256 usage:
victor@2 257 mmOMR=MainMultiOMR()
victor@2 258 mmOMR.processMovementGround("C:\Users\victor\Desktop\data\k458\Process\m2")
victor@2 259 '''
victor@2 260 percentagesArray=[]
victor@2 261 betterOMRIds=[]
victor@2 262 ff=FilesFunctions()
victor@2 263 subdirname=ff.SubDirPath(dirGeneral+"\\parts\\")
victor@2 264 filesFull=ff.getFiles(dirGeneral+"\\fullScore\\XML\\")
victor@2 265
victor@2 266 ground=ff.getGround(dirGeneral)
victor@2 267 groundparsed=converter.parse(ground, forceSource=True)
victor@2 268 finalScore=ff.getFinalScore(dirGeneral)
victor@2 269 finalScoreparsed=converter.parse(finalScore, forceSource=True)
victor@2 270
victor@2 271
victor@2 272 for dirname in subdirname:
victor@2 273 d=dirname+"/XML/"
victor@2 274 urlSplit=dirname.split("\\")
victor@2 275 part=int(urlSplit[-1])
victor@2 276 filesPart=ff.getFiles(d)
victor@2 277 files=filesPart+filesFull
victor@2 278
victor@2 279 print files
victor@2 280 pg=ProcessGroundS2()
victor@2 281 ErrorsMatrix=[]
victor@2 282 percentages=[]
victor@2 283
victor@2 284 OMRs=[]
victor@2 285 OMRs.append(groundparsed)
victor@2 286 OMRs.append(finalScoreparsed)
victor@2 287
victor@2 288
victor@2 289
victor@2 290 percentage,errors,scoreWithErrors= pg.getSimilarity(OMRs,part)
victor@2 291 ErrorsMatrix.append(errors)
victor@2 292 percentages.append(percentage)
victor@2 293 if not os.path.exists(dirname+"\\Result"):
victor@2 294 os.makedirs(dirname+"\\Result")
victor@2 295
victor@2 296 scoreWithErrors.write("musicxml", dirname+"\\Result\\result.S2.xml")
victor@2 297
victor@2 298 for i in range(len(files)):
victor@2 299 try:
victor@2 300 OMRs[1]=ff.getOMR(files[i])
victor@2 301 percentage,errors,scoreWithErrors= pg.getSimilarity(OMRs,part)
victor@2 302 ErrorsMatrix.append(errors)
victor@2 303 percentages.append(percentage)
victor@2 304 scoreWithErrors.write("musicxml", dirname+"\\Result\\"+os.path.basename(files[i]))
victor@2 305 except:
victor@2 306 print "ERROR OMR READING"
victor@2 307 ErrorsMatrix.append("ERROR OMR READING")
victor@2 308 percentages.append(0)
victor@2 309
victor@2 310
victor@2 311 f=open(d+"betterOMR.txt","r")
victor@2 312 betterOMRId=f.readlines()
victor@2 313 f.close()
victor@2 314 betterOMRIds.append(betterOMRId)
victor@2 315 print "betterOMRIds",betterOMRIds
victor@2 316 ed=ExcellData()
victor@2 317 files.insert(0,dirname+"\\Result\\result.S2.xml" )
victor@2 318 print files
victor@2 319 ed.saveData(ErrorsMatrix,files,percentages)
victor@2 320 percentagesArray.append(percentages)
victor@2 321
victor@2 322
victor@2 323 ed=ExcellData()
victor@2 324 ed.saveGlobalData(percentagesArray,dirGeneral,betterOMRIds,files)
victor@2 325 self.__joinErrorParts(dirGeneral)
victor@2 326 print "----------- END ------------"
victor@2 327
victor@2 328 ################################################################
victor@2 329 # @param rootDir root folder where all the scores to process are
victor@2 330 def runBigData(self,rootDir):
victor@2 331 '''
victor@2 332 Checks if there is any folder to process
victor@2 333 If the folder "Process" is written, but the "finalScore.xml" is not,
victor@2 334 runs the procedure
victor@2 335
victor@2 336 usage
victor@2 337
victor@2 338 mmOMR=MainMultiOMR()
victor@2 339 d="C:\\Users\\victor\\Desktop\\cach"
victor@2 340 mmOMR.runBigData(d)
victor@2 341 '''
victor@2 342 ff=FilesFunctions()
victor@2 343 for outWalk in os.walk(rootDir):
victor@2 344 dirName=outWalk[0]
victor@2 345 arrDir=dirName.split("\\")
victor@2 346 if arrDir[-1]=="Process":
victor@2 347 movements=ff.SubDirPath(dirName)
victor@2 348 for movement in movements:
victor@2 349 print movement
victor@2 350 if not os.path.isfile(movement+"\\finalScore.xml"):
victor@2 351 logging.warning("Movement:"+movement+" "+rootDir)
victor@2 352 self.processMovement(movement)
victor@2 353
victor@2 354 ################################################################
victor@2 355 # @param rootDir root folder where all the scores to process are
victor@2 356 def runLoopBigData(self,rootDir,adaptOMRs=False):
victor@2 357 '''
victor@2 358 Infinity loop for processing data
victor@2 359 Checks if there is any folder to process
victor@2 360 If the folder "Process" is written, but the "finalScore.xml" is not,
victor@2 361 runs the procedure
victor@2 362
victor@2 363 usage
victor@2 364
victor@2 365 mmOMR=MainMultiOMR()
victor@2 366 d="C:\\Users\\victor\\Desktop\\cach"
victor@2 367 mmOMR.runLoopBigData(d)
victor@2 368 '''
victor@2 369 batchOMR=BatchOMR()
victor@2 370 ff=FilesFunctions()
victor@2 371 for outWalk in os.walk(rootDir):
victor@2 372 dirName=outWalk[0]
victor@2 373 subdirList=outWalk[1]
victor@2 374 if "OMRS" in subdirList:
victor@2 375 rootScore=dirName
victor@2 376 omrFinished=True
victor@2 377 scoreFinished=True
victor@2 378 for outWalk2 in os.walk(rootScore):
victor@2 379 dirName2=outWalk2[0]
victor@2 380 files= ff.getAllImgFiles(dirName2)
victor@2 381 omrs=ff.getAllXMLFiles(dirName2)
victor@2 382 if len(files)>0 and len(omrs)<4:
victor@2 383 omrFinished=False
victor@2 384
victor@2 385 if os.path.exists(rootScore+"\\Process"):
victor@2 386 movements=ff.SubDirPath(rootScore+"\\Process")
victor@2 387 for movement in movements:
victor@2 388 if not os.path.isfile(movement+"\\finalScore.xml"):
victor@2 389 scoreFinished=False
victor@2 390 else:
victor@2 391 scoreFinished=False
victor@2 392
victor@2 393
victor@2 394 if omrFinished==True and scoreFinished==False:
victor@2 395 print "---- CONFIGURE SCORES ---"
victor@2 396 batchOMR.setupApp(rootScore)
victor@2 397 print "---- CONFIGURE GROUND ---"
victor@2 398 batchOMR.setGround(rootScore)
victor@2 399 if adaptOMRs:
victor@2 400 print "---- ADAPT OMRs ---"
victor@2 401 self.runAdaptAllOMRs(rootScore+"\\Process")
victor@2 402 print "---- RUN BIG DATA ---"
victor@2 403 self.runBigData(rootScore)
victor@2 404 print "Waiting for processing..."
victor@2 405 time.sleep(5)
victor@2 406 self.runLoopBigData(rootDir,adaptOMRs)
victor@2 407
victor@2 408
victor@2 409 ################################################################
victor@2 410 # @param rootDir root folder where all the scores to process are
victor@2 411 def runBigDataGround(self,rootDir):
victor@2 412 '''
victor@2 413 Checks if there is any folder to process to get the result
victor@2 414 If "resultGeneral.xlsx" is not written and "finalScore.xml" and "ground.xml" are in the movement root dir,
victor@2 415 launches the process
victor@2 416
victor@2 417 usage
victor@2 418
victor@2 419 mmOMR=MainMultiOMR()
victor@2 420 d="C:\\Users\\victor\\Desktop\\data"
victor@2 421 mmOMR.runBigDataGround(d)
victor@2 422 '''
victor@2 423 ff=FilesFunctions()
victor@2 424 for outWalk in os.walk(rootDir):
victor@2 425 dirName=outWalk[0]
victor@2 426 arrDir=dirName.split("\\")
victor@2 427 if arrDir[-1]=="Process":
victor@2 428 movements=ff.SubDirPath(dirName)
victor@2 429 for movement in movements:
victor@2 430 if not os.path.isfile(movement+"\\parts\\resultGeneral.xlsx"):
victor@2 431 if os.path.isfile(movement+"\\finalScore.xml") and os.path.isfile(movement+"\\ground.xml") :
victor@2 432 self.processMovementGround(movement)
victor@2 433
victor@2 434 ################################################################
victor@2 435 # @param rootDir root folder where all the scores to process are
victor@2 436 def runFinalXLS(self,rootDir):
victor@2 437 '''
victor@2 438 Checks if there is any folder to process to get the result
victor@2 439 If "resultGeneral.xlsx" is not written and "finalScore.xml" and "ground.xml" are in the movement root dir,
victor@2 440 launches the process
victor@2 441
victor@2 442 usage
victor@2 443
victor@2 444 mmOMR=MainMultiOMR()
victor@2 445 d="C:\\Users\\victor\\Desktop\\data"
victor@2 446 mmOMR.runBigDataGround(d)
victor@2 447 '''
victor@2 448 ed=ExcellData()
victor@2 449 files=0
victor@2 450 S2_sum=0
victor@2 451 CP_sum=0
victor@2 452 PS_sum=0
victor@2 453 SE_sum=0
victor@2 454 SS_sum=0
victor@2 455 ff=FilesFunctions()
victor@2 456 for outWalk in os.walk(rootDir):
victor@2 457 dirName=outWalk[0]
victor@2 458 arrDir=dirName.split("\\")
victor@2 459 if arrDir[-1]=="Process":
victor@2 460 movements=ff.SubDirPath(dirName)
victor@2 461 for movement in movements:
victor@2 462 if os.path.isfile(movement+"\\parts\\resultGeneral.xlsx"):
victor@2 463 files=files+1
victor@2 464 s2,cp,ps,se,ss=ed.processResultGeneral(movement+"\\parts\\resultGeneral.xlsx")
victor@2 465 S2_sum=S2_sum+s2
victor@2 466 CP_sum=CP_sum+cp
victor@2 467 PS_sum=PS_sum+ps
victor@2 468 SE_sum=SE_sum+se
victor@2 469 SS_sum=SS_sum+ss
victor@2 470 ed.writeFinalXLS(rootDir,S2_sum/files,CP_sum/files,PS_sum/files,SE_sum/files,SS_sum/files)
victor@2 471
victor@2 472 ################################################################
victor@2 473 # @param rootDir root folder where all the scores to process are
victor@2 474 def runLoopBigDataGround(self,rootDir):
victor@2 475 '''
victor@2 476 Infinity loop for getting the results
victor@2 477
victor@2 478 If "resultGeneral.xlsx" is not written and "finalScore.xml" and "ground.xml" are in the movement root dir,
victor@2 479 launches the process
victor@2 480
victor@2 481 usage
victor@2 482
victor@2 483 mmOMR=MainMultiOMR()
victor@2 484 d="C:\\Users\\victor\\Desktop\\data"
victor@2 485 mmOMR.runLoopBigDataGround(d)
victor@2 486 '''
victor@2 487 self.runBigDataGround(rootDir)
victor@2 488 print "Waiting for results..."
victor@2 489 time.sleep(5)
victor@2 490 self.runLoopBigDataGround(rootDir)
victor@2 491
victor@2 492
victor@2 493 ################################################################
victor@2 494 # @param dirGeneral root folder of the movement
victor@2 495
victor@2 496 def __joinErrorParts(self,dirGeneral):
victor@2 497 '''
victor@2 498 Joins the different result.S2.xml error in a single file.
victor@2 499 The output file is "fullScore_errors.xml"
victor@2 500
victor@2 501 The function tries to find the file "parts\\[idMovement]\\Result\\result.S2.xml" in each part
victor@2 502 '''
victor@2 503 ff=FilesFunctions()
victor@2 504 subdirname=ff.SubDirPath(dirGeneral+"\\parts\\")
victor@2 505 fullScore=stream.Score()
victor@2 506 for dirname in subdirname:
victor@2 507 d=dirname+"\\Result\\"
victor@2 508 urlSplit=dirname.split("\\")
victor@2 509 part=int(urlSplit[-1])
victor@2 510 f=d+"\\result.S2.xml"
victor@2 511 omr=converter.parse(f, forceSource=True)
victor@2 512 part=omr.getElementsByClass(stream.Part)[0]
victor@2 513 fullScore.append(part)
victor@2 514 fullScore.write("musicxml", dirGeneral+"\\parts\\fullScore_errors.xml")
victor@2 515
victor@2 516
victor@2 517 ###############################################
victor@2 518 # @param rootDir root folder of the general data
victor@2 519 def runCompleteProcess(self,rootDir):
victor@2 520 '''
victor@2 521 Instead of working in parallel, this function launches a serial process with the different
victor@2 522 steps involved
victor@2 523 1.- SIKULI AUTOMATISM
victor@2 524 2.- SETUP APPLICATION (copying files)
victor@2 525 3.- CONVERT GROUND (taking the .krn and convert to ground.xml)
victor@2 526 4.- RUN MULTIOMR BIG DATA
victor@2 527 5.- RUN GET RESULT BIG DATA
victor@2 528
victor@2 529
victor@2 530 usage:
victor@2 531 mmOMR=MainMultiOMR()
victor@2 532 d="C:\\Users\\victor\\Desktop\\data"
victor@2 533 mmOMR.runCompleteProcess(d)
victor@2 534 '''
victor@2 535 batchOMR=BatchOMR()
victor@2 536 logging.basicConfig(filename=rootDir+'\\BigDataLogs.log',format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
victor@2 537 print "-------SIKULI AUTOMATISM----------"
victor@2 538 logging.warning("---SIKULI AUTOMATISM--- "+rootDir)
victor@2 539 batchOMR.processAllTiffFiles(rootDir,"ALL")
victor@2 540 print "-------SETUP APPLICATION----------"
victor@2 541 logging.warning("---SETUP APPLICATION--- "+rootDir)
victor@2 542 batchOMR.setupApp(rootDir)
victor@2 543 print "-------CONVERT GROUND----------"
victor@2 544 logging.warning("---CONVERT GROUND--- "+rootDir)
victor@2 545 batchOMR.setGround(rootDir)
victor@2 546 logging.warning("------RUN MULTIOMR BIG DATA--- "+rootDir)
victor@2 547 print "-------RUN MULTIOMR BIG DATA----------"
victor@2 548 self.runBigData(rootDir)
victor@2 549 print "-------RUN GET RESULT BIG DATA----------"
victor@2 550 self.runBigDataGround(rootDir)
victor@2 551
victor@2 552 ###############################################
victor@2 553 # @param filename file to convert
victor@2 554 def runConvertKrnToMusicXML(self,filename):
victor@2 555 '''
victor@2 556 converts a .krn file to .xml using music21
victor@2 557
victor@2 558 usage:
victor@2 559 mmOMR=MainMultiOMR()
victor@2 560 file="C:\\Users\\victor\\Desktop\\data\\k458\\OMRS\\m2\\k458.krn"
victor@2 561 mmOMR.runConvertKrnToMusicXML(file)
victor@2 562 '''
victor@2 563 omr=converter.parse(filename)
victor@2 564 omr.write("musicxml", filename+'.xml')
victor@2 565
victor@2 566 ###############################################
victor@2 567 # @param filename file to convert
victor@2 568 def runConvertMidiToMusicXML(self,filename):
victor@2 569 '''
victor@2 570 converts a .mid file to .xml using music21
victor@2 571
victor@2 572 usage:
victor@2 573 mmOMR=MainMultiOMR()
victor@2 574 file="C:\\Users\\victor\\Desktop\\data\\k458\\OMRS\\m2\\k458.mid"
victor@2 575 mmOMR.runConvertKrnToMusicXML(file)
victor@2 576 '''
victor@2 577 omr=converter.parse(filename)
victor@2 578 #Reordering the different staffs
victor@2 579 omrOrdered=stream.Score()
victor@2 580 numberParts=len(omr.parts)
victor@2 581 for i in reversed(range(numberParts)):
victor@2 582 mypart=omr.parts[i]
victor@2 583
victor@2 584 omrOrdered.insert(0,mypart)
victor@2 585 omrOrdered.write("musicxml", filename+'.xml')
victor@2 586
victor@2 587 ###############################################
victor@2 588 # @param filename file to convert
victor@2 589 def runConvertVoicesToChord(self,filename):
victor@2 590 '''
victor@2 591
victor@2 592 '''
victor@2 593 mf=MeasureFunctions()
victor@2 594 omr=converter.parse(filename)
victor@2 595 omr=mf.convertVoicesToChord(omr)
victor@2 596 omr.show()
victor@2 597
victor@2 598 ###############################################
victor@2 599 # @param filename file to convert
victor@2 600 def runConvertBeamsToTriplets(self,filename):
victor@2 601 '''
victor@2 602
victor@2 603 '''
victor@2 604 mf=MeasureFunctions()
victor@2 605 omr=converter.parse(filename)
victor@2 606 omr=mf.convertBeamsToTriplets(omr)
victor@2 607 # omr.show()
victor@2 608 omr.write("musicxml", filename+'.xml')
victor@2 609
victor@2 610 def runRemovesEmptyVoices(self,filename):
victor@2 611 '''
victor@2 612
victor@2 613 '''
victor@2 614 mf=MeasureFunctions()
victor@2 615 omr=converter.parse(filename)
victor@2 616 omr=mf.removesEmptyVoices(omr)
victor@2 617 omr.write("musicxml", filename+'.xml')
victor@2 618
victor@2 619 def runRemovesGaps(self,filename):
victor@2 620 '''
victor@2 621
victor@2 622 '''
victor@2 623 mf=MeasureFunctions()
victor@2 624 omr=converter.parse(filename)
victor@2 625 omr=mf.removesGaps(omr)
victor@2 626 omr.write("musicxml", filename+'.xml')
victor@2 627
victor@2 628
victor@2 629 def runAdaptOMRs(self,dirname):
victor@2 630 '''
victor@2 631 Runs the following process in one directory.
victor@2 632 1.-Removing GAPS
victor@2 633 2.-Converting voices to chords
victor@2 634 3.-Converting triplets
victor@2 635 4.-Removing Rest Voices
victor@2 636 '''
victor@2 637 ff=FilesFunctions()
victor@2 638 mf=MeasureFunctions()
victor@2 639 files=ff.getFiles(dirname)
victor@2 640 for f in files:
victor@2 641 try:
victor@2 642 print f
victor@2 643 omr=converter.parse(f)
victor@2 644 print "--- Removing GAPS---"
victor@2 645 omr=mf.removesGaps(omr)
victor@2 646
victor@2 647 print "--- Converting voices to chords---"
victor@2 648 omr=mf.convertVoicesToChord( omr)
victor@2 649
victor@2 650 print "--- Converting triplets---"
victor@2 651 omr=mf.convertBeamsToTriplets(omr)
victor@2 652 print "--- Removing Rest Voices---"
victor@2 653 omr=mf.removeRestVoice(omr)
victor@2 654
victor@2 655 omr.write("musicxml", f)
victor@2 656 except:
victor@2 657 pass
victor@2 658
victor@2 659 def runAdaptAllOMRs(self,rootDir):
victor@2 660 '''
victor@2 661 Adapt all the files in one directory tree
victor@2 662 '''
victor@2 663 for outWalk in os.walk(rootDir):
victor@2 664 dirName=outWalk[0]
victor@2 665 self.runAdaptOMRs(dirName)
victor@2 666
victor@2 667
victor@2 668
victor@2 669
victor@2 670
victor@2 671 ##################################################
victor@2 672 # @param dirname the directory where the files are
victor@2 673 def runViewM21(self,dirname):
victor@2 674 '''
victor@2 675 Shows all the .xml files in a folder using music21
victor@2 676 to check the differences before and after processing
victor@2 677
victor@2 678 usage:
victor@2 679 mmOMR=MainMultiOMR()
victor@2 680 dirname="C:\\Users\\victor\\Desktop\\data\\k458\\OMRS\\m2"
victor@2 681 mmOMR.runViewM21(dirname)
victor@2 682 '''
victor@2 683 ff=FilesFunctions()
victor@2 684 mf=MeasureFunctions()
victor@2 685 files=ff.getFiles(dirname)
victor@2 686 for f in files:
victor@2 687 omr=converter.parse(f)
victor@2 688 omr.show()
victor@2 689 omr.show('text')
victor@2 690
victor@2 691
victor@2 692 ##################################################
victor@2 693 # @param dirname the directory where the files are
victor@2 694 def runViewWrongMeasures(self,dirname):
victor@2 695 '''
victor@2 696 Flags and prints the possible wrong measures
victor@2 697
victor@2 698 usage:
victor@2 699 mmOMR=MainMultiOMR()
victor@2 700 dirname="C:\\Users\\victor\\Desktop\\data\\k458\\OMRS\\m2"
victor@2 701 mmOMR.runViewWrongMeasures(dirname)
victor@2 702 '''
victor@2 703 ff=FilesFunctions()
victor@2 704 mf=MeasureFunctions()
victor@2 705 path = dirname
victor@2 706 print("Path:"+path)
victor@2 707 omr_files=ff.getAllFiles(path)
victor@2 708 for f in omr_files:
victor@2 709 omr=converter.parse(f)
victor@2 710 arrayErrors=mf.flagIncorrectMeasures(omr)[1]
victor@2 711 for array in arrayErrors:
victor@2 712 for i in range(len(array)):
victor@2 713 array[i]=array[i]+1
victor@2 714 print
victor@2 715 print f
victor@2 716 print "Errors measures duration:"+str(arrayErrors[0])
victor@2 717 print "Errors measures estimated:"+str(arrayErrors[1])
victor@2 718 print "Errors based on beams:"+str(arrayErrors[2])
victor@2 719 print "Errors based on last notes:"+str(arrayErrors[3])
victor@2 720
victor@2 721
victor@2 722
victor@2 723
victor@2 724
victor@2 725
victor@2 726
victor@2 727