Ask Your Question
0

I try to use hog and svm to train my data and make it can calcify whether it's a arrow or not. I prepared two file and try to train it. When I compile it shows me these error.

asked 2019-01-20 00:21:31 -0600

updated 2019-01-20 02:33:49 -0600

berak gravatar image

.

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.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-01-20 02:30:42 -0600

berak gravatar image

updated 2019-01-20 03:44:50 -0600

error means: you need integer labels, but you have something else there.

try with:

labels = np.array(labels, np.int32) # without any type, default is np.int64 !

you also don't need random shuffeling of the samples / labels here.

edit flag offensive delete link more

Comments

it still show me this error, after I comment shuffling part and make the labels part change. 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

GGOPENCV gravatar imageGGOPENCV ( 2019-01-20 11:47:09 -0600 )edit

again, make sure you're supplying int32 labels there.

berak gravatar imageberak ( 2019-01-20 12:23:48 -0600 )edit

I did what you recommend

Convert objects to Numpy Objects

samples = np.int32(samples)

samples = np.array(samples, np.int32)

labels = np.array(labels, np.int32)

Shuffle Samples

rand = np.random.RandomState(321)

shuffle = rand.permutation(len(samples))

samples = samples[shuffle]

labels = labels[shuffle]

GGOPENCV gravatar imageGGOPENCV ( 2019-01-20 13:14:20 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-01-20 00:21:31 -0600

Seen: 374 times

Last updated: Jan 20 '19