예제 #1
0
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
예제 #2
0
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()