Python 给出函数变量的值

Python 给出函数变量的值,python,Python,我有一个VoiceActivityDetector的代码,想要给出函数中的值speech_ratio 我尝试设置一个新函数来打印值 def __init__(self, wave_input_filename): self._read_wav(wave_input_filename)._convert_to_mono() self.sample_window = 0.02 #20 ms self.sample_overlap = 0.01 #10

我有一个VoiceActivityDetector的代码,想要给出函数中的值speech_ratio

我尝试设置一个新函数来打印值

 def __init__(self, wave_input_filename):
        self._read_wav(wave_input_filename)._convert_to_mono()
        self.sample_window = 0.02 #20 ms
        self.sample_overlap = 0.01 #10ms
        self.speech_window = 0.5 #half a second
        self.speech_energy_threshold = 0.6 #60% of energy in voice band
        self.speech_start_band = 300
        self.speech_end_band = 3000
        #self.speech_ratio = 0



def detect_speech(self):
        """ Detects speech regions based on ratio between speech band energy
        and total energy.
        Output is array of window numbers and speech flags (1 - speech, 0 - nonspeech).
        """
        detected_windows = np.array([])
        sample_window = int(self.rate * self.sample_window)
        sample_overlap = int(self.rate * self.sample_overlap)
        data = self.data
        sample_start = 0
        start_band = self.speech_start_band
        end_band = self.speech_end_band
        while (sample_start < (len(data) - sample_window)):
            sample_end = sample_start + sample_window
            if sample_end>=len(data): sample_end = len(data)-1
            data_window = data[sample_start:sample_end]
            energy_freq = self._calculate_normalized_energy(data_window)
            sum_voice_energy = self._sum_energy_in_band(energy_freq, start_band, end_band)
            sum_full_energy = sum(energy_freq.values())
            speech_ratio = sum_voice_energy/sum_full_energy
            #self.speech_ratio2 = speech_ratio
            # Hipothesis is that when there is a speech sequence we have ratio of energies more than Threshold
            speech_ratio = speech_ratio>self.speech_energy_threshold

            detected_windows = np.append(detected_windows,[sample_start, speech_ratio])
            sample_start += sample_overlap
        detected_windows = detected_windows.reshape(int(len(detected_windows)/2),2)
        detected_windows[:,1] = self._smooth_speech_detection(detected_windows)
        return detected_windows

def printing(self):
            print(self.speech_ratio)
            return self.speech_ratio
def\uuuu init\uuuuuuuuuuuuu(self,wave\u input\u文件名):
self._read_wav(wave_input_filename)。_convert_to_mono()
self.sample_window=0.02#20 ms
自采样重叠=0.01#10ms
self.speech_window=0.5#半秒
self.speech_energy_threshold=0.6#声带能量的60%
self.speech\u start\u波段=300
self.speech\u end\u波段=3000
#self.speech\u比率=0
def检测_语音(自我):
“”“根据语音频带能量之间的比率检测语音区域。”
和总能量。
输出是窗口编号和语音标志的数组(1-语音,0-非语音)。
"""
检测到\u windows=np.array([])个
样本窗口=int(自率*自样本窗口)
样本重叠=int(自率*自样本重叠)
data=self.data
示例_start=0
开始\u波段=self.speech\u开始\u波段
end_波段=self.speech_end_波段
而(样本开始<(镜头(数据)-样本窗口)):
样本结束=样本开始+样本窗口
如果样本长度>=len(数据):样本长度=len(数据)-1
数据窗口=数据[样本开始:样本结束]
能量\频率=自身。\计算\归一化\能量(数据窗口)
sum\u voice\u energy=self.\u sum\u energy\u在\u波段(energy\u freq、start\u波段、end\u波段)
sum\u full\u energy=sum(energy\u freq.values())
语音比=总语音能量/总满能量
#self.speech\u ratio2=speech\u比率
#Hipothesis是指当存在语音序列时,我们的能量比超过阈值
语音比=语音比>自语音能量阈值
检测到的\u窗口=np.append(检测到的\u窗口,[示例\u开始,语音\u比率])
样本\u开始+=样本\u重叠
检测到的窗口=检测到的窗口。重新整形(int(len(检测到的窗口)/2),2)
检测到的\u窗口[:,1]=自平滑\u语音\u检测(检测到的\u窗口)
返回检测到的窗口
def打印(自):
打印(自述率)
返回自语言比
当我在init中将speech_ratio设置为变量时,它不会在以后的detect_speech函数中更改该变量。
如果我没有在init函数中初始化speech\u ratio,它将根本不是我对象的属性。

您可以使用
self.speech\u ratio
尝试打印值;您应该使用相同的表达式来分配它。

较小的示例代码通常更容易解决此类问题