-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdemo.py
115 lines (96 loc) · 4.06 KB
/
demo.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# - *- coding: utf- 8 - *-
import time
from pepper.robot import Pepper
try:
import urllib
except:
import urllib.request as urllib
import base64
import json
from PIL import Image
import random
def uploadPhotoToWeb(photo):
"""we need to upload photo to web as we (me) are not able to open it from local folder"""
f = open(photo, "rb") # open our image file as read only in binary mode
image_data = f.read() # read in our image file
b64_image = base64.standard_b64encode(image_data)
client_id = "af482612ae6d1c1" # this the id which we've got after registrating the app on imgur
headers = {'Authorization': 'Client-ID ' + client_id}
data = {'image': b64_image, 'title': 'test'}
request = urllib.Request(url="https://api.imgur.com/3/upload.json", data=urllib.urlencode(data),
headers=headers)
response = urllib.urlopen(request).read()
parse = json.loads(response)
return parse['data']['link'] #returns a url of the photo
def getRandName():
"""returns a random name for the picture in order not to replace the old photo"""
randNum = random.randint(0, 1000)
return "demoPictures/photo" + str(randNum) + ".png"
class PepperDemo:
def __init__(self, ip_address, port=9559):
self.robot = None
self.robot = Pepper(ip_address, port)
self.robot.set_czech_language()
self.photoName = None
self.greetings = ["Good afternoon", "Hello", "Hi", "Hello everobody", "Welcome"]
self.asks = ["May I photograph you?","May I take your picture?", "Do you want to make your picture?"]
def wantToTakePic(self):
"""recognise answer with google speech reco"""
answers = {"no": ["no", "no way", "not", "no no", " i don't", "i dont know", "not today", "later", "tommorow"],
"yes": ["yes", "definitely", "yep", "ok", "okey dokey", "sure", "all yes", "you must",
"absolutely", "i want", "i think so", "i agree", "if you want", "if you insist", "probably", "maybe",
"yes sir"]}
recorded = self.robot.recognize_google(lang="en-US")
answer = self.getAnswer(answers, recorded)
if answer == "no":
return False
elif answer == "yes":
return True
else:
return None
def getAnswer(self, dic, recorded):
"""looks for a recorded answer in a dictionar"""
for x in dic.keys():
if dic[x] in recorded.lower():
return x
return None
def welcomeAndAsk(self):
self.robot.say(random.choice(self.greetings))
self.robot.greet()
self.robot.say(random.choice(self.asks))
def takePicture(self):
self.robot.subscribe_camera("camera_top", 2, 30)
img = self.robot.get_camera_frame(show=False)
self.robot.unsubscribe_camera()
self.robot.play_sound("/home/nao/camera1.ogg")
im = Image.fromarray(img)
self.photoName = getRandName()
im.save(self.photoName)
def showPicture(self):
link = uploadPhotoToWeb(self.photoName)
self.robot.show_image(link)
time.sleep(5)
self.robot.reset_tablet()
def recogniseAnswerAndDecide(self):
isTakePic = self.wantToTakePic()
if isTakePic:
self.robot.say("Perfect. On your marks. 3, 2, 1 .")
self.takePicture()
self.showPicture()
elif isTakePic is None:
self.robot.say("Sorry, I did not understand you. Please repeat.")
self.recogniseAnswerAndDecide()
else:
self.robot.say("Maybe next time")
def dealWithRecoErrors(self):
"""there is a modifiable grammar error sometimes occurred.
In order to deal with it you should change language to english and back"""
self.robot.set_english_language()
self.robot.set_czech_language()
def run(self):
self.dealWithRecoErrors()
self.welcomeAndAsk()
self.recogniseAnswerAndDecide()
if __name__ == "__main__":
pepperDemo = PepperDemo("10.37.1.232")
pepperDemo.run()