def realtime_detect(af, af_url, rf): if not af: # no local file, download from url # y, sr = sf.read(io.BytesIO(urlopen(af_url).read())) af = 'audio.mp3' with open(af, 'wb') as audio: audio.write(urlopen(af_url).read()) y, sr = librosa.load(af) start = 0 step = int(sr * 1) feat = [] pos = 0 total = 0 feat = [feature_extraction.feature(y, sr)] result = detect(rf, feat) return result
def realtime_detect(af, af_url, rf): if not af: # no local file, download from url # y, sr = sf.read(io.BytesIO(urlopen(af_url).read())) af = '/opt/audio_process/audio.mp3' audio_f = urlopen(af_url, timeout=5) if not audio_f: return "url request timeout" with open(af, 'wb') as audio: audio.write(audio_f.read()) y, sr = librosa.load(af) raw_feat = feature_extraction.feature(y, sr=sr) feat = raw_feat feat = feat.reshape(1, -1) result = detect(rf, feat) return result
def realtime_detect(af, af_url, model): if not af: # no local file, download from url # y, sr = sf.read(io.BytesIO(urlopen(af_url).read())) af = 'audio.mp3' with open(af, 'wb') as audio: audio.write(urlopen(af_url).read()) try: y, sr = librosa.load(af) except Exception as e: return e raw_feat = feature_extraction.feature(y, sr) feat = np.r_[raw_feat[1], raw_feat[3:6], raw_feat[12:14], raw_feat[16:18], raw_feat[24:26], raw_feat[27:30]] # reshape array for single sample feat = feat.reshape(1, -1) result = detect(model, feat) return result
def calc_feature(date_str): audio_list = glob(AUDIO_PATH + date_str + '*.mp3') feature_file = os.path.join(os.path.dirname(feature_extraction.__file__), 'feature_data/neg_' + date_str + '.log') print "saving features to {} ...".format(feature_file) feat = {} if len(audio_list) > 1: # sample the audio_list if len(audio_list) > 1500: audio_list = random.sample(audio_list, 1500) with open(feature_file, 'w') as f_file: for f in audio_list: print("processing: {}".format(f)) try: y, sr = librosa.load(f) feat['feature'] = list(feature_extraction.feature(y, sr=sr)) except Exception as e: print(e) continue feat_json = json.dumps(feat) f_file.write(feat_json+'\n')
import os import librosa import feature_extraction import svm from svmutil import * features = [] labels = [] for file_name in os.listdir('test/'): y, sr = librosa.load('test/'+file_name) feat = feature_extraction.feature(y, sr=sr) feat = feat.tolist() features.append(feat) labels.append(-1) model = svm_load_model('audio.model') pre, acc, val = svm_predict(labels, features, model) print('accuracy: ', acc) print(pre)
arg 1: audio folder arg 2: feature filename ''' if __name__ == "__main__": ff = [] try: if os.path.isdir(sys.argv[1]): # ff = [os.path.join(sys.argv[1], f) for f in os.listdir(sys.argv[1]) if f.split('.')[1]=='mp3'] ff = [os.path.join(sys.argv[1], f) for f in os.listdir(sys.argv[1])] feature_file = 'feature_data/' + sys.argv[2] except Exception as e: print(e) feat = {} # sample the audio-list if len(ff) > 1500: ff = random.sample(ff, 1500) with open(feature_file, 'w') as f_file: for f in ff: print('processing: {}'.format(f)) y, sr = librosa.load(f) try: feat['feature'] = list(feature_extraction.feature(y, sr=sr)) except Exception as e: print(e) continue feat_json = json.dumps(feat) f_file.write(feat_json+'\n') print('saved feature array to ' + feature_file)