这个项目名字叫car_detector,其中用到技术包括“梯度直方图(HOG)、随机森林分类器、图像金字塔、滑动窗口、非极大值抑制”

第一步模型训练:
1.载入8500张汽车样本以及8500张非汽车样本
2.提取所有样本的HOG特征、颜色特征、spatial binning特征(HOG特征)
3.将80%的汽车和非汽车样本用作训练,剩余的20%的样本用作测试模型的精准度(随机森林分类)
4.将训练出来的模型使用pickle库保存起来,方便侦测程序调用(模型的准确度大概为98%)
train_model.py
#!/usr/local/bin/python
#-*-coding:utf-8-*-
import cv2
import numpy as np
from os import walk
from os.path import join
import pickle
import random
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier,GradientBoostingClassifier
from sklearn.tree import DecisionTreeClassifier
POS_PATH = './data/vehicles'
NEG_PATH = './data/non-vehicles'
POS_SAMPLES = 8500
NEG_SAMPLES = 8500
TRAIN_TEST_RATIO = 0.8
MODEL_PATH = './car_detector_svm'
SCALER_PATH = './car_detector_scaler'
def get_files(folder):
image_paths = []
for root, dirs, files in walk(folder):
image_paths.extend([join(root, f) for f in files])
return image_paths
def save_pickle(data, path):
with open(path, 'wb+') as f:
pickle.dump(data, f)
def load_pickle(path):
with open(path, 'rb') as f:
return pickle.load(f)
def get_hog_descriptor():
winSize = (64,64)
blockSize = (16,16)
blockStride = (8,8)
cellSize = (8,8)
nbins = 20
derivAperture = 1
winSigma = -1.
histogramNormType = 0
L2HysThreshold = 0.2
gammaCorrection = True
nlevels = 64
signedGradients = False
return cv2.HOGDescriptor(winSize,
blockSize,
blockStride,
cellSize,
nbins,
derivAperture,
winSigma,
histogramNormType,
L2HysThreshold,
gammaCorrection,
nlevels,
signedGradients)
def extract_feature_vector(roi, hog):
if roi.shape[0] != 64 or roi.shape[1] != 64:
roi = cv2.reshape(roi, (64,64), interpolation=cv2.INTER_AREA)
feature_vector = np.array([])
# hog
feature_vector = np.hstack((feature_vector, h

&spm=1001.2101.3001.5002&articleId=100299376&d=1&t=3&u=a73b21fad01947cd921f9c3a470a7682)
3707

被折叠的 条评论
为什么被折叠?



