Spaces:
Building
Building
Create test_single_wav.py
Browse files- stt/test_single_wav.py +71 -0
stt/test_single_wav.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
"""
|
3 |
+
Single WAV file tester
|
4 |
+
Usage: python test_single_wav.py <wav_file>
|
5 |
+
"""
|
6 |
+
import sys
|
7 |
+
import wave
|
8 |
+
from google.cloud import speech
|
9 |
+
|
10 |
+
def test_wav_file(wav_file_path):
|
11 |
+
try:
|
12 |
+
print(f"Testing WAV file: {wav_file_path}")
|
13 |
+
|
14 |
+
# Read WAV file
|
15 |
+
with wave.open(wav_file_path, 'rb') as wav_file:
|
16 |
+
n_channels = wav_file.getnchannels()
|
17 |
+
sample_width = wav_file.getsampwidth()
|
18 |
+
sample_rate = wav_file.getframerate()
|
19 |
+
n_frames = wav_file.getnframes()
|
20 |
+
wav_audio = wav_file.readframes(n_frames)
|
21 |
+
|
22 |
+
print(f"WAV Info: {n_channels}ch, {sample_width*8}bit, {sample_rate}Hz, {n_frames} frames, {n_frames/sample_rate:.2f}s")
|
23 |
+
|
24 |
+
# Google STT client
|
25 |
+
credentials_path = "./credentials/google-service-account.json"
|
26 |
+
client = speech.SpeechClient.from_service_account_file(credentials_path)
|
27 |
+
|
28 |
+
# Config
|
29 |
+
config = speech.RecognitionConfig(
|
30 |
+
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
|
31 |
+
sample_rate_hertz=sample_rate,
|
32 |
+
language_code="tr-TR",
|
33 |
+
audio_channel_count=n_channels,
|
34 |
+
enable_separate_recognition_per_channel=False,
|
35 |
+
)
|
36 |
+
|
37 |
+
# Audio
|
38 |
+
audio = speech.RecognitionAudio(content=wav_audio)
|
39 |
+
|
40 |
+
# Recognize
|
41 |
+
print("Sending to Google API...")
|
42 |
+
response = client.recognize(config=config, audio=audio)
|
43 |
+
|
44 |
+
print(f"Response: {response}")
|
45 |
+
print(f"Results count: {len(response.results)}")
|
46 |
+
|
47 |
+
if response.results:
|
48 |
+
for i, result in enumerate(response.results):
|
49 |
+
print(f"Result {i}: {result}")
|
50 |
+
if result.alternatives:
|
51 |
+
print(f" Transcript: '{result.alternatives[0].transcript}'")
|
52 |
+
print(f" Confidence: {result.alternatives[0].confidence}")
|
53 |
+
return True
|
54 |
+
else:
|
55 |
+
print("No results returned")
|
56 |
+
return False
|
57 |
+
|
58 |
+
except Exception as e:
|
59 |
+
print(f"Error: {e}")
|
60 |
+
import traceback
|
61 |
+
traceback.print_exc()
|
62 |
+
return False
|
63 |
+
|
64 |
+
if __name__ == "__main__":
|
65 |
+
if len(sys.argv) != 2:
|
66 |
+
print("Usage: python test_single_wav.py <wav_file>")
|
67 |
+
sys.exit(1)
|
68 |
+
|
69 |
+
wav_file = sys.argv[1]
|
70 |
+
success = test_wav_file(wav_file)
|
71 |
+
sys.exit(0 if success else 1)
|