2015-11-04 64 views
0

我想使用speech_recognition 3.1.2使用Python 3.4,但我一直在麻煩整個時間。Python Speech_Recognition Bad Results

最初嘗試使用剛剛的例子WAV識別我得到類型錯誤時:「STR」不支持緩衝區的接口,所以我通過源梳理,並提出了以下變化:從

def read(self, size = -1): 
     buffer = self.wav_reader.readframes(self.wav_reader.getnframes() if size == -1 else size) 
     if type(buffer) is str: 
      buffer = buffer.encode(encoding="utf-8", errors="strict") 
      print(buffer) 
     if self.wav_reader.getnchannels() != 1: # stereo audio 
      try: 
       buffer = audioop.tomono(buffer, self.wav_reader.getsampwidth(), 1, 1) # convert stereo audio data to mono 
      except Exception as e: 
       print(e) 
     return buffer 

def read(self, size = -1): 
     buffer = self.wav_reader.readframes(self.wav_reader.getnframes() if size == -1 else size) 
     if self.wav_reader.getnchannels() != 1: # stereo audio 
      buffer = audioop.tomono(buffer, self.wav_reader.getsampwidth(), 1, 1) # convert stereo audio data to mono 
     return buffer 

雖然現在不會發生錯誤,但轉錄質量卻很差。我可以非常準確地運行python -m speech_recognition,所以我不確定發生了什麼。我將energy_threshold提高到4000以確保它不是環境噪音問題。我甚至使用了2種不同的識別服務(IBM和Google語音識別)。此外,由於某種原因,最後2個緩衝區是空字符串,然後我必須轉換爲字節對象

b'' 
b'' 

任何意見都會很棒!

+0

有可能是在字符串的處理方式方面差異巨大的蟒蛇 2和3之間,你可以看看:http://python3porting.com/ problems.html。所以不知道這個改變是不是微不足道的。 – toine

+0

@toine這就是我最初的想法,但它看起來像他們在Python 3中寫的所有東西,考慮到我得到的錯誤,這很奇怪。 –

回答

1

我推出了v3.1.3,它應該解決這個問題。升級pip install --upgrade SpeechRecognition並嘗試修復!

實際上有兩個因素在這裏:

  • 沒有在Python chunk的圖書館,它返回一個字符串,而不是一個空字節的對象,如果文件指針處於或超過文件末尾的錯誤。這在幾個月前已經修復,但是現在使用的大多數Python版本仍然存在該錯誤。
  • 立體聲音頻未正確轉換爲單聲道 - 聲道仍設置爲立體聲。這導致了一些有趣的聽起來音頻!

看到的變化在這裏:https://github.com/Uberi/speech_recognition