Skip to content

Instantly share code, notes, and snippets.

@me00016
Last active August 14, 2020 20:16
Show Gist options
  • Save me00016/2643a9534ec4799fb301e8119c1abad6 to your computer and use it in GitHub Desktop.
Save me00016/2643a9534ec4799fb301e8119c1abad6 to your computer and use it in GitHub Desktop.
COZMO conversing using ChatterBot 0.7.6
#!/usr/bin/env python3
# Copyright (c) 2016 Anki, Inc. (except Chatterbot part and a couple of my
# own lines of code)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License in the file LICENSE.txt or at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''COZMO conversing using ChatterBot 0.7.6
'''
import speech_recognition as sr
from chatterbot import ChatBot
from cozmo.util import degrees
import cozmo
import sys
import re
# cozmo.robot.Robot.drive_off_charger_on_connect = False
def main_program(robot: cozmo.robot.Robot):
chatbot = ChatBot('Cozmo', trainer='chatterbot.trainers.ChatterBotCorpusTrainer')
# Train based on the english corpus
# chatbot.train("chatterbot.corpus.english")
while robot.battery_voltage > 3.6:
Cozmostring = get_response(chatbot)
if Cozmostring == "freeplay_time":
if robot.is_freeplay_mode_active == False:
robot.start_freeplay_behaviors()
else:
pass
else:
robot.stop_freeplay_behaviors()
robot.set_head_angle(degrees(30.0)).wait_for_completed()
robot.set_lift_height(0.0).wait_for_completed()
robot.set_all_backpack_lights(cozmo.lights.green_light.flash())
robot.say_text(Cozmostring).wait_for_completed()
print(Cozmostring)
# Four basic backpack lights are triggered, depending on color keywords in Cozmo's replies
if re.search(r"\bRed\b|\bred\b", Cozmostring):
robot.set_all_backpack_lights(cozmo.lights.red_light)
elif re.search(r"\bGreen\b|\bgreen\b", Cozmostring):
robot.set_center_backpack_lights(cozmo.lights.green_light)
elif re.search(r"\bBlue\b|\bblue\b", Cozmostring):
robot.set_center_backpack_lights(cozmo.lights.blue_light)
elif re.search(r"\bWhite\b|\bwhite\b", Cozmostring):
robot.set_center_backpack_lights(cozmo.lights.white_light)
else:
robot.set_all_backpack_lights(cozmo.lights.off_light)
robot.play_anim('anim_workout_lowenergy_weak_01').wait_for_completed()
robot.say_text("I run out of fuel!. Could you put me on the charger, please?").wait_for_completed()
print("the end: ta ta taaaaaa!")
def get_response(chatbot):
# Get a response to an input statement
speech = return_audio()
if speech == "quit":
sys.exit()
elif speech == "silence":
Cozmostring = "freeplay_time"
else:
Cozmostring = str(chatbot.get_response(speech))
return Cozmostring
def return_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
speech = r.recognize_google(audio)
try:
speech = str(speech)
print(speech)
return speech
except TypeError:
print("TypeError! Could not convert speech to string!")
return "TypeError"
except sr.UnknownValueError:
print("...freeplay time...")
return "silence"
except sr.RequestError as e:
print("Error! No internet connection to Google Sound Recognizer.")
return "InternetConnectionError!"
cozmo.run_program(main_program)
@me00016
Copy link
Author

me00016 commented Sep 26, 2017

Here’s a program about the integration of Chatterbot chatbot with Cozmo; the result? Now, everybody can discuss with his adorable gadget about “Life, the Universe and Everything” (depending on the training set), using Google speech recognition!

Requirements:
Installation of COZMO SDK
Installation of Chatterbot
Installation of Speech Recognition

@Nimentrix
Copy link

Great job ! I'm adapting it for my french ears ;-) French Corpus is poor so i will try to translate some of the english one, and of course my trigger words are in french ;-)

@AnderPander
Copy link

Don't know if others will have this problem. But on Ubuntu 16,04 had to add --
Line 84 r.pause_threshold = 0.8
Line 85 r.dynamic_energy_threshold = False #was True
Line 86 r.adjust_for_ambient_noise(source)

for mic to work properly. If others on Linux have this problem you might add it and comment it out.
This is great work. Thanks much for the effort. I plan on some Cozmo AIML specific file's to fit his character.

Happy Holiday's.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment