def data2sepc(self, arr): st = time.time() ch = self.dataset.shape[2] spec = spectrogram(arr, ch, self.len_spec, spec_col) spec_batch = np.zeros((self.len_spec, ch, self.step, spec_col), dtype=np.float32) for i in range(self.len_spec - self.step): spec_og = spec[i:i + self.step] # bshape_og = spec_og.shape # spec_og = (spec_og.reshape((self.step, -1)).transpose((1,0))*self.gradation_arr).transpose((1,0)).reshape(shape_og) spec_batch[i] = spec_og.transpose((2, 0, 1)) return spec_batch
def generate_spectrogram(stream: Stream): specs = [] trs = [] for tr in stream.copy(): # check if tr contains NaN if np.any(np.isnan(tr.data)): continue trs.append(tr.copy()) st2 = Stream(traces=trs) st2 = st2.trim(endtime=tr.stats.starttime + 2, pad=True, fill_value=0) st2 = st2.detrend('demean').detrend('linear') st2 = st2.taper(max_percentage=1, max_length=1e-2) for tr in st2: spec = spectrogram(tr) spec /= np.max(spec) specs.append(spec) return specs
def create_training_image(waveform_file): # logger.info('here') waveform_file_path = input_data_dir / waveform_file cat = read_events(waveform_file_path.with_suffix('.xml')) st = read(waveform_file_path) if cat[0].preferred_origin() is not None: origin = cat[0].preferred_origin() else: origin = cat[0].origins[-1] if origin.evaluation_mode == 'automatic': return event_time = origin.time.timestamp try: st = st.detrend('demean').detrend( 'linear').taper(max_percentage=0.1, max_length=0.01).resample( sampling_rate=sampling_rate) except Exception as e: logger.error(e) trs = [] for tr in st: if np.nan in tr.data: continue trs.append(tr.copy()) st.traces = trs trs = [] logger.info(event_type_lookup[cat[0].event_type]) if event_type_lookup[cat[0].event_type] == 'seismic event': for arrival in cat[0].preferred_origin().arrivals: site = arrival.pick.site for tr in st.select(site=site).copy(): # filtering out low frequency before calculating the SNR tr = tr.filter('highpass', freq=100) snr = calculate_snr(tr, arrival.pick.time, pre_wl=20e-3, post_wl=20e-3) if arrival.pick.evaluation_mode == 'automatic': if snr < snr_threshold: continue tr.trim(starttime=tr.stats.starttime, endtime=tr.stats.starttime + sequence_length_second, pad=True, fill_value=0) trs.append(tr.copy().resample(sampling_rate= int(sampling_rate))) elif event_type_lookup[cat[0].event_type] == 'blast': for tr in st: if (np.max(np.abs(tr.data)) / np.std(tr.data)) < 6: continue trs.append(tr.trim(endtime=tr.stats.starttime + sequence_length_second)) else: for tr in st: trs.append(tr.trim(endtime=tr.stats.starttime + sequence_length_second)) ct = 0 for tr in trs: try: spec = spectrogram(tr) except Exception as e: logger.error(e) ct += 1 logger.info(f'{ct} exceptions') return spec = (spec / np.max(spec)) * 255 img = Image.fromarray(np.array(spec.tolist()).astype(np.uint8)) filename = f'{event_time:0.0f}_{tr.stats.site}_' \ f'{tr.stats.channel}.jpg' out_dir = (output_data_dir / event_type_lookup[cat[0].event_type]) out_dir.mkdir(parents=True, exist_ok=True) out_file_name = out_dir / filename ImageOps.grayscale(img).save(out_file_name, format='JPEG')
def run(self): """Thread main method. """ global exitFlag print("Starting Processor2") counter = 0 ts_counter = 0 #ts_factor=0 framebuf = b'' nfft = 256 # must match the value used in the spectrogram function noverlap = int( nfft / 8) # must match the value used in the spectrogram function framelen = 2 * 1 #bytes, must match channels_depth and n_channels in the MicRecorder class predbuf = np.empty((1, 0, 1)) self.model_tr = um.load_model_for_live( self.model_folder_tr, self.ep_tr ) #model_tr must be loaded in the same thread using it. if loaded in __init__(), then throws exception when used. self.model_cmd = um.load_model_for_live(self.model_folder_cmd, self.ep_cmd) #wf = wave.open("output.wav", 'wb') #wf.setnchannels(1) #wf.setsampwidth(2) #wf.setframerate(44100) T_cmd = int(1181 / 6 * 4) #number of timesteps for command detection cmd_counter = 0 detection_type = DetectionType.TRIGGER class_labels = [ "back", "close", "fullscreen", "next", "pause", "play", "prev", "volume-down", "volume-up" ] n_class = len(class_labels) predbuf_cmd = np.empty((1, 0, n_class + 1)) #self.model_tr._make_predict_function() while True: try: chunk = self.buffer.get(timeout=1) except queue.Empty as e: print( "Processor2: Warning - no data from microphone for a whole second" ) if exitFlag is True: break continue try: framebuf = b"".join([framebuf, chunk]) #wf.writeframes(chunk) n_win = int( (len(framebuf) / framelen - nfft) / (nfft - noverlap)) + 1 framearr = np.frombuffer(framebuf[:((n_win - 1) * (nfft - noverlap) + nfft) * framelen], dtype=np.int16) x = np.expand_dims(ds.spectrogram(framearr), axis=0) ts_counter += x.shape[1] #if int(ts_counter/900)>ts_factor: # ts_factor = int(ts_counter/900) # self.model_tr.reset_states() # print("Resetting") #print(x.shape, n_win) #assert x.shape[1]==n_win framebuf = framebuf[framelen * n_win * (nfft - noverlap):] if detection_type == DetectionType.TRIGGER: yp = self.model_tr.predict(x, batch_size=1) predbuf = np.concatenate((predbuf, yp), axis=1) peaks = utt.find_peaks(predbuf[0]) if len(peaks) > 0: threading.Thread(target=play_sound_1).start() print("!!! VLC !!!", counter) counter += 1 self.model_tr.reset_states() predbuf = np.empty((1, 0, 1)) detection_type = DetectionType.COMMAND cmd_counter = 0 else: predbuf = predbuf[:, -30:, :] #should be equal or larger than the window size used in utt.find_peaks(), which is currently 20, so 30 is ok else: yp = self.model_cmd.predict(x, batch_size=1) predbuf_cmd = np.concatenate((predbuf_cmd, yp), axis=1) cmd_counter += yp.shape[1] peak, cla = utt.search_peak_multi(predbuf_cmd[0]) if cla is not None: threading.Thread(target=play_sound_1).start() print("!!!", class_labels[cla], "!!!", counter) self.cmd_buffer.put_nowait(class_labels[cla]) counter += 1 self.model_cmd.reset_states() detection_type = DetectionType.TRIGGER predbuf_cmd = np.empty((1, 0, n_class + 1)) else: predbuf_cmd = predbuf_cmd[:, -30:, :] if cmd_counter > T_cmd: self.model_cmd.reset_states() detection_type = DetectionType.TRIGGER predbuf_cmd = np.empty((1, 0, n_class + 1)) threading.Thread(target=play_sound_2).start() print("Back to trigger mode") except Exception as e: print( "Processor2: Error - there was an exception while processing:", e) exitFlag = True raise e if exitFlag is True: break print("Exiting Processor2")
def run(self): """Thread main method. """ global exitFlag print("Starting Processor") counter = 0 ts_counter = 0 #ts_factor=0 framebuf = b'' nfft = 256 # must match the value used in the spectrogram function noverlap = int( nfft / 8) # must match the value used in the spectrogram function framelen = 2 * 1 #bytes, must match channels_depth and n_channels in the MicRecorder class predbuf = np.empty((1, 0, 1)) self.model = utt.load_model_for_live( self.model_folder, self.ep ) #model must be loaded in the same thread using it. if loaded in __init__(), then throws exception when used. #wf = wave.open("output.wav", 'wb') #wf.setnchannels(1) #wf.setsampwidth(2) #wf.setframerate(44100) while True: try: chunk = self.buffer.get(timeout=1) except queue.Empty as e: print( "Processor: Warning - no data from microphone for a whole second" ) if exitFlag is True: break continue try: framebuf = b"".join([framebuf, chunk]) #wf.writeframes(chunk) n_win = int( (len(framebuf) / framelen - nfft) / (nfft - noverlap)) + 1 framearr = np.frombuffer(framebuf[:((n_win - 1) * (nfft - noverlap) + nfft) * framelen], dtype=np.int16) x = np.expand_dims(ds.spectrogram(framearr), axis=0) ts_counter += x.shape[1] #if int(ts_counter/900)>ts_factor: #ts_factor = int(ts_counter/900) #self.model.reset_states() #print("Resetting") #print(x.shape, n_win) #assert x.shape[1]==n_win framebuf = framebuf[framelen * n_win * (nfft - noverlap):] yp = self.model.predict(x, batch_size=1) predbuf = np.concatenate((predbuf, yp), axis=1) peaks = utt.find_peaks(predbuf[0]) if len(peaks) > 0: print("!!! VLC !!!", counter) counter += 1 new_start = min(peaks[-1] + 75, predbuf.shape[1]) new_start = max(new_start, predbuf.shape[1] - 30) predbuf = predbuf[:, new_start:, :] self.model.reset_states() else: predbuf = predbuf[:, -30:, :] #should be equal or larger than the window size used in utt.find_peaks(), which is currently 20, so 30 is ok except Exception as e: print( "Processor: Error - there was an exception while processing:", e) exitFlag = True #raise e if exitFlag is True: break print("Exiting Processor")