# coding=utf-8

__author__ = 'hou'

from sklearn.externals import joblib
import numpy as np
import wave
import mfcc

def interval_size(Yp, interval):
	last = Yp[0]
	last_step = 0
	for i in range(0, len(Yp)):
		if Yp[i] != last:
			delta = i - last_step
			if delta < interval:
				Yp[last_step:i] = Yp[i]
			else:
				last_step = i
		last = Yp[i]

def window(Y, duration):
	time_window = duration / 60
        if time_window < 5:
                time_window = 5
	threshold = time_window
	Yp = np.ones(len(Y), dtype=np.int)

	for i in range(0, len(Y)):
		d = Y[np.max([0, i - time_window]) : np.min([i + time_window, len(Y)])]
		if (np.sum(d) < threshold):
			Yp[i] = 0

        # The interval of songs and speech should not be shorter than the window
        interval_size(Yp, time_window)
	return Yp

def run(input_wav, model):
        f = wave.open(input_wav, "r")
        duration = f.getnframes() / f.getframerate()

	X = mfcc.extract(input_wav, duration)

	clf = joblib.load(model)

	Y = clf.predict(X[:,:])
	#np.savetxt("Y.txt", Y)
	Yp = window(Y, duration)
	#np.savetxt("Yp.txt", Yp)

	last = Yp[0]
	delimit_point = [0]
	for i in range(0, len(Yp)):
		if Yp[i] != last:
			delimit_point.append(i)
		        last = Yp[i]

	return Yp, delimit_point

@@ -110,16 +110,139 @@
 t mfcc%0A%0A
+# smooth tiny interval%0A# 1100000111000 --%3E 000000000000 (111 %3C interval)%0A# 0011111000111 --%3E 111111111111 (000 %3C interval)%0A
 def inte
@@ -281,25 +281,21 @@
  Yp%5B0%5D%0A%09
-last_step
+start
  = 0%0A%09fo
@@ -355,25 +355,21 @@
 a = i - 
-last_step
+start
 %0A%09%09%09if d
@@ -392,25 +392,21 @@
 %0A%09%09%09%09Yp%5B
-last_step
+start
 :i%5D = Yp
@@ -426,17 +426,13 @@
 %09%09%09%09
-last_step
+start
  = i
@@ -448,16 +448,76 @@
  Yp%5Bi%5D%0A%0A
+# smooth error labels which occupy littlse in a window size%0A
 def wind

Add comment