Ask Your Question
0

SVM TRAINING FOR LICENSE PLATE DETECTION

asked 2016-03-25 23:59:37 -0600

Taseer gravatar image

updated 2016-03-26 01:49:01 -0600

berak gravatar image

I am currently working on License Plate recognition using custom HoG and SVM. I have the features of my image data set extracted and saved in an xml file. Here is the code:

train_list = []
response_list = []

hog = cv2.HOGDescriptor()

for i in range(2,7):
    img = cv2.imread(str(i) + "_.png")
    h = hog.compute(img)
    train_list.append(h)
    response_list.append(i)

model = cv2.SVM()
x = model.train(np.array(train_list), np.array(response_list))
model.save('trained.xml')

However, when I use SVM predict to classify if a certain image is positive or negative, I get the following error:

Input sample must have 32fC1 type in function cvPreparePredictData

Here is my code for using svm predict method:

img = cv2.imread('2.jpg',cv2.IMREAD_COLOR)
img2 = img.ravel() #conversion to a 1D matrix

svm = cv2.SVM()
svm.load('trained.xml')

svm.predict(img2)

Kindly help me resolve the above error.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-03-26 01:55:59 -0600

berak gravatar image

if you train your svm on hog features, you need the same hog features for the prediction as well !

(you can't simply throw an image at the prediction)

#
# train it:
#

hog = cv2.HOGDescriptor()

train_list = []
response_list = []   
for i in range(2,7):
    img = cv2.imread(str(i) + "_.png", cv2.IMREAD_GRAYSCALE)  ## GRAYSCALE, please!
    h = hog.compute(img)
    train_list.append(h)
    response_list.append(i)

model = cv2.SVM()
x = model.train(np.array(train_list), np.array(response_list))
model.save('trained.xml')


#
# predict:
#  
svm = cv2.SVM()
svm.load('trained.xml')

img = cv2.imread('2.jpg',cv2.IMREAD_GRAYSCALE)
h = hog.compute(img)
p = svm.predict(h)
edit flag offensive delete link more

Comments

Thank you so very much. It helped a lot !

Taseer gravatar imageTaseer ( 2016-03-26 02:46:30 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2016-03-25 23:58:42 -0600

Seen: 1,682 times

Last updated: Mar 26 '16