def process_file(wav_file, class_labels, to_csv, num_files, output_file, ten_seconds, num_predictions, threshold): print(wav_file) files = tf.io.gfile.glob(wav_file) total_predictions = [] examples = [] print('Number of predictions ' + str(num_predictions)) print('Threshold ' + str(threshold)) if not files: raise IOError("Unable to find input files. data_pattern='" + wav_file + "'") if num_files == 0: num_files = len(files) for i in range(0, num_files): if not files[i]: i = i + 1 sr, data = wavfile.read(files[i]) if data.dtype != np.int16: raise TypeError('Bad sample type: %r' % data.dtype) print(i) print(files[i]) # local import to reduce start-up time from audio.processor import WavProcessor, format_predictions with WavProcessor() as proc: if ten_seconds == False: predictions = proc.get_predictions(wav_file, sr, data, num_predictions, threshold, class_labels) print('Predictions') print(format_predictions(predictions)) if to_csv == True: examples.append(int(len(data) / 44100)) total_predictions.append(predictions) else: predictions = proc.get_predictions2(sr, data, num_predictions, threshold, class_labels) print('Predictions') for i in range(0, len(predictions)): print(str(i) + ' ' + format_predictions(predictions[i])) if to_csv == True: examples.append(int(len(data) / 44100)) total_predictions.append(predictions) if to_csv == True: from audio.processor import WavProcessor, format_predictions with WavProcessor() as proc: if ten_seconds == False: proc.toCSV(examples, wav_file, num_files, output_file, total_predictions) else: proc.toCSV2(examples, wav_file, num_files, output_file, total_predictions)
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 _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 _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()
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)
def process_file(wav_file): sr, data = wavfile.read(wav_file) if data.dtype != np.int16: raise TypeError('Bad sample type: %r' % data.dtype) # local import to reduce start-up time from audio.processor import WavProcessor, format_predictions with WavProcessor() as proc: predictions = proc.get_predictions(sr, data) print(format_predictions(predictions))
def gain_features(wav_root='Audio_Dataset', save_path='./EmotiW_youtubeAudio.npz'): # save path data_path = [] log_spectrograms_save = [] embeddings_save = [] quantized_embeddings_save = [] proc = WavProcessor() for wav_path in tqdm.tqdm(glob.glob(wav_root + '/*')): wav_name = os.path.basename(wav_path).split('.')[0] sr, data = wavfile.read( wav_path ) # sr: sample rate; data: original sample data; data.type uint16 if data.dtype != np.int16: raise TypeError('Bad sample type: %r' % data.dtype) if len(data) < sr: continue # less than 1s examples_batch, features, quantized_embeddings, predictions = proc.get_predictions( sr, data) # examples_batch: [3, 96, 60] # features: [3, 128] # quantized_embeddings: [3, 128] frame_num = np.shape(examples_batch)[0] data_path.append({'pic_path': wav_name}) log_spectrograms_save.append(examples_batch.reshape(frame_num, -1)) embeddings_save.append(features) quantized_embeddings_save.append(quantized_embeddings) np.savez_compressed( save_path, data_path=data_path, logspec=log_spectrograms_save, emb=embeddings_save, qemb=quantized_embeddings_save, )
def process_dir(start_dir, out_file): f = open(out_file, 'w') results = [] with WavProcessor() as proc: for dirpath, dirs, files in os.walk(start_dir): for filename in [f for f in files if f.lower().endswith('.wav')]: fname = os.path.join(dirpath, filename) result = process_file(fname, proc) results.append((fname, tuple(result))) print('%s %s' % (fname, result)) print(json.dumps(results), file=f) f.close()
def process_file(wav_file, ckpt_file, labels_file): sr, data = wavfile.read(wav_file) print(wav_file, end=' ') if data.dtype != np.int16: raise TypeError('Bad sample type: %r' % data.dtype) predictions = [] with WavProcessor(ckpt_file, labels_file) as proc: try: predictions = proc.get_predictions(sr, data) print(format_predictions(predictions)) except: print('CORRUPTED!') return predictions
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()
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()
from pydub import AudioSegment from audio.processor import WavProcessor, format_predictions df = pd.read_csv('labels.csv') df = df.set_index('index') # print(df) sig = AudioSegment.from_file('kitchen.wav', format="wav") _start = 0 _end = 6 for x in range(0, 100): start = int(_start * 1000) end = int(_end * 1000) segment = sig[start:end] samples = np.array(segment.get_array_of_samples()) new_sig = samples.astype(np.float32) with WavProcessor() as proc: predictions = proc.get_predictions(16000, new_sig) for p in predictions: print(str(_start / 60), ":", str(_end / 60), " - ", df.loc[p[0]].values[1], p[1]) _start += 6 _end += 6
import math import operator import json from collections import Counter import numpy as np import pandas as pd from scipy.io import wavfile from pydub import AudioSegment from audio.processor import WavProcessor, format_predictions from log_config import LOGGING proc = WavProcessor() def get_labels(csv_file='labels.csv'): df = pd.read_csv(csv_file) df = df.set_index('index') return df def get_label_val(idx): return [0.0 if val != 1 else 1.0 for val in df.loc[idx].values] def label_sig(sig, df=get_labels()): global proc