Exemplo n.º 1
0
class FBDashboardService(object):
    def __init__(self, comm_config, data_dirpath):
        self.comm_config = comm_config
        for topic, functor in self.comm_config['subscribe'].items():
            self.comm_config['subscribe'][topic] = eval(functor)
        self.comm_config['subscribe'][
            'berrynet/engine/tensorflow/result'] = self.update
        self.comm_config['subscribe'][
            'berrynet/engine/pipeline/result'] = self.update
        #self.comm_config['subscribe']['berrynet/data/rgbimage'] = self.update
        self.comm = Communicator(self.comm_config, debug=True)
        self.data_dirpath = data_dirpath
        self.frame = None

    def update(self, pl):
        if not os.path.exists(self.data_dirpath):
            try:
                os.mkdir(self.data_dirpath)
            except Exception as e:
                logger.warn('Failed to create {}'.format(self.data_dirpath))
                raise (e)

        payload_json = payload.deserialize_payload(pl.decode('utf-8'))
        if 'bytes' in payload_json.keys():
            img_k = 'bytes'
        elif 'image_blob' in payload_json.keys():
            img_k = 'image_blob'
        else:
            raise Exception('No image data in MQTT payload')
        jpg_bytes = payload.destringify_jpg(payload_json[img_k])
        payload_json.pop(img_k)
        logger.debug('inference text result: {}'.format(payload_json))

        img = payload.jpg2rgb(jpg_bytes)
        try:
            res = payload_json['annotations']
        except KeyError:
            res = [{
                'label': 'hello',
                'confidence': 0.42,
                'left': random.randint(50, 60),
                'top': random.randint(50, 60),
                'right': random.randint(300, 400),
                'bottom': random.randint(300, 400)
            }]
        self.frame = overlay_on_image(img, res)

        #timestamp = datetime.now().isoformat()
        #with open(pjoin(self.data_dirpath, timestamp + '.jpg'), 'wb') as f:
        #    f.write(jpg_bytes)
        #with open(pjoin(self.data_dirpath, timestamp + '.json'), 'w') as f:
        #    f.write(json.dumps(payload_json, indent=4))

    def update_fb(self):
        gl_draw_fbimage(self.frame)

    def run(self, args):
        """Infinite loop serving inference requests"""
        self.comm.start_nb()
Exemplo n.º 2
0
class FBDashboardService(object):
    def __init__(self, comm_config, data_dirpath):
        self.comm_config = comm_config
        for topic, functor in self.comm_config['subscribe'].items():
            self.comm_config['subscribe'][topic] = eval(functor)
        self.comm_config['subscribe'][
            'berrynet/engine/pipeline/result'] = self.update
        #self.comm_config['subscribe']['berrynet/data/rgbimage/0'] = self.update
        #self.comm_config['subscribe']['berrynet/data/rgbimage'] = self.update
        self.comm = Communicator(self.comm_config, debug=True)
        self.data_dirpath = data_dirpath
        self.frame = None

    def update(self, pl):
        if not os.path.exists(self.data_dirpath):
            try:
                os.mkdir(self.data_dirpath)
            except Exception as e:
                logger.warn('Failed to create {}'.format(self.data_dirpath))
                raise (e)

        payload_json = payload.deserialize_payload(pl.decode('utf-8'))
        #payload_json = payload_json[0]
        if 'bytes' in payload_json.keys():
            img_k = 'bytes'
        else:
            raise Exception('No image data in MQTT payload')
        jpg_bytes = payload.destringify_jpg(payload_json[img_k])
        payload_json.pop(img_k)
        #logger.debug('inference text result: {}'.format(payload_json))

        img = payload.jpg2rgb(jpg_bytes)
        self.frame = img

        #timestamp = datetime.now().isoformat()
        #with open(pjoin(self.data_dirpath, timestamp + '.jpg'), 'wb') as f:
        #    f.write(jpg_bytes)
        #with open(pjoin(self.data_dirpath, timestamp + '.json'), 'w') as f:
        #    f.write(json.dumps(payload_json, indent=4))

    def update_fb(self):
        gl_draw_fbimage(self.frame)

    def run(self, args):
        """Infinite loop serving inference requests"""
        self.comm.start_nb()
Exemplo n.º 3
0
class DydaConfigUpdateClient(object):
    def __init__(self, comm_config, debug=False):
        self.comm_config = comm_config
        for topic, functor in self.comm_config['subscribe'].items():
            self.comm_config['subscribe'][topic] = self.handleResult
        self.comm = Communicator(self.comm_config, debug=True)

    def sendConfig(self, payloadID):
        self.comm.send(self.comm_config['publish'], payloadID)
        
    def handleResult(self, pl):
        try:
            payload_json = payload.deserialize_payload(pl.decode('utf-8'))
            print(payload_json)
            self.comm.stop_nb()
            sys.exit(0)
        except Exception as e:
            logger.info(e)

    def run(self, args):
        """Infinite loop serving inference requests"""
        self.comm.start_nb()
        self.sendConfig(args['payload'])
        time.sleep(1)
Exemplo n.º 4
0
class TelegramBotService(object):
    def __init__(self, comm_config, token, target_label='', debug=False):
        self.comm_config = comm_config
        for topic, functor in self.comm_config['subscribe'].items():
            self.comm_config['subscribe'][topic] = eval(functor)
        # NOTE: Maybe change the hard-coding topic to parameter in the future.
        self.comm_config['subscribe'][
            'berrynet/data/rgbimage'] = self.single_shot
        self.comm = Communicator(self.comm_config, debug=True)
        if os.path.isfile(token):
            self.token = self.get_token_from_config(token)
        else:
            self.token = token
        self.target_label = target_label
        self.debug = debug

        # Telegram Updater employs Telegram Dispatcher which dispatches
        # updates to its registered handlers.
        self.updater = telegram.ext.Updater(self.token, use_context=True)
        self.cameraHandlers = []

        self.shot = False
        self.single_shot_chat_id = None

    def get_token_from_config(self, config):
        with open(config) as f:
            cfg = json.load(f)
        return cfg['token']

    def match_target_label(self, target_label, bn_result):
        labels = [r['label'] for r in bn_result['annotations']]
        if target_label in labels:
            logger.debug('Find {0} in inference result {1}'.format(
                target_label, labels))
            return True
        else:
            logger.debug('Not find {0} in inference result {1}'.format(
                target_label, labels))
            return False

    def update(self, pl):
        try:
            payload_json = payload.deserialize_payload(pl.decode('utf-8'))
            jpg_bytes = payload.destringify_jpg(payload_json["bytes"])
            jpg_file_descriptor = io.BytesIO(jpg_bytes)

            for u in self.cameraHandlers:
                if self.updater is None:
                    continue

                if self.target_label == '':
                    if len(payload_json['annotations']) > 0:
                        logger.debug("Send photo to %s" % u)
                        self.updater.bot.send_photo(chat_id=u,
                                                    photo=jpg_file_descriptor)
                    else:
                        logger.debug("Does not detect any object, no action")
                elif self.match_target_label(self.target_label, payload_json):
                    logger.info("Send notification photo with result to %s" %
                                u)
                    self.updater.bot.send_photo(chat_id=u,
                                                photo=jpg_file_descriptor)
                else:
                    pass
        except Exception as e:
            logger.info(e)

    def single_shot(self, pl):
        """Capture an image from camera client and send to the client.
        """
        if self.shot is True:
            try:
                payload_json = payload.deserialize_payload(pl.decode('utf-8'))
                # WORKAROUND: Support customized camera client.
                #
                # Original camera client sends an `obj` in payload,
                # Customized camera client sends an `[obj]` in payload.
                #
                # We are unifying the rules. Before that, checking the type
                # as workaround.
                if type(payload_json) is list:
                    logger.debug('WORDAROUND: receive and unpack [obj]')
                    payload_json = payload_json[0]
                jpg_bytes = payload.destringify_jpg(payload_json["bytes"])
                jpg_file_descriptor = io.BytesIO(jpg_bytes)

                logger.info('Send single shot')
                self.updater.bot.send_photo(chat_id=self.single_shot_chat_id,
                                            photo=jpg_file_descriptor)
            except Exception as e:
                logger.info(e)

            self.shot = False
        else:
            logger.debug('Single shot is disabled, do nothing.')

    def run(self, args):
        """Infinite loop serving inference requests"""
        self.comm.start_nb()
        self.connect_telegram(args)

    def connect_telegram(self, args):
        try:
            self.updater.dispatcher.add_handler(
                telegram.ext.CommandHandler('help', self.handler_help))
            self.updater.dispatcher.add_handler(
                telegram.ext.CommandHandler('hi', self.handler_hi))
            self.updater.dispatcher.add_handler(
                telegram.ext.CommandHandler('camera', self.handler_camera))
            self.updater.dispatcher.add_handler(
                telegram.ext.CommandHandler('stop', self.handler_stop))
            self.updater.dispatcher.add_handler(
                telegram.ext.CommandHandler('shot', self.handler_shot))
            if (args["has_getlog"]):
                self.updater.dispatcher.add_handler(
                    telegram.ext.CommandHandler('getlog', self.handler_getlog))
            self.updater.start_polling()
        except Exception as e:
            logger.critical(e)

    def handler_help(self, update, context):
        logger.info("Received command `help`")
        update.message.reply_text(('I support these commands:\n\n'
                                   'help - Display help message.\n'
                                   'hi - Test Telegram client.\n'
                                   'camera - Start camera.\n'
                                   'stop - Stop camera.\n'
                                   'shot - Take a shot from camera.'))

    def handler_hi(self, update, context):
        logger.info("Received command `hi`")
        update.message.reply_text('Hi, {}'.format(
            update.message.from_user.first_name))

    def handler_camera(self, update, context):
        logger.info("Received command `camera`, chat id: %s" %
                    update.message.chat_id)
        # Register the chat-id for receiving images
        if (update.message.chat_id not in self.cameraHandlers):
            self.cameraHandlers.append(update.message.chat_id)
        update.message.reply_text('Dear, I am ready to help send notification')

    def handler_stop(self, update, context):
        logger.info("Received command `stop`, chat id: %s" %
                    update.message.chat_id)
        # Register the chat-id for receiving images
        while (update.message.chat_id in self.cameraHandlers):
            self.cameraHandlers.remove(update.message.chat_id)
        update.message.reply_text('Bye')

    def handler_shot(self, update, context):
        logger.info("Received command `shot`, chat id: %s" %
                    update.message.chat_id)
        # Register the chat-id for receiving images
        self.shot = True
        self.single_shot_chat_id = update.message.chat_id
        logger.debug('Enable single shot.')

    def handler_getlog(self, update, context):
        logger.info("Received command `getlog`, chat id: %s" %
                    update.message.chat_id)
        # Create temporary tar.xz file
        tmpTGZ1 = tempfile.NamedTemporaryFile(suffix=".tar.xz")
        tmpTGZ = tarfile.open(fileobj=tmpTGZ1, mode="w:xz")
        tmpTGZPath = tmpTGZ1.name

        # Traverse /var/log
        varlogDir = os.path.abspath(os.path.join(os.sep, "var", "log"))
        for root, dirs, files in os.walk(varlogDir):
            for file in files:
                fullPath = os.path.join(root, file)
                # Check if the file is a regular file
                if not os.path.isfile(fullPath):
                    continue
                # Check if the file is accessible
                if not os.access(fullPath, os.R_OK):
                    continue
                # Pack the file
                tmpTGZ.add(name=fullPath, recursive=False)
        tmpTGZ.close()
        self.updater.bot.send_document(
            chat_id=update.message.chat_id,
            document=open(tmpTGZPath, 'rb'),
            filename=time.strftime('berrynet-varlog_%Y%m%d_%H%M%S.tar.xz'))
Exemplo n.º 5
0
class TelegramBotService(object):
    def __init__(self, comm_config, token, target_label='', debug=False):
        self.comm_config = comm_config
        for topic, functor in self.comm_config['subscribe'].items():
            self.comm_config['subscribe'][topic] = eval(functor)
        self.comm = Communicator(self.comm_config, debug=True)
        self.token = token
        self.target_label = target_label
        self.debug = debug

        # Telegram Updater employs Telegram Dispatcher which dispatches
        # updates to its registered handlers.
        self.updater = telegram.ext.Updater(self.token, use_context=True)
        self.cameraHandlers = []

    def match_target_label(self, target_label, bn_result):
        labels = [r['label'] for r in bn_result['annotations']]
        if target_label in labels:
            logger.debug('Find {0} in inference result {1}'.format(
                target_label, labels))
            return True
        else:
            logger.debug('Not find {0} in inference result {1}'.format(
                target_label, labels))
            return False

    def update(self, pl):
        try:
            payload_json = payload.deserialize_payload(pl.decode('utf-8'))
            jpg_bytes = payload.destringify_jpg(payload_json["bytes"])
            jpg_file_descriptor = io.BytesIO(jpg_bytes)

            for u in self.cameraHandlers:
                if self.updater is None:
                    continue

                if self.target_label == '':
                    logger.info("Send photo to %s" % u)
                    self.updater.bot.send_photo(chat_id=u,
                                                photo=jpg_file_descriptor)
                elif self.match_target_label(self.target_label, payload_json):
                    logger.info("Send notification photo with result to %s" %
                                u)
                    self.updater.bot.send_photo(chat_id=u,
                                                photo=jpg_file_descriptor)
                else:
                    pass
        except Exception as e:
            logger.info(e)

    def run(self, args):
        """Infinite loop serving inference requests"""
        self.comm.start_nb()
        self.connect_telegram()

    def connect_telegram(self):
        try:
            self.updater.dispatcher.add_handler(
                telegram.ext.CommandHandler('help', self.handler_help))
            self.updater.dispatcher.add_handler(
                telegram.ext.CommandHandler('hello', self.handler_hello))
            self.updater.dispatcher.add_handler(
                telegram.ext.CommandHandler('camera', self.handler_camera))
            self.updater.start_polling()
        except Exception as e:
            logger.critical(e)

    def handler_help(self, update, context):
        logger.info("Received command `help`")
        update.message.reply_text(
            'I support these commands: help, hello, camera')

    def handler_hello(self, update, context):
        logger.info("Received command `hello`")
        update.message.reply_text('Hello, {}'.format(
            update.message.from_user.first_name))

    def handler_camera(self, update, context):
        logger.info("Received command `camera`, chat id: %s" %
                    update.message.chat_id)
        # Register the chat-id for receiving images
        if (update.message.chat_id not in self.cameraHandlers):
            self.cameraHandlers.append(update.message.chat_id)
        update.message.reply_text('Dear, I am ready to help send notification')