Codementor Events

Image Recognition with SVM and Local Binary Pattern

Published Jun 23, 2018
Image Recognition with SVM and Local Binary Pattern

Okay, Let’s we get started.

The world has been changed, human is being replaced with machine.

In this medium I’ll tell you how to make a really simple gender predictor. You can modify it with a few lines of code and make it able to do a facial recognition job that can predict your name based on images. Facial recognition has been popular in AI world, so what are you waiting for?


Here I use some library that help me a lot,

  • Flask
  • sklearn
  • matplotlib
  • Local binary pattern
  • and more

Machine only knows number and number (I think so, If you don’t mind) so we need to convert the image pixel into numbers. Use Local Binary Pattern is a good thing coz it offers a simple concept to convert image to number, though it won’t be good for further research.

LBP will divide the image to some areas and count the density of the gradient in every areas then process it to histogram.


LBP

lbp = feature.local_binary_pattern(image, self.numPoints, self.radius, method="uniform")                         
(hist, _) = np.histogram(lbp.ravel(), bins=np.arange(0, self.numPoints + 3), range=(0, self.numPoints + 2))                                                 
                       
hist = hist.astype("float")                         
hist /= (hist.sum() + eps)

Piece of my LBP code, the result will be the described histogram or we just can say it is a set of array of number. Here you can see.

The result you get from code above we can call it a dataset.

Dataset

[0.021636221875666023,0.01754288260189137,0.009927043885038529,0.007963911784350686,0.007880374248151202,0.008311984851848529,0.007031075963456462,0.009189128981943098,0.01198763644462577,0.016122744486500164,0.023543662285554212,0.038496881265261615,0.05056805524608687,0.04409389619062696,0.029669748273516275,0.023641122744453607,0.014465916685210422,0.01357484963241594,0.008311984851848529,0.010581421251934477,0.008854978837145167,0.01077634216973327,0.012377478280223356,0.019659166852278264,0.02316774337265654,0.5506237469361903]

This is an example of dataset I got from one image, I used LBP and this dataset now can be used for training. Don’t forget to label it with number label as well. I labeled 1 for male, 0 for female in example.

I won’t tell you much more about the rest of my work, so let’s we go to the predictor.

First off you should define label and data variables.

E.g i have three datasets
datas = [[dataset],[dataset],[dataset]]
label = [1,0,0] 
1= male, 0=female

sklearn will help you a lot to make a SVM predictor only a few line of code.

model = LinearSVC(C=100.0, random_state=42) model.fit(datas, label)

All is set. Your training code is ready to use, now you only need to make the testing code. Simple

testing = [dataset]

prediction = model.predict(testing)[0]

after the code was executed you will see the result based on the label in data training.

result = print(prediction)

That’s all from me. you can check at my github repo.

I have tried to deploy it on Heroku, unfortunately it doesn’t work and I have no idea how to solve it, but it works fine on your local server. https://gender-recognition.herokuapp.com/

Discover and read more posts from Burhanuddin Ahmed
get started
post commentsBe the first to share your opinion
Show more replies