Python-Udacity的自驾车课程使用机器学习和计算机视觉技术进行车辆检测

  • C8_805069
    了解作者
  • 61.5MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-03 09:32
    上传日期
Udacity的自驾车课程使用机器学习和计算机视觉技术进行车辆检测
Python-Udacity的自驾车课程使用机器学习和计算机视觉技术进行车辆检测.zip
  • vehicle-detection-master
  • output
  • output_23_2.png
    119.9KB
  • output_15_0.png
    527.3KB
  • output_13_1.png
    813.6KB
  • output_9_3.png
    42.9KB
  • output_9_1.png
    49.1KB
  • output_17_0.png
    568.4KB
  • output_21_1.png
    119.9KB
  • top.png
    422.2KB
  • output_17_4.png
    469.2KB
  • output_9_2.png
    42.7KB
  • output_17_1.png
    564.8KB
  • output_17_5.png
    483.1KB
  • output_17_3.png
    487.4KB
  • output_7_0.png
    22.1KB
  • output_13_2.png
    709.6KB
  • output_17_2.png
    545.2KB
  • output_19_1.png
    4.5KB
  • output_9_4.png
    42.9KB
  • test_images
  • test2.jpg
    170.1KB
  • test1.jpg
    212.1KB
  • test6.jpg
    226.7KB
  • test4.jpg
    196.5KB
  • test3.jpg
    144.1KB
  • test5.jpg
    238.2KB
  • test_series
  • series2.jpg
    1.2MB
  • series5.jpg
    1MB
  • series1.jpg
    1.2MB
  • series6.jpg
    1MB
  • series3.jpg
    1.1MB
  • series4.jpg
    1MB
  • project_video.mp4
    24.1MB
  • result.mp4
    17.4MB
  • vehicle-detection.ipynb
    7.5MB
  • requirements.txt
    190B
  • README.md
    27KB
  • test_video.mp4
    782.4KB
  • .gitignore
    508B
内容介绍
# Vehicle Detection [Video demo](https://www.youtube.com/watch?v=I71cHXfSWxw&feature=youtu.be) ![png](./output/top.png) ## Overview Vehicle detection using these machine learning and computer vision techniques. * Linear SVM * HOG(Histogram of Oriented Gradients) feature extraction * Color space conversion * Space binning * Histogram of color extraction * Sliding Window ## Note First, you need to get training data(cars and not-cars). You can get car images from [GTI vehicle image database](http://www.gti.ssr.upm.es/data/Vehicle_database.html), [KITTI vision benchmark](http://www.cvlibs.net/datasets/kitti/)). And over 1500 images per each is good for this project. ## Dependencies * Python >= 3.4 ## Set up environment ``` pip install -r requirements.txt ``` ## Run jupyter notebook ``` jupyter notebook ``` ## Defining utility functions ```python import glob import time import cv2 import numpy as np import matplotlib.image as mpimg import matplotlib.pyplot as plt from skimage.feature import hog from sklearn.model_selection import train_test_split from sklearn.svm import LinearSVC from sklearn.preprocessing import StandardScaler from skimage.feature import hog %matplotlib inline ``` ```python # a function to extract features from a list of images def extract_features(imgs, color_space='RGB', spatial_size=(32, 32), hist_bins=32, orient=9, pix_per_cell=8, cell_per_block=2, hog_channel=0, spatial_feat=True, hist_feat=True, hog_feat=True): # Create a list to append feature vectors to features = [] # Iterate through the list of images for file in imgs: file_features = [] # Read in each one by one image = mpimg.imread(file) # apply color conversion if other than 'RGB' if color_space != 'RGB': if color_space == 'HSV': feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV) elif color_space == 'LUV': feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2LUV) elif color_space == 'HLS': feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) elif color_space == 'YUV': feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2YUV) elif color_space == 'YCrCb': feature_image = cv2.cvtColor(image, cv2.COLOR_RGB2YCrCb) else: feature_image = np.copy(image) if spatial_feat == True: spatial_features = bin_spatial(feature_image, size=spatial_size) file_features.append(spatial_features) if hist_feat == True: # Apply color_hist() hist_features = color_hist(feature_image, nbins=hist_bins) file_features.append(hist_features) if hog_feat == True: # Call get_hog_features() with vis=False, feature_vec=True if hog_channel == 'ALL': hog_features = [] for channel in range(feature_image.shape[2]): hog_features.append(get_hog_features(feature_image[:,:,channel], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True)) hog_features = np.ravel(hog_features) else: hog_features = get_hog_features(feature_image[:,:,hog_channel], orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True) # Append the new feature vector to the features list file_features.append(hog_features) features.append(np.concatenate(file_features)) # Return list of feature vectors return features def get_hog_features(img, orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True): # Call with two outputs if vis==True if vis == True: features, hog_image = hog(img, orientations=orient, pixels_per_cell=(pix_per_cell, pix_per_cell), cells_per_block=(cell_per_block, cell_per_block), transform_sqrt=False, visualise=vis, feature_vector=feature_vec) return features, hog_image # Otherwise call with one output else: features = hog(img, orientations=orient, pixels_per_cell=(pix_per_cell, pix_per_cell), cells_per_block=(cell_per_block, cell_per_block), transform_sqrt=False, visualise=vis, feature_vector=feature_vec) return features def bin_spatial(img, size=(32, 32)): color1 = cv2.resize(img[:,:,0], size).ravel() color2 = cv2.resize(img[:,:,1], size).ravel() color3 = cv2.resize(img[:,:,2], size).ravel() return np.hstack((color1, color2, color3)) def color_hist(img, nbins=32): #bins_range=(0, 256) # Compute the histogram of the color channels separately channel1_hist = np.histogram(img[:,:,0], bins=nbins) channel2_hist = np.histogram(img[:,:,1], bins=nbins) channel3_hist = np.histogram(img[:,:,2], bins=nbins) # Concatenate the histograms into a single feature vector hist_features = np.concatenate((channel1_hist[0], channel2_hist[0], channel3_hist[0])) # Return the individual histograms, bin_centers and feature vector return hist_features ``` ## Collecting data ```python # Get image file names images = glob.glob('./training-data/*/*/*.png') cars = [] notcars = [] all_cars = [] all_notcars = [] for image in images: if 'nonvehicle' in image: all_notcars.append(image) else: all_cars.append(image) # Get only 1/5 of the training data to avoid overfitting for ix, notcar in enumerate(all_notcars): if ix % 5 == 0: notcars.append(notcar) for ix, car in enumerate(all_cars): if ix % 5 == 0: cars.append(car) car_image = mpimg.imread(cars[5]) notcar_image = mpimg.imread(notcars[0]) def compare_images(image1, image2, image1_exp="Image 1", image2_exp="Image 2"): f, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3)) f.tight_layout() ax1.imshow(image1) ax1.set_title(image1_exp, fontsize=20) ax2.imshow(image2) ax2.set_title(image2_exp, fontsize=20) plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.) compare_images(car_image, notcar_image, "Car", "Not Car") ``` ![png](./output/output_7_0.png) ## Extracting features ```python color_space = 'YUV' # Can be RGB, HSV, LUV, HLS, YUV, YCrCb orient = 15 # HOG orientations pix_per_cell = 8 # HOG pixels per cell cell_per_block = 2 # HOG cells per block hog_channel = "ALL" # Can be 0, 1, 2, or "ALL" spatial_size = (32, 32) # Spatial binning dimensions hist_bins = 32 # Number of histogram bins spatial_feat = True # Spatial features on or off hist_feat = True # Histogram features on or off hog_feat = True # HOG features on or off converted_car_image = cv2.cvtColor(car_image, cv2.COLOR_RGB2YUV) car_ch1 = converted_car_image[:,:,0] car_ch2 = converted_car_image[:,:,1] car_ch3 = converted_car_image[:,:,2] converted_notcar_image = cv2.cvtColor(notcar_image, cv2.COLOR_RGB2YUV) notcar_ch1 = converted_notcar_image[:,:,0] notcar_ch2 = converted_notcar_image[:,:,1] notcar_ch3 = converted_notcar_image[:,:,2] car_hog_feature, car_hog_image = get_hog_features(car_ch1, orient, pix_per_cell, cell_per_block, vis=True, feature_vec=True) notcar_hog_feature, notcar_hog_image = get_hog_features(notcar_ch1, orient, pix_per_cell, cell_per_block, vis=True, feature_vec=True) car_ch1_features = cv2.resize(car_ch1, spatial_size) car_ch2_features = cv2.resize(car_ch2, spatial_size) car_ch3_features = cv2.resize(car_ch3, spatial_size) notcar_ch1_features = cv2.resize(notca
评论
    相关推荐