第一步骤:下载数据集:https://pan.baidu.com/s/1tk10m8fh_7_MT4NJ29my4g 密码:wdzr
第二步骤:编写代码,如下:
import cv2 import numpy as np from os.path import join datapath = "/home/utryjc/Pictures/TrainImages" def path(cls, i): return "%s/%s%d.pgm" % (datapath, cls, i+1) pos, neg = "pos-", "neg-" detect = cv2.xfeatures2d.SIFT_create() extract = cv2.xfeatures2d.SIFT_create() flann_params = dict(algorithm=1, tree=5) flann = cv2.FlannBasedMatcher(flann_params, {}) bow_kmeans_trainer = cv2.BOWKMeansTrainer(40) extract_bow = cv2.BOWImgDescriptorExtractor(extract, flann) def extract_sift(fn): im = cv2.imread(fn, 0) return extract.compute(im, detect.detect(im))[1] for i in range(8): bow_kmeans_trainer.add(extract_sift(path(pos, i))) bow_kmeans_trainer.add(extract_sift(path(neg, i))) voc = bow_kmeans_trainer.cluster() extract_bow.setVocabulary(voc) def bow_feature(fn): im = cv2.imread(fn, 0) return extract_bow.compute(im, detect.detect(im)) traindata, trainlabels = [], [] for i in range(20): traindata.extend(bow_feature(path(pos, i))) trainlabels.append(1) traindata.extend(bow_feature(path(neg, i))) trainlabels.append(-1) svm = cv2.ml.SVM_create() svm.train(np.array(traindata), cv2.ml.ROW_SAMPLE, np.array(trainlabels)) def predict(fn): f = bow_feature(fn) p = svm.predict(f) print fn, "\t", p[1][0][0] return p car, notcar = "/home/utryjc/Pictures/car5.jpeg", "/home/utryjc/Pictures/person2.jpeg" car_img = cv2.imread(car) notcar_img = cv2.imread(notcar) car_predict = predict(car) not_car_predict = predict(notcar) font = cv2.FONT_HERSHEY_COMPLEX if (car_predict[1][0][0] == 1.0): cv2.putText(car_img, 'Car_Detected', (10, 30), font, 1, (0, 255, 0), 2, cv2.LINE_AA) if (not_car_predict[1][0][0] == -1.0): cv2.putText(notcar_img, 'CAR NOT DETECTED', (10, 30), font, 1, (0, 0, 255), 2, cv2.LINE_AA) cv2.imshow('BOW + SVM Success', car_img) cv2.imshow('BOW + SVM Failture', notcar_img) cv2.waitKey(0) cv2.destroyAllWindows()
其结果,仅仅是检查出图片中是否有汽车。比较关键的部分
(1)创建两个SIFT实例,一个提取关键点,一个提取特征
detect = cv2.xfeatures2d.SIFT_create()
extract = cv2.xfeatures2d.SIFT_create()
(2)创建FLANN的匹配算法,第一个参数是选择算法,第二个参数为迭代次数
flann_params = dict(algorithm = 1, trees = 5)
flann = cv2.FlannBasedMatcher(flann_params, {})
(3)创建词带训练器,指定簇数为40
bow_kmeans_trainer = cv2.BOWKMeansTrainer(40)
(4)视觉的词汇将作为词带的类,调用BOWImgDescriptorExtractor方法返回描述符
extract_bow = cv2.BOWImgDescriptorExtractor(extract, flann)
(5)以灰度格式获取图像并提取图像的特征
def extract_sift(fn):
im = cv2.imread(fn, 0)
return extract.compute(im, detect.detect(im))[1]
(6)每个类从训练集中读取8张图片
for i in range(8):
bow_kmeans_trainer.add(extract_sift(path(pos, i)))
bow_kmeans_trainer.add(extract_sift(path(neg, i)))
(7)调用cluster函数并执行K-mean分类,主要是为了创建单词词汇
voc = bow_kmeans_trainer.cluster()
extract_bow.setVocabulary( voc )
(8)读取图片,返回基于磁带描述符提取器计算得到的描述符
def bow_feature(fn):
im = cv2.imread(fn, 0)
return extract_bow.compute(im, detect.detect(im))
(9)创建两个数组,分别对应训练数据和标签,并用分类器的标签填充他们,并生成相应的正负样本的标签
traindata, trainlabels = [], []
for i in range(20):
traindata.extend(bow_feature(path(pos, i)))
trainlabels.append(1)
traindata.extend(bow_feature(path(neg, i)))
trainlabels.append(-1)
(10)创建支持向量机的实例,通过将训练数据和表情按放到Numpy数组中进行训练
svm = cv2.ml.SVM_create()
svm.train(np.array(traindata), cv2.ml.ROW_SAMPLE, np.array(trainlabels))
(11)定义函数来显示预测的结果
def predict(fn):
f = bow_features(fn); #提取描述符
p = svm.predict(f) #根据描述符进行预测
print(fn, "\t", p[1][0][0])
return p
(12)定义两个样本的路径,并将路径中的文件读取出来梵高Numpy数组中,将这些图片传给训练好的SVM,并预测结果
car, notcar = "/home/utryjc/Pictures/car5.jpeg", "/home/utryjc/Pictures/person2.jpeg"
car_img = cv2.imread(car)
notcar_img = cv2.imread(notcar)
car_predict = predict(car)
not_car_predict = predict(notcar)
(13)最后在屏幕上显示预测的结果
font = cv2.FONT_HERSHEY_COMPLEX
if (car_predict[1][0][0] == 1.0):
cv2.putText(car_img, 'Car_Detected', (10, 30), font, 1, (0, 255, 0), 2, cv2.LINE_AA)
if (not_car_predict[1][0][0] == -1.0):
cv2.putText(notcar_img, 'CAR NOT DETECTED', (10, 30), font, 1, (0, 0, 255), 2, cv2.LINE_AA)