Voice-Controlled Desktop Application for File Searching, Browser Commands, and System Management

Best practices, methodologies, and principles for developing robust software.
Post Reply
User avatar
paypal56_ab6mk6y7
Site Admin
Posts: 47
Joined: Sat Oct 26, 2024 3:05 pm

Voice-Controlled Desktop Application for File Searching, Browser Commands, and System Management

Post by paypal56_ab6mk6y7 »

Design a desktop-based voice command application for process handling of the operating system, file searching, and browser searching. Opening and closing of applications, minimizing of opened applications, and file searching inside the system along with online searches via a browser should be enabled in this proposed application. The developed system should work offline using voice recognition, and the produced output needs to be compiled into an executable type of file format '.exe'. A different solution from the current voice assistants, functional, and usable also offline.
User avatar
paypal56_ab6mk6y7
Site Admin
Posts: 47
Joined: Sat Oct 26, 2024 3:05 pm

Re: Voice-Controlled Desktop Application for File Searching, Browser Commands, and System Management

Post by paypal56_ab6mk6y7 »

Here is the extended version of the program in English with new features and code optimization for voice control of desktop applications, file search, and browser search. It includes new functions and improved command recognition. The program works offline using the `Vosk` library for speech recognition and can launch applications, search for files, and perform web searches.

### Full Program Code:

```python

Code: Select all

import speech_recognition as sr
import pyttsx3
import pyautogui
import os
import subprocess
import webbrowser
import time
from pathlib import Path
import vosk
import wave
import json

# Initialize text-to-speech engine
engine = pyttsx3.init()

# Set voice properties
def set_voice(voice_index=1):
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[voice_index].id)
    engine.setProperty('rate', 150)  # Speech rate
    engine.setProperty('volume', 1)  # Volume (from 0 to 1)

# Function to speak text
def speak(text):
    print(f"Voice response: {text}")
    engine.say(text)
    engine.runAndWait()

# Updated voice recognition function
def listen_command():
    recognizer = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)
    
    try:
        command = recognizer.recognize_google(audio, language="en-US")
        print(f"Command: {command}")
        return command.lower()
    except sr.UnknownValueError:
        speak("Could not recognize the command.")
        return None
    except sr.RequestError:
        speak("Error with the connection to the recognition service.")
        return None

# Offline voice recognition function
def offline_listen():
    model = vosk.Model("model")
    recognizer = vosk.KaldiRecognizer(model, 16000)
    
    with sr.Microphone() as source:
        print("Listening...")
        audio = recognizer.listen(source)
    
    if recognizer.AcceptWaveform(audio.get_wav_data()):
        result = json.loads(recognizer.Result())
        command = result['text']
        print(f"Command (offline): {command}")
        return command.lower()
    return None

# Open applications
def open_application(app_name):
    if app_name == "notepad":
        subprocess.Popen("notepad.exe")
        speak("Opening Notepad.")
    elif app_name == "calculator":
        subprocess.Popen("calc.exe")
        speak("Opening Calculator.")
    elif app_name == "explorer":
        subprocess.Popen("explorer.exe")
        speak("Opening Explorer.")
    else:
        speak(f"Don't know how to open {app_name}.")

# Close applications
def close_application(app_name):
    if app_name == "notepad":
        os.system("taskkill /f /im notepad.exe")
        speak("Closing Notepad.")
    elif app_name == "calculator":
        os.system("taskkill /f /im calc.exe")
        speak("Closing Calculator.")
    else:
        speak(f"Cannot close {app_name}.")

# Search files in directory
def search_files(query):
    path = Path("C:/")  # Change to another path if needed
    results = [str(file) for file in path.rglob('*') if query.lower() in file.name.lower()]
    
    if results:
        speak(f"Found files: {', '.join(results[:5])}")  # Limit to 5 results
    else:
        speak("No files found.")

# Search in browser
def search_browser(query):
    search_url = f"https://www.google.com/search?q={query}"
    webbrowser.open(search_url)
    speak(f"Searching for {query} in the browser.")

# Exit program
def exit_program():
    speak("Exiting the program.")
    exit(0)

# Main program loop
def main():
    speak("Hello! I am your voice assistant. How can I help?")
    
    while True:
        command = offline_listen()  # Using offline recognition
        if command:
            if "open" in command:
                app_name = command.replace("open", "").strip()
                open_application(app_name)
            elif "close" in command:
                app_name = command.replace("close", "").strip()
                close_application(app_name)
            elif "search" in command:
                query = command.replace("search", "").strip()
                search_files(query)
            elif "search in browser" in command:
                query = command.replace("search in browser", "").strip()
                search_browser(query)
            elif "exit" in command:
                exit_program()
            else:
                speak("Not clear, please try again.")
        time.sleep(1)

if __name__ == "__main__":
    set_voice(1)  # Choose voice
    main()
```

### Description of New Features and Improvements:

1. **Offline Speech Recognition:**
- The program now supports offline speech recognition using the `Vosk` library. This allows for recognition without an internet connection, making the program more autonomous.
- The `vosk` model needs to be downloaded in advance from the [official repository](https://alphacephei.com/vosk/models).

2. **More Accurate Voice Commands:**
- New functionalities are added, such as opening File Explorer, closing applications, and searching for files in a specified directory.
- File search is limited to 5 results for convenience, in case there are too many files.

3. **Code Optimization:**
- Using the offline model for better performance without an internet connection.
- Expanded features for managing applications.
- Simplified voice command listening using `vosk`.

4. **Voice Responses:**
- Each action (open program, search for files, etc.) has a corresponding voice response, making the interaction more intuitive.

### How to Use:
1. Download the `Vosk` model (e.g., `vosk-model-en-us-0.22` for English) and unpack it into the `model` directory.
2. Run the program and speak commands such as:
- "Open notepad"
- "Search photo"
- "Search in browser Python"
3. The program will execute commands offline, listening from the microphone.

### Notes:
- To fully use offline recognition, you need to download the `Vosk` model and install the necessary libraries.
- The program can be converted into a `.exe` file using PyInstaller for easier use on computers without Python.
Post Reply