Is there way to Amazon KVS to lambda data streaming with Amazon connect?

90 views Asked by At

I'm using amazon kinesis video stream with lambda on python.

I'm trying to send audio stream to lambda from kvs.

These codes what I was tryed but error says "Source must be an audio source"

First, get stream audio. StreamingBody...

Second, Read da StreamingBody with read() function.

Third, Then I got da bytes data from stream.

At last, Listening bytes audio with SpeechRecognizer. then said "Source must be an audio source".

Please help me...


import io
import boto3
import asyncio
import speech_recognition as sr

kinesis_client = boto3.client("kinesisvideo")

def lambda_handler(event, context):
    media_stream = event["Details"]["ContactData"]["MediaStreams"]

    customer_audio = media_stream["Customer"]["Audio"]

    stream_arn = customer_audio["StreamARN"]

    # <botocore.response.StreamingBody object at 0x7f9c0d849810>
    audio_stream = getMedia(stream_arn)

    record_audio_stream(audio_stream)


def getMedia(streamArn):
    endpoint_response = kinesis_client.get_data_endpoint(
        StreamARN=streamArn, APIName="GET_MEDIA"
    )
    data_endpoint = endpoint_response["DataEndpoint"]

    kinesis_video_client = boto3.client(
        "kinesis-video-media", endpoint_url=data_endpoint
    )

    response = kinesis_video_client.get_media(
        StreamARN=streamArn, StartSelector={"StartSelectorType": "EARLIEST"}
    )

    payload = response["Payload"]

    return payload

def record_audio_stream(self, audio_stream):

    async def recording():
        recognizer = sr.Recognizer()

        audio_bytes = audio_stream.read()

        with io.BytesIO(audio_bytes) as source:
            audio = recognizer.listen(source)
            recognized_text = recognizer.recognize_google(audio)
            print(recognized_text)

    asyncio.run(recording())
0

There are 0 answers