Real-Time Transcription Intermediate
Real-time transcription converts speech to text as it is spoken, producing results within milliseconds. This lesson covers streaming APIs, microphone capture, handling partial results, and building low-latency transcription applications.
Streaming vs. Batch Transcription
| Aspect | Batch | Streaming |
|---|---|---|
| Input | Complete audio file | Audio chunks in real-time |
| Latency | Seconds to minutes | Milliseconds |
| Results | Final transcript only | Partial + final results |
| Use case | Recorded audio, podcasts | Live captions, voice interfaces |
Real-Time with Google Cloud Streaming
import pyaudio from google.cloud import speech # Audio recording parameters RATE = 16000 CHUNK = 1600 # 100ms chunks client = speech.SpeechClient() config = speech.RecognitionConfig( encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16, sample_rate_hertz=RATE, language_code="en-US", enable_automatic_punctuation=True, ) streaming_config = speech.StreamingRecognitionConfig( config=config, interim_results=True, # Get partial results ) # Open microphone stream audio = pyaudio.PyAudio() stream = audio.open( format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK ) def audio_generator(): while True: data = stream.read(CHUNK, exception_on_overflow=False) yield speech.StreamingRecognizeRequest(audio_content=data) requests = audio_generator() responses = client.streaming_recognize(streaming_config, requests) for response in responses: for result in response.results: if result.is_final: print(f"Final: {result.alternatives[0].transcript}") else: print(f"Partial: {result.alternatives[0].transcript}", end="\r")
Real-Time with Azure Speech SDK
import azure.cognitiveservices.speech as speechsdk speech_config = speechsdk.SpeechConfig( subscription="YOUR_KEY", region="eastus" ) # Use default microphone audio_config = speechsdk.AudioConfig(use_default_microphone=True) recognizer = speechsdk.SpeechRecognizer( speech_config=speech_config, audio_config=audio_config ) # Event handlers for real-time results def on_recognizing(evt): print(f"Partial: {evt.result.text}", end="\r") def on_recognized(evt): print(f"Final: {evt.result.text}") recognizer.recognizing.connect(on_recognizing) recognizer.recognized.connect(on_recognized) # Start continuous recognition recognizer.start_continuous_recognition() input("Press Enter to stop...\n") recognizer.stop_continuous_recognition()
Real-Time with Faster Whisper
For a fully local, privacy-preserving solution, you can stream audio chunks to Faster Whisper using a Voice Activity Detection (VAD) approach:
from faster_whisper import WhisperModel import numpy as np import pyaudio import webrtcvad model = WhisperModel("base", compute_type="int8") vad = webrtcvad.Vad(3) # Aggressiveness 0-3 RATE = 16000 CHUNK_DURATION = 0.03 # 30ms for VAD CHUNK = int(RATE * CHUNK_DURATION) audio = pyaudio.PyAudio() stream = audio.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNK) buffer = [] silence_count = 0 print("Listening... (Ctrl+C to stop)") while True: data = stream.read(CHUNK, exception_on_overflow=False) is_speech = vad.is_speech(data, RATE) if is_speech: buffer.append(data) silence_count = 0 elif buffer: silence_count += 1 if silence_count > 30: # ~1 second of silence audio_data = np.frombuffer(b"".join(buffer), dtype=np.int16) audio_float = audio_data.astype(np.float32) / 32768.0 segments, _ = model.transcribe(audio_float, beam_size=3) for seg in segments: print(seg.text, flush=True) buffer = [] silence_count = 0
Try It Yourself
Build a real-time transcription demo using your microphone. Start with the Azure or Google streaming example, then try the local Faster Whisper approach for comparison.
Next: Speaker Diarization →Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX