def speaker_diarization(filename, n_speakers, mid_window=2.0, mid_step=0.2, short_window=0.05, lda_dim=35, plot_res=False): """ ARGUMENTS: - filename: the name of the WAV file to be analyzed - n_speakers the number of speakers (clusters) in the recording (<=0 for unknown) - mid_window (opt) mid-term window size - mid_step (opt) mid-term window step - short_window (opt) short-term window size - lda_dim (opt LDA dimension (0 for no LDA) - plot_res (opt) 0 for not plotting the results 1 for plotting """ sampling_rate, signal = audioBasicIO.read_audio_file(filename) signal = audioBasicIO.stereo_to_mono(signal) duration = len(signal) / sampling_rate base_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data/models") classifier_all, mean_all, std_all, class_names_all, _, _, _, _, _ = \ at.load_model_knn(os.path.join(base_dir, "knn_speaker_10")) classifier_fm, mean_fm, std_fm, class_names_fm, _, _, _, _, _ = \ at.load_model_knn(os.path.join(base_dir, "knn_speaker_male_female")) mid_feats, st_feats, _ = \ mtf.mid_feature_extraction(signal, sampling_rate, mid_window * sampling_rate, mid_step * sampling_rate, round(sampling_rate * short_window), round(sampling_rate * short_window * 0.5)) mid_term_features = np.zeros( (mid_feats.shape[0] + len(class_names_all) + len(class_names_fm), mid_feats.shape[1])) for index in range(mid_feats.shape[1]): feature_norm_all = (mid_feats[:, index] - mean_all) / std_all feature_norm_fm = (mid_feats[:, index] - mean_fm) / std_fm _, p1 = at.classifier_wrapper(classifier_all, "knn", feature_norm_all) _, p2 = at.classifier_wrapper(classifier_fm, "knn", feature_norm_fm) start = mid_feats.shape[0] end = mid_feats.shape[0] + len(class_names_all) mid_term_features[0:mid_feats.shape[0], index] = mid_feats[:, index] mid_term_features[start:end, index] = p1 + 1e-4 mid_term_features[end::, index] = p2 + 1e-4 mid_feats = mid_term_features # TODO feature_selected = [ 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 ] mid_feats = mid_feats[feature_selected, :] mid_feats_norm, mean, std = at.normalize_features([mid_feats.T]) mid_feats_norm = mid_feats_norm[0].T n_wins = mid_feats.shape[1] # remove outliers: dist_all = np.sum(distance.squareform(distance.pdist(mid_feats_norm.T)), axis=0) m_dist_all = np.mean(dist_all) i_non_outliers = np.nonzero(dist_all < 1.2 * m_dist_all)[0] # TODO: Combine energy threshold for outlier removal: # EnergyMin = np.min(mt_feats[1,:]) # EnergyMean = np.mean(mt_feats[1,:]) # Thres = (1.5*EnergyMin + 0.5*EnergyMean) / 2.0 # i_non_outliers = np.nonzero(mt_feats[1,:] > Thres)[0] # print i_non_outliers mt_feats_norm_or = mid_feats_norm mid_feats_norm = mid_feats_norm[:, i_non_outliers] # LDA dimensionality reduction: if lda_dim > 0: # extract mid-term features with minimum step: window_ratio = int(round(mid_window / short_window)) step_ratio = int(round(short_window / short_window)) mt_feats_to_red = [] num_of_features = len(st_feats) num_of_stats = 2 for index in range(num_of_stats * num_of_features): mt_feats_to_red.append([]) # for each of the short-term features: for index in range(num_of_features): cur_pos = 0 feat_len = len(st_feats[index]) while cur_pos < feat_len: n1 = cur_pos n2 = cur_pos + window_ratio if n2 > feat_len: n2 = feat_len short_features = st_feats[index][n1:n2] mt_feats_to_red[index].append(np.mean(short_features)) mt_feats_to_red[index + num_of_features].\ append(np.std(short_features)) cur_pos += step_ratio mt_feats_to_red = np.array(mt_feats_to_red) mt_feats_to_red_2 = np.zeros( (mt_feats_to_red.shape[0] + len(class_names_all) + len(class_names_fm), mt_feats_to_red.shape[1])) limit = mt_feats_to_red.shape[0] + len(class_names_all) for index in range(mt_feats_to_red.shape[1]): feature_norm_all = (mt_feats_to_red[:, index] - mean_all) / std_all feature_norm_fm = (mt_feats_to_red[:, index] - mean_fm) / std_fm _, p1 = at.classifier_wrapper(classifier_all, "knn", feature_norm_all) _, p2 = at.classifier_wrapper(classifier_fm, "knn", feature_norm_fm) mt_feats_to_red_2[0:mt_feats_to_red.shape[0], index] = \ mt_feats_to_red[:, index] mt_feats_to_red_2[mt_feats_to_red.shape[0]:limit, index] = p1 + 1e-4 mt_feats_to_red_2[limit::, index] = p2 + 1e-4 mt_feats_to_red = mt_feats_to_red_2 mt_feats_to_red = mt_feats_to_red[feature_selected, :] mt_feats_to_red, mean, std = at.normalize_features([mt_feats_to_red.T]) mt_feats_to_red = mt_feats_to_red[0].T labels = np.zeros((mt_feats_to_red.shape[1], )) lda_step = 1.0 lda_step_ratio = lda_step / short_window for index in range(labels.shape[0]): labels[index] = int(index * short_window / lda_step_ratio) clf = sklearn.discriminant_analysis.\ LinearDiscriminantAnalysis(n_components=lda_dim) clf.fit(mt_feats_to_red.T, labels) mid_feats_norm = (clf.transform(mid_feats_norm.T)).T if n_speakers <= 0: s_range = range(2, 10) else: s_range = [n_speakers] cluster_labels = [] sil_all = [] cluster_centers = [] for speakers in s_range: k_means = sklearn.cluster.KMeans(n_clusters=speakers) k_means.fit(mid_feats_norm.T) cls = k_means.labels_ means = k_means.cluster_centers_ cluster_labels.append(cls) cluster_centers.append(means) sil_1 = [] sil_2 = [] for c in range(speakers): # for each speaker (i.e. for each extracted cluster) clust_per_cent = np.nonzero(cls == c)[0].shape[0] / float(len(cls)) if clust_per_cent < 0.020: sil_1.append(0.0) sil_2.append(0.0) else: # get subset of feature vectors mt_feats_norm_temp = mid_feats_norm[:, cls == c] # compute average distance between samples # that belong to the cluster (a values) dist = distance.pdist(mt_feats_norm_temp.T) sil_1.append(np.mean(dist) * clust_per_cent) sil_temp = [] for c2 in range(speakers): # compute distances from samples of other clusters if c2 != c: clust_per_cent_2 = np.nonzero(cls == c2)[0].shape[0] /\ float(len(cls)) mid_features_temp = mid_feats_norm[:, cls == c2] dist = distance.cdist(mt_feats_norm_temp.T, mid_features_temp.T) sil_temp.append( np.mean(dist) * (clust_per_cent + clust_per_cent_2) / 2.0) sil_temp = np.array(sil_temp) # ... and keep the minimum value (i.e. # the distance from the "nearest" cluster) sil_2.append(min(sil_temp)) sil_1 = np.array(sil_1) sil_2 = np.array(sil_2) sil = [] for c in range(speakers): # for each cluster (speaker) compute silhouette sil.append( (sil_2[c] - sil_1[c]) / (max(sil_2[c], sil_1[c]) + 1e-5)) # keep the AVERAGE SILLOUETTE sil_all.append(np.mean(sil)) imax = int(np.argmax(sil_all)) # optimal number of clusters num_speakers = s_range[imax] # generate the final set of cluster labels # (important: need to retrieve the outlier windows: # this is achieved by giving them the value of their # nearest non-outlier window) cls = np.zeros((n_wins, )) for index in range(n_wins): j = np.argmin(np.abs(index - i_non_outliers)) cls[index] = cluster_labels[imax][j] # Post-process method 1: hmm smoothing for index in range(1): # hmm training start_prob, transmat, means, cov = \ train_hmm_compute_statistics(mt_feats_norm_or, cls) hmm = hmmlearn.hmm.GaussianHMM(start_prob.shape[0], "diag") hmm.startprob_ = start_prob hmm.transmat_ = transmat hmm.means_ = means hmm.covars_ = cov cls = hmm.predict(mt_feats_norm_or.T) # Post-process method 2: median filtering: cls = scipy.signal.medfilt(cls, 13) cls = scipy.signal.medfilt(cls, 11) class_names = ["speaker{0:d}".format(c) for c in range(num_speakers)] # load ground-truth if available gt_file = filename.replace('.wav', '.segments') # if groundtruth exists if os.path.isfile(gt_file): seg_start, seg_end, seg_labs = read_segmentation_gt(gt_file) flags_gt, class_names_gt = segments_to_labels(seg_start, seg_end, seg_labs, mid_step) if plot_res: fig = plt.figure() if n_speakers > 0: ax1 = fig.add_subplot(111) else: ax1 = fig.add_subplot(211) ax1.set_yticks(np.array(range(len(class_names)))) ax1.axis((0, duration, -1, len(class_names))) ax1.set_yticklabels(class_names) ax1.plot(np.array(range(len(cls))) * mid_step + mid_step / 2.0, cls) if os.path.isfile(gt_file): if plot_res: ax1.plot( np.array(range(len(flags_gt))) * mid_step + mid_step / 2.0, flags_gt, 'r') purity_cluster_m, purity_speaker_m = \ evaluate_speaker_diarization(cls, flags_gt) print("{0:.1f}\t{1:.1f}".format(100 * purity_cluster_m, 100 * purity_speaker_m)) if plot_res: plt.title("Cluster purity: {0:.1f}% - " "Speaker purity: {1:.1f}%".format( 100 * purity_cluster_m, 100 * purity_speaker_m)) if plot_res: plt.xlabel("time (seconds)") if n_speakers <= 0: plt.subplot(212) plt.plot(s_range, sil_all) plt.xlabel("number of clusters") plt.ylabel("average clustering's sillouette") plt.show() return cls
def WhatIsThis(self, data): # There are two completely separate models, one is a classifier that uses pyaudioanalysis, the other is a deepspeech model # Convert or cast the raw audio data to numpy array log.debug('Converting data to numpy') if len(data) % 2 != 0: log.critical('Data length: {0}'.format(len(data))) log.critical('Data: {0}'.format(data)) return { #bullshit 'loudness': 0.0, 'class': 'bullshit', 'probability': 1.0, 'text': 'fuckitall', } AccumulatedData_np = np.frombuffer(data, np.int16) # Get the loudness, hope this works rms = np.sqrt(np.mean(AccumulatedData_np**2)) log.debug(f'Raw loudness: {rms}') # normalize it, make it between 0.0 and 1.0. # rms = round((rms - 20.0) / 45, 2) # rms = float(np.clip(rms, 0.0, 1.0)) seg_len = len(AccumulatedData_np) log.debug('seg_len ' + str(seg_len)) # Run the classifier. This is ripped directly out of paura.py and carelessly sutured into place. There's so much blood! Thank you!!! log.debug('Running classifier') try: [mt_feats, _, _] = mF.mid_feature_extraction(AccumulatedData_np, self.fs, seg_len, seg_len, round(self.fs * self.st_win), round(self.fs * self.st_step)) cur_fv = (mt_feats[:, 0] - self.MEAN) / self.STD except ValueError: log.error('Yeah, that thing happened') log.critical('Data length: {0}'.format(len(data))) log.critical('Data: {0}'.format(data)) return { #bullshit 'loudness': 0.0, 'class': 'bullshit', 'probability': 1.0, 'text': 'fuckitall', } # classify vector: [res, prob] = aT.classifier_wrapper(self.classifier, "svm_rbf", cur_fv) win_class = self.class_names[int(res)] win_prob = round(prob[int(res)], 2) log.info('Classified {0:s} with probability {1:.2f}'.format( win_class, win_prob)) # Run the accumulated audio data through deepspeech, if it's speech if win_class == 'lover': log.debug('Running deepspeech model') text = self.model.stt(AccumulatedData_np) log.info('Recognized: %s', text) else: text = 'undefined' # Save the utterance to a wav file. I hope later I'll be able to use this for training a better model, after I learn how to do that. # log.debug('Saving wav file') # wf = wave.open(os.path.join(self.save_dir, str(int(time.time())) + '_' + win_class + '_' + text.replace(' ', '_') + '.wav'), 'wb') # wf.setnchannels(1) # wf.setsampwidth(2) # wf.setframerate(16000) # wf.writeframes(data) # wf.close() # return an object return { 'loudness': rms, 'class': win_class, 'probability': win_prob, 'text': text, }
def QE_speaker_diarization( sampling_rate, signal, n_speakers, classifier_all, mean_all, std_all, class_names_all, classifier_fm, mean_fm, std_fm, class_names_fm, # Load models from avove mid_window=2.0, mid_step=0.2, short_window=0.05, lda_dim=35, plot_res=False): """ ARGUMENTS: - filename: the name of the WAV file to be analyzed #QE_:-> ADAPTED HERE TO RECEIVE DIRECTLY THE DATA sampling_rate, signal INSTEAD OF filename - n_speakers the number of speakers (clusters) in the recording (<=0 for unknown) - mid_window (opt) mid-term window size - mid_step (opt) mid-term window step - short_window (opt) short-term window size - lda_dim (opt LDA dimension (0 for no LDA) - plot_res (opt) 0 for not plotting the results 1 for plotting """ """ Otras opciones a explorar para diarization https://hackernoon.com/speaker-diarization-the-squad-way-2205e0accbda https://github.com/YongyuG/s4d-diarization-gao/blob/master/s4d/diar.py, muy buena pinta https://pypi.org/project/s4d/ https://projets-lium.univ-lemans.fr/s4d/ https://medium.com/datadriveninvestor/speaker-diarization-22121f1264b1 https://arxiv.org/pdf/2005.08072v1.pdf https://github.com/calclavia/tal-asrd https://github.com/josepatino/pyBK https://github.com/wq2012/awesome-diarization https://www.researchgate.net/publication/221480626_The_Detection_of_Overlapping_Speech_with_Prosodic_Features_for_Speaker_Diarization """ # sampling_rate, signal = audioBasicIO.read_audio_file(filename) # NO ES NECESARIO DADOJ QUE LO PASO COMO ARGUMENTOS DE ENTRADA EN LUGAR DE filename signal = audioBasicIO.stereo_to_mono( signal) # eliminar si ya viene en mono como condicion duration = len(signal) / sampling_rate """ #QE_: In order to avoid a recurrent load of the models, they are loaded more globally only once and then passed as arguments # So this part is copied in the module avove , QE_main: base_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data/models") classifier_all, mean_all, std_all, class_names_all, _, _, _, _, _ = \ at.load_model_knn(os.path.join(base_dir, "knn_speaker_10")) classifier_fm, mean_fm, std_fm, class_names_fm, _, _, _, _, _ = \ at.load_model_knn(os.path.join(base_dir, "knn_speaker_male_female")) """ mid_feats, st_feats, _ = \ mtf.mid_feature_extraction(signal, sampling_rate, mid_window * sampling_rate, mid_step * sampling_rate, round(sampling_rate * short_window), round(sampling_rate * short_window * 0.5)) mid_term_features = np.zeros( (mid_feats.shape[0] + len(class_names_all) + len(class_names_fm), mid_feats.shape[1])) for index in range(mid_feats.shape[1]): feature_norm_all = (mid_feats[:, index] - mean_all) / std_all feature_norm_fm = (mid_feats[:, index] - mean_fm) / std_fm _, p1 = at.classifier_wrapper(classifier_all, "knn", feature_norm_all) _, p2 = at.classifier_wrapper(classifier_fm, "knn", feature_norm_fm) start = mid_feats.shape[0] end = mid_feats.shape[0] + len(class_names_all) mid_term_features[0:mid_feats.shape[0], index] = mid_feats[:, index] mid_term_features[start:end, index] = p1 + 1e-4 mid_term_features[end::, index] = p2 + 1e-4 mid_feats = mid_term_features # TODO feature_selected = [ 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 ] mid_feats = mid_feats[feature_selected, :] mid_feats_norm, mean, std = at.normalize_features([mid_feats.T]) mid_feats_norm = mid_feats_norm[0].T n_wins = mid_feats.shape[1] # remove outliers: dist_all = np.sum(distance.squareform(distance.pdist(mid_feats_norm.T)), axis=0) m_dist_all = np.mean(dist_all) i_non_outliers = np.nonzero(dist_all < 1.2 * m_dist_all)[0] # TODO: Combine energy threshold for outlier removal: # EnergyMin = np.min(mt_feats[1,:]) # EnergyMean = np.mean(mt_feats[1,:]) # Thres = (1.5*EnergyMin + 0.5*EnergyMean) / 2.0 # i_non_outliers = np.nonzero(mt_feats[1,:] > Thres)[0] # print i_non_outliers mt_feats_norm_or = mid_feats_norm mid_feats_norm = mid_feats_norm[:, i_non_outliers] # LDA dimensionality reduction: if lda_dim > 0: # extract mid-term features with minimum step: window_ratio = int(round(mid_window / short_window)) step_ratio = int(round(short_window / short_window)) mt_feats_to_red = [] num_of_features = len(st_feats) num_of_stats = 2 for index in range(num_of_stats * num_of_features): mt_feats_to_red.append([]) # for each of the short-term features: for index in range(num_of_features): cur_pos = 0 feat_len = len(st_feats[index]) while cur_pos < feat_len: n1 = cur_pos n2 = cur_pos + window_ratio if n2 > feat_len: n2 = feat_len short_features = st_feats[index][n1:n2] mt_feats_to_red[index].append(np.mean(short_features)) mt_feats_to_red[index + num_of_features].\ append(np.std(short_features)) cur_pos += step_ratio mt_feats_to_red = np.array(mt_feats_to_red) mt_feats_to_red_2 = np.zeros( (mt_feats_to_red.shape[0] + len(class_names_all) + len(class_names_fm), mt_feats_to_red.shape[1])) limit = mt_feats_to_red.shape[0] + len(class_names_all) for index in range(mt_feats_to_red.shape[1]): feature_norm_all = (mt_feats_to_red[:, index] - mean_all) / std_all feature_norm_fm = (mt_feats_to_red[:, index] - mean_fm) / std_fm _, p1 = at.classifier_wrapper(classifier_all, "knn", feature_norm_all) _, p2 = at.classifier_wrapper(classifier_fm, "knn", feature_norm_fm) mt_feats_to_red_2[0:mt_feats_to_red.shape[0], index] = \ mt_feats_to_red[:, index] mt_feats_to_red_2[mt_feats_to_red.shape[0]:limit, index] = p1 + 1e-4 mt_feats_to_red_2[limit::, index] = p2 + 1e-4 mt_feats_to_red = mt_feats_to_red_2 mt_feats_to_red = mt_feats_to_red[feature_selected, :] mt_feats_to_red, mean, std = at.normalize_features([mt_feats_to_red.T]) mt_feats_to_red = mt_feats_to_red[0].T labels = np.zeros((mt_feats_to_red.shape[1], )) lda_step = 1.0 lda_step_ratio = lda_step / short_window for index in range(labels.shape[0]): labels[index] = int(index * short_window / lda_step_ratio) clf = sklearn.discriminant_analysis.\ LinearDiscriminantAnalysis(n_components=lda_dim) clf.fit(mt_feats_to_red.T, labels) mid_feats_norm = (clf.transform(mid_feats_norm.T)).T ########################################################################################################################################## if n_speakers <= 0: s_range = range( 2, 10 ) #QE_: Adapt in this case to range 1-10? We are going to use this diarizantion in short windows, 250-500 ms ########################################################################################################################################### else: s_range = [n_speakers] cluster_labels = [] sil_all = [] cluster_centers = [] for speakers in s_range: k_means = sklearn.cluster.KMeans(n_clusters=speakers) k_means.fit(mid_feats_norm.T) cls = k_means.labels_ means = k_means.cluster_centers_ cluster_labels.append(cls) cluster_centers.append(means) sil_1 = [] sil_2 = [] for c in range(speakers): # for each speaker (i.e. for each extracted cluster) clust_per_cent = np.nonzero(cls == c)[0].shape[0] / float(len(cls)) if clust_per_cent < 0.020: sil_1.append(0.0) sil_2.append(0.0) else: # get subset of feature vectors mt_feats_norm_temp = mid_feats_norm[:, cls == c] # compute average distance between samples # that belong to the cluster (a values) dist = distance.pdist(mt_feats_norm_temp.T) sil_1.append(np.mean(dist) * clust_per_cent) sil_temp = [] for c2 in range(speakers): # compute distances from samples of other clusters if c2 != c: clust_per_cent_2 = np.nonzero(cls == c2)[0].shape[0] /\ float(len(cls)) mid_features_temp = mid_feats_norm[:, cls == c2] dist = distance.cdist(mt_feats_norm_temp.T, mid_features_temp.T) sil_temp.append( np.mean(dist) * (clust_per_cent + clust_per_cent_2) / 2.0) sil_temp = np.array(sil_temp) # ... and keep the minimum value (i.e. # the distance from the "nearest" cluster) sil_2.append(min(sil_temp)) sil_1 = np.array(sil_1) sil_2 = np.array(sil_2) sil = [] for c in range(speakers): # for each cluster (speaker) compute silhouette sil.append( (sil_2[c] - sil_1[c]) / (max(sil_2[c], sil_1[c]) + 1e-5)) # keep the AVERAGE SILLOUETTE sil_all.append(np.mean(sil)) imax = int(np.argmax(sil_all)) # optimal number of clusters num_speakers = s_range[imax] # generate the final set of cluster labels # (important: need to retrieve the outlier windows: # this is achieved by giving them the value of their # nearest non-outlier window) cls = np.zeros((n_wins, )) for index in range(n_wins): j = np.argmin(np.abs(index - i_non_outliers)) cls[index] = cluster_labels[imax][j] # Post-process method 1: hmm smoothing for index in range(1): # hmm training start_prob, transmat, means, cov = \ train_hmm_compute_statistics(mt_feats_norm_or, cls) hmm = hmmlearn.hmm.GaussianHMM(start_prob.shape[0], "diag") hmm.startprob_ = start_prob hmm.transmat_ = transmat hmm.means_ = means hmm.covars_ = cov cls = hmm.predict(mt_feats_norm_or.T) # Post-process method 2: median filtering: cls = scipy.signal.medfilt(cls, 13) cls = scipy.signal.medfilt(cls, 11) class_names = ["speaker{0:d}".format(c) for c in range(num_speakers)] # load ground-truth if available gt_file = filename.replace('.wav', '.segments') # if groundtruth exists if os.path.isfile(gt_file): seg_start, seg_end, seg_labs = read_segmentation_gt(gt_file) flags_gt, class_names_gt = segments_to_labels(seg_start, seg_end, seg_labs, mid_step) """ if plot_res: fig = plt.figure() if n_speakers > 0: ax1 = fig.add_subplot(111) else: ax1 = fig.add_subplot(211) ax1.set_yticks(np.array(range(len(class_names)))) ax1.axis((0, duration, -1, len(class_names))) ax1.set_yticklabels(class_names) ax1.plot(np.array(range(len(cls))) * mid_step + mid_step / 2.0, cls) if os.path.isfile(gt_file): if plot_res: ax1.plot(np.array(range(len(flags_gt))) * mid_step + mid_step / 2.0, flags_gt, 'r') purity_cluster_m, purity_speaker_m = \ evaluate_speaker_diarization(cls, flags_gt) print("{0:.1f}\t{1:.1f}".format(100 * purity_cluster_m, 100 * purity_speaker_m)) if plot_res: plt.title("Cluster purity: {0:.1f}% - " "Speaker purity: {1:.1f}%".format(100 * purity_cluster_m, 100 * purity_speaker_m)) if plot_res: plt.xlabel("time (seconds)") if n_speakers <= 0: plt.subplot(212) plt.plot(s_range, sil_all) plt.xlabel("number of clusters") plt.ylabel("average clustering's sillouette") plt.show() """ return cls
def mid_term_file_classification(input_file, model_name, model_type, plot_results=False, gt_file=""): """ This function performs mid-term classification of an audio stream. Towards this end, supervised knowledge is used, i.e. a pre-trained classifier. ARGUMENTS: - input_file: path of the input WAV file - model_name: name of the classification model - model_type: svm or knn depending on the classifier type - plot_results: True if results are to be plotted using matplotlib along with a set of statistics RETURNS: - segs: a sequence of segment's endpoints: segs[i] is the endpoint of the i-th segment (in seconds) - classes: a sequence of class flags: class[i] is the class ID of the i-th segment """ labels = [] accuracy = 0.0 class_names = [] cm = np.array([]) if not os.path.isfile(model_name): print("mtFileClassificationError: input model_type not found!") return labels, class_names, accuracy, cm # Load classifier: if model_type == "knn": classifier, mean, std, class_names, mt_win, mid_step, st_win, \ st_step, compute_beat = at.load_model_knn(model_name) else: classifier, mean, std, class_names, mt_win, mid_step, st_win, \ st_step, compute_beat = at.load_model(model_name) if compute_beat: print("Model " + model_name + " contains long-term music features " "(beat etc) and cannot be used in " "segmentation") return labels, class_names, accuracy, cm # load input file sampling_rate, signal = audioBasicIO.read_audio_file(input_file) # could not read file if sampling_rate == 0: return labels, class_names, accuracy, cm # convert stereo (if) to mono signal = audioBasicIO.stereo_to_mono(signal) # mid-term feature extraction: mt_feats, _, _ = \ mtf.mid_feature_extraction(signal, sampling_rate, mt_win * sampling_rate, mid_step * sampling_rate, round(sampling_rate * st_win), round(sampling_rate * st_step)) posterior_matrix = [] # for each feature vector (i.e. for each fix-sized segment): for col_index in range(mt_feats.shape[1]): # normalize current feature v feature_vector = (mt_feats[:, col_index] - mean) / std # classify vector: label_predicted, posterior = \ at.classifier_wrapper(classifier, model_type, feature_vector) labels.append(label_predicted) # update probability matrix posterior_matrix.append(np.max(posterior)) labels = np.array(labels) # convert fix-sized flags to segments and classes segs, classes = labels_to_segments(labels, mid_step) segs[-1] = len(signal) / float(sampling_rate) # Load grount-truth: labels_gt, class_names_gt, accuracy, cm = \ load_ground_truth(gt_file, labels, class_names, mid_step, plot_results) return labels, class_names, accuracy, cm
def FileClassification(input_file, model_name, model_type, gt=False, gt_file=""): ''' TODO: This function needs to be refactored according to the code in audioSegmentation.mid_term_file_classification() ''' if not os.path.isfile(model_name): print("mtFileClassificationError: input model_type not found!") return (-1, -1, -1, -1) # Load classifier with load_model: [classifier, MEAN, STD, class_names, mt_win, mt_step, st_win, st_step, compute_beat] = aT.load_model(model_name) # Using audioBasicIO from puAudioAnalysis, the input audio stream is loaded [fs, x] = audioBasicIO.read_audio_file(input_file) if fs == -1: # could not read file return (-1, -1, -1, -1) x = audioBasicIO.stereo_to_mono(x) # convert stereo (if) to mono duration = len(x) / fs # mid-term feature extraction using pyAudioAnalysis mtFeatureExtraction: [mt_feats, _, _] = mF.mid_feature_extraction(x, fs, mt_win * fs, mt_step * fs, round(fs * st_win), round(fs * st_step)) flags = [] Ps = [] flags_ind = [] for i in range(mt_feats.shape[1]): # for each feature vector (i.e. for each fix-sized segment): cur_fv = (mt_feats[:, i] - MEAN) / STD [res, P] = aT.classifier_wrapper(classifier, model_type, cur_fv) if res == 0.0: if numpy.max(P) > 0.5: flags_ind.append(res) flags.append(class_names[int(res)]) # update class label matrix Ps.append(numpy.max(P)) # update probability matrix else: flags_ind.append(-1) flags.append('None') Ps.append(-1) if res == 1.0: if numpy.max(P) > 0.9: flags_ind.append(res) flags.append(class_names[int(res)]) # update class label matrix Ps.append(numpy.max(P)) # update probability matrix else: flags_ind.append(-1) flags.append('None') Ps.append(-1) if res == 2.0: if numpy.max(P) > 0.6: flags_ind.append(res) flags.append(class_names[int(res)]) # update class label matrix Ps.append(numpy.max(P)) # update probability matrix else: flags_ind.append(-1) flags.append('None') Ps.append(-1) if res == 3.0: if numpy.max(P) > 0.3: flags_ind.append(res) flags.append(class_names[int(res)]) # update class label matrix Ps.append(numpy.max(P)) # update probability matrix else: flags_ind.append(-1) flags.append('None') Ps.append(-1) if res == 4.0: if numpy.max(P) > 0.3: flags_ind.append(res) flags.append(class_names[int(res)]) # update class label matrix Ps.append(numpy.max(P)) # update probability matrix else: flags_ind.append(-1) flags.append('None') Ps.append(-1) flags_ind = numpy.array(flags_ind) # 1-window smoothing for i in range(1, len(flags_ind) - 1): if flags_ind[i - 1] == flags_ind[i + 1]: flags_ind[i] = flags_ind[i + 1] # convert fix-sized flags to segments and classes (segs, classes) = aS.labels_to_segments(flags, mt_step) segs[-1] = len(x) / float(fs) if gt == True: # Load grount-truth: if os.path.isfile(gt_file): [seg_start_gt, seg_end_gt, seg_l_gt] = aS.read_segmentation_gt(gt_file) flags_gt, class_names_gt = aS.segments_to_labels(seg_start_gt, seg_end_gt, seg_l_gt, mt_step) flags_ind_gt = [] # print(class_names) for j, fl in enumerate(flags_gt): # "align" labels with GT # print(class_names_gt[flags_gt[j]]) if class_names_gt[flags_gt[j]] in class_names: flags_ind_gt.append(class_names.index(class_names_gt[flags_gt[j]])) else: flags_ind_gt.append(-1) flags_ind_gt = numpy.array(flags_ind_gt) cm = numpy.zeros((len(class_names_gt), len(class_names_gt))) for i in range(min(flags_ind.shape[0], flags_ind_gt.shape[0])): cm[int(flags_ind_gt[i]), int(flags_ind[i])] += 1 else: cm = [] flags_ind_gt = numpy.array([]) acc = aS.plot_segmentation_results(flags_ind, flags_ind_gt, class_names, mt_step, False) else: cm = [] flags_ind_gt = numpy.array([]) acc = aS.plot_segmentation_results(flags_ind, flags_ind_gt, class_names, mt_step, False) if acc >= 0: print("Overall Accuracy: {0:.3f}".format(acc)) return (flags_ind, class_names_gt, acc, cm) else: return (flags_ind, class_names, acc, cm)
def mid_term_file_classification(input_file, model_name, model_type, plot_results=False): """ This function performs mid-term classification of an audio stream. Towards this end, supervised knowledge is used, i.e. a pre-trained classifier. ARGUMENTS: - input_file: path of the input WAV file - model_name: name of the classification model - model_type: svm or knn depending on the classifier type - plot_results: True if results are to be plotted using matplotlib along with a set of statistics RETURNS: - segs: a sequence of segment's endpoints: segs[i] is the endpoint of the i-th segment (in seconds) - classes: a sequence of class flags: class[i] is the class ID of the i-th segment """ labels = [] accuracy = 0.0 class_names = [] cm = np.array([]) # print("model_name: ", model_name) if not os.path.isfile(model_name): # print("mtFileClassificationError: input model_type not found!") return labels, class_names, accuracy, cm # print("class_names: ", class_names) classifier, mean, std, class_names, mt_win, mid_step, st_win, \ st_step, compute_beat = at.load_model(model_name) # load input file sampling_rate, signal = audioBasicIO.read_audio_file(input_file) print("signal: ", signal.shape) # convert stereo (if) to mono signal = audioBasicIO.stereo_to_mono(signal) # mid-term feature extraction: mt_feats, _, _ = mtf.mid_feature_extraction(signal, sampling_rate, mt_win * sampling_rate, mid_step * sampling_rate, round(sampling_rate * st_win), round(sampling_rate * st_step)) class_probabilities = [] # print("class_names: ", class_names) # for each feature vector (i.e. for each fix-sized segment): for col_index in range(mt_feats.shape[1]): # normalize current feature v feature_vector = (mt_feats[:, col_index] - mean) / std # print("col_index: ", col_index) # classify vector: label_predicted, prob = at.classifier_wrapper(classifier, model_type, feature_vector) labels.append(label_predicted) # update probability matrix class_probabilities.append(prob) labels = np.array(labels) return labels, class_names, mid_step, class_probabilities
def record_audio(block_size, fs=8000, show_spec=False, show_chroma=False, log_sounds=False, logs_all=False): # inialize recording process mid_buf_size = int(fs * block_size) pa = pyaudio.PyAudio() stream = pa.open(format=FORMAT, channels=1, rate=fs, input=True, frames_per_buffer=mid_buf_size) mid_buf = [] count = 0 global all_data global outstr all_data = [] # initalize counters etc time_start = time.time() outstr = datetime.datetime.now().strftime("%Y_%m_%d_%I:%M%p") out_folder = outstr + "_segments" if log_sounds: if not os.path.exists(out_folder): os.makedirs(out_folder) # load segment model [classifier, MEAN, STD, class_names, mt_win, mt_step, st_win, st_step, _] = aT.load_model("model") while 1: try: block = stream.read(mid_buf_size) count_b = len(block) / 2 format = "%dh" % (count_b) shorts = struct.unpack(format, block) cur_win = list(shorts) mid_buf = mid_buf + cur_win del cur_win # time since recording started: e_time = (time.time() - time_start) # data-driven time data_time = (count + 1) * block_size x = numpy.int16(mid_buf) seg_len = len(x) # extract features # We are using the signal length as mid term window and step, # in order to guarantee a mid-term feature sequence of len 1 [mt_feats, _, _] = mF.mid_feature_extraction(x, fs, seg_len, seg_len, round(fs * st_win), round(fs * st_step)) cur_fv = (mt_feats[:, 0] - MEAN) / STD # classify vector: [res, prob] = aT.classifier_wrapper(classifier, "svm_rbf", cur_fv) win_class = class_names[int(res)] win_prob = prob[int(res)] if logs_all: all_data += mid_buf mid_buf = numpy.double(mid_buf) # Compute spectrogram if show_spec: (spec, t_axis, freq_axis_s) = sF.spectrogram(mid_buf, fs, 0.050 * fs, 0.050 * fs) freq_axis_s = numpy.array(freq_axis_s) # frequency axis # most dominant frequencies (for each short-term window): dominant_freqs = freq_axis_s[numpy.argmax(spec, axis=1)] # get average most dominant freq max_freq = numpy.mean(dominant_freqs) max_freq_std = numpy.std(dominant_freqs) # Compute chromagram if show_chroma: (chrom, TimeAxisC, freq_axis_c) = sF.chromagram(mid_buf, fs, 0.050 * fs, 0.050 * fs) freq_axis_c = numpy.array(freq_axis_c) # most dominant chroma classes: dominant_freqs_c = freq_axis_c[numpy.argmax(chrom, axis=1)] # get most common among all short-term windows max_freqC = most_common(dominant_freqs_c)[0] # Plot signal window signalPlotCV = plotCV( scipy.signal.resample(mid_buf + 16000, plot_w), plot_w, plot_h, 32000) cv2.imshow('Signal', signalPlotCV) cv2.moveWindow('Signal', 50, status_h + 50) # Show spectrogram if show_spec: i_spec = numpy.array(spec.T * 255, dtype=numpy.uint8) i_spec2 = cv2.resize(i_spec, (plot_w, plot_h), interpolation=cv2.INTER_CUBIC) i_spec2 = cv2.applyColorMap(i_spec2, cv2.COLORMAP_JET) cv2.putText(i_spec2, "max_freq: %.0f Hz" % max_freq, (0, 11), cv2.FONT_HERSHEY_PLAIN, 1, (200, 200, 200)) cv2.imshow('Spectrogram', i_spec2) cv2.moveWindow('Spectrogram', 50, plot_h + status_h + 60) # Show chromagram if show_chroma: i_chroma = numpy.array((chrom.T / chrom.max()) * 255, dtype=numpy.uint8) i_chroma2 = cv2.resize(i_chroma, (plot_w, plot_h), interpolation=cv2.INTER_CUBIC) i_chroma2 = cv2.applyColorMap(i_chroma2, cv2.COLORMAP_JET) cv2.putText(i_chroma2, "max_freqC: %s" % max_freqC, (0, 11), cv2.FONT_HERSHEY_PLAIN, 1, (200, 200, 200)) cv2.imshow('Chroma', i_chroma2) cv2.moveWindow('Chroma', 50, 2 * plot_h + status_h + 60) # Activity Detection: print("{0:.2f}\t{1:s}\t{2:.2f}".format(e_time, win_class, win_prob)) if log_sounds: # TODO: log audio files out_file = os.path.join( out_folder, "{0:.2f}_".format(e_time).zfill(8) + win_class + ".wav") #shutil.copyfile("temp.wav", out_file) wavfile.write(out_file, fs, x) textIm = numpy.zeros((status_h, plot_w, 3)) statusStrTime = "time: %.1f sec" % e_time + \ " - data time: %.1f sec" % data_time + \ " - loss : %.1f sec" % (e_time - data_time) cv2.putText(textIm, statusStrTime, (0, 11), cv2.FONT_HERSHEY_PLAIN, 1, (200, 200, 200)) cv2.putText(textIm, win_class, (0, 33), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255)) cv2.imshow("Status", textIm) cv2.moveWindow("Status", 50, 0) mid_buf = [] ch = cv2.waitKey(10) count += 1 except IOError: print("Error recording")
def record_audio(block_size, devices, use_yeelight_bulbs=False, fs=8000): # initialize the yeelight devices: bulbs = [] if use_yeelight_bulbs: for d in devices: bulbs.append(Bulb(d)) try: bulbs[-1].turn_on() except: bulbs = [] # initialize recording process mid_buf_size = int(fs * block_size) pa = pyaudio.PyAudio() stream = pa.open(format=FORMAT, channels=1, rate=fs, input=True, frames_per_buffer=mid_buf_size) mid_buf = [] count = 0 global all_data global outstr all_data = [] outstr = datetime.datetime.now().strftime("%Y_%m_%d_%I:%M%p") # load segment model [classifier, mu, std, class_names, mt_win, mt_step, st_win, st_step, _] = aT.load_model("model") [clf_energy, mu_energy, std_energy, class_names_energy, mt_win_en, mt_step_en, st_win_en, st_step_en, _] = \ aT.load_model("energy") [clf_valence, mu_valence, std_valence, class_names_valence, mt_win_va, mt_step_va, st_win_va, st_step_va, _] = \ aT.load_model("valence") while 1: block = stream.read(mid_buf_size) count_b = len(block) / 2 format = "%dh" % (count_b) shorts = struct.unpack(format, block) cur_win = list(shorts) mid_buf = mid_buf + cur_win del cur_win if len(mid_buf) >= 5 * fs: # data-driven time x = numpy.int16(mid_buf) seg_len = len(x) # extract features # We are using the signal length as mid term window and step, # in order to guarantee a mid-term feature sequence of len 1 [mt_f, _, _] = mF(x, fs, seg_len, seg_len, round(fs * st_win), round(fs * st_step)) fv = (mt_f[:, 0] - mu) / std # classify vector: [res, prob] = aT.classifier_wrapper(classifier, "svm_rbf", fv) win_class = class_names[int(res)] if prob[class_names.index("silence")] > 0.8: soft_valence = 0 soft_energy = 0 print("Silence") else: # extract features for music mood [f_2, _, _] = mF(x, fs, round(fs * mt_win_en), round(fs * mt_step_en), round(fs * st_win_en), round(fs * st_step_en)) [f_3, _, _] = mF(x, fs, round(fs * mt_win_va), round(fs * mt_step_va), round(fs * st_win_va), round(fs * st_step_va)) # normalize feature vector fv_2 = (f_2[:, 0] - mu_energy) / std_energy fv_3 = (f_3[:, 0] - mu_valence) / std_valence [res_energy, p_en] = aT.classifier_wrapper(clf_energy, "svm_rbf", fv_2) win_class_energy = class_names_energy[int(res_energy)] [res_valence, p_val] = aT.classifier_wrapper(clf_valence, "svm_rbf", fv_3) win_class_valence = class_names_valence[int(res_valence)] soft_energy = p_en[class_names_energy.index("high")] - \ p_en[class_names_energy.index("low")] soft_valence = p_val[class_names_valence.index("positive")] - \ p_val[class_names_valence.index("negative")] print(win_class, win_class_energy, win_class_valence, soft_valence, soft_energy) all_data += mid_buf mid_buf = [] h, w, _ = img.shape y_center, x_center = int(h / 2), int(w / 2) x = x_center + int((w / 2) * soft_valence) y = y_center - int((h / 2) * soft_energy) radius = 20 emo_map_img_2 = emo_map_img.copy() color = numpy.median(emo_map[y - 2:y + 2, x - 2:x + 2], axis=0).mean(axis=0) emo_map_img_2 = cv2.circle( emo_map_img_2, (x, y), radius, (int(color[0]), int(color[1]), int(color[2])), -1) emo_map_img_2 = cv2.circle(emo_map_img_2, (x, y), radius, (255, 255, 255), 2) cv2.imshow('Emotion Color Map', emo_map_img_2) # set yeelight bulb colors if use_yeelight_bulbs: for b in bulbs: if b: # attention: color is in bgr so we need to invert: b.set_rgb(int(color[2]), int(color[1]), int(color[0])) cv2.waitKey(10) count += 1
def get_coordinates_from_audio(block, rms_min_max=[0, 25000]): mid_buf = [] global all_data global outstr all_data = [] outstr = datetime.datetime.now().strftime("%Y_%m_%d_%I:%M%p") # load segment model [classifier, mu, std, class_names, mt_win, mt_step, st_win, st_step, _] = aT.load_model("model") [clf_energy, mu_energy, std_energy, class_names_energy, mt_win_en, mt_step_en, st_win_en, st_step_en, _] = \ aT.load_model("energy") [clf_valence, mu_valence, std_valence, class_names_valence, mt_win_va, mt_step_va, st_win_va, st_step_va, _] = \ aT.load_model("valence") count_b = len(block) / 2 format_h = "%dh" % (count_b) shorts = struct.unpack(format_h, block) cur_win = list(shorts) mid_buf = mid_buf + cur_win del cur_win # data-driven time x = numpy.int16(mid_buf) seg_len = len(x) r = audioop.rms(x, 2) if r < rms_min_max[0]: # set new min incase the default value is exceded rms_min_max[0] = r if r > rms_min_max[1]: # set new max incase the default value is exceded rms_min_max[1] = r r_norm = float(r - rms_min_max[0]) / float(rms_min_max[1] - rms_min_max[0]) r_map = int(r_norm * 255) print( f'RMS: {r}; MIN: {rms_min_max[0]}; MAX: {rms_min_max[1]}; NORM: {r_norm}; MAP: {r_map}' ) # extract features # We are using the signal length as mid term window and step, # in order to guarantee a mid-term feature sequence of len 1 [mt_f, _, _] = mF(x, fs, seg_len, seg_len, round(fs * st_win), round(fs * st_step)) fv = (mt_f[:, 0] - mu) / std # classify vector: [res, prob] = aT.classifier_wrapper(classifier, "svm_rbf", fv) win_class = class_names[int(res)] if prob[class_names.index("silence")] > 0.8: soft_valence = 0 soft_energy = 0 print("Silence") else: # extract features for music mood [f_2, _, _] = mF(x, fs, round(fs * mt_win_en), round(fs * mt_step_en), round(fs * st_win_en), round(fs * st_step_en)) [f_3, _, _] = mF(x, fs, round(fs * mt_win_va), round(fs * mt_step_va), round(fs * st_win_va), round(fs * st_step_va)) # normalize feature vector fv_2 = (f_2[:, 0] - mu_energy) / std_energy fv_3 = (f_3[:, 0] - mu_valence) / std_valence [res_energy, p_en] = aT.classifier_wrapper(clf_energy, "svm_rbf", fv_2) win_class_energy = class_names_energy[int(res_energy)] [res_valence, p_val] = aT.classifier_wrapper(clf_valence, "svm_rbf", fv_3) win_class_valence = class_names_valence[int(res_valence)] soft_energy = p_en[class_names_energy.index("high")] - \ p_en[class_names_energy.index("low")] soft_valence = p_val[class_names_valence.index("positive")] - \ p_val[class_names_valence.index("negative")] print(win_class, win_class_energy, win_class_valence, soft_valence, soft_energy) global prev_valence_and_energy if prev_valence_and_energy == None: prev_valence_and_energy = (soft_valence, soft_energy) valence_difference = abs(prev_valence_and_energy[0] - soft_valence) energy_difference = abs(prev_valence_and_energy[1] - soft_energy) bound = 0.2 should_change = valence_difference > bound or energy_difference > bound all_data += mid_buf mid_buf = [] h, w, _ = img y_center, x_center = int(h / 2), int(w / 2) x = x_center + int( (w / 2) * soft_valence if not should_change else prev_valence_and_energy[0]) y = y_center - int( (h / 2) * soft_energy if not should_change else prev_valence_and_energy[1]) if should_change: prev_valence_and_energy = (soft_valence, soft_energy) radius = 20 alpha = format(r_map, '02x') return [soft_valence, soft_energy, alpha]