def extractInstance(filename):
	instances =[]
#	calculate the sections to distinguish examples providing the treshold
	secBeg, secEnd = signSection(filename, 0.3)
	f = open(filename, 'r')
	xs =[]
	ys =[]
	t = -1
	flag = 0;
	ex=0
	for line in f:
		t+=1
		a = line.split('\n')
		a = a[0].split('\t')
		if (len(a) > 1):
			temp = a[0].split(' ')
			z=float(temp[2])
#	finds the example in the file
			if(t > secBeg[ex] and t <secEnd[ex]):
				xs.append(float(temp[0]))
				ys.append(float(temp[1]))
				flag = 1
			else:
				if(flag == 1):
					flag = 0
					if(ex < len(secEnd)-1):
						ex+=1
					pair =(xs,ys)
					instances.append(pair)
#	returns the sequence for the example	
	#print instances[0][0]
	return instances
Example #2
0
def similarity(treshold, instance1, instance2, sign1, sign2):
    x1 = arange(0)
    y1 = arange(0)
    x2 = arange(0)
    y2 = arange(0)
    b, e = signSection(treshold, sign1, instance1)
    t = -1

    for line in sign1:
        t += 1
        if t > b:
            if t < e:
                a = line.split("\n")
                a = a[0].split("\t")
                if len(a) > 1:
                    temp = a[0].split(" ")
                    x1 = append(x1, float(temp[0]))
                    y1 = append(y1, float(temp[1]))
            else:
                break
    b, e = signSection(treshold, sign2, instance2)
    t = -1
    for line in sign2:
        t += 1
        if t > b:
            if t < e:
                a = line.split("\n")
                a = a[0].split("\t")
                if len(a) > 1:
                    temp = a[0].split(" ")
                    x2 = append(x2, float(temp[0]))
                    y2 = append(y2, float(temp[1]))
            else:
                break
    dtw = mlpy.Dtw()
    dx = dtw.compute(x1, x2)
    dy = dtw.compute(y1, y2)
    o.write(str(dx))
    o.write("\n")
    o.write(str(dy))
    o.write("\n")
    o.write("====================================================================================")
    o.write("\n")
Example #3
0
currentIndex = 0
ainstances=[]
signRange={}
allinstances={}
similMatr=[]
o = open('C:\Users\Sonmaz\Desktop\gestures\output.txt','w')#call the following function with the sign file and the example between 0 and 10

leetteers = ['a','b']
#allinstances['ax']=[]
#allinstances['ax'].append(xins)
#allinstances['ay']=[]
#allinstances['ay'].append(yins)

#convert python arrays to numpy arrays:
for letter in leetteers:
	xins, yins = signSection('C:\Users\Sonmaz\Desktop\gestures\graffiti-'+letter+'.txt', 0.3)
	pair=(currentIndex,currentIndex+len(xins))
	currentIndex+=len(xins)
	signRange[letter]=pair
	print 'sign length'
	print len(xins)
	print len(yins)
	
	for samp in range(len(xins)):
		temp = instance()
		temp.sign = letter
		temp.x=xins[samp]
		temp.y=yins[samp]
		ainstances.append(temp)

randflag=False
Example #4
0
def svmexperiment():
	o = open('C:\Users\Sonmaz\Desktop\gestures\output.txt','a')#call the following function with the sign file and the example between 0 and 10
	for letter in letters:
		xins, yins = signSection('C:\Users\Sonmaz\Desktop\gestures\graffiti-'+letter+'.txt', 0.3)
		for i in [0,1,2]:
			rand= random.uniform(0,len(xins))
			rand=int(floor(rand))
			testD.append([letter,xins.pop(rand),yins.pop(rand)])
		for i in range(len(xins)):
			trainD.append([letter,xins[i],yins[i]]) 	
#===================================================================end of reading the file==========================================	
	
	for td in testD:
		tmp = Gesture()
		tmp.name = td[0]
		l = len(td[1])
		ls = (l / (n+1))
		for i in range(0, n):
			tmp.frame1.append([td[1][i * ls : (i+1) * ls],td[2][i * ls : (i+1) * ls]])
		for fr1 in range(len(tmp.frame1)-1):
			if len(tmp.frame1[fr1][0]+tmp.frame1[fr1+1][0]) < ls:
				break
			if len(tmp.frame1[fr1][1]+tmp.frame1[fr1+1][1]) < ls:
				break
			tmp.frame.append([tmp.frame1[fr1][0]+tmp.frame1[fr1+1][0],tmp.frame1[fr1][1]+tmp.frame1[fr1+1][1]])
		for fr in tmp.frame:
			trans=[]
			trans.append(fft.fft(fr[0]))
			trans.append(fft.fft(fr[1]))
			
			sum1= 0
			sum2 = 0
			tmp.features.append(abs(trans[0][0]))
			tmp.features.append(abs(trans[1][0]))
			for i in range(len(trans[0])):
				sum1 += trans[0][i]*trans[0][i]
			for i in range(len(trans[1])):
				sum2 += trans [1][i]*trans[1][i]
			tmp.features.append(int(abs(sum1)/(2*ls - 1)))
			tmp.features.append(int(abs(sum2)/(2*ls - 1)))

		while len(tmp.features) < (n+1):
			tmp.features.append(0)
		print 'faaaaaa'
		print len(tmp.features)
		gesturesD.append(tmp)
	for tr in trainD:
		tmp = Gesture()
		tmp.name = tr[0]
		l = len(tr[1])
		ls = l / (n+1)
		for i in range(0, n):
			tmp.frame1.append([tr[1][i * ls : (i+1) * ls],tr[2][i * ls : (i+1) * ls]])		
			
		for fr1 in range(len(tmp.frame1)-1):
			if len(tmp.frame1[fr1][0]+tmp.frame1[fr1+1][0]) < ls:
				break
			if len(tmp.frame1[fr1][1]+tmp.frame1[fr1+1][1]) < ls:
				break
			tmp.frame.append([tmp.frame1[fr1][0]+tmp.frame1[fr1+1][0],tmp.frame1[fr1][1]+tmp.frame1[fr1+1][1]])		
		for fr in tmp.frame:
			trans=[]
			#print fr[0]
			trans.append(fft.fft(fr[0]))
			trans.append(fft.fft(fr[1]))
			sum1= 0
			sum2 = 0
			tmp.features.append(abs(trans[0][0]))
			tmp.features.append(abs(trans[1][0]))
			for i in range(len(trans[0])):
				sum1 += trans[0][i]*trans[0][i]
			for i in range(len(trans[1])):
				sum2 += trans[1][i]*trans[1][i]
			tmp.features.append(int(abs(sum1)/(2*ls - 1)))
			tmp.features.append(int(abs(sum2)/(2*ls - 1)))
		print 'taaaa'
		while len(tmp.features) < (n+1):
			tmp.features.append(0)
		print len(tmp.features)
		gesturesT.append(tmp)
#============================================================================end of feature extraction	
	truelabels=[]
	attributes=[]
	labels=[]
	testAtr=[]
	
	for ge in gesturesT:
		tempAtr=[]
		for fe in ge.features:
			tempAtr.append(fe)
		attributes.append(tempAtr)
		if(ge.name == 'e'):
			labels.append(-1)
		else:
			labels.append(1)
		
	for ge in gesturesD:
		tempAtr=[]
		for fe in ge.features:
			tempAtr.append(fe)
		testAtr.append(tempAtr)
		if(ge.name == 'e'):
			truelabels.append(-1)
		else:
			truelabels.append(1)
	#print attributes	
	attributes = array(attributes)
	print attributes
	mysvm = Svm()
	#print testAtr
	testAtr=array(testAtr) 
	print 'sep'
	labels=array(labels)
	#labels = array([-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1])
	mysvm.compute(attributes,labels)
	print testAtr
	predictedlabels = mysvm.predict(testAtr)
	o.write('\n'+str(truelabels))
	o.write(str(predictedlabels))
	correct = 0
	for la in range(len(truelabels)):
		if truelabels[la] == predictedlabels[la]:
			correct += 1
	print correct