def __init__(self, rate, data, filename): self.filename = filename shp = data.shape if len(shp) > 1 and shp[0] > shp[1]: data = data.T data = wnl.lib_to_mono(data) data = data[: rate * 30] self.rate = rate self.data = data self.bvratio = wnl.beat_variance_ratio(rate, data) self.silence_ratio = wnl.silence_ratio(rate, data) self.mfcc, self.mfcc_delta = wnl.librosa_mfcc_delta(rate, data) self.chromagram = wnl.librosa_chromagram(rate, data) self.spectroid = wnl.spec_centroid(rate, data)
def get_feature_choice_cmd(ftr=None, ftr_sel=None, path=None, cls=None, win_len=None): if ftr is None: print("Please select the feature (Separated by | for multiple):") print("1. Beat Variance Ratio") print("2. Silence Ratio") print("3. MFCC") print("4. MFCC Delta") print("5. Chromagram") print("6. Spectroid") print("7. FFT average over 1s window") print("8. ZCR over window") print("9. MFCC over window") ftr = input() # ftr = [int(x) for x in ftr.split('|')] ftr = [opts.split('$')[0] for opts in ftr.split('|')] ftr_sel = {opts.split('$')[0]: opts.split('$')[1] for opts in ftr.split('|')} if path is None: path = input("Enter path to features: \n") if cls is None: cls = ["Entertainment", "Music", "Comedy", "Film & Animation", "News & Politics", "Sports", "People & Blogs", "Howto & Style", "Pets & Animals"] if win_len is None: win_len = 0.04 start = True # features = np.empty(shape=(0, 0)) features = {} f_max_len = {} classes = {} # Path is an array containing potentially multiple features files that can be used to load Video objects from disk. for p in path: with open(p + SHORT_FEATS, "rb") as inp: unpickle = pkl.Unpickler(inp) count = 0 # Create the UnPickler object and loop until there are no objects left in the file. Break from loop then. while True: try: cur_feature = {} vid = unpickle.load() # If video is in the approved class list add all selected features to a dictionary cur_feature if vid.get_category_from_name() in cls: count += 1 if 1 in ftr: cur_feature[1] = vid.bvratio if 2 in ftr: cur_feature[2] = vid.silence_ratio if 3 in ftr: cur_feature[3] = np.array(vid.mfcc).reshape((1, -1))[0] if 4 in ftr: cur_feature[4] = np.array(vid.mfcc_delta).reshape((1, -1))[0] if 5 in ftr: cur_feature[5] = np.array(vid.chromagram).reshape((1, -1))[0] if 6 in ftr: cur_feature[6] = vid.spectroid[0] if 7 in ftr: cur_feature[7] = vid.get_windowed_fft(int(np.ceil(vid.rate * win_len))) if 8 in ftr: cur_feature[8] = vid.get_windowed_zcr(int(np.ceil(vid.rate * win_len))) if 9 in ftr: cur_feature[9] = np.array(wnl.get_window_mfcc(vid.mfcc, int(np.ceil(vid.rate * win_len)))) \ .reshape((1, -1))[0] # This section was designed under the assumption that the features could be returned in various # 2d layouts. It essentially checks the size of the current feature against the largest # number of columns so far. It then pads the smaller one with 0's # This can definitely be refactored into simpler, more readable code. if start: for i in ftr: features[i] = np.array([cur_feature[i]]) f_shape = features[i].shape if hasattr(cur_feature[i], "__len__"): if len(f_shape) > 1: f_max_len[i] = f_shape[1] else: f_max_len[i] = len(f_shape) start = False # classes = np.array(vid.get_category_from_name()) classes[i] = [vid.get_category_from_name()] else: for i in ftr: if hasattr(cur_feature[i], "__len__"): if len(cur_feature[i].shape) > 1: if cur_feature[i].shape[1] > f_max_len[i]: if len(features[i].shape) > 1: features[i] = np.pad(features[i], ((0, 0), (0, cur_feature[i].shape[1] - f_max_len[i])), mode="constant") f_max_len[i] = cur_feature[i].shape[1] else: features[i] = np.pad(features[i], (0, cur_feature[i].shape[1] - f_max_len[i]), mode="constant") f_max_len[i] = cur_feature[i].shape[1] elif cur_feature[i].shape[1] < f_max_len[i]: cur_feature[i] = np.pad(cur_feature[i], ((0, 0), (0, f_max_len[i] - cur_feature[i].shape[1])), mode="constant") elif len(cur_feature[i].shape) == 1: if cur_feature[i].shape[0] > f_max_len[i]: if len(features[i].shape) > 1: features[i] = np.pad(features[i], ((0, 0), (0, cur_feature[i].shape[0] - f_max_len[i])), mode="constant") f_max_len[i] = cur_feature[i].shape[0] else: features[i] = np.pad(features[i], (0, cur_feature[i].shape[0] - f_max_len[i]), mode="constant") f_max_len[i] = cur_feature[i].shape[0] elif cur_feature[i].shape[0] < f_max_len[i]: cur_feature[i] = np.pad(cur_feature[i], (0, f_max_len[i] - cur_feature[i].shape[0]), mode="constant") features[i] = np.vstack((features[i], [cur_feature[i]])) # classes = np.append(classes, [vid.get_category_from_name()]) classes[i].append(vid.get_category_from_name()) except EOFError: print("EOF") break except TypeError: print("Unable to load object") except pkl.UnpicklingError: print("Unable to load object2") gc.collect() # Join each feature into one large array. # Keep track of the indices for each feature that needs reduction applied later select_ind = [] print("Count = ", count) total_feature = features[ftr[0]] if ftr_sel[ftr[0]] > 0: select_ind = [(0, len(total_feature[0]), ftr_sel[ftr[0]])] for i in range(1, len(ftr)): if ftr_sel[ftr[i]] > 0: start = len(total_feature[0]) select_ind.append((start, start + len(features[ftr[i]][0]), ftr_sel[ftr[i]])) total_feature = np.hstack((total_feature, features[ftr[i]])) # targets = np.array(classes) targets = [value for key, value in classes.items()] return total_feature, targets[0], select_ind
def get_windowed_zcr(self, block_length): return wnl.get_windowed_zcr(self.data, block_length)