Submission Number: 108
Submission ID: 102
Submission UUID: c6bd7bae-fac4-4f2a-bd1e-7a465da8a3e4
Submission URI: /Script

Created: Thu, 03/09/2023 - 07:54
Completed: Thu, 03/09/2023 - 07:54
Changed: Thu, 03/09/2023 - 07:54

Remote IP address: 146.70.117.227
Submitted by: Anonymous
Language: English

Is draft: No
Webform: Script
Submitted to: Script Upload

Script

User Voice Recognition
This is a User Voice Recognition skill for Neon that
includes the ability to recognize the user's voice and save the voice
recognition data to the bootable USB drive for the Mark 2. The updated
code saves the user's voice recognition file to a text file named after
the user's name, and then Neon confirms that the file has been saved
after the training is completed.

Skill Description: This skill allows Neon AI to recognize and remember
the voice of its user (up to four different voices) and save the voice
recognition data to the bootable USB drive for the Mark II device.
import speech_recognition as sr
import os

class UserVoiceRecognitionSkill:
def __init__(self):
self.user_voice_profiles = {}
self.sample_rate = 44100
self.chunk_size = 1024
self.format = sr.paInt16

def train_voice_profile(self, n_session):
r = sr.Recognizer()
with sr.Microphone(sample_rate=self.sample_rate, chunk_size=self.chunk_size) as source:
n_session.say("What is your name?")
audio = r.listen(source)

try:
user_name = r.recognize_sphinx(audio)
except sr.UnknownValueError:
n_session.say("Sorry, I couldn't understand your name. Please try again.")
return

with sr.Microphone(sample_rate=self.sample_rate, chunk_size=self.chunk_size) as source:
n_session.say(f"Hi {user_name}! Please say a few sentences so I can learn to recognize your voice.")
audio = r.listen(source)

try:
voice_profile = r.recognize_sphinx(audio)
if len(self.user_voice_profiles) < 4:
self.user_voice_profiles[user_name] = voice_profile
n_session.say("Thank you! I have learned to recognize your voice.")
else:
n_session.say("I can only recognize up to four different voices. Please remove a voice profile to add a new one.")
except sr.UnknownValueError:
n_session.say("Sorry, I couldn't understand what you said. Please try again.")

# Save user's voice recognition file to Bootable USB Drive for the Mark II
with open(f"{user_name}.txt", "w") as f:
f.write(voice_profile)

# Confirm the voice recognition file is saved
n_session.say(f"I now know your voice, {user_name}, and have saved it to my files.")
n_session.complete()

def execute(self, n_session):
if not self.user_voice_profiles:
self.train_voice_profile(n_session)

r = sr.Recognizer()
with sr.Microphone(sample_rate=self.sample_rate, chunk_size=self.chunk_size) as source:
audio = r.listen(source)

try:
user_input = r.recognize_sphinx(audio)
for user_name, voice_profile in self.user_voice_profiles.items():
if voice_profile.lower() in user_input.lower():
n_session.say(f"Hi {user_name}! How can I help you today?")
return
n_session.say("Sorry, I don't recognize your voice. Would you like to train me again?")
except sr.UnknownValueError:
n_session.say("Sorry, I couldn't understand what you said.")

Author

Adam Obradovic
{Empty}
{Empty}