OpenCV Python不支持的响应类型

时间:2015-07-13 08:04:02

标签: python opencv

我目前正在使用OpenCV和Python编写Bag Of Words分析器。我已经能够从图像中提取信息,学习但问题是训练部分。作为基线,我正在使用this并将其翻译成Python,但我接触到了学习部分,当它应该训练分类器时,它无法说它具有不受支持的响应类型(我假设标签不在格式正确)。我已经尝试了一些但我无法让它工作。有什么想法吗?

import cv2
import os
import numpy as np


dictSize = 1000
retries = 1
flags = cv2.KMEANS_PP_CENTERS
tc = (cv2.TERM_CRITERIA_MAX_ITER, 10, 0.001)


matcher = cv2.DescriptorMatcher_create("FlannBased")
extractor = cv2.DescriptorExtractor_create("SURF")
detector = cv2.FeatureDetector_create("SURF")

bowTrainer = cv2.BOWKMeansTrainer(dictSize,tc,retries,flags)
bowDE = cv2.BOWImgDescriptorExtractor(extractor,matcher)


def extractTrainingVocabulary(path):
    global bowTrainer
    global extractor
    lst=os.listdir(path)

    for i in range(0,len(lst)):
        if lst[i][0] != ".":
            fullPath = path + lst[i]
            print "Processing Image " + fullPath
            img = cv2.imread(fullPath)
            if not (len(img) == 0):
                keypoints = detector.detect(img)

                if (len(keypoints) == 0):
                    print "Warning! Could not find any keypoints in image " + fullPath
                else:
                    # Returns 2 vars. The underscore is used to discard the first one
                    _,features = extractor.compute(img,keypoints)


                    bowTrainer.add(features)
            else:
                print "Could not read image " + fullPath

def extractBOWDescriptor(path, descriptors, labels):
    global bowTrainer
    global extractor
    lst=os.listdir(path)

    for i in range(0,len(lst)):
        if lst[i][0] != ".":
            fullPath = path + lst[i]
            print "Processing Image " + fullPath
            img = cv2.imread(fullPath)
            if not (len(img) == 0):
                keypoints = detector.detect(img)

                if (len(keypoints) == 0):
                    print "Warning! Could not find any keypoints in image " + fullPath
                else:

                    bowDescriptor = bowDE.compute(img,keypoints)
                    # descriptors.append(bowDescriptor)
                    #np.vstack((descriptors,bowDescriptor))

                    descriptors = np.vstack((descriptors,bowDescriptor))


                    #labels.append(lst[i][:-4])
                    labels = np.vstack((labels,float(lst[i][:-4])))



            else:
                print "Could not read image " + fullPath

    return labels, descriptors
def main():
    global bowDE

    # LEARN
    print "Creating Dict..."
    extractTrainingVocabulary("./testImages/")
    descriptors = bowTrainer.getDescriptors()

    print "Clustering " + str(len(descriptors)) + " features. This might take a while..."
    dictionary = bowTrainer.cluster()
    print "Done clustering"


    # EXTRACT
    size1 = 0,dictSize
    trainingData = np.zeros(size1,dtype=np.float32)
    size2 = 0,1
    labels = np.zeros(size2,dtype=np.float32)



    bowDE.setVocabulary(dictionary)
    labels,trainingData = extractBOWDescriptor("./evalImages/",trainingData,labels)


    print(trainingData)
    print(labels)

    print "Training classifier"

    size3 = len(trainingData),len(trainingData[0])
    responseData = np.zeros(size3,dtype=np.float32)






    classifier = cv2.NormalBayesClassifier()
    classifier.train(trainingData,labels)


main()

修改

根据@berak的建议,我更改了以下内容:

labels = np.vstack((labels,float(lst[i][:-4]))) - > labels = np.vstack((labels,int(lst[i][:-4])))

labels = np.zeros(size2,dtype=np.float32) - > labels = np.zeros(size2,dtype=np.int32)

不幸的是仍然失败。现在我得到以下内容:

error: (-5) There is only a single class in function cvPreprocessCategoricalResponses

0 个答案:

没有答案