class Capture(object): _ask_data = None _captor = None _save_path = None _processor_sleep_time = 0.01 _process_buf = None _sample_rate = 16000 def __init__(self, min_time, max_time, path=None): if path is not None: if not os.path.exists(path): raise FileNotFoundError('"{}" doesn\'t exist'.format(path)) if not os.path.isdir(path): raise FileNotFoundError('"{}" isn\'t a directory'.format(path)) monitorhook = open('monitorhook.log', 'w') monitorhook.write('{:.0f}'.format(time.time())) monitorhook.close() self._save_path = path self._ask_data = threading.Event() self._captor = Captor(min_time, max_time, self._ask_data, self._process) def start(self): self._captor.start() self._process_loop() def _process(self, data): self._process_buf = np.frombuffer(data, dtype=np.int16) def _process_loop(self): with WavProcessor() as proc: self._ask_data.set() while True: if self._process_buf is None: # Waiting for data to process time.sleep(self._processor_sleep_time) continue self._ask_data.clear() if self._save_path: f_path = os.path.join( self._save_path, 'record_{:.0f}.wav'.format(time.time())) wavfile.write(f_path, self._sample_rate, self._process_buf) logger.info('"{}" saved.'.format(f_path)) logger.info('Start processing.') monitorhook = open('monitorhook.log', 'w') monitorhook.write('{:.0f}'.format(time.time())) monitorhook.close() predictions = proc.get_predictions(self._sample_rate, self._process_buf) logger.info('Predictions: {}'.format( format_predictions(predictions))) logger.info('Stop processing.') self._process_buf = None self._ask_data.set()
def __init__(self, min_time, max_time, path=None): if path is not None: if not os.path.exists(path): raise FileNotFoundError('"{}" doesn\'t exist'.format(path)) if not os.path.isdir(path): raise FileNotFoundError('"{}" isn\'t a directory'.format(path)) self._save_path = path self._ask_data = threading.Event() self._captor = Captor(min_time, max_time, self._ask_data, self._process)
class Capture(object): _ask_data = None _captor = None _save_path = None _processor_sleep_time = 0.01 _process_buf = None _sample_rate = 16000 def __init__(self, min_time, max_time, path=None): if path is not None: if not os.path.exists(path): raise FileNotFoundError('"{}" doesn\'t exist'.format(path)) if not os.path.isdir(path): raise FileNotFoundError('"{}" isn\'t a directory'.format(path)) self._save_path = path self._ask_data = threading.Event() self._captor = Captor(min_time, max_time, self._ask_data, self._process) def start(self): self._captor.start() self._process_loop() def _process(self, data): self._process_buf = np.frombuffer(data, dtype=np.int16) def _process_loop(self): with WavProcessor() as proc: self._ask_data.set() while True: if self._process_buf is None: # Waiting for data to process time.sleep(self._processor_sleep_time) continue self._ask_data.clear() if self._save_path: f_path = os.path.join( self._save_path, 'record_{:.0f}.wav'.format(time.time()) ) wavfile.write(f_path, self._sample_rate, self._process_buf) logger.info('"{}" saved.'.format(f_path)) logger.info('Start processing.') predictions = proc.get_predictions( self._sample_rate, self._process_buf) logger.info( 'Predictions: {}'.format(format_predictions(predictions)) ) logger.info('Stop processing.') self._process_buf = None self._ask_data.set()
def __init__(self, min_time, max_time, path=None): if path is not None: if not os.path.exists(path): raise FileNotFoundError('"{}" doesn\'t exist'.format(path)) if not os.path.isdir(path): raise FileNotFoundError('"{}" isn\'t a directory'.format(path)) monitorhook = open('monitorhook.log', 'w') monitorhook.write('{:.0f}'.format(time.time())) monitorhook.close() self._save_path = path self._ask_data = threading.Event() self._captor = Captor(min_time, max_time, self._ask_data, self._process)
def __init__(self, *args, **kwargs): min_time = kwargs.pop('min_capture_time', 5) max_time = kwargs.pop('max_capture_time', 5) self._save_path = kwargs.pop('save_path', None) super(Daemon, self).__init__(*args, **kwargs) self.events_queue = deque(maxlen=10) self._ask_data_event = threading.Event() self._shutdown_event = threading.Event() self._process_thread = threading.Thread(target=self._process_loop, name='processor') self._process_thread.setDaemon(True) self._captor = Captor(min_time, max_time, self._ask_data_event, self._process, self._shutdown_event)
def __init__(self, *args, **kwargs): kwargs.setdefault('routes', routes) min_time = kwargs.pop('min_capture_time', 5) max_time = kwargs.pop('max_capture_time', 5) self._save_path = kwargs.pop('max_capture_time', None) super(Daemon, self).__init__(*args, **kwargs) self.events_queue = deque(maxlen=10) self.dh_cfg = Config('config.json', update_callback=self._restart_dh) self.dh_status = DHStatus() self._web_thread = threading.Thread(target=self._web_loop, daemon=True, name='web') self._ask_data_event = threading.Event() self._process_thread = threading.Thread(target=self._process_loop, daemon=True, name='processor') self._captor = Captor(min_time, max_time, self._ask_data_event, self._process)
def __init__(self, *args, **kwargs): kwargs.setdefault('routes', routes) min_time = kwargs.pop('min_capture_time', 5) max_time = kwargs.pop('max_capture_time', 5) self._save_path = kwargs.pop('max_capture_time', None) super(Daemon, self).__init__(*args, **kwargs) self.events_queue = deque(maxlen=10) self.dh_cfg = Config('config.json', update_callback=self._restart_dh) self.dh_status = DHStatus() self._web_thread = threading.Thread( target=self._web_loop, daemon=True, name='web') self._ask_data_event = threading.Event() self._process_thread = threading.Thread( target=self._process_loop, daemon=True, name='processor') self._captor = Captor( min_time, max_time, self._ask_data_event, self._process)
class Daemon(Server): _process_thread = None _process_buf = None _ask_data_event = None _shutdown_event = None _captor = None _sample_rate = 16000 _processor_sleep_time = 0.01 events_queue = None def __init__(self, *args, **kwargs): min_time = kwargs.pop('min_capture_time', 5) max_time = kwargs.pop('max_capture_time', 5) self._save_path = kwargs.pop('save_path', None) super(Daemon, self).__init__(*args, **kwargs) self.events_queue = deque(maxlen=10) self._ask_data_event = threading.Event() self._shutdown_event = threading.Event() self._process_thread = threading.Thread(target=self._process_loop, name='processor') self._process_thread.setDaemon(True) self._captor = Captor(min_time, max_time, self._ask_data_event, self._process, self._shutdown_event) def _start_capture(self): logger.info('Start captor') self._captor.start() def _start_process(self): logger.info('Start processor loop') self._process_thread.start() def _process(self, data): self._process_buf = np.frombuffer(data, dtype=np.int16) def _on_startup(self): self._start_process() self._start_capture() def _on_shutdown(self): self._shutdown_event.set() def _process_loop(self): with WavProcessor() as proc: self._ask_data_event.set() while self.is_running: if self._process_buf is None: # Waiting for data to process time.sleep(self._processor_sleep_time) continue self._ask_data_event.clear() if self._save_path: f_path = os.path.join( self._save_path, 'record_{:.0f}.wav'.format(time.time()) ) wavfile.write(f_path, self._sample_rate, self._process_buf) logger.info('"{}" saved'.format(f_path)) logger.info('Start processing') predictions = proc.get_predictions( self._sample_rate, self._process_buf) formatted = format_predictions(predictions) logger.info('Predictions: {}'.format(formatted)) self.events_queue.append((datetime.datetime.now(), formatted)) self._send_dh(predictions) logger.info('Stop processing') self._process_buf = None self._ask_data_event.set() def _send_dh(self, data): if not self.dh_status.connected: logger.error('Devicehive is not connected') return self.deviceHive.handler.send(data)
class Capture(object): _ask_data = None _captor = None _save_path = None _processor_sleep_time = 0.01 _process_buf = None _sample_rate = 16000 def __init__(self, min_time, max_time, path=None): if path is not None: if not os.path.exists(path): raise FileNotFoundError('"{}" doesn\'t exist'.format(path)) if not os.path.isdir(path): raise FileNotFoundError('"{}" isn\'t a directory'.format(path)) self._save_path = path self._ask_data = threading.Event() self._captor = Captor(min_time, max_time, self._ask_data, self._process) def start(self): self._captor.start() self._process_loop() def _process(self, data): self._process_buf = np.frombuffer(data, dtype=np.int16) def _process_loop(self): with WavProcessor() as proc: self._ask_data.set() while True: i = 0 pred_values = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] while i < 50: if self._process_buf is None: # Waiting for data to process time.sleep(self._processor_sleep_time) continue self._ask_data.clear() if self._save_path: f_path = os.path.join( self._save_path, 'record_{:.0f}.wav'.format(time.time())) wavfile.write(f_path, self._sample_rate, self._process_buf) logger.info('"{}" saved.'.format(f_path)) logger.info(str(i) + ' Start processing.') predictions = proc.get_predictions(self._sample_rate, self._process_buf) for p in predictions: pred_values = [ x + y for x, y in zip(pred_values, get_labels(p[0])) ] # print(get_labels(x[0])) # logger.info( # 'Predictions: {}'.format(format_predictions(predictions)) # ) logger.info('Stop processing.') self._process_buf = None self._ask_data.set() i += 1 #arr_index = np.argmax(np.array(pred_values)) arr_index = np.argwhere(pred_values == np.amax(pred_values)) arr_index = arr_index.flatten().tolist() #print(pred_values[int(arr_index)]) #print(arr_index) # if(isinstance(arr_index,np.int64)): # print(labels[int(arr_index)]) # else: for x in arr_index: print(labels[int(x)]) print(pred_values)
class Daemon(Server): _dh_thread = None _process_thread = None _web_thread = None _process_buf = None _ask_data_event = None _captor = None _sample_rate = 16000 _processor_sleep_time = 0.01 dh_cfg = None deviceHive = None dh_status = None events_queue = None base_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'web') def __init__(self, *args, **kwargs): kwargs.setdefault('routes', routes) min_time = kwargs.pop('min_capture_time', 5) max_time = kwargs.pop('max_capture_time', 5) self._save_path = kwargs.pop('max_capture_time', None) super(Daemon, self).__init__(*args, **kwargs) self.events_queue = deque(maxlen=10) self.dh_cfg = Config('config.json', update_callback=self._restart_dh) self.dh_status = DHStatus() self._web_thread = threading.Thread( target=self._web_loop, daemon=True, name='web') self._ask_data_event = threading.Event() self._process_thread = threading.Thread( target=self._process_loop, daemon=True, name='processor') self._captor = Captor( min_time, max_time, self._ask_data_event, self._process) def send_dh(self, data): if not self._is_dh_connected(): logger.warning('Devicehive is not connected') return self.deviceHive.handler.send(data) def start(self): self._start_web() self.dh_cfg.load() # this will start DH thread automatically self._start_capture() self._start_process() def _start_web(self): logger.info('Start server http://{}:{}'.format(*self.server_address)) self._web_thread.start() def _start_dh(self): if self._is_dh_connected(): logging.info('Devicehive already started') return logger.info('Start devicehive') self._dh_thread = threading.Thread( target=self._dh_loop, daemon=True, name='device_hive') self._dh_thread.start() def _stop_dh(self): if not self._is_dh_connected(): logging.info('Devicehive already stopped') return # TODO: Now it's only one proper way to call disconnect, we need # implement better way to do it self.deviceHive.transport.handler.handler.api.disconnect() def _restart_dh(self): if self._is_dh_connected(): self._stop_dh() self._start_dh() def _is_dh_connected(self): return self.dh_status.connected def _start_capture(self): logger.info('Start captor') self._captor.start() def _start_process(self): logger.info('Start processor loop') self._process_thread.start() def _web_loop(self): self.serve_forever() def _dh_loop(self): self.dh_status.set_connecting() self.deviceHive = DeviceHive( DeviceHiveHandler, self.dh_cfg.data['deviceid']) error = '' try: self.dh_status.set_connected() url = self.dh_cfg.data['url'] refresh_token = self.dh_cfg.data['token'] self.deviceHive.connect(url, refresh_token=refresh_token) except TransportError as e: logger.exception(e) error = str(e) finally: self.dh_status.set_disconnected(error) logger.info('Stop devicehive') def _process(self, data): self._process_buf = np.frombuffer(data, dtype=np.int16) def _process_loop(self): with WavProcessor() as proc: self._ask_data_event.set() while True: if self._process_buf is None: # Waiting for data to process time.sleep(self._processor_sleep_time) continue self._ask_data_event.clear() if self._save_path: f_path = os.path.join( self._save_path, 'record_{:.0f}.wav'.format(time.time()) ) wavfile.write(f_path, self._sample_rate, self._process_buf) logger.info('"{}" saved'.format(f_path)) logger.info('Start processing') predictions = proc.get_predictions( self._sample_rate, self._process_buf) formatted = format_predictions(predictions) logger.info( 'Predictions: {}'.format(formatted)) self.events_queue.append((datetime.datetime.now(), formatted)) self.send_dh(predictions) logger.info('Stop processing') self._process_buf = None self._ask_data_event.set()
class Capture(object): _ask_data = None _captor = None _save_path = None _processor_sleep_time = 0.01 _process_buf = None _sample_rate = 16000 def __init__(self, min_time, max_time, path=None): if path is not None: if not os.path.exists(path): raise FileNotFoundError('"{}" doesn\'t exist'.format(path)) if not os.path.isdir(path): raise FileNotFoundError('"{}" isn\'t a directory'.format(path)) self._save_path = path self._ask_data = threading.Event() self._captor = Captor(min_time, max_time, self._ask_data, self._process) def start(self): self._captor.start() self._process_loop() def _process(self, data): self._process_buf = np.frombuffer(data, dtype=np.int16) def _process_loop(self): with WavProcessor() as proc: self._ask_data.set() try: producer = KafkaProducer( bootstrap_servers=config['KAFKA']['bootstrap_servers']) except NoBrokersAvailable: producer = None print("No brokers available, running off-line") while True: if self._process_buf is None: # Waiting for data to process time.sleep(self._processor_sleep_time) continue self._ask_data.clear() if self._save_path: f_path = os.path.join( self._save_path, 'record_{:.0f}.wav'.format(time.time())) wavfile.write(f_path, self._sample_rate, self._process_buf) predictions = proc.get_predictions(self._sample_rate, self._process_buf) for prediction in predictions: if prediction[1] > 0.4: if producer is not None: producer.send(config['KAFKA']['topic'], bytes(str(prediction[0]).encode())) print(prediction[0]) self._process_buf = None self._ask_data.set()
class Daemon(Server): _dh_thread = None _process_thread = None _web_thread = None _process_buf = None _ask_data_event = None _captor = None _sample_rate = 16000 _processor_sleep_time = 0.01 dh_cfg = None deviceHive = None dh_status = None events_queue = None base_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'web') def __init__(self, *args, **kwargs): kwargs.setdefault('routes', routes) min_time = kwargs.pop('min_capture_time', 5) max_time = kwargs.pop('max_capture_time', 5) self._save_path = kwargs.pop('max_capture_time', None) super(Daemon, self).__init__(*args, **kwargs) self.events_queue = deque(maxlen=10) self.dh_cfg = Config('config.json', update_callback=self._restart_dh) self.dh_status = DHStatus() self._web_thread = threading.Thread(target=self._web_loop, daemon=True, name='web') self._ask_data_event = threading.Event() self._process_thread = threading.Thread(target=self._process_loop, daemon=True, name='processor') self._captor = Captor(min_time, max_time, self._ask_data_event, self._process) def send_dh(self, data): if not self._is_dh_connected(): logger.warning('Devicehive is not connected') return self.deviceHive.handler.send(data) def start(self): self._start_web() self.dh_cfg.load() # this will start DH thread automatically self._start_capture() self._start_process() def _start_web(self): logger.info('Start server http://{}:{}'.format(*self.server_address)) self._web_thread.start() def _start_dh(self): if self._is_dh_connected(): logging.info('Devicehive already started') return logger.info('Start devicehive') self._dh_thread = threading.Thread(target=self._dh_loop, daemon=True, name='device_hive') self._dh_thread.start() def _stop_dh(self): if not self._is_dh_connected(): logging.info('Devicehive already stopped') return # TODO: Now it's only one proper way to call disconnect, we need # implement better way to do it self.deviceHive.transport.handler.handler.api.disconnect() def _restart_dh(self): if self._is_dh_connected(): self._stop_dh() self._start_dh() def _is_dh_connected(self): return self.dh_status.connected def _start_capture(self): logger.info('Start captor') self._captor.start() def _start_process(self): logger.info('Start processor loop') self._process_thread.start() def _web_loop(self): self.serve_forever() def _dh_loop(self): self.dh_status.set_connecting() self.deviceHive = DeviceHive(DeviceHiveHandler, self.dh_cfg.data['deviceid']) error = '' try: self.dh_status.set_connected() url = self.dh_cfg.data['url'] refresh_token = self.dh_cfg.data['token'] self.deviceHive.connect(url, refresh_token=refresh_token) except TransportError as e: logger.exception(e) error = str(e) finally: self.dh_status.set_disconnected(error) logger.info('Stop devicehive') def _process(self, data): self._process_buf = np.frombuffer(data, dtype=np.int16) def _process_loop(self): with WavProcessor() as proc: self._ask_data_event.set() while True: if self._process_buf is None: # Waiting for data to process time.sleep(self._processor_sleep_time) continue self._ask_data_event.clear() if self._save_path: f_path = os.path.join( self._save_path, 'record_{:.0f}.wav'.format(time.time())) wavfile.write(f_path, self._sample_rate, self._process_buf) logger.info('"{}" saved'.format(f_path)) logger.info('Start processing') predictions = proc.get_predictions(self._sample_rate, self._process_buf) formatted = format_predictions(predictions) logger.info('Predictions: {}'.format(formatted)) self.events_queue.append((datetime.datetime.now(), formatted)) self.send_dh(predictions) logger.info('Stop processing') self._process_buf = None self._ask_data_event.set()
class Capture(object): _ask_data = None _captor = None _save_path = None _processor_sleep_time = 0.01 _process_buf = None _sample_rate = 16000 def __init__(self, min_time, max_time, path=None): if path is not None: if not os.path.exists(path): raise FileNotFoundError('"{}" doesn\'t exist'.format(path)) if not os.path.isdir(path): raise FileNotFoundError('"{}" isn\'t a directory'.format(path)) self._save_path = path self._ask_data = threading.Event() self._captor = Captor(min_time, max_time, self._ask_data, self._process) def start(self): self._captor.start() self._process_loop() def _process(self, data): self._process_buf = np.frombuffer(data, dtype=np.int16) def _process_loop(self): with WavProcessor() as proc: self._ask_data.set() while True: if self._process_buf is None: # Waiting for data to process time.sleep(self._processor_sleep_time) continue self._ask_data.clear() if self._save_path: f_path = os.path.join( self._save_path, 'record_{:.0f}.wav'.format(time.time())) wavfile.write(f_path, self._sample_rate, self._process_buf) logger.info('"{}" saved.'.format(f_path)) logger.info('Start processing.') predictions = proc.get_predictions(self._sample_rate, self._process_buf) logger.info('Predictions: {}'.format( format_predictions(predictions))) my_dict = {x[0]: x[1] for x in predictions} msg_json = { "app_id": "openears", "dev_id": "TEST001", "payload_fields": my_dict, "time": int(time.time() * 1e3) } msg_str = json.dumps(msg_json) auth = {"username": mqtt_user, "password": mqtt_password} publish.single("pipeline/openears/TEST001", payload=msg_str, hostname=mqtt_host, port=mqtt_port, auth=auth) print('message sent') logger.info('Stop processing.') self._process_buf = None self._ask_data.set()
class Capture(object): _ask_data = None _captor = None _save_path = None _processor_sleep_time = 0.01 _process_buf = None _sample_rate = 16000 def __init__(self, min_time, max_time, path=None): if path is not None: if not os.path.exists(path): raise FileNotFoundError('"{}" doesn\'t exist'.format(path)) if not os.path.isdir(path): raise FileNotFoundError('"{}" isn\'t a directory'.format(path)) self._save_path = path self._ask_data = threading.Event() self._captor = Captor(min_time, max_time, self._ask_data, self._process) def start(self): self._captor.start() self._process_loop() def _process(self, data): self._process_buf = np.frombuffer(data, dtype=np.int16) def _process_loop(self): with WavProcessor() as proc: self._ask_data.set() while True: if self._process_buf is None: # Waiting for data to process time.sleep(self._processor_sleep_time) continue self._ask_data.clear() if self._save_path: f_path = os.path.join( self._save_path, 'record_{:.0f}.wav'.format(time.time())) wavfile.write(f_path, self._sample_rate, self._process_buf) logger.info('"{}" saved.'.format(f_path)) logger.info('Start processing.') predictions = proc.get_predictions(self._sample_rate, self._process_buf) for i in predictions: if i[0] == "Vehicle" and (float(i[1]) > .2): print("OBS Release") GPIO.output(pin, GPIO.HIGH) else: GPIO.output(pin, GPIO.LOW) print(i[0]) print(i[1]) #print("Type: ") #print(type(predictions)) #logger.info( #'Predictions: {}'.format(format_predictions(predictions)) #) logger.info('Stop processing.') self._process_buf = None self._ask_data.set()