Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

opencv 3.2 python svm problem

hi , i have trouble with predict hog image data with SVM

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_nang/(2np.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))

click to hide/show revision 2
No.2 Revision

updated 2017-05-23 11:24:35 -0600

berak gravatar image

opencv 3.2 python svm problem

hi , i ,i have trouble with predict hog image data with SVM

import cv2
import numpy as np

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_nang/(2np.pi)) 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)

y=svm.predict(test1) print y

print y

results ######results for any image gives below one

one (0.0, array([[ 0.]], dtype=float32))

dtype=float32))

opencv 3.2 python svm problem

hi ,i ,I have trouble with predict hog image data with SVMSVM.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/tutorial_py_svm_opencv.html

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))