victor@2: ''' victor@2: Created on 10/11/2014 victor@2: victor@2: @organization: Lancaster University & University of Leeds victor@2: @version: 1.0 victor@2: Created on 11/12/2014 victor@2: victor@2: @author: Victor Padilla victor@2: @contact: v.padilla@lancaster.ac.uk victor@2: victor@2: Functions related to SIKULI automatism victor@2: ''' victor@2: import logging victor@2: import os victor@2: import sys victor@2: from subprocess import Popen victor@2: import shutil victor@2: from music21 import converter victor@2: from Functions import FilesFunctions victor@2: victor@2: victor@2: victor@2: class BatchOMR: victor@2: ''' victor@2: Class for the SIKULI process victor@2: ''' victor@2: ################################################## victor@2: # @param idOMR It can be "SE" "SS" "CP" "PS" "ALL" victor@2: # @param dirName folder where the images are and .xml will be victor@2: # @param files array with tiff files victor@2: def Batch(self,idOMR,dirName,files): victor@2: ''' victor@2: Runs the SIKULI process. victor@2: - idOMR: SE, SS, CP, PS victor@2: - if exists, skip victor@2: - it takes all the .tif files and convert them to SE.xml, SS.xml, CP.xml or PS.xml victor@2: - it waits until the process is finished victor@2: victor@2: usage: victor@2: ff=FilesFunctions() victor@2: dirName="C:\\Users\\victor\\Desktop\\data" victor@2: files= ff.getAllImgFiles(dirName) victor@2: batchOMR=BatchOMR() victor@2: batchOMR.Batch("PS",dirName,files) victor@2: ''' victor@2: victor@2: print "Processing...",dirName+"/"+idOMR+".xml" victor@2: if os.path.exists(dirName+"/"+idOMR+".xml"): victor@2: print "....Completed ",dirName+"/"+idOMR+".xml" victor@2: return victor@2: rootApp=os.path.dirname(sys.argv[0]) victor@2: rootApp=rootApp.replace("\\","/") victor@2: print rootApp victor@2: strFiles="" victor@2: for f in files: victor@2: strFiles=strFiles+" \""+f+"\"" victor@2: print dirName,strFiles victor@2: victor@2: dirName=dirName.replace("\\","/") victor@2: dirName="\""+dirName+"/\"" victor@2: victor@2: cwd=rootApp+"/sikuli1.0.1/runScript.cmd -r "+rootApp+"/sikuli/"+idOMR+".sikuli --args " victor@2: cwd=cwd+dirName+strFiles victor@2: print cwd victor@2: p = Popen(cwd) victor@2: p.wait() victor@2: p.communicate() victor@2: print "....Completed ",dirName+"/"+idOMR+".xml" victor@2: victor@2: victor@2: ############################################### victor@2: # @param rootDir root process folder victor@2: def cleanXMLFiles(self,rootDir): victor@2: ''' victor@2: Removes all the .xml files and .mro files in the tree folder victor@2: It is used for testing mainly victor@2: victor@2: usage: victor@2: victor@2: batchOMR=BatchOMR() victor@2: batchOMR.cleanXMLFiles("C:\\Users\\victor\\Desktop\\data") victor@2: ''' victor@2: for outWalk in os.walk(rootDir): victor@2: dirName=outWalk[0] victor@2: fileList=outWalk[2] victor@2: for myfile in fileList: victor@2: if myfile.endswith('.xml'): victor@2: os.remove(dirName+"/"+myfile) victor@2: print myfile victor@2: if myfile.endswith('.mro'): victor@2: os.remove(dirName+"/"+myfile) victor@2: print myfile victor@2: victor@2: victor@2: ############################################### victor@2: # @param rootDir root process folder victor@2: # @param idOMR can be "PS" "CP" "SE" "SS" "ALL" victor@2: def processAllTiffFiles(self,rootDir,idOMR): victor@2: ''' victor@2: Runs the complete process for the tree folder victor@2: idOMR can be: victor@2: PS victor@2: CP victor@2: SE victor@2: SS victor@2: ALL (run all the OMRs). Capella is removed for many problems associated victor@2: victor@2: usage: victor@2: victor@2: batchOMR=BatchOMR() victor@2: batchOMR.processAllTiffFiles("C:\\Users\\victor\\Desktop\\data","SE") victor@2: ''' victor@2: print rootDir victor@2: logging.basicConfig(filename=rootDir+'\\BigDataLogs.log',format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') victor@2: for outWalk in os.walk(rootDir): victor@2: dirName=outWalk[0] victor@2: print('Found directory: %s' % dirName) victor@2: ff=FilesFunctions() victor@2: files= ff.getAllImgFiles(dirName) victor@2: if len(files)>0: victor@2: logging.warning(idOMR+" "+dirName) victor@2: if idOMR=="PS": victor@2: self.Batch("PS",dirName,files) victor@2: if idOMR=="SE": victor@2: self.Batch("SE",dirName,files) victor@2: if idOMR=="SS": victor@2: self.Batch("SS",dirName,files) victor@2: if idOMR=="CP": victor@2: self.Batch("CP",dirName,files) victor@2: if idOMR=="ALL": victor@2: # logging.warning("CP: "+dirName) victor@2: # Batch("CP",dirName,files) victor@2: logging.warning("PS: "+dirName) victor@2: self.Batch("PS",dirName,files) victor@2: logging.warning("SE: "+dirName) victor@2: self.Batch("SE",dirName,files) victor@2: logging.warning("SS: "+dirName) victor@2: self.Batch("SS",dirName,files) victor@2: victor@2: victor@2: ############################################### victor@2: # @param rootDir root process folder victor@2: def setupApp(self,rootDir): victor@2: ''' victor@2: Configures the main folder to process the data victor@2: Creates the "Process" close to "OMRS" folder and copy the .xml files. victor@2: - the parts files in "part/XML" folder with the structure: victor@2: - idpart.version.omrId.xml victor@2: - the full scores in fullScore/XML" folder with the structure: victor@2: - FS.version.omrId.xml victor@2: usage: victor@2: batchOMR=BatchOMR() victor@2: batchOMR.setupApp("C:\\Users\\victor\\Desktop\\data") victor@2: ''' victor@2: ff=FilesFunctions() victor@2: ################ victor@2: # FOR PARTS #### victor@2: ################ victor@2: for outWalk in os.walk(rootDir): victor@2: dirName=outWalk[0] victor@2: subdirList=outWalk[1] victor@2: if "OMRS" in subdirList: victor@2: rootScore=dirName victor@2: if(dirName.upper().find("\\OMRS\\")!=-1 and dirName.upper().find("\\XML")!=-1 and dirName.upper().find("\\PARTS")!=-1): victor@2: arrDir=dirName.split("\\") victor@2: idpart=arrDir[len(arrDir)-2] victor@2: version=arrDir[len(arrDir)-3] victor@2: idmovement=arrDir[len(arrDir)-5] victor@2: files= ff.getAllXMLFiles(dirName) victor@2: myfolder=rootScore+"\\Process\\"+idmovement+"\\parts\\"+idpart+"\\XML\\" victor@2: if not os.path.exists(myfolder): victor@2: os.makedirs(myfolder) victor@2: for f in files: victor@2: if not os.path.isfile(myfolder+idpart+"."+version+"."+f): victor@2: shutil.copy2(dirName+"\\"+f, myfolder+idpart+"."+version+"."+f) victor@2: victor@2: ########################### victor@2: # FOR FULL SCORE ########## victor@2: ########################### victor@2: for outWalk in os.walk(rootDir): victor@2: dirName=outWalk[0] victor@2: subdirList=outWalk[1] victor@2: if "OMRS" in subdirList: victor@2: rootScore=dirName victor@2: if(dirName.upper().find("\\OMRS\\")!=-1 and dirName.upper().find("\\XML")!=-1 and dirName.upper().find("\\FULLSCORE")!=-1): victor@2: arrDir=dirName.split("\\") victor@2: version=arrDir[len(arrDir)-2] victor@2: idmovement=arrDir[len(arrDir)-4] victor@2: files= ff.getAllXMLFiles(dirName) victor@2: myfolder=rootScore+"\\Process\\"+idmovement+"\\fullScore\\XML\\" victor@2: for f in files: victor@2: if not os.path.exists(myfolder): victor@2: os.makedirs(myfolder) victor@2: if not os.path.isfile(myfolder+"FS."+version+"."+f): victor@2: shutil.copy2(dirName+"\\"+f, myfolder+"FS."+version+"."+f) victor@2: victor@2: victor@2: victor@2: victor@2: victor@2: #################################### victor@2: # @param rootDir root process folder victor@2: def setGround(self,rootDir): victor@2: ''' victor@2: Process kern files in the OMRS folder (.krn) and set them as a "ground.xml" victor@2: in "Process" folder victor@2: victor@2: usage: victor@2: batchOMR=BatchOMR() victor@2: batchOMR.setGround("C:\\Users\\victor\\Desktop\\data") victor@2: ''' victor@2: for outWalk in os.walk(rootDir): victor@2: dirName=outWalk[0] victor@2: ff=FilesFunctions() victor@2: kernFile=ff.getKernFile(dirName) victor@2: newpath=dirName.replace("\\OMRS\\","\\Process\\") victor@2: victor@2: if not os.path.isfile(newpath+'\\ground.xml'): victor@2: if kernFile!=None: victor@2: victor@2: print dirName+"\\"+kernFile victor@2: xmlFile=ff.getXMLFile(dirName) victor@2: victor@2: #if we have a .xml file, we copy it victor@2: if xmlFile!=None: victor@2: shutil.copyfile(dirName+"\\"+xmlFile,newpath+"\\ground.xml") victor@2: else: victor@2: omr=converter.parse(dirName+"\\"+kernFile) victor@2: omr.write("musicxml", newpath+'\\ground.xml') victor@2: