Ask Your Question

lanka's profile - activity

2017-05-24 01:26:14 -0600 commented question opencv 3.2 python svm problem

how can we use SVM in OpenCV 3.2 any simple code example for binary classification(two categories)??

2017-05-24 01:20:23 -0600 commented question opencv 3.2 python svm problem

no.i didn't how can I use that for this??

2017-05-24 01:10:39 -0600 commented question opencv 3.2 python svm problem

any error in above code explanation??

2017-05-24 00:35:58 -0600 commented question opencv 3.2 python svm problem

yes i have updated for img=cv2.imread('test.jpg',1) to img=cv2.imread('test.jpg',0) and tested again...not working yet.

2017-05-24 00:23:51 -0600 commented question opencv 3.2 python svm problem

this is how i made SVM data file...yes it is a grayscale image

positive_path='E:/work/2017/SVM/pos'
negative_path='E:/work/2017/SVM/neg'
 Get positive samples
for filename in glob.glob(os.path.join(positive_path, '*.jpg')):
    img = cv2.imread(filename, 0)
    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, 0)
    hist = hog(img)
    samples.append(hist)
    labels.append(0)

samples = np.float32(samples)
labels = np.array(labels)

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(samples, cv2.ml.ROW_SAMPLE,labels)
svm.save('svm_data.dat')
2017-05-23 12:41:37 -0600 commented question opencv 3.2 python svm problem

sorry...my question is ..i have trained svm model using negative and positive colour image data.size 100*80. for that, i have used (http://docs.opencv.org/3.1.0/dd/d3b/t...) this example is given in website (little modified for my purpose..I think its fine). then save to data file. after reading a random image from my image collection(same size colour image)..predict with svm.predict function to get label result..problem is its always given for the zero(0) for all positives and negative samples. this is the problem I have faced.

2017-05-23 12:30:11 -0600 received badge  Editor (source)
2017-05-23 12:16:08 -0600 commented question opencv 3.2 python svm problem

actually, i need predict my input image(test.jpg) using SVM model...but always its gives the same answer((0.0, array([[ 0.]], dtype=float32)).

2017-05-23 11:22:12 -0600 asked a question opencv 3.2 python svm problem

hi ,I have trouble with predict hog image data with SVM.i have trained my own model using two image categories.and labelled as 0 and 1. created svm_data.dat file based on this example http://docs.opencv.org/3.1.0/dd/d3b/t...

import cv2
import numpy as np


SZ=20
bin_n = 16 # Number of bins
affine_flags = cv2.WARP_INVERSE_MAP|cv2.INTER_LINEAR
def hog(img):
    gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
    gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
    mag, ang = cv2.cartToPolar(gx, gy)
    bins = np.int32(bin_n*ang/(2*np.pi))    # quantizing binvalues in (0...16)
    bin_cells = bins[:10,:10], bins[10:,:10], bins[:10,10:], bins[10:,10:]
    mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
    hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
    hist = np.hstack(hists)     # hist is a 64 bit vector
    return hist
svm=cv2.ml.SVM_load('svm_data.dat') // train data
img=cv2.imread('test.jpg',1)
test=hog(img)
test1 = np.float32(test).reshape(-1,64)
y=svm.predict(test1)

print y

######results for any image gives below one
(0.0, array([[ 0.]], dtype=float32))