https://github.com/brianleedick/voice... {link to code}...or, here's cut and pasted version of the code I used to turn my windows webcam into a voice activated camera:
import speech_recognition as sr
import time
import pyautogui
import psutil
import pygetwindow as gw
from tkinter import Tk, Label
def listen_for_command():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
recognizer.adjust_for_ambient_noise(source) # Adjust for ambient noise
try:
audio = recognizer.listen(source, timeout=2) # Set a timeout for listening
command = recognizer.recognize_google(audio)
return command.lower()
except sr.WaitTimeoutError:
pass # No need to print the timeout error
except sr.UnknownValueError:
pass # No need to print the unknown value error
except sr.RequestError:
print("Could not request results; check your network connection.")
return ""
def parse_command(command):
if "webcam" in command and "take a picture in" in command:
try:
if "one second" in command:
return 1
else:
seconds = int(command.split("take a picture in ")[1].split(" ")[0])
return seconds
except (IndexError, ValueError):
pass # No need to print the error message
return None
def countdown(seconds):
root = Tk()
root.overrideredirect(True)
root.geometry("+0+0")
label = Label(root, font=('Helvetica', 48), fg='red')
label.pack()
for i in range(seconds, 0, -1):
label.config(text=f"{i}")
root.update()
time.sleep(1)
root.destroy()
def click_camera_button():
Get the window of the camera application
windows = gw.getWindowsWithTitle('Camera')
if windows:
window = windows[0]
window.activate()
time.sleep(0.5) # Give some time for the window to activate
Coordinates of the camera button relative to the window
relative_button_x, relative_button_y = 1240, 532 # Replace with the actual coordinates
Calculate the absolute position of the button
button_x = window.left + relative_button_x
button_y = window.top + relative_button_y
pyautogui.moveTo(button_x, button_y)
pyautogui.click()
def is_webcam_app_running():
for proc in psutil.process_iter(['pid', 'name']):
if proc.info['name'].lower() == 'windowscamera.exe': # Process name for Windows Camera app
return True
return False
while True:
if is_webcam_app_running():
while is_webcam_app_running():
command = listen_for_command()
if command:
seconds = parse_command(command)
if seconds is not None:
countdown(seconds)
click_camera_button()
break # Exit inner loop to listen for new command after taking the picture
if not is_webcam_app_running():
break
time.sleep(1) # Check every 1 second for better responsiveness