svm.predict方法上的函数'cv :: ml :: SVMImpl :: predict'错误中的samples.cols == var_count && samples.type()== 5

时间:2019-02-05 20:47:05

标签: python-3.x svm opencv3.0

我正在使用svm在opencv python中创建对象分类器。训练数据集包含200个正像和200个负像。对于正图像,首先拍摄200张图像,然后从图像中裁剪目标对象,然后将其调整为(64,128)大小以进行HOG计算。然后对于负图像,首先创建图像金字塔,然后应用64X128的滑动窗口,然后为正图像以及带有标签1和0的负图像的所有窗口计算HOG。在生猪特征上训练了svm模型。 我收到错误信息“ cv2.error:OpenCV(3.4.2)C:\ projects \ opencv-python \ opencv \ modules \ ml \ src \ svm.cpp:2010:错误:(-215:断言失败)当我使用res = svm调用预测函数时,函数'cv :: ml :: SVMImpl :: predict '“中的samples.cols == var_count && samples.type()== 5在函数'cv :: ml :: SVMImpl :: predict '” .predict(samples [0])。ravel()方法。

import cv2
import os
import time
import numpy as np
import imutils

positive_path='C:\\Users\\Admin\\3D Objects\\datqaet with hog and svm\\ROI images'
negative_path='C:\\Users\\Admin\\3D Objects\\datqaet with hog and svm\\Negative images'

def pyramid(img):     #Create image Pyramid
    minSize=(30, 30)
    imgarr = []
    while True:
        scale = 2
        imgarr.append(img)
        w = int(img.shape[1] / scale)
        img = imutils.resize(img, width=w)
        if img.shape[0] < minSize[1] or img.shape[1] < minSize[0]:
            break

    return imgarr


def sliding_window(image, stepSize, windowSize):  #Sliding window for negative images
    sliding = []
    for y in range(0, image.shape[0], stepSize):
        for x in range(0, image.shape[1], stepSize):
             sliding.append((x, y, image[y:y + windowSize[1], x:x + windowSize[0]]))
    return sliding


def get_hog() : 
    winSize = (64,128)
    blockSize = (16,16)
    blockStride = (16,16)
    cellSize = (8,8)
    nbins = 9
    derivAperture = 1
    winSigma = 4.
    histogramNormType = 0
    L2HysThreshold = 0.2
    gammaCorrection = 0
    nlevels = 64
    signedGradient = True
    hog = cv2.HOGDescriptor(winSize,blockSize,blockStride,cellSize,nbins,derivAperture,winSigma,histogramNormType,L2HysThreshold,gammaCorrection,nlevels, signedGradient)
    return hog

samples = []
labels = []
sam = []
hog = get_hog()
for filename in os.listdir(positive_path):
    img = cv2.imread(os.path.join(positive_path,filename),0)   #RGB image
    img = cv2.resize(img,(64,128))
    img = np.array(img)
    hist = hog.compute(img)
    hist = cv2.normalize(hist,None)
    sam.append(img)
    samples.append(hist)
    labels.append(1)
i=0
for filename in os.listdir(negative_path):
    img = cv2.imread(os.path.join(negative_path,filename),0)
    (winW, winH) = (64,128)
    pyr  = pyramid(img)
    for resized in pyr:
        sliding = sliding_window(resized, stepSize=32, windowSize=(winW, winH))
        for (x, y, window) in sliding:
            if window.shape[0] != winH or window.shape[1] != winW:
                continue      
            hist = hog.compute(window)
            hist = cv2.normalize(hist,None)
            sam.append(window)
            samples.append(hist)
            labels.append(0)
    print(i)
    i=i+1


samples = np.array(samples,dtype=np.float32)
labels =  np.array(labels,dtype=int)
samples = np.squeeze(samples)
print(len(samples))
print(samples.shape)
rand = np.random.RandomState(10)
shuffle = rand.permutation(len(samples))
sam = samples[shuffle]
samples = sam[shuffle]
labels =  labels[shuffle]

svm = cv2.ml.SVM_create()

svm.setKernel(cv2.ml.SVM_LINEAR)
svm.setType(cv2.ml.SVM_C_SVC)
svm.setC(2.67)
svm.setGamma(5.383)
svm_params = dict( kernel_type = cv2.ml.SVM_LINEAR,
                    svm_type = cv2.ml.SVM_C_SVC,
                    C=2.67, gamma=5.383 )


svm.train(samples,cv2.ml.ROW_SAMPLE,labels)
print("trained")
res = svm.predict(samples[0]).ravel()
print(res)
cap = cv2.VideoCapture(0)
while True:
    ret, img = cap.read()
    img=cv2.resize(img,(400,400))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    (winW, winH) = (64,128)
    pyr  = pyramid(img)
    for resized in pyr:
        sliding = sliding_window(resized, stepSize=32, windowSize=(winW, winH))
        for (x, y, window) in sliding:
            if window.shape[0] != winH or window.shape[1] != winW:
                continue      
            hist = hog.compute(window)
            hist = cv2.normalize(hist,None)
            hist = np.reshape(hist,(1,hist.shape[0]))
            res = svm.predict(hist)[1].ravel()
            if res == 1:
                print("found")
            cv2.imshow('img',img)
            cv2.waitKey(10)

0 个答案:

没有答案