import glob import sys PY3 = sys.version_info[0] == 3
if PY3: from functools import reduce
import numpy as np import cv2 as cv import cv2
built-in modules
import os import itertools as it from contextlib import contextmanager
samples = [] labels = []
Get positive samples
for filename in glob.glob(os.path.join("/redarrows", '*.jpg')): img = cv2.imread(filename, 1) hist = hog(img) samples.append(hist) labels.append(1)
Get negative samples
for filename in glob.glob(os.path.join("/cat", '*.jpg')): img = cv2.imread(filename, 1) 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
svm = cv2.ml.SVM_create() svm.setType(cv2.ml.SVM_C_SVC) svm.setKernel(cv2.ml.SVM_RBF) # cv2.ml.SVM_LINEAR
svm.setDegree(0.0)
svm.setGamma(5.383)
svm.setCoef0(0.0)
svm.setC(2.67)
svm.setNu(0.0)
svm.setP(0.0)
svm.setClassWeights(None)
Train
svm.train(samples, cv2.ml.ROW_SAMPLE, labels) svm.save('svm_data.dat')
When I try to compile it, it shows me the error is python arrow.py OpenCV Error: Bad argument (in the case of classification problem the responses must be categorical; either specify varType when creating TrainData, or pass integer responses) in train, file /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/ml/src/svm.cpp, line 1618 Traceback (most recent call last): File "arrow.py", line 59, in <module> svm.train(samples, cv2.ml.ROW_SAMPLE, labels) cv2.error: /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/ml/src/svm.cpp:1618: error: (-5) in the case of classification problem the responses must be categorical; either specify varType when creating TrainData, or pass integer responses in function train
Please give me some suggestion about this. I am so confused.