class _dialog(ALModule): """ class _dialog :wrapper arround naoqi events. contains handlers for nao,callbacks called by server.py. """ def __init__(self, args): """class constructor.""" self.NAO_IP = "nao.local" self.NAO_PORT = 9559 self.motion = ALProxy("ALMotion", "nao.local", self.NAO_PORT) self.nao = Nao() self.names = ["nathan", "michel", "sophie", "delphine"] print "_dialog instance have been instanciated" def __call__(self): """return instance, usefull if called from othet script.""" return self def readInstuction(self): """reads, and returns jsonified isntructions.""" instru = json.loads(open('../dialog.json').read()) return instru def stopDialog(self): """join the threads, will close when stop is heard.""" print "before join" self.thread.join() print "after join" def updateJson(self, jsonFile): """write json file in dialog.json.""" f = open('../dialog.json', 'w') print jsonFile f.write(str(jsonFile)) def createsFaceEvent(self): """triger facecallback when face is detected.""" print "in face event" self.memory = ALProxy("ALMemory", self.NAO_IP, self.NAO_PORT) faceProxy = ALProxy("ALFaceDetection", self.NAO_IP, self.NAO_PORT) period = 500 faceProxy.subscribe("Face_detect", period, 0.0) detected = False i = 0 while detected is False: time.sleep(0.5) val = self.memory.getData("FaceDetected") if(val and isinstance(val, list) and len(val) >= 2): self.faceCallback() detected = True else: print "i can't see anyone right now" i += 1 if (i is 30): detected = True self.nao.say("je n'ai vu personne , cliques sur le bouton réessayer") def startReco(self): """spawn face event in a new Thread.""" print "will spawn thread" self.thread = Thread(target=self.createsFaceEvent) self.thread.start() def getJson(self): """read and return dialog.json as a string.""" instru = open('../dialog.json').read() return instru def faceCallback(self): """function called when nao detects a face.""" print "face detected" self.nao.say("j'ai vu quelqu'un") self.vocabulary = ["stop"] for line in self.readInstuction(): self.vocabulary.append(str(line)) print "instru :", line # print "je suis pret" self.nao.setVocabulary(self.vocabulary) self.createVoiceEvent() time.sleep(1) def createVoiceEvent(self): """called to init voice event.""" voc_list = self.readInstuction() WordRecognized = False i = 0 end = True while WordRecognized is False: time.sleep(1) val = self.memory.getData("WordRecognized") anws = val[0] # anws = raw_input("what didu say ?\n") print "u said : ", anws if anws == "stop": self.nao.say("ok j'arette") end = True WordRecognized = True elif (anws in self.names): print "mmm ur called ", anws self.nao.say("ha tu es" + anws) end = False self.memory.insertData("WordRecognized", " ") elif anws != "auto": if anws == "nao": end = False WordRecognized = True self.nao.say("c'est moi !") self.memory.insertData("WordRecognized", " ") elif anws in self.vocabulary: WordRecognized = True end = False # print voc_list[anws], anws text = random.choice(voc_list[anws]) print "i'll say :", str(text['anwser']) self.nao.say(str(text['anwser'])) ac = text['action'].split(" ", 1) if len(ac) > 1: action = self.nao.getAction().get(str(ac[0])) if action is not None: action(str(ac[1])) elif len(ac) <= 0: action = self.nao.getAction().get(str(text['action'])) if action is not None: action() self.memory.insertData("WordRecognized", " ") else: pass else: end = False i += 1 if (i is 30): WordRecognized = True if end is False: self.createVoiceEvent()
class qrRetreiver(): """docstring for qrRetreiver.""" def __init__(self): """instatiate qrRetreiver.""" """class constructor.""" self.NAO_IP = "nao.local" self.NAO_PORT = 9559 self.motion = ALProxy("ALMotion", "nao.local", self.NAO_PORT) self.memory = ALProxy("ALMemory", self.NAO_IP, self.NAO_PORT) self.nao = Nao() pass """ REGION methodes """ def makeQr(self, text): """make a qrCode, with specified txt, returns it.""" code = pyqrcode.create(str(text)) code.png("./pics/" + str(text) + '_qr.png', scale=8) content = open("./pics/" + str(text) + '_qr.png', 'r').read() return content def getAll(self): """return all qrcodes.""" # reg = "(.*_qr)\.png\b" reg = "*_qr.png" ret_arr = [] for file in os.listdir('./pics'): if fnmatch.fnmatch(file, reg): ret_arr.append(file) return ret_arr def getImage(self): """main method, wait until qr code is found.""" period = 1000 qrCodeProxy = ALProxy("ALBarcodeReader", self.NAO_IP, self.NAO_PORT) qrCodeProxy.subscribe("Testh_qr", period, 0.0) detected = False i = 0 while detected is False: time.sleep(0.5) val = self.memory.getData("BarcodeReader/BarcodeDetected") print val if (val is not None): if(len(val) >= 1): detected = True todo = val[0][0] ac = todo.split(" ", 1) if len(ac) > 1: action = self.nao.getAction().get(str(ac[0])) action(str(ac[1])) self.memory.insertData("BarcodeReader/BarcodeDetected", "") else: action = self.nao.getAction().get(todo) action() self.memory.insertData("BarcodeReader/BarcodeDetected", "") i += 1 if i is 30: detected = True self.nao.say("je n ai pas reussi a trouver le code, essayes encore si tu veux")
@crossdomain(origin='*') def get_latest_pictures(): """Return the picture with provided name (0.png ...).""" print 'here' pic = glob.glob('./pics/' + request.args.get("n") + '.png') print pic[0] return send_file(pic[0], mimetype='image/png') @app.route('/take_pic') @crossdomain(origin='*') def take_pic(): """remove and permute old pic, save the new one.""" os.remove("./pics/2.png") os.rename("./pics/1.png", "./pics/2.png") os.rename("./pics/0.png", "./pics/1.png") photoCaptureProxy = ALProxy("ALPhotoCapture", "192.168.1.8", 9559) photoCaptureProxy.setResolution(2) photoCaptureProxy.setPictureFormat("png") photoCaptureProxy.takePictures(1, "/home/nao/NaoAccueil/www/Modules/python/pics", "_0") img = Image.open("./pics/_0.png") resultat = img.rotate(90, expand=1) resultat.save("./pics/0.png") return send_file("./pics/0.png", mimetype='image/png') print "streamer" nao = Nao() nao.say("je suis pret !") app.run(host='0.0.0.0', debug=False, threaded=True)