简体   繁体   中英

NameError: global name 'numpy' is not defined

I am trying to write a feature extractor by gathering essentia's (a MIR library) functions. The flow chart is like: individual feature extraction, pool, PoolAggregator, concatenate to form the whole feature list from poolAggregator using np.concatenate

The script runs well under ipython notebook even without importing numpy. I'm just congregating the array or float number I got from the previous stage but the error message: "NameError: global name 'numpy' is not defined" shows.

I've tried to put "import numpy as np" at the top of the module:

import numpy as np
def featureExtractor(path):

or in the function:

def featureExtractor(path):
    import numpy as np

or outside the module in the main file:

import numpy as np
from featureExtractor import featureExtractor

None of these can solve it, please help me.

The following is the script:

from essentia.standard import *
import essentia

def featureExtractor(path):
    loader = MonoLoader(filename = path)
    x = loader()

    pool = essentia.Pool()
    spectrum = Spectrum()
    w = Windowing(type = 'hann')
    # Create needed objects
    mfcc = MFCC()
    centroid = Centroid()

    for frame in FrameGenerator(x, frameSize = 1024, hopSize = 512):    

        mfcc_bands, mfcc_coeffs = mfcc(spectrum(w(frame))) # output: vector_real
        spec_centroid = centroid(spectrum(w(frame))) # output: real

        pool.add('lowlevel.mfcc', mfcc_coeffs)
        pool.add('lowlevel.centroid', spec_centroid)

    aggrPool = PoolAggregator(defaultStats = [ 'mean', 'var' ])(pool) 
    # calculate mean and var for each feature

    # build a feature vector of type array
    list = ['lowlevel.centroid.mean', 'lowlevel.centroid.var',
            'lowlevel.mfcc.mean', 'lowlevel.mfcc.var']

    feature_vec = []

    for name in list:
        feature = aggrPool[name]
        if type(feature) != float:  # for those type == array
           feature_vec = np.concatenate([feature_vec,feature], axis = 0)
        else: # for those type == float
           feature_vec.append(feature)
    return feature_vec

Then I command in the main file:

path = "/~/Downloads/~.wav"
from featureExtractor import featureExtractor
featureExtractor(path)

I got the error:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-109-40b5bbac9b17> in <module>()
      1 from featureExtractor import featureExtractor
      2 
----> 3 featureExtractor(path)

/~/ipython_notebook/featureExtractor.py in featureExtractor(path)
     66         for name in list:
     67                 feature = aggrPool[name]
---> 68         if type(feature) != float:  # for those type == array
     69                 feature_vec = np.concatenate([feature_vec,feature], axis = 0)
     70         else: # for those type == float

NameError: global name 'numpy' is not defined

And I got the same error no matter where I put the command (as described in above)

import numpy as np

Try simply

import numpy

at the top of the file /~/ipython_notebook/featureExtractor.py

It seems that your code expect numpy and not np as the module name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM