Speaker Identification for Streaming with Python
Tags: machinelearning, programming, python, challenge
Published: 2023-08-11 18:52:12

Open-source speaker recognition is the only option for developers, unless they work for large enterprises. Recently, at Picovoice we made our [internal tool for Speaker Recognition public](https://picovoice.ai/blog/speaker-recognition-for-developers/). So, those who prefer production-ready solutions now have an option! 

Let's get started!

**1. Install [Picovoice Eagle Speaker Recognition]**(https://picovoice.ai/platform/eagle/) using `pip`. We will be using [pvrecorder](https://picovoice.ai/company/#tools)  to get cross-platform audio, so install that as well:

```
pip3 install pveagle pvrecorder
```
**2. Grab your `AccessKey`**
If you haven't create an account on [Picovoice Console](https://console.picovoice.ai/) for free and grab your `AccessKey`

**3. Enroll Speakers**
Import `pveagle` and create an instance of the `EagleProfiler` class:

```
import pveagle

access_key = "{YOUR_ACCESS_KEY}";
try:
    eagle_profiler = pveagle.create_profiler(access_key=access_key)
except pveagle.EagleError as e:
    # Handle error
    pass
```
Don't forget to replace the placeholder with your `AccessKey`!

Now, import `pvrecorder` and create an instance of the recorder as well. Use the `EagleProfiler`'s `.min_enroll_samples` as the `frame_length`:

```
from pvrecorder import PvRecorder

DEFAULT_DEVICE_INDEX = -1
recorder = PvRecorder(
    device_index=DEFAULT_DEVICE_INDEX,
    frame_length=eagle_profiler.min_enroll_samples)
```
The `.enroll()` function generates a percentage value to know when Enrollment is done and another speaker can be enrolled:

```
recorder.start()

enroll_percentage = 0.0
while enroll_percentage < 100.0:
    audio_frame = recorder.read()
    enroll_percentage, feedback = eagle_profiler.enroll(audio_frame)

recorder.stop()
```
**4. Export the `Speaker Profile`** 

We'll need use in the next step to identify / verify the speaker!

```
speaker_profile = eagle_profiler.export()
```
You can reuse the `speaker_profile` object. Check out the [docs](https://picovoice.ai/docs/api/eagle-python/) for details. 

Add more speakers by creating additional profiles by calling the `.reset()` function on the `EagleProfiler`, and repeating the `.enroll()` step.

**5. Clean up used resources:**
Once you create profiles for all speakers, let's clean up used resources!

```
recorder.delete()
eagle_profiler.delete()
```
**6. Recognize Speakers:**

```
```
import pveagle

access_key = "{YOUR_ACCESS_KEY}"
profiles = [speaker_profile_1, speaker_profile_2]
try:
    eagle = pveagle.create_recognizer(
        access_key=access_key,
        speaker_pofiles=profiles)
except pveagle.EagleError as e:
    # Handle error
    pass
```
Set up `pvrecorder` to use with Eagle Speaker Recognition:

```
recorder = PvRecorder(
    device_index=DEFAULT_DEVICE_INDEX,
    frame_length=eagle.frame_length)
```
Pass audio frames into the `eagle.process()` function get back speaker scores:

```
while True:
    audio_frame = recorder.read()
    scores = eagle.process(audio_frame)
```
When finished, again clean up used resources:


```
eagle.delete()
```



## Connect them All Together

```
import pveagle
from pvrecorder import PvRecorder


DEFAULT_DEVICE_INDEX = -1
access_key = "{YOUR_ACCESS_KEY}";


# Step 1: Enrollment
try:
    eagle_profiler = pveagle.create_profiler(access_key=access_key)
except pveagle.EagleError as e:
    pass


enroll_recorder = PvRecorder(
    device_index=DEFAULT_DEVICE_INDEX,
    frame_length=eagle_profiler.min_enroll_samples)


enroll_recorder.start()


enroll_percentage = 0.0
while enroll_percentage < 100.0:
    audio_frame = enroll_recorder.read()
    enroll_percentage, feedback = eagle_profiler.enroll(audio_frame)


enroll_recorder.stop()


speaker_profile = eagle_profiler.export()


enroll_recorder.delete()
eagle_profiler.delete()


# Step 2: Recognition
try:
    eagle = pveagle.create_recognizer(
        access_key=access_key,
        speaker_pofiles=[speaker_profile])
except pveagle.EagleError as e:
    pass


recognizer_recorder = PvRecorder(
    device_index=DEFAULT_DEVICE_INDEX,
    frame_length=eagle.frame_length)


recognizer_recorder.start()


while True:
    audio_frame = recorder.read()
    scores = eagle.process(audio_frame)
    print(scores)


recognizer_recorder.stop()


recognizer_recorder.delete()
eagle.delete()
```

---
For more information:

- [Check the original article],(https://picovoice.ai/blog/speaker-recognition-in-python/)
- Learn more about Speaker [Recognition](https://picovoice.ai/blog/speaker-recognition/), [Identification](https://picovoice.ai/blog/speaker-identification/) and [Verification](https://picovoice.ai/blog/voice-biometrics/),
- [Check out other Eagle Speaker Recognition SDKs](https://picovoice.ai/docs/eagle/)
- Visit [Eagle Speaker Recognition GitHub repository](https://github.com/Picovoice/eagle) for open-source demos and to create issues. While on GitHub, if you like building with Eagle Speaker Recognition, give it a star and help fellow devs find it easily.