def handleConfig(self, pl): payload_json = "" try: payload_json = payload.deserialize_payload(pl.decode('utf-8')) self.comm.send(self.comm_config['publish'], payload.serialize_payload(payload_json)) except Exception as e: logger.info(e) # output config file with open(self.comm_config['configfile'], 'w') as configfile: configfile.write(payload.serialize_payload(payload_json)) configfile.close() # restart service subprocess.run(["supervisorctl", "restart", "bnpipeline-bndyda"])
def handleConfig(self, pl): payload_json = "" try: id = pl.decode('utf-8') if (id in self.idlist): configFilename = self.idlist[id] f = open(configFilename) payload_json = payload.deserialize_payload(f.read()) self.sendConfig(payload.serialize_payload(payload_json)) else: logger.warning("ID %s is not in idlist" % (id)) return except Exception as e: logger.info(e) # output config file with open(self.comm_config['configfile'], 'w') as configfile: configfile.write(payload.serialize_payload(payload_json)) configfile.close() # restart service subprocess.run(["supervisorctl", "restart", "bnpipeline-bndyda"])
def result_hook(self, generalized_result): logger.debug('result_hook, annotations: {}'.format( generalized_result['annotations'])) self.comm.send('berrynet/engine/tensorflow/result', payload.serialize_payload(generalized_result))
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 if args['mode'] == 'stream': counter = 0 # Check input stream source if args['stream_src'].isdigit(): # source is a physically connected camera stream_source = '/dev/video{}'.format(int(args['stream_src'])) capture = cv2.VideoCapture(int(args['stream_src'])) else: # source is an IP camera stream_source = args['stream_src'] capture = cv2.VideoCapture(args['stream_src']) 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 =====') logger.debug('Stream Source: {}'.format(stream_source)) logger.debug('Camera FPS: {}'.format(cam_fps)) logger.debug('Output FPS: {}'.format(out_fps)) logger.debug('Interval: {}'.format(interval)) #logger.debug('Warmup Counter: {}'.format(warmup_counter)) logger.debug('====================================') while True: status, im = capture.read() if (status is False): logger.warn('ERROR: Failure happened when reading frame') # NOTE: Hard-coding rotation for AIKEA onboard camera. # We will add parameter support in the future. im = tinycv.rotate_ccw_opencv(im) 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() #logger.debug('write frame to /tmp/output.jpg') #cv2.imwrite('/tmp/output.jpg', im) retval, jpg_bytes = cv2.imencode('.jpg', im) obj = {} obj['timestamp'] = datetime.now().isoformat() obj['bytes'] = payload.stringify_jpg(jpg_bytes) obj['meta'] = { 'roi': [{ 'top': 50, #'left': 341, #'bottom': 500, #'right': 682, #'left': 640, #'bottom': 980, #'right': 1280, 'left': 10, 'bottom': 600, 'right': 600, 'overlap_threshold': 0.5 }] } logger.debug('timestamp: {}'.format(obj['timestamp'])) logger.debug('bytes len: {}'.format(len(obj['bytes']))) logger.debug('meta: {}'.format(obj['meta'])) mqtt_payload = payload.serialize_payload([obj]) comm.send('berrynet/data/rgbimage', mqtt_payload) logger.debug('send: {} ms'.format(duration(t))) else: pass elif args['mode'] == 'file': # Prepare MQTT payload im = cv2.imread(args['filepath']) retval, jpg_bytes = cv2.imencode('.jpg', im) t = datetime.now() obj = {} obj['timestamp'] = datetime.now().isoformat() obj['bytes'] = payload.stringify_jpg(jpg_bytes) obj['meta'] = { 'roi': [{ 'top': 50, 'left': 10, 'bottom': 600, 'right': 600, 'overlap_threshold': 0.5 }] } mqtt_payload = payload.serialize_payload([obj]) logger.debug('payload: {} ms'.format(duration(t))) logger.debug('payload size: {}'.format(len(mqtt_payload))) # Client publishes payload t = datetime.now() comm.send('berrynet/data/rgbimage', 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']))