Stop OpenCV for seventeen seconds? python - python

I have a motion detection program in OpenCV, and I want it to play a sound when it detects motion. I use winsound. However, OpenCV still seems to be gathering frames while the sound is playing, so I want to know a way to stop all OpenCV processes for about 17 seconds. I tried time.sleep and running it with the -u tag. Neither worked. Any ideas? Thanks.

I was able to solve the problem by putting what I want to run before resuming processes (playing the sound) in another script, performing import sound and breaking out of the loop to stop the program, and I can't figure out how to start it again. For my purposes, I can restart it manually.

Related

Detect audio coming from microphone

Ive seen other questions similar to this, but they all involve detecting audio and then recording. I'd like my microphone connected to my computer to always be recording, and any time audio is detected it runs another part of a script. What modules would I need? How would I do this? Im pretty sure this would involve seeing when the audio volume goes above a certain point and then activating the rest of the script, because obviously there will always be some sort of sound coming from the microphone.
This might help if you want them to say something and then if they say the right word or whatever run the script.
https://pypi.org/project/SpeechRecognition/

playing sound in Python with the ability to cut it off mid-play

I am writing a bit of python code that plays a sound file (MP3 or the equivalent) and should cut that sound off if the user strikes a (hardware) button that is wired into the system. This will be on a Raspberry Pi running Raspbian. The libraries I’ve used historically for playing sound all play to completion. The closest approach I can think of would be using an external sound player (OMXplayer perhaps) and then searching for and killing its process if the button is pressed, but this feels inelegant. Can anybody suggest a better approach?
Using Pyaudio you could simply check a flag before writing each buffer to the stream, and if the audio should stop, fade out the current buffer and then either continue the stream with silence (write 0's) or stop the stream and exit the program. This could be easily be achieved with a latency of ~1/10s after button press.
You would also need to create a UI thread in the application to handle commands since the stopping flag is switched on user input.

Pyglet Event Audio

I would like to know how I can start an audio without having to enter the Pyglet loop. My question is based on the fact that I'm already using a loop, for Pygame. Unfortunately, with the latter, I found the fact that it has an inaccurate control with regard to the volume of the audio. So I wanted to use Pyglet to play it. The fact is that I can not allow myself to start a loop with Pyglet, because it blocks the script, so, returning to the question, how can I use the audio playback event independently?

Shutting down a Python program using PyQt and PyGame.music()

I've been working on a media player in python that uses pygame.mixer to play music and PyQt4 to build the UI.
I'm currently using a while loop to check if a song is finished so the next song can be loaded in from the queue. In order to make sure that the user can still interact with the PyQt buttons while the while loop is running, I have put QtCore.QCoreApplication.processEvents() into the while loop.
However, now when I close the main window while music is playing, the program does not stop (as it normally does) and the music keeps playing.
The program also can't detect the app.aboutToQuit.connect() command while the music is playing (meaning it can when music isn't playing).
Any help would be greatly appreciated.
I found the answer to this issue a while ago so thought I should put it here for anyone else.
I gave the program an internal exit button in the ui, then made the button call os._exit(0). It worked perfectly then.
Checking the Python and PyQt documentation gave no comment on closing a application window while a while loop is being performed.
Any other solutions would be appreciated though.

how can I play a sound with pygame while the system is waiting?

I'm using the pygame function pygame.time.delay() to make pygame freeze while presenting some pictures or text, and I want to play a sound at the same time.
The problem, I think, is that in order for pygame mixer to work the main loop must be running, so putting the system on wait it also stops the mixer.
Any easy way to solve this problem? thanks
pygame.time.delay() is a bad idea for waiting while presenting things, it not only will prevent you from playing music it can also freeze your screen (blank screen).
You should process events and update the display although you have static image.
Some alternatives are:
Use a counter in your loop to know how long you should keep the same image in the screen.
Use a timer to receive a User Event that will notify you that you need to change the current state.

Categories