[IoT] MirAI – Intelligent & Smart Mirror Assistant

3.8k words

Overview

MirAI is an intelligent connected mirror that enhances daily life through voice interactions, real-time information, and smart home integration. Using Azure Cognitive Services and OpenAI, MirAI provides a seamless user experience with natural language processing, weather updates, calendar synchronization, and much more.

My Role

  • Implemented voice recognition and speech synthesis using Azure Speech Services.
  • Integrated OpenAI for intelligent conversation handling.
  • Designed and built real-time weather synchronization.
  • Developed a voice-activated user interface for smooth interaction.

Tools & Technologies

Azure Cognitive Services (Speech-to-Text, Text-to-Speech)
OpenAI API (Chat-based interactions)
Python (Core development language)
SimpleAudio (Audio feedback system)
Requests (Fetching weather data from OpenWeather API)
Dotenv (Managing environment variables)


Code Highlights

Speech Recognition with Azure Speech Services

speech_recognition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  
import azure.cognitiveservices.speech as speechsdk

def Start_recording(output_folder):
speech_config = speechsdk.SpeechConfig(
subscription=settings['speechKey'], region=settings['region'],
speech_recognition_language=settings['language']
)

audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

print("Listening...")
result = speech_recognizer.recognize_once()

return result.text if result.reason == speechsdk.ResultReason.RecognizedSpeech else "Speech not recognized."

OpenAI Chat Integration

openai_integration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
  
import openai

openai.api_key = "YOUR_OPENAI_KEY"

def complete_openai(name, prompt, token=100):
response = openai.completions.create(
model="gpt-3.5-turbo-instruct",
prompt=f"You are MirAI, a smart mirror assistant. Address the user by name: {name}. Here is the input: {prompt}",
temperature=0.7,
max_tokens=token
)
return response.choices[0].text.strip()

Fetching Weather Data

weather_fetch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  
import requests
import os

API_KEY = os.getenv("OPENWEATHER_KEY")

def get_weather_forecast(city):
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&units=imperial&appid={API_KEY}"
response = requests.get(url).json()

if response.get("cod") != 200:
return "City not found."

weather = response["weather"][0]["main"]
temp_fahrenheit = response["main"]["temp"]
temp_celsius = round((temp_fahrenheit - 32) * 5/9)

return f"The weather in {city} is {weather}, with a temperature of {temp_celsius}°C."

Processing User Requests

ask_request.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  
def AskRequest(name):
user_input = Start_recording(output_folder=output_folder)
conversation.append(user_input)
print(user_input)

result = ""

user_input_lower = user_input.lower()
if 'current temperature' in user_input_lower:
prompt = f"Based on this input: '{user_input}', extract only the city name, without quotes or formatting."
city_result = complete_openai(name, prompt=prompt, token=3000)

prompt = f"Based on this weather data: {get_weather_forecast(city_result)}, generate a short assistant-like response."
result = complete_openai(name, prompt=prompt, token=3000)

else:
result = complete_openai(name, prompt=user_input, token=3000)

conversation.append(result)
speak(result, output_folder=output_folder)

Playing a Confirmation Sound

play_sound.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  
import simpleaudio as sa
import numpy as np

def play_sound():
frequency = 440
duration = 0.1
sample_rate = 44100
amplitude = 16000

waveform = np.sin(2 * np.pi * np.arange(sample_rate * duration) * frequency / sample_rate)
waveform = (waveform * amplitude).astype(np.int16)

audio = sa.play_buffer(waveform, 1, 2, sample_rate)
audio.wait_done()

MirAI transforms a regular mirror into an AI-powered assistant, making daily routines smoother and more interactive.