- Install the SpeechRecognition library: use the pip command to enter the following command in the terminal to install:
plaintextCopy code
pip install SpeechRecognition
- Import the SpeechRecognition library: Import the SpeechRecognition library in the Python code, as follows:
pythonCopy code
import speech_recognition as sr
- Create a Recognizer object: Use the Recognizer class to create a Recognizer object as follows:
pythonCopy code
r = sr.Recognizer()
- 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)
- 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)
- Output converted text: Output the converted text to the console or write to a text file, as follows:
pythonCopy code
print("转换后的文字为:", text)
pythonCopy code
with open("output.txt", "w") as file: file.write(text)
Use python to convert language to text
Guess you like
Origin blog.csdn.net/ihateright/article/details/130912492#comments_26734308
Ranking