Skip to content

Commit 5aaa190

Browse files
committed
refactor example code
1 parent b938ad9 commit 5aaa190

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

python-api-examples/speaker-identification.py

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python3
12
"""
23
This script shows how to use Python APIs for speaker identification with
34
a microphone.
@@ -41,16 +42,29 @@
4142
--model ./wespeaker_zh_cnceleb_resnet34.onnx
4243
"""
4344

45+
import argparse
4446
import functools
47+
import queue
48+
import threading
4549
from collections import defaultdict
4650
from pathlib import Path
47-
from typing import Dict, List, Literal, Tuple, Union
51+
from typing import Dict, List, Literal, Optional, Tuple, Union
4852

4953
import numpy as np
5054
import sherpa_onnx
5155
import soundfile as sf
5256
from numpy.typing import NDArray
5357

58+
try:
59+
import sounddevice as sd
60+
except ImportError:
61+
print("Please install sounddevice first. You can use\n\t")
62+
print("pip install sounddevice")
63+
print("\nto install it")
64+
import sys
65+
66+
sys.exit(1)
67+
5468

5569
def load_speaker_embedding_model(
5670
model: Union[str, Path],
@@ -166,25 +180,6 @@ def compute_avg_speaker_embedding(
166180
return embeddings_sum / len(filenames)
167181

168182

169-
# %%
170-
# The following code is required for command line interface.
171-
# If you only need the packaged functions, you can use only the code above
172-
import argparse
173-
import queue
174-
import threading
175-
from typing import Optional
176-
177-
try:
178-
import sounddevice as sd
179-
except ImportError:
180-
print("Please install sounddevice first. You can use\n\t")
181-
print("pip install sounddevice")
182-
print("\nto install it")
183-
import sys
184-
185-
sys.exit(1)
186-
187-
188183
class Args(argparse.Namespace):
189184
speaker_file: Path
190185
model: Path
@@ -274,8 +269,8 @@ def print_microphone_device_info(self) -> None:
274269
print("Microphone device information:\n")
275270
print(f"Device ID: {device_info['index']}")
276271
print(f"Name: {device_info['name']}")
277-
print(f"Default Channels: {device_info['max_input_channels']}")
278-
print(f"Default SampleRate: {device_info['default_samplerate']}")
272+
print(f"Default Microphone Channels: {device_info['max_input_channels']}")
273+
print(f"Default Microphone SampleRate: {device_info['default_samplerate']}")
279274
print("=" * 50)
280275

281276
def read_mic(self) -> None:
@@ -315,7 +310,7 @@ def infer_speaker(
315310
stream.input_finished()
316311

317312
embedding = np.array(extractor.compute(stream), dtype=np.float32)
318-
name = manager.search(embedding, threshold=threshold)
313+
name: str = manager.search(embedding, threshold=threshold)
319314
return name or "unknown"
320315

321316

@@ -343,16 +338,12 @@ def main() -> None:
343338
input("Press Enter to stop recording")
344339
recorder.stop_recording()
345340

346-
print("Compute embedding")
347341
name = recorder.infer_speaker(extractor, manager, args.threshold)
348-
print(f"Predicted name: {name}")
342+
print(f"Predicted name: {name}\n")
349343

350344

351345
if __name__ == "__main__":
352346
try:
353347
main()
354348
except KeyboardInterrupt:
355349
print("\nCaught Ctrl + C. Exiting")
356-
except Exception as e:
357-
print(e)
358-
raise

0 commit comments

Comments
 (0)