Python OpenCV响应不受支持

2024-04-23 18:07:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用OpenCV和Python编写一个词组分析器。我已经能够从图像中提取信息,但问题是训练部分。作为基线,我使用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


Tags: pathimglabelslenifnpnotcv2