code: def train_hmm_from_directory(
      folder_path, hmm_model_name, mid_window, mid_step
  ):
      """
      This function trains a HMM model for segmentation-classification using
      a where WAV files and .segment (ground-truth files) are stored
      ARGUMENTS:
       - folder_path:     the path of the data diretory
       - hmm_model_name:  the name of the HMM model to be stored
       - mt_win:          mid-term window size
       - mt_step:         mid-term window step
      RETURNS:
       - hmm:            an object to the resulting HMM
       - class_names:    a list of class_names
  
      After training, hmm, class_names, along with the mt_win
      and mt_step values are stored in the hmm_model_name file
      """
  
      flags_all = np.array([])
      class_names_all = []
      for i, f in enumerate(glob.glob(folder_path + os.sep + "*.wav")):
          # for each WAV file
          wav_file = f
          gt_file = f.replace(".wav", ".segments")
          if os.path.isfile(gt_file):
              seg_start, seg_end, seg_labs = read_segmentation_gt(
                  gt_file
              )
              flags, class_names = segments_to_labels(
                  seg_start, seg_end, seg_labs, mid_step
              )
              for c in class_names:
                  # update class names:
                  if c not in class_names_all:
                      class_names_all.append(c)
              sampling_rate, signal = audioBasicIO.read_audio_file(
                  wav_file
              )
              feature_vector, _, _ = mtf.mid_feature_extraction(
                  signal,
                  sampling_rate,
                  mid_window * sampling_rate,
                  mid_step * sampling_rate,
                  round(sampling_rate * 0.050),
                  round(sampling_rate * 0.050),
              )
  
              flag_len = len(flags)
              feat_cols = feature_vector.shape[1]
              min_sm = min(feat_cols, flag_len)
              feature_vector = feature_vector[:, 0:min_sm]
              flags = flags[0:min_sm]
  
              flags_new = []
              # append features and labels
              for j, fl in enumerate(flags):
                  flags_new.append(
                      class_names_all.index(class_names_all[flags[j]])
                  )
  
              flags_all = np.append(flags_all, np.array(flags_new))
  
              if i == 0:
                  f_all = feature_vector
              else:
                  f_all = np.concatenate(
                      (f_all, feature_vector), axis=1
                  )
  
      # compute HMM statistics
      (
          class_priors,
          transmutation_matrix,
          means,
          cov,
      ) = train_hmm_compute_statistics(f_all, flags_all)
      # train the HMM
      hmm = hmmlearn.hmm.GaussianHMM(class_priors.shape[0], "diag")
      hmm.covars_ = cov
      hmm.means_ = means
      hmm.startprob_ = class_priors
      hmm.transmat_ = transmutation_matrix
  
      save_hmm(
          hmm_model_name, hmm, class_names_all, mid_window, mid_step
      )
  
      return hmm, class_names_all
  
imports: from __future__ import print_function
  import os
  import csv
  import glob
  import scipy
  import sklearn
  import numpy
  import hmmlearn.hmm
  import sklearn.cluster
  import pickle
  import matplotlib.pyplot
  from scipy.spatial import distance
  import sklearn.discriminant_analysis
  from sklearn.preprocessing import StandardScaler
  import sys
  import pyAudioAnalysis.audioBasicIO
  import pyAudioAnalysis.audioTrainTest
  import pyAudioAnalysis.MidTermFeatures
  import pyAudioAnalysis.ShortTermFeatures
label_desc: Use image node_id 11 for calling a global function with example usage: train_hmm_from_directory(folder_path, hmm_model_name, mid_window, mid_step) and returns: hmm,  class_names_all