Ejemplo n.º 1
0
    def on_event(self, event, data):
        if event == "chat" and self.fb_id:
            msg = data["message"]["text"]
            if msg == "train":
                chatbot.send_fb_message(self.fb_id, "start training")
                self.X, self.y = collect_img_hue(self.operating_instructions, self.user,
                                                 start=self.start, end=self.end)
                self.learner.data_update(self.X, self.y)
                self.learner.train()
                chatbot.send_fb_message(self.fb_id, "finish training")
                joblib.dump(self.learner.classifier, 'huelv2.pkl')

            if msg == "control true":
                self.control = True
            if msg == "control false":
                self.control = False

        if event == "timer":
            if self.learner.classifier is not None and self.control:
                d = get_current_images(self.user, self.cam_ids)
                all_state = self.operating_instructions[self.learner.predict(d)]
                hue_data = json.dumps([
                    {"id": "1", "state": all_state},
                    {"id": "2", "state": all_state},
                    {"id": "3", "state": all_state}
                ])
                self.output = [{"platform": "hue", "data": hue_data}]
Ejemplo n.º 2
0
    def on_event(self, event, data):
        if event == "chat" and self.fb_id:
            msg = data["message"]["text"]
            if msg == "train":
                chatbot.send_fb_message(self.fb_id, "start training")
                self.X_cols, self.labels = HueDBReader.read_db(
                    self.start, self.end)
                self.learner.data_update(self.X, self.y)
                self.learner.train()
                chatbot.send_fb_message(self.fb_id, "finish training")
                joblib.dump(self.learner.classifier, 'huelv2.pkl')

            if msg == "control true":
                self.control = True
            if msg == "control false":
                self.control = False
Ejemplo n.º 3
0
    def on_event(self, event, data):
        if event == "image":
            if self.classifier is not None:
                self.counter += 1
                feats, summs = self.get_current_images()
                
                if feats is None:
                    return
                #datasets.data2vec([], [], data, False, False, False, False, False, True)

                try:
                    pred = self.classifier.predict([feats])[0]
                    probs = self.classifier.predict_proba([feats])[0]
                    
                    self.history.append(probs)
                    if len(self.history) < self.last_avg*2:
                        return
                    
                    if len(self.history) > 20:
                        history = self.history[-20:]
                        
                    if self.classes[pred] == "watch":
                        state = "watch"
                        self.last_state = state
                        probs[pred] = 1.0
                    else:    
                        # moving average
                        avg_dist = np.mean(self.history[-self.last_avg:], axis=0)
                        state = self.classes[np.argmax(avg_dist)]#self.classes[pred]
                        pred = np.argmax(avg_dist)
                        probs = avg_dist
                        self.last_state = state
                    
                    if self.stream:
                        chatbot.send_fb_message(self.fb_id, state + " " + str(int(probs[pred] * 100.0)) + "%")
                        #chatbot.send_fb_message(self.fb_id, str([int(p*100.0) for p in avg_dist]))
                        #if self.counter % 20 == 0:
                        #    show_image_chat(summs[0], self.fb_id, send_img=True, message=state + " " + str(probs[pred]))
                        #chatbot.send_fb_message(self.fb_id, "===END===")
                        
                    if probs[pred] < self.prob_threshold:
                        return
                except Exception as e:
                    logger.error(str(e))
                    return

                self.output = []
                    
                if state == "bed":
                    all_state = {"on": False}
                    #chatbot.send_fb_message(self.fb_id, "turning off lights")
                else:
                    all_state = {"on": True}
                    
                    if state == "study":
                        all_state.update({"bri":254,"hue":33016,"sat":53})
                        #chatbot.send_fb_message(self.fb_id, "brightening lights")
                    else:
                        all_state.update({"bri":254,"hue":14957,"sat":141})
                        
                if state == "study":
                    if not self.music_on:
                        chatbot.send_fb_message(self.fb_id, "playing music")
                        self.output.append({"platform": "play_youtube", "data": ""})
                        self.music_on = True
                else:
                    if self.music_on:
                        chatbot.send_fb_message(self.fb_id, "stopping music")
                        self.output.append({"platform": "stop_youtube", "data":""})
                        self.music_on = False
                    
                hue_data = json.dumps([
                        {"id": "1", "state": all_state},
                        {"id": "2", "state": all_state},
                        {"id": "3", "state": all_state}
                    ])

                self.output.append({"platform": "hue", "data": hue_data})
                
                if state == "watch":
                    if not self.tv_on:
                        self.output.append({"platform": "irkit", "data": ["TV"]})
                        self.tv_on = True
                        chatbot.send_fb_message(self.fb_id, "toggling TV (to on)")
                else:
                    if self.tv_on:
                        self.output.append({"platform": "irkit", "data": ["TV"]})
                        self.tv_on = False
                        chatbot.send_fb_message(self.fb_id, "toggling TV (to off)")

                probs = self.classifier.predict_proba([feats])

                #logger.debug(self.classes)
                #logger.debug(probs)
                ##logger.debug("current: {}, pred: {}".format(self.last_pred, pred))
                #logger.debug(state)
                ##logger.debug("time since last change: " + str(time.time() - self.last_change))

                min_state_time = 60
            
            if self.train:
                self.train = False
                self.learn_and_load(force_update=True)
                chatbot.send_fb_message(self.fb_id, "retrained")
                
        elif event == "chat" and self.fb_id:
            msg = data["message"]["text"]
            if msg == "train":
                #self.learn_and_load()
                self.train = True
            elif msg == "stream":
                self.stream = not self.stream
            elif msg == "state":
                chatbot.send_fb_message(self.fb_id, self.last_state)
                chatbot.send_fb_message(self.fb_id, str(len(self.last_images)))
                for img in self.last_images:
                    show_image_chat_raw(img, self.fb_id)
            elif msg.startswith("avg"):
                prev = self.last_avg
                self.last_avg = int(msg.strip().split()[-1])
                chatbot.send_fb_message(self.fb_id, "changed averaging from %s to %s" % (prev, self.last_avg))
            elif msg.startswith("threshold"):
                prev = self.prob_threshold
                self.prob_threshold = float(msg.strip().split()[-1])
                chatbot.send_fb_message(self.fb_id, "changed threshold from %s to %s" % (prev, self.prob_threshold))
Ejemplo n.º 4
0
    def on_event(self, event, data):
        if event == "timer":
            if self.classifier is not None:
                feats, summs = self.get_current_images()
                #datasets.data2vec([], [], data, False, False, False, False, False, True)

                try:
                    pred = self.classifier.predict([feats])[0]
                    probs = self.classifier.predict_proba([feats])[0]
                    state = self.classes[pred]
                except Exception as e:
                    logger.error(str(e))
                    return

                if self.stream:
                    chatbot.send_fb_message(self.fb_id, state + " " + str(probs[pred]))
                    show_image_chat(summs[0], self.fb_id, send_img=True, message=state + " " + str(probs[pred]))
                    #chatbot.send_fb_message(self.fb_id, "===END===")
                    
                self.output = []
                    
                if state == "bed":
                    all_state = {"on": False}
                else:
                    all_state = {"on": True}
                    
                    if state == "study":
                        all_state.update({"bri":254,"hue":33016,"sat":53})
                        
                    else:
                        all_state.update({"bri":254,"hue":14957,"sat":141})
                        
                if state == "study":
                    if not self.music_on:
                        chatbot.send_fb_message(self.fb_id, "playing music")
                        self.output.append({"platform": "play_youtube", "data": ""})
                        self.music_on = True
                else:
                    if self.music_on:
                        chatbot.send_fb_message(self.fb_id, "stopping music")
                        self.output.append({"platform": "stop_youtube", "data":""})
                        self.music_on = False
                    
                hue_data = json.dumps([
                        {"id": "1", "state": all_state},
                        {"id": "2", "state": all_state},
                        {"id": "3", "state": all_state}
                    ])

                self.output.append({"platform": "hue", "data": hue_data})
                
                if state == "watch":
                    if not self.tv_on:
                        self.output.append({"platform": "irkit", "data": ["TV"]})
                        self.tv_on = True
                        chatbot.send_fb_message(self.fb_id, "toggling TV (to on)")
                else:
                    if self.tv_on:
                        self.output.append({"platform": "irkit", "data": ["TV"]})
                        self.tv_on = False
                        chatbot.send_fb_message(self.fb_id, "toggling TV (to off)")

                probs = self.classifier.predict_proba([feats])

                logger.debug(self.classes)
                logger.debug(probs)
                #logger.debug("current: {}, pred: {}".format(self.last_pred, pred))
                logger.debug(state)
                #logger.debug("time since last change: " + str(time.time() - self.last_change))

                min_state_time = 60
            
            if self.train:
                self.train = False
                self.learn_and_load(force_update=True)
                chatbot.send_fb_message(self.fb_id, "retrained")
                
        elif event == "chat" and self.fb_id:
            msg = data["message"]["text"]
            if msg == "train":
                #self.learn_and_load()
                self.train = True
            elif msg == "stream":
                self.stream = not self.stream
Ejemplo n.º 5
0
    def on_event(self, event, data):
        if event == "image":
            if not self.wait and time.time() - self.ask_time > self.duration:
                d = get_current_images(self.user, self.cam_ids)
                index, confidence = self.learner.predict('TV', [d])
                index = int(index[0])
                predicted_on = self.classes[index]
                if self.tv_on and (not predicted_on):
                    self.wait = True
                    self.ask_time = time.time()
                    self.re.append({
                        "platform": "irkit",
                        "data": ['TV', 'off'],
                        "confirmation": "テレビをけしますか?"
                    })
                elif (not self.tv_on) and predicted_on:
                    self.wait = True
                    self.ask_time = time.time()
                    self.re.append({
                        "platform": "irkit",
                        "data": ['TV', 'on'],
                        "confirmation": "テレビをつけますか?"
                    })

            if time.time() - self.ask_time > self.duration:
                self.wait = False

        if event == 'confirmation':
            if data['platform'] == 'irkit':
                self.wait = False
                if 'answer' in data:
                    if data['confirmation'] == 'テレビをつけますか?' and data['answer']:
                        self.tv_on = True
                    if data['confirmation'] == 'テレビをけしますか?' and data['answer']:
                        self.tv_on = False

        if event == "chat":
            msg = data["message"]["text"].split()
            if msg[0] == "irkit":
                if msg[1] == "TV":
                    if msg[2] == "on":
                        if self.tv_on:
                            chatbot.send_fb_message(self.fb_id, "TVは既についています")
                        else:
                            self.re.append({
                                "platform": "irkit",
                                "data": msg[1:],
                                "confirmation": "テレビをつけますか?"
                            })
                    elif msg[2] == "off":
                        if self.tv_on:
                            self.re.append({
                                "platform": "irkit",
                                "data": msg[1:],
                                "confirmation": "テレビをけしますか?"
                            })
                        else:
                            chatbot.send_fb_message(self.fb_id, "TVはすでにきえています")

                elif msg[1] == "AirConditioning":
                    pass

        if event == "speech" and data["type"] == "speech":
            msg = data["text"]
            if "テレビ" in msg and "つけて" in msg:
                if self.tv_on:
                    self.re.append({"platform": "tts", "data": "TVはすでについています"})
                else:
                    self.re.append({"platform": "tts", "data": "TVをつけます"})
                    self.re.append({"platform": "irkit", "data": ['TV', 'on']})
                    self.tv_on = True
            if "テレビ" in msg and "消して" in msg:
                if self.tv_on:
                    self.re.append({"platform": "tts", "data": "TVを消します"})
                    self.re.append({
                        "platform": "irkit",
                        "data": ['TV', 'off']
                    })
                    self.tv_on = False
                else:
                    self.re.append({"platform": "tts", "data": "TVは既に消えています"})