如何在RPI3运行的智能魔镜中打开指定网页及实现语音控制
Got it, let's tackle these two requirements for your RPI3-based smart mirror step by step. I've messed around with similar setups before, so here's what worked reliably for me:
Most smart mirror setups use Chromium as the default browser, so we'll focus on that:
Manual Testing First: If you just want to quickly test opening a webpage in fullscreen (perfect for a mirror), fire up the terminal and run:
chromium-browser --kiosk https://www.google.comThe
--kioskflag forces Chromium to run in fullscreen without any UI elements. For a cleaner experience, add extra flags:--start-fullscreen --incognito --noerrdialogsto hide the address bar, avoid cache buildup, and suppress error popups.Auto-Launch on Boot (Production Setup): To make the webpage open automatically when your RPI3 starts up:
- Open the autostart config file with
nano ~/.config/lxsession/LXDE-pi/autostart - Add this line at the end (replace the URL with whatever you need):
@chromium-browser --kiosk --noerrdialogs --incognito https://www.google.com - Save and exit with
Ctrl+OthenCtrl+X. Reboot your Pi, and the page should load automatically.
- Open the autostart config file with
For voice control, Python with the SpeechRecognition library is a solid choice—it's straightforward and plays nicely with the Pi's audio hardware. Here's how to set it up:
Step 1: Install Required Tools
First, install dependencies via terminal:
sudo apt-get update && sudo apt-get install python3-pip python3-pyaudio pip3 install SpeechRecognition pyttsx3
SpeechRecognitionhandles converting audio to text (we'll use Google's API for better accuracy—requires internet)pyttsx3is an offline text-to-speech engine so your mirror can respond out loud- Don't forget to grant audio permissions to your user:
sudo usermod -a -G audio pithen reboot.
Step 2: Write the Voice Control Script
Create a file named mirror_voice.py in your home directory (e.g., /home/pi/mirror_voice.py) with this code:
import speech_recognition as sr import pyttsx3 import subprocess # Initialize text-to-speech engine engine = pyttsx3.init() def speak(text): engine.say(text) engine.runAndWait() def listen_for_command(): r = sr.Recognizer() with sr.Microphone() as source: print("Listening for your command...") r.adjust_for_ambient_noise(source) # Cut down background noise audio = r.listen(source) try: # Convert audio to text using Google's API command = r.recognize_google(audio).lower() print(f"Detected command: {command}") return command except sr.UnknownValueError: speak("Sorry, I didn't catch that. Could you repeat?") return None except sr.RequestError: speak("Sorry, my speech service is unavailable right now.") return None def open_google_webpage(): # Close any existing Chromium windows first (optional but clean) subprocess.run(["pkill", "chromium-browser"], check=False) # Launch Chromium in kiosk mode with Google subprocess.Popen([ "chromium-browser", "--kiosk", "--noerrdialogs", "--incognito", "https://www.google.com" ]) speak("Opening the Google webpage for you.") if __name__ == "__main__": speak("Smart mirror is ready. Please give your command.") # Keep listening for commands indefinitely while True: command = listen_for_command() if command and "smart mirror, please open google webpage" in command: open_google_webpage() # You can add more command triggers here if you want to expand functionality
Step 3: Test the Script
Run the script with python3 /home/pi/mirror_voice.py, then say your command clearly. The mirror should respond verbally and launch the Google page. If it doesn't pick up your voice, double-check your microphone connection and audio permissions.
Step 4: Auto-Launch the Voice Script on Boot
To make voice control start automatically when your Pi boots, add this line to the same autostart file we edited earlier (~/.config/lxsession/LXDE-pi/autostart):
@python3 /home/pi/mirror_voice.py
Save, reboot, and your mirror will be ready to listen for your command as soon as it starts up.
内容的提问来源于stack exchange,提问作者Smith john

