在Python 3.2(Windows)中导入pocketsphinx失败:"ImportError: No module named pocketsphinx

0 投票
1 回答
2571 浏览
提问于 2025-04-17 18:22

我正在尝试在 Python 3.2 上使用 pocketsphinx,但我感到很困惑。我把构建好的包放在了 site-packages 目录里,并确保我使用的是正确的音频文件格式。根据文档,Python 是被支持的,因为有 Python 的绑定。我在论坛上查了一下,似乎需要一个 _init_.py 文件,但这个文件没有包含在内,这可能是个问题?有什么建议可以解决这个问题吗?

我目前使用的是 pocketsphinx 和 sphinxbase 版本 0.8,下载链接和链接。我放入 site-packages 的文件都是 pocketsphinx 目录下 Debug 文件夹中的文件,具体包括:

  • sphinxbase.dll
  • pocketsphinx(.dll, .exp, .ilk, .lib, .pdb)
  • pocketsphinx_batch(.exe, .ilk, .pdb)
  • pocketsphinx_continuous(.exe, .ilk, .pdb)
  • pocketsphinx_mdef_convert(.exe, .ilk, .pdb)

我使用的 Python 代码如下:

import pocketsphinx as ps
from pocketsphinx import sphinxbase

hmmd = 'C:\Python32\Lib\site-packages\pocketsphinx\model\hmm\en_US\hub4wsj_sc_8k'
lmd = 'C:\Python32\Lib\site-packages\pocketsphinx\model\lm\en_US\hub4.5000.DMP'
dictd = 'C:\Python32\Lib\site-packages\pocketsphinx\model\lm\en_US\hub4.5000.dic'
fraw1 = file(r'C:\Users\Stefan\Documents\2012\40I6\test1.wav', 'rb')
fraw1.seek(44)
speechRec = ps.Decoder(hmm = hmmd, lm = lmd, dict = dictd) speechRec.decode_raw(fRaw1)
result = speechRec.get_hyp()
print (result[0])

1 个回答

1

要编译Python模块,可以按照以下步骤操作:

  1. 根据需要,用Visual Studio构建sphinxbase和pocketsphinx。
  2. 把sphinxbase.lib和sphinxbase.dll从sphinxbase/bin/Release文件夹复制到sphinxbase/python和pocketsphinx/python文件夹。
  3. 把pocketsphinx.lib和pocketsphinx.dll从pocketsphinx/bin/Release文件夹复制到sphinxbase/python和pocketsphinx/python文件夹。
  4. 打开终端。
  5. 切换到sphinxbase/python目录。
  6. 运行以下命令:

    python setup_win32.py install

  7. 切换到pocketsphinx/python目录。

  8. 运行以下命令:

    python setup_win32.py install

  9. 运行命令来测试Python:

    python ps_test.py

撰写回答