def classifySMFolderWrapper(inputFolder, outputFile, model_type, model_name, outputMode=False): if not os.path.isfile(model_name): raise Exception("Input model_name not found!") types = ('*.wav','*.mp3') wavFilesList = [] for files in types: wavFilesList.extend(glob.glob((inputFolder + files))) wavFilesList = sorted(wavFilesList) if len(wavFilesList) == 0: print("No WAV files found!") return Results = [] for wavFile in wavFilesList: [Result, P, classNames] = aT.file_classification(wavFile, model_name, model_type) Result = int(Result) Results.append(Result) if outputMode: outfile=open(outputFile, 'w+') outfile.write("{0:s}\t{1:s}".format(wavFile, classNames[Result])+"\n") # print("{0:s}\t{1:s}".format(wavFile, classNames[Result])) Results = np.array(Results) # print distribution of classes: [Histogram, _] = np.histogram(Results, bins=np.arange(len(classNames) + 1)) for i, h in enumerate(Histogram): print("{0:20s}\t\t{1:d}".format(classNames[i], h))
def is_cough(file_path): wav_file_path = convert_to_wav(file_path) yamnet_veredict = yamnet_classifier(wav_file_path) svm_veredict = aT.file_classification(wav_file_path, "cough_classifier/svm_cough", "svm") svm_predict = svm_veredict[1][0] accepted = [yamnet_veredict, svm_predict] return accepted
def main(): #check time elapsed record_sound() out = aT.file_classification("recorded_sound.wav", "svmSMtemp", "svm") #play recording accordingly if out[1][0] >= 0.5: play_music("playback_sound.wav") print("detected coyote howl")
def test_function(model_name, model_type): test_samples = [ 'audio/test/male/arctic_a0014.wav', 'audio/test/male/U5.wav', 'audio/test/female/007030085.wav', 'audio/test/female/arctic_a0006.wav' ] for sample in test_samples: print(aT.file_classification(sample, model_name, model_type))
def classifySMFileWrapper(inputFile, model_type, model_name): if not os.path.isfile(model_name): raise Exception("Input model_name not found!") if not os.path.isfile(inputFile): raise Exception("Input audio file not found!") [Result, P, classNames] = aT.file_classification(inputFile, model_name, model_type) print("{0:s}\t{1:s}".format("Class", "Probability")) for i, c in enumerate(classNames): print("{0:s}\t{1:.2f}".format(c, P[i])) print("Winner class: " + classNames[int(Result)])
def audio(): if request.method == 'POST': f = request.files['file'] f.save(secure_filename(f.filename)) d = aT.file_classification(f.filename, "svmSentimentAnalysis", "svm") print(d) per1 = d[1][0] per2 = d[1][1] if per1 > per2: transcript = d[2][0] else: transcript = d[2][1] return render_template('audio.html', d=transcript)
def auto_test(path,mode_path,mode_type,answer_name,check_name): path_v1 = os.path.join(path, "*") path_list = glob.glob(path_v1) wav_file_list = [[] for i in range(len(path_list))] f = open(answer_name + ".txt", 'w') for num, i in enumerate(path_list): for string in step_params.container().get_music_type(): x = os.path.join(i, string) y = glob.glob(x) wav_file_list[num].extend(y) for i in range(len(wav_file_list)): for j in range(len(wav_file_list[i])): class_id, probability, classes = audioTrainTest.file_classification(wav_file_list[i][j], mode_path, mode_type) probability = probability.tolist() f.write(str(wav_file_list[i][j]).split('/')[-1]+"\n"+str(probability.index(max(probability)))+"\n"+str(i)+"\n") f.close()
def prosodyfile(): for filename in glob.glob("./uploads/*.wav"): print("filename", filename) isSignificant = 0.8 # try different values. # P: list of probabilities Result, P, classNames = aT.file_classification(filename, "svmModel1", "svm") print(("result is", Result)) print(("classNames is", classNames)) print(("P is", P)) print(type(P)) print(("result is", Result)) winner = np.argmax(P) le = len(P) for i in range(le): P[i] = round(P[i], 4) print("P", P[i]) # if P[winner] > isSignificant : with open("./uploads/audio_emotions.txt", "a") as text_file: text_file.write("Your emotion is in category: " + classNames[winner] + ", with probability: " + str(P[winner]) + "\n") with open("./uploads/audio_coordinates.txt", "a") as text_file: print("filename write", filename) text_file.write(str(P) + "\n") print("just before disaster") data, samplerate = sf.read(filename) # sf.write('last.flac', data, samplerate) # filename = "finaltest.wav" r = sr.Recognizer() with sr.AudioFile(filename) as source: audio_data = r.record(source) text = r.recognize_google(audio_data, language="en-IN") print(text) # print(type(text)) # # with open("./uploads/audio_text.docx", "a") as text_file: # # text_file.write(text+"\n") # mydoc = docx.Document() # mydoc.add_paragraph(text) # mydoc.save("./uploads/audio_text.docx") # print("after writing docx") with open("./uploads/audio_text.txt", "a") as text_file: text_file.write(text + "\n")
def is_cough(file_path): wav_file_path = convert_to_wav(file_path) yamnet_veredict = yamnet_classifier(wav_file_path) svm_veredict = aT.file_classification(wav_file_path, "cough_classifier/svm_cough", "svm") svm_predict = svm_veredict[1][0] #accepted = [yamnet_veredict, svm_predict] #return accepted X_new = pd.DataFrame({'Yamnet': [yamnet_veredict], 'SVM': [svm_predict]}) stacking_prediction = stacking_classifier.predict_proba(X_new)[:, 1] optimal_threshold = 0.4264 if (stacking_prediction >= optimal_threshold): return True else: return False
def predict_object(object_no, model_name, algorithm, dirpath, column_names): """Predicts the class for all wav objects in the directory, with matching object_no. Uses the trained model with name model_name. Args: model_name (str): pyAudioAnalysis model name. algorithm (str): pyAudioAnalysis algorithm. object_no (int): object of the experiment. dirpath (str): path of the wav files. column_names (list): table column names. Returns: pd.DataFrame: table of results. """ if not os.path.isdir(dirpath): print("Can't find path " + dirpath) # create empty array (column) for each name results_dict = {name: [] for name in column_names} class_name = column_names[2] file_pattern = "o" + str(object_no) + r"_([\w\d_]+)_audio.wav" files = [f for f in os.listdir(dirpath) if re.match(file_pattern, f)] for fname in files: seq_no = re.match(file_pattern, fname).group(1) c, p, probs_names = \ aT.file_classification(os.path.join(dirpath,fname), model_name, algorithm) results_dict['Object'].append(object_no) results_dict['Sequence'].append(seq_no) results_dict[class_name].append(c) # class probabilities for k in range(len(p)): results_dict[class_name + ' prob' + str(k)].append(p[k]) print("classified obj" + str(object_no) + " " + seq_no + " : " + str(c)) return pd.DataFrame(results_dict).sort_values(['Object', 'Sequence'])
def check(path,mode_path,mode_type,answer_name,check_name): path_v1 = os.path.join(path, "*") path_list = glob.glob(path_v1) f = open(answer_name + ".txt", 'w') for i in range(len(path_list)): class_id, probability, classes = audioTrainTest.file_classification(path_list[i], mode_path,mode_type) probability = probability.tolist() f.write(str(path_list[i]).split('/')[-1]+ "\n" + str(probability.index(max(probability)))+ "\n") f.close() f = open(answer_name + ".txt", 'r') answer = f.readlines() f.close() f = open(check_name + ".txt", 'r') check = f.readlines() total = len(answer)/2.0 correct = 0 for i in range(len(answer)): temindex = check.index(answer[i]) if temindex!=-1 and answer[i+1]==check[temindex+2]: correct+=1 i+=2 print(correct/total)
states = glob.glob(region + "/*") os.mkdir("tempTest" + region[region.find("/"):]) for state in states: paths = glob.glob(state + "/*") for path in paths: shutil.move(path, "tempTest" + region[region.find("/"):]) # Classifies all testing data and gets accuracy rate correct = 0 totalTested = 0 regions = glob.glob("tempTest/*") for region in regions: currentRegion = region[region.find("/")+1:] paths = glob.glob(region + "/*") for path in paths: Result, P, classNames = aT.file_classification(path, "model", "randomforest") if classNames[np.argmax(P)] == currentRegion: correct = correct + 1 totalTested = totalTested + 1 # Moves all files back to original directory regions = glob.glob("tempTest/*") for region in regions: paths = glob.glob(region + "/*") for path in paths: end = path.find(" ") if not path[end+1:end+2].isdigit(): end = path[end+1:].find(" ") + end + 1 if end != -1: shutil.move(path, "test" + path[path.find("/"):end])
from pyAudioAnalysis import audioTrainTest as aT ID, Prob, Class = aT.file_classification( "C:/Users/asus/Desktop/Test.wav", "C:/Users/asus/PycharmProjects/untitled/RF_Mirex", "randomforest") print(ID) print(Prob) print(Class)
# "iNNovationMerge DailyCodeHub" # Theme : Audio analysis week with Python # Audio Classification using pretrained two class KNN model(knn_sm) from pyAudioAnalysis import audioTrainTest as aT c, p, p_nam = aT.file_classification("data/doremi.wav", "data/models/knn_sm", "knn") for k in range(len(p_nam)): print(f'P({p_nam[k]}={p[k]})') # Output : # P(speech=0.0) # P(music=1.0)
# "iNNovationMerge DailyCodeHub" # Theme : Audio analysis week with Python # Audio Classification using pretrained musical class SVM model(svm_rbf_musical_genre_6) from pyAudioAnalysis import audioTrainTest as aT c, p, p_nam = aT.file_classification("data/doremi.wav", "data/models/svm_rbf_musical_genre_6","svm") for k in range(len(p_nam)): print(f'P({p_nam[k]}={p[k]})')) # Output : # P(Blues=0.19340571058645775) # P(Classical=0.05183508791839752) # P(Electronic=0.3333299536117741) # P(Jazz=0.02182702350701883) # P(Rap=0.22459715419862877) # P(Rock=0.17500507017772288)
''' # For Single File class_id, probability, classes = aT.file_classification("D:\\Capstone\\Testing\\Single Song\\Passenger - Let Her Go 🎵.mp3", "C:\\Users\\arman\\PycharmProjects\\Capstone\\Features\\RF_test", "randomforest") print(class_id, probability, classes) print(classes[int(class_id)]) ''' d = "D:\\Capstone\\Testing\\Single Song\\" # downsample done songs directory, I suggest make a new copy of that and then run for path in os.listdir(d): source = os.path.join(d, path) #if os.path.isfile(source): print(source) class_id, probability, classes = aT.file_classification( source, "C:\\Users\\arman\\PycharmProjects\\Capstone\\Features\\RF_test", "randomforest") print(class_id, classes) for count, name in enumerate(classes): if (count == int(class_id)): print(name) # PRINT OUT THE VALUE ACCORDING TO KEY output_dir = "D:\\Capstone\\Single Song\\" + name # make a new directory where songs in their emotions can be saved print(output_dir) # PRINT OUT THE DIRECTORY ACCORDING TO VALUE ################### EXCEPTION HANDLING ############### try: os.makedirs(output_dir, 0o777, exist_ok=True) except OSError as exc: if exc.errno != errno.EEXIST: raise print("Creation of the directory %s failed" % output_dir)
# "iNNovationMerge DailyCodeHub" # Theme : Audio analysis week with Python # Audio Classification using pretrained eight class SVM model(svm_rbf_movie8class) from pyAudioAnalysis import audioTrainTest as aT c, p, p_nam = aT.file_classification("data/doremi.wav", "data/models/svm_rbf_movie8class", "svm") for k in range(len(p_nam)): print(f'P({p_nam[k]}={p[k]})') # Output : # P(Speech=0.13171912921682888) # P(Music=0.7011823461550247) # P(Others1=0.00980347558801528) # P(Others2=0.020903164965289794) # P(Others3=0.00724064282867633) # P(Shots=0.010905822090526755) # P(Fights=0.018400925456362103) # P(Screams=0.09984449369927641)
def main(argv): if argv[1] == "-shortTerm": for i in range(nExp): [Fs, x] = audioBasicIO.read_audio_file("diarizationExample.wav") duration = x.shape[0] / float(Fs) t1 = time.time() F = MidTermFeatures.short_term_feature_extraction( x, Fs, 0.050 * Fs, 0.050 * Fs) t2 = time.time() perTime1 = duration / (t2 - t1) print "short-term feature extraction: {0:.1f} x realtime".format( perTime1) elif argv[1] == "-classifyFile": for i in range(nExp): [Fs, x] = audioBasicIO.read_audio_file("diarizationExample.wav") duration = x.shape[0] / float(Fs) t1 = time.time() aT.file_classification("diarizationExample.wav", "svmSM", "svm") t2 = time.time() perTime1 = duration / (t2 - t1) print "Mid-term feature extraction + classification \t {0:.1f} x realtime".format( perTime1) elif argv[1] == "-mtClassify": for i in range(nExp): [Fs, x] = audioBasicIO.read_audio_file("diarizationExample.wav") duration = x.shape[0] / float(Fs) t1 = time.time() [flagsInd, classesAll, acc] = aS.mid_term_file_classification("diarizationExample.wav", "svmSM", "svm", False, '') t2 = time.time() perTime1 = duration / (t2 - t1) print "Fix-sized classification - segmentation \t {0:.1f} x realtime".format( perTime1) elif argv[1] == "-hmmSegmentation": for i in range(nExp): [Fs, x] = audioBasicIO.read_audio_file("diarizationExample.wav") duration = x.shape[0] / float(Fs) t1 = time.time() aS.hmm_segmentation('diarizationExample.wav', 'hmmRadioSM', False, '') t2 = time.time() perTime1 = duration / (t2 - t1) print "HMM-based classification - segmentation \t {0:.1f} x realtime".format( perTime1) elif argv[1] == "-silenceRemoval": for i in range(nExp): [Fs, x] = audioBasicIO.read_audio_file("diarizationExample.wav") duration = x.shape[0] / float(Fs) t1 = time.time() [Fs, x] = audioBasicIO.read_audio_file("diarizationExample.wav") segments = aS.silence_removal(x, Fs, 0.050, 0.050, smooth_window=1.0, Weight=0.3, plot=False) t2 = time.time() perTime1 = duration / (t2 - t1) print "Silence removal \t {0:.1f} x realtime".format(perTime1) elif argv[1] == "-thumbnailing": for i in range(nExp): [Fs1, x1] = audioBasicIO.read_audio_file("scottish.wav") duration1 = x1.shape[0] / float(Fs1) t1 = time.time() [A1, A2, B1, B2, Smatrix] = aS.music_thumbnailing(x1, Fs1, 1.0, 1.0, 15.0) # find thumbnail endpoints t2 = time.time() perTime1 = duration1 / (t2 - t1) print "Thumbnail \t {0:.1f} x realtime".format(perTime1) elif argv[1] == "-diarization-noLDA": for i in range(nExp): [Fs1, x1] = audioBasicIO.read_audio_file("diarizationExample.wav") duration1 = x1.shape[0] / float(Fs1) t1 = time.time() aS.speaker_diarization("diarizationExample.wav", 4, LDAdim=0, PLOT=False) t2 = time.time() perTime1 = duration1 / (t2 - t1) print "Diarization \t {0:.1f} x realtime".format(perTime1) elif argv[1] == "-diarization-LDA": for i in range(nExp): [Fs1, x1] = audioBasicIO.read_audio_file("diarizationExample.wav") duration1 = x1.shape[0] / float(Fs1) t1 = time.time() aS.speaker_diarization("diarizationExample.wav", 4, PLOT=False) t2 = time.time() perTime1 = duration1 / (t2 - t1) print "Diarization \t {0:.1f} x realtime".format(perTime1)
"svm", "svmModel", False) aT.extract_features_and_train(subdirectories, 1.0, 1.0, aT.shortTermWindow, aT.shortTermStep, "knn", "knnModel", False) aT.extract_features_and_train(subdirectories, 1.0, 1.0, aT.shortTermWindow, aT.shortTermStep, "randomforest", "randomforestModel", False) ''' Result, P, classNames = aT.file_classification( "D:\\Audio_Speech_Actors_01-24\\Fearful\\03-01-06-01-01-01-01.wav", "svmModel", "svm") maxx = np.argmax(P) print(Result) print(P) print(classNames) print(classNames[maxx], P[maxx]) ''' subdirectories = os.listdir('D:\MER_bimodal_dataset\Test') subdirectories = ['D:\MER_bimodal_dataset\Test' + "\\" + subDirName for subDirName in subdirectories] #print(subdirectories) ans = ['SereneJoy', 'Happy', 'SereneJoy', 'Melancholy', 'Tense', 'SereneJoy', 'Happy', 'Happy', 'Tense', 'Happy', 'Melancholy', 'SereneJoys', 'Melancholy', 'Melancholy', 'Happy', 'Happy', 'Tense', 'Happy', 'Tense', 'Happy',
# "iNNovationMerge DailyCodeHub" # Theme : Audio analysis week with Python # Audio Classification using pretrained two class SVM model(svm_rbf_sm) from pyAudioAnalysis import audioTrainTest as aT c, p, p_nam = aT.file_classification("data/doremi.wav", "data/models/svm_rbf_sm", "svm_rbf") print(f'P({p_nam[0]}={p[0]})') print(f'P({p_nam[1]}={p[1]})') # Output : # P(speech=1.605096086774166e-06) # P(music=0.9999983949039132)
def classifyFolderWrapper(inputFolder, modelType, modelName, outputMode=False): if not os.path.isfile(modelName): raise Exception("Input modelName not found!") if modelType == 'svm': [ Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, compute_beat ] = aT.load_model(modelName) elif modelType == 'knn': [ Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, compute_beat ] = aT.load_model_knn(modelName) PsAll = numpy.zeros((len(classNames), )) files = "*.wav" if os.path.isdir(inputFolder): strFilePattern = os.path.join(inputFolder, files) else: strFilePattern = inputFolder + files wavFilesList = [] wavFilesList.extend(glob.glob(strFilePattern)) wavFilesList = sorted(wavFilesList) if len(wavFilesList) == 0: print("No WAV files found!") return Results = [] for wavFile in wavFilesList: [Fs, x] = audioBasicIO.readAudioFile(wavFile) signalLength = x.shape[0] / float(Fs) [Result, P, classNames] = aT.file_classification(wavFile, modelName, modelType) PsAll += (numpy.array(P) * signalLength) Result = int(Result) Results.append(Result) if outputMode: print("{0:s}\t{1:s}".format(wavFile, classNames[Result])) Results = numpy.array(Results) # print distribution of classes: [Histogram, _] = numpy.histogram(Results, bins=numpy.arange(len(classNames) + 1)) if outputMode: for i, h in enumerate(Histogram): print("{0:20s}\t\t{1:d}".format(classNames[i], h)) PsAll = PsAll / numpy.sum(PsAll) if outputMode: fig = plt.figure() ax = fig.add_subplot(111) plt.title("Classes percentage " + inputFolder.replace('Segments', '')) ax.axis((0, len(classNames) + 1, 0, 1)) ax.set_xticks(numpy.array(range(len(classNames) + 1))) ax.set_xticklabels([" "] + classNames) ax.bar(numpy.array(range(len(classNames))) + 0.5, PsAll) plt.show() return classNames, PsAll
import os data_dir = 'C:/MachineLearningPractice/emovo/EMOVOdata' emotion_dirs = [x[0] for x in os.walk(data_dir)] print(emotion_dirs[1:]) from pyAudioAnalysis import audioTrainTest as aT aT.extract_features_and_train(emotion_dirs[1:], 1.0, 1.0, aT.shortTermWindow, aT.shortTermStep, "svm", "svmSMtemp", False, train_percentage=0.80) aT.file_classification( 'C:/MachineLearningPractice/emovo/EMOVOdata/sadness/tri-f1-b1.wav', "svmSMtemp", "svm")
argvs.append(sys.argv[1]) # DiviceId (Phone) interviewFileDir = './FullInterviewData' voiceFileDir = './voiceData' final_loc = os.path.join( os.path.join(interviewFileDir, argvs[0]), 'seperated') #각 폴더 내부에 sperated 폴더를 만들어 분할파일을 저장할 것이다. trained_data_loc = os.path.join(os.path.join(voiceFileDir, argvs[0]), "svmSMtemp") if not os.path.join(os.path.join(interviewFileDir, argvs[0]), 'class_log.txt'): text = open( os.path.join(os.path.join(interviewFileDir, argvs[0]), 'class_log.txt'), 'w') else: text = open( os.path.join(os.path.join(interviewFileDir, argvs[0]), 'class_log.txt'), 'a') temp = os.listdir(final_loc) temp.sort(key=lambda x: float(x.split('_')[-1][:-4])) for file_name in temp: class_id, probability, classes = aT.file_classification( os.path.join(final_loc, file_name), trained_data_loc, "svm") text.write('[' + file_name.split('_')[-1][:-4] + '] [Speaker: ' + str(int(class_id)) + ']' + '\n') text.close()
# [mid_features, short_features, mid_feature_names]=MidTermFeatures.directory_feature_extraction("F:\\tempFile\pythonWav",10,10,1,1,False) # [features, classNames,_] =\ # MidTermFeatures.multiple_directory_feature_extraction(['F:\\tempFile\pythonWav\people','F:\\tempFile\pythonWav\\nopeople'], 10,10, 0.2, 0.2) from pyAudioAnalysis.MidTermFeatures import directory_feature_extraction from dataAnalysis import training from dataAnalysis import step_params answer = training.freature_extraction( ['F:\\tempFile\pythonWav\people', 'F:\\tempFile\pythonWav\\nopeople'], 10, 10, 0.2, 0.2, ['zcr_mean', 'energy_mean']) # audioTrainTest.extract_features_and_train(['F:\\tempFile\pythonWav\people','F:\\tempFile\pythonWav\\nopeople'], 10,10, 0.2, 0.2,"svm","F:\\tempFile\mode_test\\v2\mode") class_id, probability, classes = audioTrainTest.file_classification( "F:\\tempFile\pythonWav\\nopeople\\05-风起天阑.wav", "F:\\tempFile\mode_test\\v1\mode", "svm") print(class_id, probability, classes) # a=1 l = [] path = "" path_v1 = os.path.join(path, "*") path_list = glob.glob(path_v1) wav_file_list = [[] for i in range(len(path_list))] for num, i in enumerate(path_v1): for string in step_params.container.music_type(): x = os.path.join(i, string) y = glob.glob(x) wav_file_list[num].extend(y) for i in range(len(wav_file_list)):
def classify(fileloc, location, gender, age): _, res, label = aT.file_classification(fileloc, "model/model", "randomforest") print(detect(location, gender, age), res[1]) # return (0.2 * detect(location, gender, age)) + (0.8 * res[1]) return res[1]
import numpy as np import os basepath = 'C:/Users/Guillem/Desktop/HACKATHON 2020/Unlabeled audio/TEST/' cough_path = os.path.join(basepath, 'Cough/') nocough_path = os.path.join(basepath, 'No_Cough/') pred_cough_yamnet = [] pred_cough_svm = [] for i in os.listdir(cough_path): wav_path = os.path.join(cough_path, i) prediction_yamnet = yamnet_classifier(wav_path) prediction_svm = aT.file_classification(wav_path, "cough_classifier/svm_cough", "svm")[1][0] pred_cough_yamnet.append(prediction_yamnet) pred_cough_svm.append(prediction_svm) pred_nocough_yamnet = [] pred_nocough_svm = [] for i in os.listdir(nocough_path): wav_path = os.path.join(nocough_path, i) prediction_yamnet = yamnet_classifier(wav_path) prediction_svm = aT.file_classification(wav_path, "cough_classifier/svm_cough", "svm")[1][0] pred_nocough_yamnet.append(prediction_yamnet)
def single_file_classification(wav_path, classifier_model_path): class_id, probability, classes = aT.file_classification( wav_path, classifier_model_path, "randomforest") return class_id, probability, classes