Ejemplo n.º 1
0
def speech_to_text(audio):

    authenticator = IAMAuthenticator(
        'cCRifK-sspu8RhZ41WVx8bT8GWaHBR-sI90MwIHn7YOx')
    speech_to_text = Stt(authenticator=authenticator)

    speech_to_text.set_service_url(
        'https://api.us-south.speech-to-text.watson.cloud.ibm.com/instances/6c83017a-3519-4b2d-936b-9476a6757492'
    )

    # La variable 'file' contendrá el audio, no sé si vas a usar el método open para abrirlo yo supongo
    # Pero bueno, eso no es problema, simplemente de donde obtengas el file lo abres y lo almacenas en la variable
    # Voy a asumir que ya está creada
    # EN el open iria la ruta

    with open(audio, 'rb') as file:

        resultado = speech_to_text.recognize(
            audio=file,
            content_type='audio/mp3',
            word_alternatives_threshold=0.9,
            model='es-MX_BroadbandModel').get_result()
        json_string = json.dumps(resultado, indent=2)
        data_resultado = json.loads(json_string)
        # Este texto es el que pasaremos por todo el procedimiento a partir de la linea 35 del otro archivo jeje
        text = data_resultado['results'][0]['alternatives'][0]['transcript']
        print(text)

    return text
Ejemplo n.º 2
0
def transcribe_audio(path_to_audio_file):
    authenticator = IAMAuthenticator(
        'hLItcSq3n1DJdH8SDHYxsLdn0MPpqLP8Wo30WO9Ea-OL')

    username = os.environ.get("BLUEMIX_USERNAME")
    password = os.environ.get("BLUEMIX_PASSWORD")
    speech_to_text = SpeechToText(authenticator=authenticator)
    speech_to_text.set_service_url(
        'https://api.us-east.speech-to-text.watson.cloud.ibm.com/instances/f56cac90-ea6c-4c6e-a0d2-f6a07b9ce7e9'
    )
    sstring = 'speechText'

    dir_path = os.path.dirname(os.path.realpath(__file__))
    print(dir_path)
    dir_path = dir_path.replace(sstring, '')

    with open(dir_path + "/speech.wav", 'rb') as audio_file:
        return speech_to_text.recognize(audio_file,
                                        content_type='audio/wav').get_result()