OPENCV - 在opencv c ++中训练的python中使用svm文件是可能的吗?

时间:2015-02-26 00:11:00

标签: python opencv numpy svm

我使用c ++ opencv训练了一个人脸识别系统。我做了一些测试,识别在c ++代码中工作得非常好,所以我用这个训练保存了一个文件。当我尝试在python上加载此文件时,预测始终为0.0。我是python和numpy数组的新手。下面我将用c ++编写代码(可以正常工作),然后在python中编写加载图像并应用预测的代码。

  

C ++(运作良好)

Mat face;
face = frame( faceRect );
// Convert to grayscale
cvtColor( face, face, COLOR_BGR2GRAY );
// Equalize hist
equalizeHist( face, face );
// Resize to 60x60
resize( face, face, samplesSize );
// Convert to CV_32FC1
face.convertTo( face, CV_32FC1 );
// Reshape to a row vector, 1x3600
face = face.reshape( 1, 1 );
// Make prediction
float prediction = svm.predict( face, true );
cout << "Predicao: " << prediction << endl;    
  

的Python:

def faceRecogntion( self, faceRoi):
    # 1- Convert to grayscale
    grayface = cv2.cvtColor(faceRoi, cv2.COLOR_BGR2GRAY)
    # 2- Equalize hist
    cv2.equalizeHist( grayface, grayface)
    # 3 - Resize it to 60x60
    dim = ( 60, 60)
    grayface = cv2.resize(grayface, dim, interpolation = cv2.INTER_AREA)
    print 'Image size: ' + str(grayface.shape)
    # 4 - Convert to 32F
    faceFloat = np.array(grayface, dtype=np.float32);
    faceFloat = faceFloat.reshape( -1, 3600)
    # 5- Apply prediction
    result = self.svm.predict( faceFloat )
    print result
    cv2.waitKey(0)
    return

谢谢;)

1 个答案:

答案 0 :(得分:0)

在C ++中,您将returnDFVal设置为predict()函数内的true,在这种情况下,如果您的问题是2级分类,则该方法返回签名距离的决策函数值在边缘,你应该尝试修改预测:

faceFloat = np.float32(grayface).reshape(-1, 3600)
result = self.svm.predict(faceFloat, True)

P.S .: C ++中的face是否包含与python中faceFloat相同的元素(顺序相同)?