🐛[bugfix] Model : resultを取得する際にエラーが出る問題を修正

This commit is contained in:
misyaguziya
2024-12-21 17:34:10 +09:00
parent 0c500effba
commit b545abce44

View File

@@ -51,12 +51,11 @@ class AudioTranscriber:
audio, time_spoken = audio_queue.get() audio, time_spoken = audio_queue.get()
self.updateLastSampleAndPhraseStatus(audio, time_spoken) self.updateLastSampleAndPhraseStatus(audio, time_spoken)
result = {"confidence": 0, "text": "", "language": None} confidences = [{"confidence": 0, "text": "", "language": None}]
try: try:
audio_data = self.audio_sources["process_data_func"]() audio_data = self.audio_sources["process_data_func"]()
match self.transcription_engine: match self.transcription_engine:
case "Google": case "Google":
confidences = []
for language, country in zip(languages, countries): for language, country in zip(languages, countries):
try: try:
text, confidence = self.audio_recognizer.recognize_google( text, confidence = self.audio_recognizer.recognize_google(
@@ -67,12 +66,7 @@ class AudioTranscriber:
confidences.append({"confidence": confidence, "text": text, "language": language}) confidences.append({"confidence": confidence, "text": text, "language": language})
except Exception: except Exception:
pass pass
result = max(confidences, key=lambda x: x["confidence"])
case "Whisper": case "Whisper":
confidences = []
audio_data = np.frombuffer(audio_data.get_raw_data(convert_rate=16000, convert_width=2), np.int16).flatten().astype(np.float32) / 32768.0 audio_data = np.frombuffer(audio_data.get_raw_data(convert_rate=16000, convert_width=2), np.int16).flatten().astype(np.float32) / 32768.0
if isinstance(audio_data, torch.Tensor): if isinstance(audio_data, torch.Tensor):
audio_data = audio_data.detach().numpy() audio_data = audio_data.detach().numpy()
@@ -99,7 +93,6 @@ class AudioTranscriber:
confidences.append({"confidence": info.language_probability, "text": text, "language": language}) confidences.append({"confidence": info.language_probability, "text": text, "language": language})
if (len(languages) == 1) or (transcription_lang[language][country][self.transcription_engine] == info.language): if (len(languages) == 1) or (transcription_lang[language][country][self.transcription_engine] == info.language):
break break
result = max(confidences, key=lambda x: x["confidence"])
except UnknownValueError: except UnknownValueError:
pass pass
@@ -108,6 +101,7 @@ class AudioTranscriber:
finally: finally:
pass pass
result = max(confidences, key=lambda x: x["confidence"])
if result["text"] != "": if result["text"] != "":
self.updateTranscript(result) self.updateTranscript(result)
return True return True