使用Opencv和Python进行SVM训练和预测

时间:2019-08-21 19:29:11

标签: python opencv image-processing

我是OpenCv的初学者;我观看了一些教程,并进行了眼周(眼睛区域)检测,并在特定数据集上训练了SVM。

对于预测,它给出了错误;我

cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\core\src\alloc.cpp:55: error: (-4:Insufficient memory) Failed to allocate 422528400 bytes in function 'cv::OutOfMemoryError'

我搜索并尝试了许多解决方案,但未能解决我的问题。如果有人以正确的方式指导我,将不胜感激

培训文件

import cv2
import glob
import numpy as np
from sklearn import svm
samples = []
labels = []    
# Get positive samples
for filename in glob.glob("C:\\Users\\ATech\\AppData\\Local\\Programs\\Python\\Python37-32\\interface\\images\\iphone_day_light_short\\periocular_images\\*.png"):
    img = cv2.imread(filename, 1)
    hog = cv2.HOGDescriptor()
    hist = hog.compute(img)
    #hist = hog(img)
    samples.append(hist)
    labels.append(1)

# Get negative samples
for file in glob.glob("C:\\Users\\ATech\\AppData\\Local\\Programs\\Python\\Python37-32\\interface\\images\\iphone_day_light_short\\negetive_images\\*.png"):
    img = cv2.imread(file, 1)
    img = cv2.resize(img, (240, 160))
    hog = cv2.HOGDescriptor()
    hist = hog.compute(img)
    #hist = hog(img)
    samples.append(hist)
    labels.append(0)

# Convert objects to Numpy Objects
samples = np.float32(samples)
labels = np.array(labels)


# Shuffle Samples
rand = np.random.RandomState(321)
shuffle = rand.permutation(len(samples))
samples = samples[shuffle]
labels = labels[shuffle]    

# Create SVM classifier
#Retrieves all the uncompressed support vectors of a linear SVM.
svm = cv2.ml.SVM_create() #creates empty model; paramerts we set and then we train that
svm.setType(cv2.ml.SVM_C_SVC)#C-Support Vector Classification. n-class classification (n ≥ 2), allows imperfect separation of classes with penalty multiplier C for outliers.
svm.setKernel(cv2.ml.SVM_RBF) #define kernel type

#cv2.ml.SVM_LINEAR
# svm.setDegree(0.0) #It’s basically the degree of the polynomial used to find the hyperplane to split the data.
svm.setGamma(5.383) #gamma is a parameter for non linear hyperplanes
# svm.setCoef0(0.0) #when kernel is polynomial or sigmoid
svm.setC(2.67) #paneltty error
#the coefficient C affects the trade-off between complexity and proportion of nonseparable samples and must be selected by the user."
'# svm.setNu(0.0) #'
# svm.setP(0.0)
# svm.setClassWeights(None)#supply a single colummn array additional w8 factors for slack variables
#used only by c-svm classifier

# Train
svm.train(samples, cv2.ml.ROW_SAMPLE, labels) 
svm.save("svm_trained.csv")

预测文件

import cv2
import numpy as np
import imutils
from cv2 import CascadeClassifier
#import svm
svm = cv2.ml.SVM_create()
svm = cv2.ml.SVM_load("C:\\Users\\ATech\\AppData\\Local\\Programs\\Python\\Python37-32\\interface\\images\\svm_trained.xml")
classifier=CascadeClassifier("C:\\Users\\ATech\\AppData\\Local\\Programs\\Python\\Python37-32\\interface\\images\\svm_trained.xml")

sample = []
image = cv2.imread("C:\\Users\\ATech\\AppData\\Local\\Programs\\Python\\Python37-32\\interface\\images\\1.png")
#image = imutils.resize(img, width=min(400, img.shape[1]))
#image= cv2.resize(img, (240, 160))
hog = cv2.HOGDescriptor()
#hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
hist = hog.compute(image)
sample.append(hist)
sample = np.float32(sample)
#hist = cv2.resize(hist, (1, 1))
res = svm.predict(sample)
print (res)

0 个答案:

没有答案