python - 使用 Opencv 和 Python 训练 SVM 和预测
问题描述
我是 OpenCv 的初学者;我看了一些教程,为了进行眼周(眼睛区域)检测,我在特定数据集上训练了 SVM;
对于预测,它给出错误;IE
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)
解决方案
推荐阅读
- javascript - 键值对出现在 LocalStorage 中,但尽管有 JSON stringify/setItem/etc,但在刷新后不会持续存在
- c# - C#Bitmap Rotate和Filp的原点
- vhdl - VHDL中顺序语句的速度是多少?
- javascript - 保持尾随零 Intl.numberformat
- .net - Linux dotnet 应用程序以屏幕奇怪的行为开始,或者我只是不明白它为什么会这样
- kubernetes - OCP:带有 SRIOV 接口的多容器 POD 问题
- mysql - 带有 MySQL 的 AWS EC2 未使用可用资源附近的任何地方(Big INSERT..SELECT)
- python - 合并数据框中具有空值的列
- kubernetes - 请求无法到达 Seldon 核心输入转换器
- google-apps-script - onEdit updates top row only after editing multiple rows, but not after copy paste