Use python to convert language to text

  1. Install the SpeechRecognition library: use the pip command to enter the following command in the terminal to install:
     

    plaintextCopy code

    pip install SpeechRecognition

  2. Import the SpeechRecognition library: Import the SpeechRecognition library in the Python code, as follows:
     

    pythonCopy code

    import speech_recognition as sr

  3. Create a Recognizer object: Use the Recognizer class to create a Recognizer object as follows:
     

    pythonCopy code

    r = sr.Recognizer()

  4. Read audio data from an audio file: Use the AudioFile class to read audio data from an audio file as follows:
     

    pythonCopy code

    with sr.AudioFile(audio_file) as source: audio_data = r.record(source)

    Note: Here, audio_file is the path of the audio file you want to convert to text.
  5. Call the recognize_google() method for speech recognition: Use the recognize_google() method to perform speech recognition on the read audio data, as follows:
     

    pythonCopy code

    text = r.recognize_google(audio_data)

  6. Output converted text: Output the converted text to the console or write to a text file, as follows:
     

    pythonCopy code

    print("转换后的文字为:", text)

    or:
     

    pythonCopy code

    with open("output.txt", "w") as file: file.write(text)

Guess you like

Origin blog.csdn.net/ihateright/article/details/130912492#comments_26734308