How to prepare SVM classifier using HOG functions in OpenCV 3.0 in Python?

I want to train a new HoG head and shoulder classifier using Python bindings from OpenCV 3.x. What is my pipeline for extracting functions, training SVM and then running it in a test database?

It seems that there is such a pipeline for C ++: an SVM classifier based on HOG functions for object detection in OpenCV and here: https://github.com/DaHoC/trainHOG/wiki/trainHOG-Tutorial . For Python, there is a description of that how to extract the HOG function installed here: Get HOG image functions from OpenCV + Python? However, this only works for OpenCV 2.x because you can no longer initialize the classifier with _winSizeand other such variables. In addition, it is only for extraction functions, not for training or discovery using a recently trained classifier.

The output cv2.HOGdescriptor()has a parameter svmDetector, but I don’t know how to use it, because OpenCV 3.x does not come with Python documentation, and OpenCV 2.x only lists HoG in its GPU module, although there is a CPU implementation.

Is it possible to view the through conveyor and an explanation of some parameters?

+4
source share
1 answer

I currently have the same problem and I saw the following document from OpenCV:

OCR handwritten data using SVM

Where you can find part of your answer:

deskewed = [map(deskew,row) for row in train_cells]
hogdata = [map(hog,row) for row in deskewed]
trainData = np.float32(hogdata).reshape(-1,64)
responses = np.float32(np.repeat(np.arange(10),250)[:,np.newaxis])
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.train(trainData, cv2.ml.ROW_SAMPLE, responses)
svm.save('svm_data.dat')

That is, I work. As soon as I solved it, I will update the answer. But at this point, I hope this helps you.

.................................................. ..........................

opencv digits.py:

\ OpenCV\Sources\\

opencv SVM. opencv 3.1.

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)

svm.train(samples_train, cv2.ml.ROW_SAMPLE, labels_train)

resp = svm.predict(samples_test)[1].ravel()
print resp, labels_test

err = (labels_test != resp).mean()
print('error: %.2f %%' % (err*100))

confusion = np.zeros((10, 10), np.int32)
for i, j in zip(labels_test, resp):
    confusion[i, j] += 1
print('confusion matrix:')
print(confusion)
print()

............................................................................

, :

samples = []
labels = []    

# Get positive samples
for filename in glob.glob(os.path.join(positive_path, '*.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(negative_path, '*.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')

.

+5

All Articles