简体   繁体   中英

Image classification using SVM - Python

I have a set of images classified as good quality image and bad quality image. I have to train a classification model so that any new image can be classified as good/bad. SVM seems to be the best approach to do it. I have done image processing in MATLAB but not in python.

Can anyone suggest how to do it in python? What are the libraries? For SVM scikit is there, what about feature extraction of image and PCA?

I would start reading this simple tutorial and then move into the OpenCV tutorials for Python. Also, if you are familiar with the sklearn interface there is Scikit-Image .

I am using opencv 2.4,python 2.7 and pycharm

SVM is a machine learning model for data classification.Opencv2.7 has pca and svm.The steps for building an image classifier using svm is

  1. Resize each image
  2. convert to gray scale
  3. find PCA
  4. flat that and append it to training list
  5. append labels to training labels

Sample code is

for file in listing1:
 img = cv2.imread(path1 + file)
 res=cv2.resize(img,(250,250))
 gray_image = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
 xarr=np.squeeze(np.array(gray_image).astype(np.float32))
 m,v=cv2.PCACompute(xarr)
 arr= np.array(v)
 flat_arr= arr.ravel()
 training_set.append(flat_arr)
 training_labels.append(1)

Now Training

trainData=np.float32(training_set)
responses=np.float32(training_labels)
svm = cv2.SVM()
svm.train(trainData,responses, params=svm_params)
svm.save('svm_data.dat')

I think this will give you some idea.

Take a look at dlib and opencv . Both are mature computer vision frameworks implemented in C++ with python bindings. That is important because it means it is relying on compiled code under the hood so it is significantly faster than if it was done in straight python. I believe the implementation of the SVM in dlib is based on more resent research at the moment so you may want to take that into consideration as you may get better results using it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM