利用“HOG特征+随机森林分类器”制作汽车检测程序(完整源码下载)

这个项目名字叫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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值