forked from jesnk/pepper_demo_bilab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reacting_to_events.py
97 lines (84 loc) · 3.36 KB
/
reacting_to_events.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#! /usr/bin/env python
# -*- encoding: UTF-8 -*-
"""Example: A Simple class to get & read FaceDetected Events"""
import qi
import time
import sys
import argparse
class HumanGreeter(object):
"""
A simple class to react to face detection events.
"""
def __init__(self, session):
"""
Initialisation of qi framework and event detection.
"""
super(HumanGreeter, self).__init__()
# app.start()
# session = app.session
# Get the service ALMemory.
self.memory = session.service("ALMemory")
# Connect the event callback.
self.subscriber = self.memory.subscriber("FaceDetected")
self.subscriber.signal.connect(self.on_human_tracked)
# Get the services ALTextToSpeech and ALFaceDetection.
self.tts = session.service("ALTextToSpeech")
self.face_detection = session.service("ALFaceDetection")
self.face_detection.subscribe("HumanGreeter")
self.got_face = False
def on_human_tracked(self, value):
"""
Callback for event FaceDetected.
"""
if value == []: # empty value when the face disappears
self.got_face = False
elif not self.got_face: # only speak the first time a face appears
self.got_face = True
print "I saw a face!"
# self.tts.say("Hello, you!")
# First Field = TimeStamp.
timeStamp = value[0]
print "TimeStamp is: " + str(timeStamp)
# Second Field = array of face_Info's.
faceInfoArray = value[1]
for j in range( len(faceInfoArray)-1 ):
faceInfo = faceInfoArray[j]
# First Field = Shape info.
faceShapeInfo = faceInfo[0]
# Second Field = Extra info (empty for now).
faceExtraInfo = faceInfo[1]
print "Face Infos : alpha %.3f - beta %.3f" % (faceShapeInfo[1], faceShapeInfo[2])
print "Face Infos : width %.3f - height %.3f" % (faceShapeInfo[3], faceShapeInfo[4])
# print "Face Extra Infos :" + str(faceExtraInfo)
def run(self):
"""
Loop on, wait for events until manual interruption.
"""
print "Starting HumanGreeter"
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print "Interrupted by user, stopping HumanGreeter"
self.face_detection.unsubscribe("HumanGreeter")
#stop
sys.exit(0)
# if __name__ == "__main__":
# parser = argparse.ArgumentParser()
# parser.add_argument("--ip", type=str, default="127.0.0.1",
# help="Robot IP address. On robot or Local Naoqi: use '127.0.0.1'.")
# parser.add_argument("--port", type=int, default=9559,
# help="Naoqi port number")
#
# args = parser.parse_args()
# try:
# # Initialize qi framework.
# connection_url = "tcp://" + args.ip + ":" + str(args.port)
# app = qi.Application(["HumanGreeter", "--qi-url=" + connection_url])
# except RuntimeError:
# print ("Can't connect to Naoqi at ip \"" + args.ip + "\" on port " + str(args.port) +".\n"
# "Please check your script arguments. Run with -h option for help.")
# sys.exit(1)
#
# human_greeter = HumanGreeter(app)
# human_greeter.run()