convert numpy array

asked 2017-04-17 10:50:11 -0500

krisscer gravatar image

updated 2017-04-23 03:52:14 -0500

berak gravatar image

Hi I am loading an image from a file and i need to convert it from an numpy array to either a dictionary , list or tuple . Any suggestions pls ?

I am loading an image from a file using face = cv2.imread('faces/face.jpg') Then passing face to this method

results = classify(models,face)

def classify(models, dataSet):
    results = {}
    for trueClazz in CLASSES:
        count = 0
        correct = 0
        print("Class,Predicted Class,Problem")
        predClazz, prob = predict(models, dataSet)
        print "%s,%s,%f" % (trueClazz,predClazz, prob)
        count += 1
    print('classifying')
    return results

The error is :

  File "C:\Python27\lib\site-packages\svm.py", line 71, in gen_svm_nodearray
    raise TypeError('xi should be a dictionary, list or tuple')
TypeError: xi should be a dictionary, list or tuple
edit retag flag offensive close merge delete

Comments

could you add a code(snippet), that explains your situation, and also, the exact errormsg ?

berak gravatar imageberak ( 2017-04-17 14:58:43 -0500 )edit
  • and the predict method ?
  • this is not using opencv's SVM, or is it ?
berak gravatar imageberak ( 2017-04-23 03:53:42 -0500 )edit

def predict(models, item): maxProb = 0.0 bestClass = "" for clazz, model in models.iteritems(): prob = predictSingle(model, item) if prob > maxProb: maxProb = prob bestClass = clazz print('predicting') return (bestClass, maxProb)

def predictSingle(model, item):
output = svm_predict([0], [item], model, "-q -b 1")
prob = output[2][0][0]
print('predicting single')
return prob

This is a snippet of the predict method and the predict single method it is using SVM

krisscer gravatar imagekrisscer ( 2017-04-23 04:35:35 -0500 )edit

this looks like from the python libsvm wrapper ?

how did you train it ? i can only guess here, but probably you will have to make a list of pixels from the face image in the very same way it was done for training (you cannot use the image / numpy array directly)

berak gravatar imageberak ( 2017-04-23 05:09:53 -0500 )edit