Streaming using the Python SDK

Streaming using the SDK

Install the Behavioral Signals SDK from PyPI:

pip install behavioralsignals

Then you can easily start a stream as such:

from behavioralsignals import Client, StreamingOptions
from behavioralsignals.utils import make_audio_stream, print_results

client = Client()

audio_stream, sample_rate = make_audio_stream(
    "audio.wav",
    chunk_size=0.25,
)

options = StreamingOptions(
    sample_rate=sample_rate,
    encoding="LINEAR_PCM",
)

for result in client.behavioral.stream_audio(
    audio_stream=audio_stream,
    options=options,
):
    print_results(result.results)

Client() reads your credentials from the BEHAVIORALSIGNALS_CID and BEHAVIORALSIGNALS_API_KEY environment variables (see setup).

chunk_size is specified in seconds, so 0.25 corresponds to 250 ms.

  • The client.behavioral.stream_audio method expects an Iterator[bytes] that corresponds to the bytes of the audio stream.
  • The StreamingOptions define the sample rate and encoding of the streamed audio.
  • The level option of StreamingOptions sets which results you get. It defaults to segment, so the SDK returns only segment results. Use level="utterance" for utterance results, or level="all" for both. A bare gRPC client gets both by default.
  • To also get Focus results, add e.g. focus=["angry", "low_confidence"] and, optionally, sensitivity="high" to the StreamingOptions.

Also check the 🎤 streaming from microphone and 📁 streaming from file examples in the Github repo


Did this page help you?