Google Speech-to-text api因多处理python而失败

问题描述 投票:0回答:1

我是新手,正在尝试将google-cloud语音转文本与python和多处理结合使用。这是一个重现我的问题的简单示例。我正在Windows上运行代码。

当我运行代码而不进行多处理时,它可以正常工作。

import io
from tqdm import tqdm
from multiprocessing import Pool, freeze_support, cpu_count

from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types

# Instantiates a client
CLIENT = speech.SpeechClient()

def speech_to_text(file_name, language= "en-US"):

    with io.open(file_name, 'rb') as audio_file:
        content = audio_file.read()
        audio = types.RecognitionAudio(content=content)

    config = types.RecognitionConfig(
        encoding=enums.RecognitionConfig.AudioEncoding.ENCODING_UNSPECIFIED,
        sample_rate_hertz=16000,
        language_code= language)
    # Detects speech in the audio file
    response = CLIENT.recognize(config, audio)
    transcript = ""
    if len(response.results):
        transcript = response.results[0].alternatives[0].transcript
    return transcript

def worker(ix):
    audio_file_name = "audio.mp3"
    transcript = speech_to_text(audio_file_name)

if __name__ == "__main__":

    n_cores = cpu_count() - 1
    freeze_support()  # for Windows support
    with Pool(n_cores) as p:
        max_ = len(range(2))
        with tqdm(total=max_) as pbar:
            for i, result in enumerate(tqdm(p.imap_unordered(worker, range(2)))):
                pbar.update()

这是我收到的错误消息:

Traceback (most recent call last):
  File "C:\Users\me\Anaconda3\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "C:\Users\me\Anaconda3\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\me\Anaconda3\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\Users\me\Anaconda3\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\me\Anaconda3\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\me\Anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\me\Desktop\outCaptcha\multiproc.py", line 10, in <module>
    from google.cloud import speech
  File "C:\Users\me\.virtualenvs\outCaptcha\lib\site-packages\google\cloud\speech.py", line 20, in <module>
    from google.cloud.speech_v1 import SpeechClient
  File "C:\Users\me\.virtualenvs\outCaptcha\lib\site-packages\google\cloud\speech_v1\__init__.py", line 17, in <module>
    from google.cloud.speech_v1.gapic import speech_client
  File "C:\Users\me\.virtualenvs\outCaptcha\lib\site-packages\google\cloud\speech_v1\gapic\speech_client.py", line 23, in <module>
    import google.api_core.client_options
  File "C:\Users\me\.virtualenvs\outCaptcha\lib\site-packages\google\api_core\__init__.py", line 23, in <module>
    __version__ = get_distribution("google-api-core").version
  File "C:\Users\me\AppData\Roaming\Python\Python37\site-packages\pkg_resources\__init__.py", line 481, in get_distribution
    dist = get_provider(dist)
  File "C:\Users\me\AppData\Roaming\Python\Python37\site-packages\pkg_resources\__init__.py", line 357, in get_provider
    return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
  File "C:\Users\me\AppData\Roaming\Python\Python37\site-packages\pkg_resources\__init__.py", line 900, in require
    needed = self.resolve(parse_requirements(requirements))
  File "C:\Users\me\AppData\Roaming\Python\Python37\site-packages\pkg_resources\__init__.py", line 786, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'google-api-core' distribution was not found and is required by the application

非常感谢您的帮助。如果您需要有关此问题的任何详细信息,请告诉我

python-3.x google-api-python-client google-speech-api
1个回答
1
投票

就我而言,这解决了问题。

  • easy_install --upgrade google-api-core
  • easy_install --upgradegoogle-cloud-speech

我希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.