def mtFileClassification(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
    '''

    if not os.path.isfile(model_name):
        print("mtFileClassificationError: input model_type not found!")
        return (-1, -1, -1, -1)
    # Load classifier:
    if model_type == "knn":
        [classifier, MEAN, STD, class_names, mt_win, mt_step, st_win, st_step, compute_beat] = \
            aT.load_model_knn(model_name)
    else:
        [classifier, MEAN, STD, class_names, mt_win, mt_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 (-1, -1, -1, -1)
    [fs, x] = audioBasicIO.readAudioFile(input_file)       # load input file
    if fs == -1:                                           # could not read file
        return (-1, -1, -1, -1)
    x = audioBasicIO.stereo2mono(x)                        # convert stereo (if) to mono
    duration = len(x) / fs
    # mid-term feature extraction:
    [mt_feats, _, _] = aF.mtFeatureExtraction(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       # normalize current feature vector
        [res, P] = aT.classifierWrapper(classifier, model_type, cur_fv)    # classify vector
        flags_ind.append(res)
        flags.append(class_names[int(res)])              # update class label matrix
        Ps.append(numpy.max(P))                          # update probability matrix
    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) = flags2segs(flags, mt_step)
    segs[-1] = len(x) / float(fs)

    # Load grount-truth:        
    if os.path.isfile(gt_file):
        [seg_start_gt, seg_end_gt, seg_l_gt] = readSegmentGT(gt_file)
        flags_gt, class_names_gt = segs2flags(seg_start_gt, seg_end_gt, seg_l_gt, mt_step)
        flags_ind_gt = []
        for j, fl in enumerate(flags_gt):
            # "align" labels with GT
            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 = plotSegmentationResults(flags_ind, flags_ind_gt,
                                  class_names, mt_step, not plot_results)
    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 mtFileClassification ( input_file , model_name , model_type , plot_results = False , gt_file = "" ) : if not os . path . isfile ( model_name ) : print ( "mtFileClassificationError: input model_type not found!" ) return ( - 1 , - 1 , - 1 , - 1 ) if model_type == "knn" : [ classifier , MEAN , STD , class_names , mt_win , mt_step , st_win , st_step , compute_beat ] = aT . load_model_knn ( model_name ) else : [ classifier , MEAN , STD , class_names , mt_win , mt_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 ( - 1 , - 1 , - 1 , - 1 ) [ fs , x ] = audioBasicIO . readAudioFile ( input_file ) if fs == - 1 : return ( - 1 , - 1 , - 1 , - 1 ) x = audioBasicIO . stereo2mono ( x ) duration = len ( x ) / fs [ mt_feats , _ , _ ] = aF . mtFeatureExtraction ( 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 ] ) : cur_fv = ( mt_feats [ : , i ] - MEAN ) / STD [ res , P ] = aT . classifierWrapper ( classifier , model_type , cur_fv ) flags_ind . append ( res ) flags . append ( class_names [ int ( res ) ] ) Ps . append ( numpy . max ( P ) ) flags_ind = numpy . array ( flags_ind ) 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 ] ( segs , classes ) = flags2segs ( flags , mt_step ) segs [ - 1 ] = len ( x ) / float ( fs ) if os . path . isfile ( gt_file ) : [ seg_start_gt , seg_end_gt , seg_l_gt ] = readSegmentGT ( gt_file ) flags_gt , class_names_gt = segs2flags ( seg_start_gt , seg_end_gt , seg_l_gt , mt_step ) flags_ind_gt = [ ] for j , fl in enumerate ( flags_gt ) : 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 = plotSegmentationResults ( flags_ind , flags_ind_gt , class_names , mt_step , not plot_results ) 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 )
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