使用SIFT BOW在OpenCV中支持SVM的培训

时间:2019-01-25 13:21:04

标签: computer-vision svm sift

我一直试图编写一个程序来对人脸和飞机的图像进行如下分类: 我会读取所有灰度图像并调整其大小,然后使用kmeans聚类生成词汇表,以用于BOW。 我给svm进行预测的每个测试输入最终都会预测出它是一张脸(预测= 0)。 同样是从代码中的训练数据本身获取的示例,对于svm而言,分类应该相当简单 我应该做不同的测试或培训吗?预先感谢

import glob
import cv2
import numpy as np
import glob
from sklearn.svm import SVC

def training_procedure_sift():
    images_faces = [cv2.resize(cv2.imread(file, 0),(240,159)) for file in glob.glob('training/faces/*.jpg')]
    images_airplanes = [cv2.resize(cv2.imread(file, 0),(240,159)) for file in glob.glob('training/airplanes/*.jpg')]
    sift = cv2.xfeatures2d.SIFT_create()
    flann_params = dict(algorithm = 1, trees = 5)
    matcher = cv2.FlannBasedMatcher(flann_params, {}) 
    bow_extract = cv2.BOWImgDescriptorExtractor(sift , matcher)
    bow_train = cv2.BOWKMeansTrainer(150)
    train_data, trainlabels = [], []
    for img in images_faces:
        bow_train.add(sift.compute(img, sift.detect(img))[1])
    for img in images_airplanes:
        bow_train.add(sift.compute(img, sift.detect(img))[1])
    voc = bow_train.cluster()
    bow_extract.setVocabulary(voc)
    for img in images_faces:
            train_data.extend(bow_extract.compute(img, sift.detect(img)))
            trainlabels.append(0)
    for img in images_airplanes:
            train_data.extend(bow_extract.compute(img, sift.detect(img)))
            trainlabels.append(1)

    clf = SVC(gamma='auto')
    clf.fit(np.array(train_data), np.array(trainlabels)) 

    for i in np.arange(12):
        siftkp = sift.detect(images_airplanes[i])
        bowsig = bow_extract.compute(img, siftkp)
        print(clf.predict(bowsig))

0 个答案:

没有答案