Example #1
0
    def generalize_result(self, eng_input, eng_output):
        # Pipeline returns None if any error happened
        if eng_output is None:
            eng_output = {}

        # If pipeline generate multiple outputs simultaneously
        #
        # In this case, the format of engine output is
        #
        #     {
        #         'annotations': {...},
        #         'bytes': '...'
        #     }
        if all(key in eng_output.keys() for key in ['annotations', 'bytes']):
            logger.debug('Pipeline output type: multiple')
            logger.debug('eng_input type = {0}, len = {1}'.format(
                type(eng_input), len(eng_input)))

            # FIXME: Re-cap why eng_input is a list and only contains 1 item.
            eng_input = eng_input[0]

            try:
                eng_input['annotations'] = eng_output['annotations']
                logger.debug('output image type: {0}, len: {1}'.format(
                    type(eng_output['bytes']), len(eng_output['bytes'])))
                pipeline_img = eng_output['bytes'][0]
                retval, jpg_bytes = cv2.imencode('.jpg', pipeline_img)
                eng_input['bytes'] = payload.stringify_jpg(jpg_bytes)
            except Exception as e:
                logger.critical(e)
        else:
            logger.debug('Pipeline output type: simple')

            # FIXME: Workaround for spec incompatibility
            # DLBox spec use 'image_blob', but BerryNet use 'bytes', so we have to
            # do a convert here
            if isinstance(eng_output, list):
                inf_output = eng_output[0]
            else:
                inf_output = eng_output
            if len(eng_input) > 1:
                for i in range(len(eng_input)):
                    try:
                        retval, jpg_bytes = cv2.imencode('.jpg', inf_output)
                        eng_input[i]['bytes'] = payload.stringify_jpg(
                            jpg_bytes)
                        #eng_input[i].pop('bytes')
                    except Exception as e:
                        print(e)

            else:
                try:
                    eng_input, = eng_input
                    retval, jpg_bytes = cv2.imencode('.jpg', inf_output)
                    eng_input['bytes'] = payload.stringify_jpg(jpg_bytes)
                    #eng_input.pop('bytes')
                except Exception as e:
                    print(e)

        return eng_input
Example #2
0
def main():
    # Test Movidius engine
    args = parse_args()
    if args['debug']:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)
    if args['model_package'] != '':
        dlmm = DLModelManager()
        meta = dlmm.get_model_meta(args['model_package'])
        args['model'] = meta['model']
        args['label'] = meta['label']
    logger.debug('model filepath: ' + args['model'])
    logger.debug('label filepath: ' + args['label'])

    comm_config = {
        'subscribe': {},
        'broker': {
            'address': 'localhost',
            'port': 1883
        }
    }
    if args['service_name'] == 'Classification':
        mvng = MovidiusEngine(args['model'], args['label'])
        service_functor = MovidiusClassificationService
    elif args['service_name'] == 'MobileNetSSD':
        mvng = MovidiusMobileNetSSDEngine(args['model'], args['label'])
        service_functor = MovidiusMobileNetSSDService
    else:
        logger.critical('Legal service names are Classification, MobileNetSSD')
    engine_service = service_functor(args['service_name'],
                                     mvng,
                                     comm_config)
    engine_service.run(args)
Example #3
0
 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)
Example #4
0
 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)
Example #5
0
def main():
    args = parse_args()
    if args['debug']:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)

    comm_config = {
        'subscribe': {},
        'broker': {
            'address': args['broker_ip'],
            'port': args['broker_port']
        }
    }
    comm = Communicator(comm_config, debug=True)

    duration = lambda t: (datetime.now() - t).microseconds / 1000

    metadata = json.loads(args.get('meta', '{}'))

    if args['mode'] == 'stream':
        counter = 0
        fail_counter = 0

        # Check input stream source
        if args['stream_src'].isdigit():
            # source is a physically connected camera
            stream_source = int(args['stream_src'])
        else:
            # source is an IP camera
            stream_source = args['stream_src']
        capture = cv2.VideoCapture(stream_source)
        cam_fps = capture.get(cv2.CAP_PROP_FPS)
        if cam_fps > 30 or cam_fps < 1:
            logger.warn('Camera FPS is {} (>30 or <1). Set it to 30.'.format(cam_fps))
            cam_fps = 30
        out_fps = args['fps']
        interval = int(cam_fps / out_fps)

        # warmup
        #t_warmup_start = time.time()
        #t_warmup_now = time.time()
        #warmup_counter = 0
        #while t_warmup_now - t_warmup_start < 1:
        #    capture.read()
        #    warmup_counter += 1
        #    t_warmup_now = time.time()

        logger.debug('===== VideoCapture Information =====')
        if stream_source.isdigit():
            stream_source_uri = '/dev/video{}'.format(stream_source)
        else:
            stream_source_uri = stream_source
        logger.debug('Stream Source: {}'.format(stream_source_uri))
        logger.debug('Camera FPS: {}'.format(cam_fps))
        logger.debug('Output FPS: {}'.format(out_fps))
        logger.debug('Interval: {}'.format(interval))
        logger.debug('Send MQTT Topic: {}'.format(args['topic']))
        #logger.debug('Warmup Counter: {}'.format(warmup_counter))
        logger.debug('====================================')

        while True:
            status, im = capture.read()

            # To verify whether the input source is alive, you should use the
            # return value of capture.read(). It will not work by capturing
            # exception of a capture instance, or by checking the return value
            # of capture.isOpened().
            #
            # Two reasons:
            # 1. If a dead stream is alive again, capture will not notify
            #    that input source is dead.
            #
            # 2. If you check capture.isOpened(), it will keep retruning
            #    True if a stream is dead afterward. So you can not use
            #    the capture return value (capture status) to determine
            #    whether a stream is alive or not.
            if (status is True):
                counter += 1
                if counter == interval:
                    logger.debug('Drop frames: {}'.format(counter-1))
                    counter = 0

                    # Open a window and display the ready-to-send frame.
                    # This is useful for development and debugging.
                    if args['display']:
                        cv2.imshow('Frame', im)
                        cv2.waitKey(1)

                    t = datetime.now()
                    retval, jpg_bytes = cv2.imencode('.jpg', im)
                    mqtt_payload = payload.serialize_jpg(jpg_bytes, args['hash'], metadata)
                    comm.send(args['topic'], mqtt_payload)
                    logger.debug('send: {} ms'.format(duration(t)))
                else:
                    pass
            else:
                fail_counter += 1
                logger.critical('ERROR: Failure #{} happened when reading frame'.format(fail_counter))

                # Re-create capture.
                capture.release()
                logger.critical('Re-create a capture and reconnect to {} after 5s'.format(stream_source))
                time.sleep(5)
                capture = cv2.VideoCapture(stream_source)
    elif args['mode'] == 'file':
        # Prepare MQTT payload
        im = cv2.imread(args['filepath'])
        retval, jpg_bytes = cv2.imencode('.jpg', im)

        t = datetime.now()
        mqtt_payload = payload.serialize_jpg(jpg_bytes, args['hash'], metadata)
        logger.debug('payload: {} ms'.format(duration(t)))
        logger.debug('payload size: {}'.format(len(mqtt_payload)))

        # Client publishes payload
        t = datetime.now()
        comm.send(args['topic'], mqtt_payload)
        logger.debug('mqtt.publish: {} ms'.format(duration(t)))
        logger.debug('publish at {}'.format(datetime.now().isoformat()))
    else:
        logger.error('User assigned unknown mode {}'.format(args['mode']))