I'm trying to use the PocketSphinx speech recognition engine in my Python script, but I keep encountering an error stating "missing PocketSphinx module: ensure that PocketSphinx is set up correctly." I've already installed PocketSphinx using pip, but it seems like Python can't find the module.
Here's the relevant portion of my code:
from pydub import AudioSegment
import os
# Function to convert OPUS files to WAV format
def convert_opus_to_wav(opus_file, output_wav_file):
sound = AudioSegment.from_file(opus_file)
sound.export(output_wav_file, format="wav")
# Function to transcribe audio using a speech-to-text service
import speech_recognition as sr
def transcribe_audio(audio_file):
recognizer = sr.Recognizer()
with sr.AudioFile(audio_file) as source:
audio_data = recognizer.record(source)
try:
transcribed_text = recognizer.recognize_sphinx(audio_data)
return transcribed_text
except sr.UnknownValueError:
print("Speech recognition could not understand audio")
except sr.RequestError as e:
print(f"Error with PocketSphinx recognizer: {e}")
return "" # Return empty string if transcription fails
# Directory containing OPUS files
opus_dir = "/Users/ethanhumphreys/Downloads/Blinkist SiteRip Audio Collection (August 2023)"
# Create a directory to store text files
output_dir = "/Users/ethanhumphreys/Documents/txt files"
os.makedirs(output_dir, exist_ok=True)
# Iterate through each OPUS file
for filename in os.listdir(opus_dir):
if filename.endswith(".opus"):
opus_file = os.path.join(opus_dir, filename)
# Convert OPUS file to WAV
wav_file = os.path.join(output_dir, os.path.splitext(filename)[0] + ".wav")
convert_opus_to_wav(opus_file, wav_file)
# Transcribe the WAV file
transcribed_text = transcribe_audio(wav_file)
# Write transcribed text to a text file
text_output_file = os.path.join(output_dir, os.path.splitext(filename)[0] + ".txt")
with open(text_output_file, "w") as text_file:
text_file.write(transcribed_text)
I've verified that PocketSphinx is installed by running pip show pocketsphinx, which displays information about the installed package. However, when I run my script, I still get the error mentioned above.
Can anyone provide guidance on what might be causing this issue and how I can resolve it?
im on macbook by the way and all txt file return empty
full error message:
/usr/bin/python3 /Users/ethanhumphreys/PycharmProjects/botham.ox0001/aud to txt/main.py
/Users/ethanhumphreys/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:34: NotOpenSSLWarning: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
warnings.warn(
Error with PocketSphinx recognizer: missing PocketSphinx module: ensure that PocketSphinx is set up correctly.
Error with PocketSphinx recognizer: missing PocketSphinx module: ensure that PocketSphinx is set up correctly.
Error with PocketSphinx recognizer: missing PocketSphinx module: ensure that PocketSphinx is set up correctly.
Traceback (most recent call last):
File "/Users/ethanhumphreys/PycharmProjects/botham.ox0001/aud to txt/main.py", line 46, in <module>
convert_opus_to_wav(opus_file, wav_file)
File "/Users/ethanhumphreys/PycharmProjects/botham.ox0001/aud to txt/main.py", line 5, in convert_opus_to_wav
sound = AudioSegment.from_file(opus_file)
File "/Library/Python/3.9/site-packages/pydub/audio_segment.py", line 768, in from_file
p_out, p_err = p.communicate(input=stdin_data)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/subprocess.py", line 1134, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/subprocess.py", line 1979, in _communicate
ready = selector.select(timeout)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/selectors.py", line 416, in select
fd_event_list = self._selector.poll(timeout)
KeyboardInterrupt
Process finished with exit code 130 (interrupted by signal 2: SIGINT)
installation command:
ethanhumphreys@Ethans-MacBook-Air botham.ox0001 % pip install pocketsphinx
Collecting pocketsphinx
Downloading pocketsphinx-5.0.3-cp311-cp311-macosx_10_9_universal2.whl.metadata (6.8 kB)
Collecting sounddevice (from pocketsphinx)
Downloading sounddevice-0.4.6-py3-none-macosx_10_6_x86_64.macosx_10_6_universal2.whl.metadata (1.4 kB)
Requirement already satisfied: CFFI>=1.0 in /opt/homebrew/lib/python3.11/site-packages (from sounddevice->pocketsphinx) (1.16.0)
Requirement already satisfied: pycparser in /opt/homebrew/lib/python3.11/site-packages (from CFFI>=1.0->sounddevice->pocketsphinx) (2.21)
Downloading pocketsphinx-5.0.3-cp311-cp311-macosx_10_9_universal2.whl (29.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 29.6/29.6 MB 6.9 MB/s eta 0:00:00
Downloading sounddevice-0.4.6-py3-none-macosx_10_6_x86_64.macosx_10_6_universal2.whl (107 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 108.0/108.0 kB 12.0 MB/s eta 0:00:00
Installing collected packages: sounddevice, pocketsphinx
Successfully installed pocketsphinx-5.0.3 sounddevice-0.4.6