【问题标题】:How to import google cloud speech recognition in Google App Engine (python)如何在谷歌应用引擎(python)中导入谷歌云语音识别
【发布时间】:2018-07-05 16:42:19
【问题描述】:

我想在我的 Google App Engine python 应用程序上使用 google.cloud 库。自从我在本地安装了这个库以来,我所有的测试都在我的本地工作。我希望 GAE 默认支持它,但它是 not supported

这是我得到的错误:

from google.cloud import speech
ImportError: No module named cloud

我查看了在 https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27 添加 3rd 方库

按照说明操作后,我又遇到了一个错误。

(/base/alloc/tmpfs/dynamic_runtimes/python27/54c5883f70296ec8_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py:263)
Traceback (most recent call last):
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/54c5883f70296ec8_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/54c5883f70296ec8_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/54c5883f70296ec8_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/base/data/home/apps/s~goooogle-translate/20180126t023051.407206565499030997/main.py", line 18, in <module>
    from google.cloud import speech
  File "/base/data/home/apps/s~goooogle-translate/20180126t023051.407206565499030997/lib/google/cloud/speech/__init__.py", line 22, in <module>
    from google.cloud.speech.client import Client
  File "/base/data/home/apps/s~goooogle-translate/20180126t023051.407206565499030997/lib/google/cloud/speech/client.py", line 25, in <module>
    from google.cloud.speech._gax import GAPICSpeechAPI
  File "/base/data/home/apps/s~goooogle-translate/20180126t023051.407206565499030997/lib/google/cloud/speech/_gax.py", line 17, in <module>
    from google.cloud.gapic.speech.v1.speech_client import SpeechClient
  File "/base/data/home/apps/s~goooogle-translate/20180126t023051.407206565499030997/lib/google/cloud/gapic/speech/v1/speech_client.py", line 31, in <module>
    from google.gapic.longrunning import operations_client
  File "/base/data/home/apps/s~goooogle-translate/20180126t023051.407206565499030997/lib/google/gapic/longrunning/operations_client.py", line 45, in <module>
    from google.gax import api_callable
  File "/base/data/home/apps/s~goooogle-translate/20180126t023051.407206565499030997/lib/google/gax/__init__.py", line 36, in <module>
    import multiprocessing as mp
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/54c5883f70296ec8_unzipped/python27_dist/lib/python2.7/multiprocessing/__init__.py", line 65, in <module>
    from multiprocessing.util import SUBDEBUG, SUBWARNING
  File "/base/alloc/tmpfs/dynamic_runtimes/python27/54c5883f70296ec8_unzipped/python27_dist/lib/python2.7/multiprocessing/util.py", line 41, in <module>
    from subprocess import _args_from_interpreter_flags
ImportError: cannot import name _args_from_interpreter_flags

我几乎不明白这一点。如果您之前已经这样做过,请告诉我如何在 GAE 中设置 google.cloud。

【问题讨论】:

  • 尝试:sudo pip install --upgrade google-cloud-speech
  • @BenyaminJafari 我尝试过并尝试再次部署,但源代码没有更改:将 0 个文件上传到 Google Cloud Storage

标签: python google-app-engine google-cloud-platform google-app-engine-python google-speech-api


【解决方案1】:

正如另一篇关于 using Client Libraries in App Engine Standard 的 Stack Overflow 帖子中已经回答的那样,GAE 标准不支持 Google 客户端库,因此您可以使用 App Engine Flexible,一个 Compute Engine 实例 或使用 REST API(它还有一个适用于 App Engine 标准的 Python 库)。

如果您特别需要使用 google.cloud 库,则必须使用 App Engine Flexible 而不是 Standard,但如果您更愿意使用 Standard 环境,下面我将分享示例 App Engine Standard 的代码通过 Google API Python Client Library 使用 Cloud Speech API 的应用程序。这个库不是 GAE 标准内置的,所以你必须vendor it as if it was a third-party library。为此,您必须在本地应用程序的目录中创建lib 文件夹,以及我在下面共享的requirements.txt 文件,然后使用命令pip install -t lib/ -r requirements.txt 安装此库。

运行此示例 GAE 应用程序所需的文件:

requirements.txt

google-api-python-client

appengine_config.py

from google.appengine.ext import vendor

# Add any libraries install in the "lib" folder.
vendor.add('lib')

app.yaml

runtime: python27
api_version: 1
threadsafe: True

handlers:
- url: /.*
  script: main.app

main.py

import webapp2
from apiclient.discovery import build
from oauth2client.client import GoogleCredentials

class MainPage(webapp2.RequestHandler):
    # Presentation page
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('This is a sample App Engine Standard application working with Cloud Speech API! :)\n\nGo to /speechAPI to transcribe your audio file (you will need to upload it to one of your Cloud Storage buckets)!')

class Recognize(webapp2.RequestHandler):
    # Working with Python API Client Library (NOT NEW CLIENT LIBRARIES)
    def get(self):
        # Add credentials
        credentials = GoogleCredentials.get_application_default()
        service = build('speech', 'v1', credentials=credentials)

        # Methods available in: https://developers.google.com/resources/api-libraries/documentation/speech/v1/python/latest/index.html
        collection = service.speech()

        # Build the data structure JSON-like
        data = {}
        data['audio'] = {}
        data['audio']['uri'] = 'gs://<BUCKET>/<FOLDER>/<FILE>'
        data['config'] = {}
        data['config']['encoding'] = '<ENCODING>'
        data['config']['languageCode'] = '<LANGUAGE_CODE>'
        data['config']['sampleRateHertz'] = <SAMPLE_RATE>

        # Build the request and execute it
        request = collection.recognize(body=data)
        res = request.execute()

        # Webapp2 Response
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write(res)


app = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/speechAPI', Recognize),
], debug=True)

此应用程序也可以在本地开发服务器中运行,因此您可以在将其部署到 App Engine 之前使用命令 dev_appserver.py app.yaml 对其进行测试。

【讨论】:

    【解决方案2】:

    试试这个:sudo pip install --upgrade google-cloud-speech

    或者:

    我正在使用另一个库,
    这个方法对你有帮助吗?

    import speech_recognition as sp
    import time
    
    print("Say something!")
    
    while True:
        rec = sp.Recognizer()
        with sp.Microphone() as mic:
            audio = rec.listen(mic)
        try:
            print(rec.recognize_google(audio))
        except sp.UnknownValueError:
            print("I cannot understand what you said")
            time.sleep(0.5)
            print("Say again")
        except sp.RequestError as e:
            print("Error".format(e))
        word = rec.recognize_google(audio)
    
        if word == 'goodbye':
            break
    

    安装:

    sudo pip install SpeechRecognition
    
    sudo pip install pyaudio
    

    如果发现错误:

    sudo apt-get install python-pyaudio
    
    sudo apt-get install libjack-jackd2-dev portaudio19-dev
    

    再说一遍:

    sudo pip install pyaudio
    

    如果你发现错误,试试这个:

    sudo pip install --upgrade pyaudio
    

    【讨论】:

      猜你喜欢
      • 2014-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 2015-02-01
      相关资源
      最近更新 更多