forked from cltl/pepper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreeting.py
63 lines (44 loc) · 2.11 KB
/
greeting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Example Application that greets known and unknown people"""
from pepper.framework import *
from pepper import config
from time import time
class GreetingApplication(AbstractApplication, # All Applications inherit from AbstractApplication
FaceRecognitionComponent, # We need Face Recognition to Greet People by Name
StatisticsComponent,
SpeechRecognitionComponent,
TextToSpeechComponent): # We need Text to Speech to actually greet people
GREET_TIMEOUT = 15 # Only Greet people once every X seconds
def __init__(self, backend):
"""Greets New and Known People"""
super(GreetingApplication, self).__init__(backend)
self.name_time = {} # Dictionary of <name, time> pairs, to keep track of who is greeted when
def on_face_known(self, faces):
"""
On Person Event.
Called every time a known face is detected.
"""
for person in faces:
if self.is_greeting_appropriate(person.name):
self.say("Hello, {}!".format(person.name))
def on_face_new(self, faces):
"""
On New Person Event.
Called every time an unknown face is detected.
"""
if self.is_greeting_appropriate("new"):
self.say("I see a new person!, Hello stranger!")
def is_greeting_appropriate(self, name):
"""Returns True if greeting is appropriate and updates Greeting Time"""
# Appropriateness arises when
# 1. person hasn't been seen before, or
# 2. enough time has passed since last sighting
if name not in self.name_time or (time() - self.name_time[name]) > self.GREET_TIMEOUT:
# Store last seen time (right now) in name_time dictionary
self.name_time[name] = time()
# Return "Appropriate"
return True
# Return "Not Appropriate"
return False
if __name__ == "__main__":
# Run Application with Backend specified in Global Configuration File
GreetingApplication(config.get_backend()).run()