I am using an external sound card with a built-in microphone for voice recognition on a Raspberry Pi 3 model B. The issue is that when I run the code the code executes but then stops on "SAY SOMETHING". When I terminate the code I get these errors.
This is my code:
import speech_recognition as voice
def voice_recognition():
speech = voice.Recognizer()
with voice.Microphone() as source:
print("SAY SOMETHING")
audio = speech.listen(source)
try:
command = speech.recognize_google(audio)
check = "forward" in command
check1 = "backward" in command
if(check == True):
print ('1')
if(check1 == True):
print ('2')
else:
print ('3')
except:
pass
voice_recognition()
And I am getting these errors:
Traceback (most recent call last):
File "/home/pi/Desktop/voice.py", line 29, in <module>
voice_recognition()
File "/home/pi/Desktop/voice.py", line 9, in voice_recognition
audio = speech.listen(source)
File "/usr/local/lib/python3.5/dist-packages/speech_recognition/__init__.py", line
620, in listen
buffer = source.stream.read(source.CHUNK)
File "/usr/local/lib/python3.5/dist-packages/speech_recognition/__init__.py", line
161, in read
return self.pyaudio_stream.read(size, exception_on_overflow=False)
File "/usr/local/lib/python3.5/dist-packages/pyaudio.py", line 608, in read
return pa.read_stream(self._stream, num_frames, exception_on_overflow)
The "errors" that you're seeing are normal when you terminate a Python program. If you run any Python code and terminate it (by pressing Ctrl+C on the command line) before it finishes, you will see a traceback of where the code terminated.
According to the speech_recognition library reference, Recognizer.listen() will not use a timeout by default (you can use listen(timeout=20) to timeout if nothing is received in 20 seconds). The code is probably using a wrong microphone, so the library never receives audio, so the program never terminates.
You should be able to use Microphone.list_microphone_names() to get a list of available microphones, and pass the index of your external microphone to the constructor (e.g. Microphone(device_index=3)).
Related
I was trying to set up MicroPython on PyCharm 2021.3.3, unfortunately I was not able to successfully connect my board to IDE. I'm getting trying to use tools>>MicroPython>>MicroPython REPL:
Found the device, but could not connect via port 'COM4': could not open port 'COM4': PermissionError(13, 'Odmowa dostępu.', None, 5)
I'm not sure what to suggest. :-(
Press ENTER to continue
Then I have tried to flush simple program to control despite previous error and it spits out:
Connecting to COM4
Traceback (most recent call last):
File "C:\Users\reczul\AppData\Roaming\JetBrains\PyCharm2021.3\plugins\intellij-micropython\scripts\microupload.py", line 139, in <module>
main(sys.argv[1:])
File "C:\Users\reczul\AppData\Roaming\JetBrains\PyCharm2021.3\plugins\intellij-micropython\scripts\microupload.py", line 56, in main
board = Pyboard(port)
File "C:\Users\reczul\PycharmProjects\pythonProject1\venv\lib\site-packages\ampy\pyboard.py", line 147, in __init__
raise PyboardError('failed to access ' + device)
ampy.pyboard.PyboardError: failed to access COM4
What can I do to fix this error?
The sample code I have used:
from machine import Pin, Timer
led = Pin(25, Pin.OUT)
tim = Timer()
def tick(timer):
global led
led.toggle()
tim.init(freq=2.5, mode=Timer.PERIODIC, callback=tick)```
Assuming you are using a Pi Pico.
You are most likely getting this error because you pressed the 'BOOTSEL' button while plugging your device in.
Drag your UF2 file onto the device, it should reboot.
Don't unplug the device and hold the 'BOOTSEL' this time.
It should be good to go now.
I am using a peaktech 4046 : 160MHz Function/arbitrary Waveform Generator. I developping on pyton and I am using the pyvisa librairy.
The connection is well established and the generator applies the query. But it generates the following error and stops the program (it doesn't do anything after the error).
Here is the code :
import pyvisa
rm = pyvisa.ResourceManager()
inst = rm.open_resource('TCPIP0::130.79.192.123::5025::SOCKET')
print(inst.session)
print(inst.io_protocol)
inst.query("source1:function squ")
And here is what I have in my terminal :
2
IOProtocol.normal
Traceback (most recent call last):
File "c:\Users\Labo préclinique\Desktop\ProjetPython\importation de librairies\Forum.py", line 7, in <module>
inst.query("source1:function squ ")
File "C:\Users\Labo préclinique\AppData\Local\Programs\Python\Python39\lib\site-packages\pyvisa\resources\messagebased.py", line 644, in query
return self.read()
File "C:\Users\Labo préclinique\AppData\Local\Programs\Python\Python39\lib\site-packages\pyvisa\resources\messagebased.py", line 486, in read
message = self._read_raw().decode(enco)
File "C:\Users\Labo préclinique\AppData\Local\Programs\Python\Python39\lib\site-packages\pyvisa\resources\messagebased.py", line 442, in _read_raw
chunk, status = self.visalib.read(self.session, size)
File "C:\Users\Labo préclinique\AppData\Local\Programs\Python\Python39\lib\site-packages\pyvisa\ctwrapper\functions.py", line 2337, in read
ret = library.viRead(session, buffer, count, byref(return_count))
File "C:\Users\Labo préclinique\AppData\Local\Programs\Python\Python39\lib\site-packages\pyvisa\ctwrapper\highlevel.py", line 222, in _return_handler
return self.handle_return_value(session, ret_value) # type: ignore
File "C:\Users\Labo préclinique\AppData\Local\Programs\Python\Python39\lib\site-packages\pyvisa\highlevel.py", line 251, in handle_return_value
raise errors.VisaIOError(rv)
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.
I have tried (to no avail) :
-changing SOCKET to INSTR
-using a timout much longeur ( inst.timeout = 10000)
-adding a end term (tried \n and \r) with : inst.read_termination = '\n'
So I don't know what to do anymore... I need to give more than one command, so the program must not stop so fast. I suspect that my function generator is not sending anything back, but I don't know how to make sure this is the case.
What I wish to know is : Why do I have a time out error if the connection is well established and the request is executed on the device ? How to do the request in a proper way ?
Thank you in advance !!
PS : I know how to catch the error (with try except) but I'd rather have an Ok answer thant a KO one.
Try to get a list of resources by
rm.list_resources()
and check that your resource TCPIP0::130.79.192.123::5025::SOCKET in it.
Then check the standard request to the resource from tutorial:
inst.query("*IDN?")
query is a short form for a write operation to send a message, followed by a read. So you could do this in two actions to specify the error(read or write error?):
inst.write('"source1:function squ"')
print(inst.read())
Please, check the name of query source1:function squ because I don't see it in the documentation. Maybe you should use "source1:am:interanal:function square(p. 57 of documentation) or change squ -> square?
Accordingly documentation, you could try to set infinite timeout to your request by
del inst.timeout
Also, you could add read_termination/write_termination option to specify when you'll finish your reading/writing by
inst = rm.open_resource('TCPIP0::130.79.192.123::5025::SOCKET', read_termination='\r')
And the last chance is changing the options query_delay and send_end.
I have this function and I need to close the file before exiting the function say("some text").
def say(self, text):
tts = gTTS(text, lang='fr')
file="text.mp3"
tts.save(file)
audio = MP3(file)
p = vlc.MediaPlayer(file)
p.play()
time.sleep((audio.info.length)) #to avoid it listening to itself
p.stop()
return file
Because If I don't do this, I have this error
OS Error: [Errno -9993] Illegal combination of I/O device
I think that this error occurs because I'm trying to listen just after the call of the function say and the file is still open.
nb: I'm working with Python 3
Though i am not to experienced with TTS, shouldnt it work if you just do close(os.getcwd()+'/'+file)?
I trying to open two serial ports in Python with the following code:
for i in range(0, 2):
if platform.system() == "Darwin":
pos = 2+i
else:
pos = i
port = serial.Serial(current_ports[pos], BAUD_RATE, timeout=TIMEOUT)
time.sleep(1.516)
port.write('#')
time.sleep(1.516)
out = ''
print "Reading MAC Address...."
while port.inWaiting() > 0:
out += port.read(1)
print out
if out == '04:E9:E5:00:EC:51':
led_port = port
elif out == '04:E9:E5:01:0C:E0':
matrix_port = port
Sometimes the ports open successfully, sometimes they don't. When they don't, I get this error message:
Reading MAC Address....
Traceback (most recent call last):
File "animation.py", line 227, in <module>
main()
File "animation.py", line 208, in main
led_port, matrix_port = get_ports()
File
"/Users/collinschupman/Documents/FutureCities/MurmurWall/Onsite/Raspi/helper_functions.py", line 41, in get_ports
while port.inWaiting() > 0:
File "/Library/Python/2.7/site-packages/serial/serialposix.py", line 449, in inWaiting
s = fcntl.ioctl(self.fd, TIOCINQ, TIOCM_zero_str)
IOError: [Errno 6] Device not configured
As you can see, it gets to the inWaiting() call and then throws this error.
For a little reference, the code is sending a message to a couple Arduinos so they can be identified by their MAC addresses.
Is there anything blatantly incorrect I'm doing Python-side which would cause this setup to fail once and a while? I'd say this code works 50% of the time right now.
Thanks,
Collin
Is there a getty process running on the serial ports on the Arduinos? Or even on the serial port of the local machine which is connecting to the Arduinos? If so perhaps it is interfering and should be stopped. Here is a reference which may be relevant: http://codeandlife.com/2012/07/29/arduino-and-raspberry-pi-serial-communication/
I had similar problem and I fixed it with the following solution
$ sudo nano /etc/inittab
and go to the bottom of the file, you will see
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
or similar
simply add a # character to the beginning,now it looks like :
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
save the file and reboot
Hope this works
Reference:
http://www.hobbytronics.co.uk/raspberry-pi-serial-port
I'm recently started to use python with mobile app automation, as i decided to use python, the main instruments that I've found were monkeyrunner and androidviewclient.
But there is the first issue with which i dont know what to do:
package = 'com.mypackage.android'
activity = '.launchActivity'
component = package + "/" + activity
device, serialno = ViewClient.connectToDeviceOrExit()
device.startActivity(component=component)
time.sleep(3)
vc = ViewClient(device, serialno)
vc.dump()
showMenu = vc.findViewById("id/no_id/8")
showMenu.touch()
as i'm running it in windows cmd - monkeyrunner mypath\test-case1.py
i receive an exception:
131213 18:42:32.555:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception
131213 18:42:32.555:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions]Traceback (most recent call last):
File "C:\Python27\tests\1.py", line 26, in <module>
device, serialno = ViewClient.connectToDeviceOrExit()
File "C:\Program Files (x86)\Android\AndroidViewClient\AndroidViewClient-maste
r\AndroidViewClient\src\com\dtmilano\android\viewclient.py", line 1381, in conne
ctToDeviceOrExit
ViewClient.setAlarm(timeout+5)
File "C:\Program Files (x86)\Android\AndroidViewClient\AndroidViewClient-maste
r\AndroidViewClient\src\com\dtmilano\android\viewclient.py", line 1341, in setAl
arm
signal.alarm(timeout)
File "C:\Program Files (x86)\Android\android-sdk\tools\lib\jython-standalone-2
.5.3.jar\Lib\signal.py", line 222, in alarm
NotImplementedError: alarm not implemented on this platform
am I doing something wrong? Please help.
Thank you a lot!
This is how setAlarm looks like
#staticmethod
def setAlarm(timeout):
osName = platform.system()
if osName.startswith('Windows'): # alarm is not implemented in Windows
return
signal.alarm(timeout)
so, it tries to identify that is Windows and then not invoking signal.alarm() which is not implemented, but for some reason it fails in your case.
Try to print the result of osName to see what went wrong.
UPDATE
Now I see, you are using monkeyrunner as the interpreter but AndroidViewClient >= 4.0.0 is 100% pure python, so you should run your scripts using a python 2.x interpreter.