# -*- coding: utf-8 -*-
"""
Created on Wed Jun 07 10:46:00 2017
@author: Lijun
"""
import re
import csv
import codecs
import xlwt
from xlrd import open_workbook
from xlwt import easyxf, XFStyle, Pattern
import os
f = xlwt.Workbook(style_compression=2)
def set_style(name,height,colors = 0,bold=False):
alignment = xlwt.Alignment()
alignment.horz = xlwt.Alignment.HORZ_CENTER
alignment.vert = xlwt.Alignment.VERT_CENTER
style = xlwt.XFStyle()
font = xlwt.Font()
font.name = name
font.color_index = 4
font.height = height
style.font = font
style.bold = bold
style.alignment = alignment
return style
def set_color(name,height,colors = 0,bold=False):
alignment = xlwt.Alignment()
alignment.horz = xlwt.Alignment.HORZ_CENTER
alignment.vert = xlwt.Alignment.VERT_CENTER
style = xlwt.XFStyle()
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = colors
font = xlwt.Font()
font.name = name
font.color_index = 4
font.height = height
style.font = font
style.bold = bold
style.alignment = alignment
style.pattern = pattern
return style
def generateExcel(scorepath,evaluateResultOriginpath,sfResultpath,sfResultOriginpath):
timesure = False
yinfusure = False
isLocatesure = False
locatesure = False
backsure = False
Rhythemsure = False
yuepu = [] # 乐谱
time = [] #时间
yinfu= [] # 音符
isLocate= [] #是否确定定位
locate = [] # 实时定位
locateNot= [] #非实时定位
isBack = [] #回弹
Excess = [] #多弹
Omission = [] #漏弹
Rhythem = [] #节奏快慢
starttime = [] #开始时间
endtime = [] #结束时间
longtime = [] #时长
count = 0
files = open(scorepath)
timefileNot= open(sfResultpath)
timefile= open(sfResultOriginpath)
evaluate = open(evaluateResultOriginpath)
for line in files.readlines():
if (line.strip()==''):
count+=1
if(count==3):
temp = []
line = line.strip()
if(line!=''):
yuepu.append(line)
for line in timefileNot.readlines():
line = line.strip()
if line=='onset时间' or line=='时间':
timesure = True
yinfusure = False
isLocatesure = False
locatesure = False
if line == '音符':
timesure = False
yinfusure = True
isLocatesure = False
locatesure = False
if line == '是否确定定位' or line=='是否确定':
timesure = False
yinfusure = False
isLocatesure = True
locatesure = False
if line == '定位':
timesure = False
yinfusure = False
isLocatesure = False
locatesure = True
if (timesure and line!='onset时间' and line!='时间'):
time.append(line)
if yinfusure and line!='音符':
yinfu.append(line)
if isLocatesure and line!='是否确定定位' and line!='是否确定':
isLocate.append(line)
if locatesure and line!='定位':
locateNot.append(line)
for line in timefile.readlines():
line = line.strip()
if line=='onset时间' or line=='时间':
timesure = True
yinfusure = False
isLocatesure = False
locatesure = False
if line == '音符':
timesure = False
yinfusure = True
isLocatesure = False
locatesure = False
if line == '是否确定定位' or line=='是否确定':
timesure = False
yinfusure = False
isLocatesure = True
locatesure = False
if line == '定位':
timesure = False
yinfusure = False
isLocatesure = False
locatesure = True
if locatesure and line!='定位':
locate.append(line)
'''
for line in evaluate.readlines():
line = line.strip()
if line=='是否回弹 多弹音符1,多弹音符2 漏弹音符1,漏弹音符2 正确演奏音符1,正确演奏音符2':
backsure = True
Rhythemsure = False
if line=='节奏快慢(0表示正确、1表示快了、-1表示慢了) 小节开始时间 小节结束时间 小节时长':
backsure = False
Rhythemsure = True
if Rhythemsure and line!='节奏快慢(0表示正确、1表示快了、-1表示慢了) 小节开始时间 小节结束时间 小节时长':
res = re.split(r'\t',line)
Rhythem.append(res[0])
if len(res)>2:
starttime.append(res[1])
endtime.append(res[2])
longtime.append(res[3])
if backsure and line!='' and line!='是否回弹 多弹音符1,多弹音符2 漏弹音符1,漏弹音符2 正确演奏音符1,正确演奏音符2' and line!='小节级别节奏' :
res = re.split(r'\t',line)
isBack.append(res[0])
if len(res)>=2:
Excess.append(res[1])
if len(res)>=3:
Omission.append(res[2])
'''
count1 = 0
count2 = 0
for line in evaluate.readlines():
line = line.strip()
count1+=1
if count1==3:
backsure = True
Rhythemsure = False
if line=='':
backsure = False
Rhythemsure = True
if Rhythemsure:
count2+=1
if backsure:
res = re.split(r'\t',line)
isBack.append(res[0])
if len(res)>=2:
Excess.append(res[1])
if len(res)>=3:
Omission.append(res[2])
if count2>3 and Rhythemsure:
res = re.split(r'\t',line)
Rhythem.append(res[0])
if len(res)>2:
starttime.append(res[1])
endtime.append(res[2])
longtime.append(res[3])
sheet = re.split(r'/',scorepath)[-1].split('.')[0]
sheets = re.split(r'/',scorepath)
if sheets[-2]==sheet:
sheet1 = f.add_sheet(sheet.decode('gbk'),cell_overwrite_ok=True)
elif sheets[-3]==sheet:
s = sheet+sheets[-2]
sheet1 = f.add_sheet(s.decode('gbk'),cell_overwrite_ok=True)
elif sheets[-4]==sheet:
s = sheet+sheets[-3]+sheets[-2]
sheet1 = f.add_sheet(s.decode('gbk'),cell_overwrite_ok=True)
tall_style = xlwt.easyxf('font:height 450;') # 36pt,类型小初的字号
first_row = sheet1.row(0)
first_row.set_style(tall_style)
nRef = 0
nTot = 0
nCorr = 0
Excession = 0
Omiss = 0
Recall = 0.0
Precision = 0.0
F_measure = 0.0
Accuracy = 0.0
nTot1 = 0
nTot2 = 0
row0 = [u'乐谱',u'时间',u'音符', u'回弹-1/重弹0/跳跃2',u'多弹',u'漏弹',u'是否确定定位',u'实时定位',u'非实时定位' ,u'正确结果',u'错误原因',u'节奏正确0/快1/慢-1',u'开始时间',u'结束时间',u'时长' ,u'标记说明',u'乐谱跟踪说明',u'乐谱跟踪错误说明']
row1 = [u'错误',u'倍频',u'不正确定位']
row2 = [u'1.实时定位不确定定位时,为了避免回跳、乱跳,暂时只考虑了上一定位及其下一个位置,可能直到确定定位 实时定位都是错误的,比如停在最开始出错的位置不动',u'2.确定定位后重新计算此前不确定的定位,选择匹配程度最高的路径 或 匹配程度最高的路径有多条时选 包含的位置最多的�