class Audio(object): _AFTER_PLAYING_DURATION = 1 def __init__(self): self._thread = None self._timeSinceStopPlaying = None def _play(self, name): playsound(name) def play(self, name): if self._thread is not None and not self._thread.isAlive(): self._thread.stop() self._thread = None self._thread = StoppableThread(target=self._play, args=(name, )) self._thread.start() self._timeSinceStopPlaying = None def isPlaying(self): ret = self._thread is not None and self._thread.isAlive() if not ret and self._thread is not None and self._timeSinceStopPlaying is None: self._timeSinceStopPlaying = Stopwatch() return ret def isAfterPlaying(self): if self._thread is None: return False if not self._thread.isAlive(): if self._timeSinceStopPlaying is None: self._timeSinceStopPlaying = Stopwatch() return True else: return self._timeSinceStopPlaying.get( ) / 1000 < self._AFTER_PLAYING_DURATION return False
class FpsCalc(object): _MAX_HISTORY = 5 def __init__(self): self._history = [] self._stopWatch = Stopwatch() def log(self): self._history.append(self._stopWatch.get()) if len(self._history) > self._MAX_HISTORY: self._history = self._history[-self._MAX_HISTORY:] startTime = self._history[0] stopTime = self._history[-1] if startTime == stopTime: fps = 1.0 else: fps = (1000 * len(self._history)) / (stopTime - startTime) return fps
class Intent(object): def __init__(self): self._messageDuration = Config.MESSAGE_DURATION self._speech = Speech("audio/") self._startPlayingVideoTime = None self._messageBox = MessageBox() if Config.ENABLE_BROWSER: print("Initing browser intent") self._browserService = BrowserService() def isBusy(self): #TODO: Add a bit of delay to keep busy status 2 seconds after talking return self.isTalking() or self.isAfterTalking( ) or self.isPlayingVideo() def isTalking(self): return self._speech.isSpeaking() def isAfterTalking(self): return self._speech.isAfterSpeaking() #voice part def askToPlayVideoOrMessage(self): self._speech.speak( "Do you want me to play some music for you or open the messagebox? say music or message" ) def GreetingUser(self, emotion_label): if emotion_label == "Happy": self._speech.speak( "It's a beautiful day to be grateful and happy, keep on rocking!" ) if emotion_label == "Sad": self._speech.speak("You seem sad! would you like a hug?") if emotion_label == "Angry": self._speech.speak( "Uh oh, you seem angry! I have kids, please don't hurt me") def askUserMood(self): self._speech.speak("It's a long day right? How are you feeling today") def readyToPlayVideo(self): self._speech.speak( "Well, I am so glad to join you!Hang on a second. Let me play some music for you!" ) def sayGoodby(self): self._speech.speak("Okay! I spent a great time with you! Bye-bye!") def askToPlayMoreVideo(self): self._speech.speak( "It's a great music right? Do you want to play more?") def askToPlayOrRecordMessage(self): self._speech.speak( "Welcome to messagebox! Do you want to listen to previous message or record a new one? say play or record" ) def askToPlayMessageBox(self): self._speech.speak("Then do you want to switch to messagebox?") def ReadyToRecordMessage(self): self._speech.speak( "Ok, you have 15 seconds to record your current mood.") def ReadyToPlayMessage(self): self._speech.speak("This is the message from past you") def askToPlayMusic(self): self._speech.speak("Then, would you like to listen to music with me?") def finishRecord(self): self._speech.speak("Ok,finish record") def playVideo(self, emotion_label): ret = True if Config.ENABLE_BROWSER: ret = self._browserService.searchAndPlay(emotion_label) print("**********") print(ret) if ret: self._startPlayingVideoTime = Stopwatch() return ret def playNextSong(self): self._browserService.playNextSong() def recordMessage(self): self._messageBox.recordMessage() time.sleep(self._messageDuration) def playMessage(self): self._messageBox.playMessage() time.sleep(self._messageDuration) def isPlayingVideo(self): if self._startPlayingVideoTime is None: return False elapsedSec = self._startPlayingVideoTime.get() / 1000 return elapsedSec < Config.VIDEO_PLAYBACK_TIME def stopVideo(self): if Config.ENABLE_BROWSER: self._browserService.stop()
sw = Stopwatch() show_preview = True show_annot = False while True: img = video_reader.read_frame() num_frames += 1 if img is None: break objects, annot_image = pose_estimator.detect( img, return_annotated_image=show_preview) roll, pitch, game_state, left_wing_roll_target, right_wing_roll_target, body_height = motion.parse_objects( objects) socket_sender.send(int(roll), int(pitch), game_state, int(left_wing_roll_target), int(right_wing_roll_target), int(body_height)) elapsed_ms = max(1, sw.get()) if show_preview: cv2.imshow("Original", img) if annot_image is not None: size = annot_image.shape height = size[0] width = size[1] if height > 0 and width > 0: cv2.imshow("Annotated", annot_image) cv2.waitKey(1)
class Intent(object): def __init__(self): self._speech = Speech("audio/") self._startPlayingVideoTime = None if Config.ENABLE_BROWSER: print("Initing browser intent") self._browserService = BrowserService() def isBusy(self): #TODO: Add a bit of delay to keep busy status 2 seconds after talking return self.isTalking() or self.isAfterTalking( ) or self.isPlayingVideo() def isTalking(self): return self._speech.isSpeaking() def isAfterTalking(self): return self._speech.isAfterSpeaking() def askToComeAndPlay(self): self._speech.speak("Hi Dexie? do you want to come and play?") def askToBringObject(self): self._speech.speak("Dexie? Do you want to bring me something?") def askToBringNewObject(self, oldObjectName): self._speech.speak( "We have just played with {0} already. Why don'y you bring me something else?" .format(self._appendObjectNameAbbreviation(oldObjectName))) def askToBringAnotherObject(self): self._speech.speak( "Well, that was fun isn't it? Do you want to bring me something else?" ) def _appendObjectNameAbbreviation(self, objectName): objectName = objectName.lower() if objectName[0] in {'a', 'i', 'e', 'o', 'u'}: objectName = "an {0}".format(objectName) else: objectName = "a {0}".format(objectName) return objectName def dontHaveVideo(self, objectName): self._speech.speak( "I am sorry. I cannot find a video about {0}! Do you want to bring me something else?" .format(self._appendObjectNameAbbreviation(objectName))) def objectRecognised(self, objectName): self._speech.speak("Hey, I think that is {0}!".format( self._appendObjectNameAbbreviation(objectName))) def playVideo(self, objectName): ret = True self._speech.speak( "Hang on a second. Let me play you a video about {0}!".format( self._appendObjectNameAbbreviation(objectName))) if Config.ENABLE_BROWSER: ret = self._browserService.searchAndPlay(objectName) if ret: self._startPlayingVideoTime = Stopwatch() return ret def isPlayingVideo(self): if self._startPlayingVideoTime is None: return False elapsedSec = self._startPlayingVideoTime.get() / 1000 return elapsedSec < Config.VIDEO_PLAYBACK_TIME def stopVideo(self): if Config.ENABLE_BROWSER: self._browserService.stop()