Google speech-to-text api fails with multiprocessing python - python-3.x

I'm newbie and trying to use the google-cloud speech-to-text with python and multiprocessing. Here is a simple example to reproduce my issue.
I'm running the code on Windows.
When I run the code without multiprocessing, it works fine.
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()
Here is the Error message that I get:
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
Thanks a lot for your help.
Please let me know if you need any details about the issue

In my case this solved the problem.
easy_install --upgrade google-api-core
easy_install --upgrade
google-cloud-speech
I hope this helps.

Related

Repeated error while running Face Recognition vs code

I am new to Deep Learning and my first Project is FACIAL EMOTION RECOGINISTION
I am trying to use this DeepFace library but seems kind of stuck at the moment can anyone help ?
import cv2
from cv2 import cvtColor
from deepface import DeepFace
import matplotlib.pyplot as plt
img = cv2.imread('Images\happy\happy_001.jpg')
# plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
# plt.show()
predictions = DeepFace.analyze(img, actions = ['age', 'gender', 'race', 'emotion'])
and the error i am getting is
Traceback (most recent call last):
File "C:\Users\asus\OneDrive - Graphic Era University\Desktop\ML AND AI\FACE RECOG\test.py", line 11, in <module>
predictions=DeepFace.analyze(img, actions = ['age', 'gender', 'race', 'emotion'])
File "C:\Python 3.9\lib\site-packages\deepface\DeepFace.py", line 355, in analyze
models['gender'] = build_model('Gender')
File "C:\Python 3.9\lib\site-packages\deepface\DeepFace.py", line 61, in build_model
model = model()
File "C:\Python 3.9\lib\site-packages\deepface\extendedmodels\Gender.py", line 49, in loadModel
gender_model.load_weights(home+'/.deepface/weights/gender_model_weights.h5')
File "C:\Python 3.9\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Python 3.9\lib\site-packages\h5py\_hl\files.py", line 507, in __init__
fid = make_fid(name, mode, userblock_size, fapl, fcpl, swmr=swmr)
File "C:\Python 3.9\lib\site-packages\h5py\_hl\files.py", line 220, in make_fid
fid = h5f.open(name, flags, fapl=fapl)
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py\h5f.pyx", line 106, in h5py.h5f.open
OSError: Unable to open file (truncated file: eof = 232972459, sblock->base_addr = 0, stored_eof = 537149760)
I certainly don't know how to solve this .. can anyone help?
I am using VS CODE with python 3.9.6
Check that file: "h5py\h5f.pyx", line 106, in h5py.h5f.open. I guess your issue rises from there. Try to re-install the libs. Also, here your problem was discussed and, probably, solved: https://github.com/keras-team/keras/issues/6221

Cannot import pyrebase GAE

I have an application which works perfectly locally. But, for some reason, in GAE (standard python37) it seems to have an issue importing pyrebase (pyrebase==3.0.27 in requirements.txt). Any idea how to fix this?
Thanks in advance!
Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
worker.init_process()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
self.load_wsgi()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
self.wsgi = self.app.wsgi()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
return self.load_wsgiapp()
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
return util.import_app(self.app_uri)
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app
__import__(module)
File "/workspace/main.py", line 8, in <module>
import pyrebase
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/pyrebase/__init__.py", line 1, in <module>
from .pyrebase import initialize_app
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/pyrebase/pyrebase.py", line 17, in <module>
from oauth2client.service_account import ServiceAccountCredentials
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/oauth2client/service_account.py", line 25, in <module>
from oauth2client import client
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/oauth2client/client.py", line 47, in <module>
from oauth2client import crypt
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/oauth2client/crypt.py", line 55, in <module>
from oauth2client import _pycrypto_crypt
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/oauth2client/_pycrypto_crypt.py", line 17, in <module>
from Crypto.PublicKey import RSA
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/Crypto/PublicKey/__init__.py", line 29, in <module>
from Crypto.Util.asn1 import (DerSequence, DerInteger, DerBitString,
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/Crypto/Util/asn1.py", line 33, in <module>
from Crypto.Util.number import long_to_bytes, bytes_to_long
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/Crypto/Util/number.py", line 398
s = pack('>I', n & 0xffffffffL) + s
SyntaxError: invalid syntax
Pyrebase 3.0.27 is having compatibility issue with Python version 3. Instead, use the latest version of Pyrebase: Pyrebase4.
The OP solved the problem by changing Pyrebase version in requirements.txt:
From pyrebase==3.0.27 to Pyrebase4==4.5.0

Python google cloud function deployment failure - websockets

I am trying to deploy a python script from Google source code and it is failing when attempting to import create_connection from the websockets library.
Here is the top of my script. It fails during import of the gemini package, which in turn is importing websockets:
import json
import os
from gemini import PrivateClient
and my requirements.txt
gemini-python==0.2.1
The Google Cloud Functions log shows this error:
Traceback (most recent call last):
File "/layers/google.python.pip/pip/bin/functions-framework", line 8, in <module>
sys.exit(_cli())
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/_cli.py", line 37, in _cli
app = create_app(target, source, signature_type)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/__init__.py", line 255, in create_app
spec.loader.exec_module(source_module)
File "<frozen importlib._bootstrap_external>", line 843, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/workspace/main.py", line 7, in <module>
import gemini
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gemini/__init__.py", line 3, in <module>
from .basewebsocket import BaseWebSocket
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gemini/basewebsocket.py", line 9, in <module>
from websocket import create_connection, WebSocketConnectionClosedException
ImportError: cannot import name 'create_connection' from 'websocket' (/layers/google.python.pip/pip/lib/python3.8/site-packages/websocket/__init__.py)
This script runs on my Mac using Python 3.8. Not sure what to do as this seems environment specific. Any help is appreciated.
Cloud Functions does not support WebSockets.
You may want to consider an alternative compute service such as Cloud Run which does

ImportError: cannot import name 'Pdb' from 'IPython.core.debugger'

It seems I can't use Jupyter notebook at all. I've already tried to uninstall and install again but this error persists.
[I 19:33:14.916 NotebookApp] KernelRestarter: restarting kernel (1/5),
new random ports Traceback (most recent call last): File
"/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py",
line 193, in _run_module_as_main
"main", mod_spec) File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py",
line 85, in _run_code
exec(code, run_globals) File "/usr/local/lib/python3.7/site-packages/ipykernel_launcher.py", line
15, in
from ipykernel import kernelapp as app File "/usr/local/lib/python3.7/site-packages/ipykernel/init.py", line
2, in
from .connect import * File "/usr/local/lib/python3.7/site-packages/ipykernel/connect.py", line
13, in
from IPython.core.profiledir import ProfileDir File "/usr/local/lib/python3.7/site-packages/IPython/init.py", line 54,
in
from .core.application import Application File "/usr/local/lib/python3.7/site-packages/IPython/core/application.py",
line 25, in
from IPython.core import release, crashhandler File "/usr/local/lib/python3.7/site-packages/IPython/core/crashhandler.py",
line 27, in
from IPython.core import ultratb File "/usr/local/lib/python3.7/site-packages/IPython/core/ultratb.py", line
115, in
from IPython.core import debugger File "/usr/local/lib/python3.7/site-packages/IPython/core/debugger.py",
line 47, in
from pdb import Pdb as OldPdb File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pdb.py",
line 76, in
import code File "/Users/robot/Dropbox/coding/ytautomator/temp/testing_code/code.py",
line 17, in
fig, ax = plt.subplots(figsize=(15, 8)) File "/usr/local/lib/python3.7/site-packages/matplotlib/pyplot.py", line
1209, in subplots
fig = figure(**fig_kw) File "/usr/local/lib/python3.7/site-packages/matplotlib/pyplot.py", line
545, in figure
**kwargs) File "/usr/local/lib/python3.7/site-packages/matplotlib/backend_bases.py",
line 3251, in new_figure_manager
fig = fig_cls(*args, **kwargs) File "/usr/local/lib/python3.7/site-packages/matplotlib/figure.py", line
367, in init
FigureCanvasBase(self) # Set self.canvas. File "/usr/local/lib/python3.7/site-packages/matplotlib/backend_bases.py",
line 1582, in init
self._fix_ipython_backend2gui() File "/usr/local/lib/python3.7/site-packages/matplotlib/backend_bases.py",
line 1609, in _fix_ipython_backend2gui
ip = IPython.get_ipython() File "/usr/local/lib/python3.7/site-packages/IPython/core/getipython.py",
line 22, in get_ipython
from IPython.core.interactiveshell import InteractiveShell File "/usr/local/lib/python3.7/site-packages/IPython/core/interactiveshell.py",
line 46, in
from IPython.core.debugger import Pdb ImportError: cannot import name 'Pdb' from 'IPython.core.debugger'
(/usr/local/lib/python3.7/site-packages/IPython/core/debugger.py)
I'm using Python 3.7.4 with Anaconda in Mac OS 10.14.6
My pip free is the following:
appnope==0.1.0
attrs==19.3.0
backcall==0.1.0
bleach==3.1.0
cycler==0.10.0
decorator==4.4.0
defusedxml==0.6.0
entrypoints==0.3
importlib-metadata==0.23
ipdb==0.12.2
ipykernel==5.1.2
ipython==7.8.0
ipython-genutils==0.2.0
jedi==0.15.1
Jinja2==2.10.3
json5==0.8.5
jsonschema==3.1.1
jupyter-client==5.3.4
jupyter-core==4.6.0
jupyterlab==1.1.4
jupyterlab-server==1.0.6
kiwisolver==1.1.0
MarkupSafe==1.1.1
matplotlib==3.1.1
mistune==0.8.4
more-itertools==7.2.0
nbconvert==5.6.0
nbformat==4.4.0
notebook==6.0.1
numpy==1.17.2
pandas==0.25.1
pandocfilters==1.4.2
parso==0.5.1
pexpect==4.7.0
pickleshare==0.7.5
prometheus-client==0.7.1
prompt-toolkit==2.0.10
ptyprocess==0.6.0
Pygments==2.4.2
pyparsing==2.4.2
pyrsistent==0.15.4
python-dateutil==2.8.0
pytz==2019.3
pyzmq==18.1.0
Send2Trash==1.5.0
six==1.12.0
terminado==0.8.2
testpath==0.4.2
tornado==6.0.3
traitlets==4.3.3
wcwidth==0.1.7
webencodings==0.5.1
zipp==0.6.0
I'd be glad if anyone can help
Thanks
Remove the file code.py in your folder. It causes a confliction.

Error while creating a service using nameko for Pysnmp get_request--Attempted "nameko_entrypoints" operation on ASN.1 schema object

I am new to pysnmp and nameko. I have been assignment a work to create a service in nameko framework to perform snmp get_request using pysnmp library.
Below is the code i have tried
from pysnmp.hlapi import *
from nameko.rpc import rpc
class GreetingService(object):
name = "greeting_service"
#rpc
def getFunc(oid):
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
CommunityData('public', mpModel=0),
UdpTransportTarget(('snmp.live.gambitcommunications.com', 161)),
ContextData(),
ObjectType(ObjectIdentity('SNMPv2-MIB', oid, 0)))
)
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for varBind in varBinds:
print(' = '.join([x.prettyPrint() for x in varBind]))
if __name__ == "__main__":
getFunc('sysName')
when i try to start the service using terminal with the following command
$ nameko run helloworld
I get the following error message.
syed#syed-ThinkPad-E480:~/Pysnmp$ nameko run helloworld
Traceback (most recent call last):
File "/home/syed/.local/bin/nameko", line 11, in <module>
sys.exit(main())
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/main.py", line 112, in main
args.main(args)
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/commands.py", line 110, in main
main(args)
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 181, in main
import_service(path)
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 71, in import_service
if inspect.getmembers(potential_service, is_entrypoint):
File "/usr/lib/python3.7/inspect.py", line 354, in getmembers
if not predicate or predicate(value):
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 35, in is_entrypoint
return hasattr(method, ENTRYPOINT_EXTENSIONS_ATTR)
File "/home/syed/.local/lib/python3.7/site-packages/pyasn1/type/base.py", line 221, in __getattr__
raise error.PyAsn1Error('Attempted "%s" operation on ASN.1 schema object' % attr)
pyasn1.error.PyAsn1Error: Attempted "nameko_entrypoints" operation on ASN.1 schema object
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Original exception was:
Traceback (most recent call last):
File "/home/syed/.local/bin/nameko", line 11, in <module>
sys.exit(main())
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/main.py", line 112, in main
args.main(args)
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/commands.py", line 110, in main
main(args)
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 181, in main
import_service(path)
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 71, in import_service
if inspect.getmembers(potential_service, is_entrypoint):
File "/usr/lib/python3.7/inspect.py", line 354, in getmembers
if not predicate or predicate(value):
File "/home/syed/.local/lib/python3.7/site-packages/nameko/cli/run.py", line 35, in is_entrypoint
return hasattr(method, ENTRYPOINT_EXTENSIONS_ATTR)
File "/home/syed/.local/lib/python3.7/site-packages/pyasn1/type/base.py", line 221, in __getattr__
raise error.PyAsn1Error('Attempted "%s" operation on ASN.1 schema object' % attr)
pyasn1.error.PyAsn1Error: Attempted "nameko_entrypoints" operation on ASN.1 schema object
Please help me out to understand weather what i have tried is correct way or else it is wrong.. If so how to rectify the mistake.
Any help will be appreciable.
Thanks in advance
It has something to do with the way how nameko hooks up your code...
It seems to try looking up nameko_entrypoints attribute at all objects it can find in your module eventually running into ASN.1 schema objects (which are sacred and should not be used for anything other than blueprinting purposes).
My suggestion would be to replace from pysnmp.hlapi import * with specific imports of pysnmp classes/functions you are using in your code. That should hopefully hide fragile pieces out of nameko's sight.

Resources