## 车辆检测
将会进行如下操作:
1. 定义图像特征提取方
2. 读入车辆和非车辆图像数据
3. 数据预处理,shuffle,标准化
4. 训练模型,使用的是svm
5. 测试模型
6. 使用滑动窗口和多尺度的图像大小在图像上检测车辆
- 滑动窗口
- 多尺度图像
- heatmap
- label
- box center
7. 检测视频中行驶的车辆
**总结:车辆检测主要是两方面的内容,1:物体识别 2:滑动窗口**
- 汽车和非汽车图片展示
```python
import glob
import matplotlib.pyplot as plt
vehicle_lable = 1
non_vehicle_label = 0
# 读入数据
vehicles_file_paths = glob.glob("./vehicles/*/*.png")
non_vehicles_file_paths = glob.glob("./non-vehicles/*/*.png")
print("汽车样本数量:" , len(vehicles_file_paths))
print("非汽车样本数量:" , len(non_vehicles_file_paths))
# 显示汽车和非汽车图片
vehicle_img = mpimg.imread(vehicles_file_paths[0])
non_vehicle_img = mpimg.imread(non_vehicles_file_paths[0])
fig, (ax1, ax2) = plt.subplots(1, 2 ,figsize=(8, 20))
ax1.imshow(vehicle_img)
ax1.set_title("vehicle")
ax2.imshow(non_vehicle_img)
ax2.set_title("non_vehicle")
plt.show()
```
![1](imgs/1.jpg)
- 提取HOG特征
> hog参数说明:
>
> orientations:每个cell直方图的方向的个数
>
> pix_per_cell:每个cell像素个数
>
> cells_per_block:每个block的cell个数
```python
import numpy as np
import cv2
import matplotlib.image as mpimg
def convert_color(img, conv):
"""
颜色空间转换
"""
return cv2.cvtColor(img, conv)
def get_hog_features(img, orient, pix_per_cell, cell_per_block, vis=False, feature_vec=True):
"""
提取图像的hog特征
"""
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),
block_norm= 'L2-Hys',
transform_sqrt=False,
visualise=vis, feature_vector=feature_vec)
return features, hog_image
else:
features = hog(img, orientations=orient,
pixels_per_cell=(pix_per_cell, pix_per_cell),
cells_per_block=(cell_per_block, cell_per_block),
block_norm= 'L2-Hys',
transform_sqrt=False,
visualise=vis, feature_vector=feature_vec)
return features
# 测试提取HOG特征
vehicle_img = mpimg.imread(vehicles_file_paths[0])
non_vehicle_img = mpimg.imread(non_vehicles_file_paths[0])
v_hog_feats,v_hog_imgs = get_hog_features(vehicle_img[:, :, 0], 9, 8, 2, vis=True, feature_vec=False)
n_hog_feats,n_hog_imgs = get_hog_features(non_vehicle_img[:, :, 0], 9, 8, 2, vis=True, feature_vec=False)
# 显示汽车和非汽车图片
vehicle_img = mpimg.imread(vehicles_file_paths[0])
non_vehicle_img = mpimg.imread(non_vehicles_file_paths[0])
fig, (ax1, ax2) = plt.subplots(1, 2 ,figsize=(8, 20))
ax1.imshow(v_hog_imgs, cmap="gray")
ax1.set_title("vehicle")
ax2.imshow(n_hog_imgs, cmap="gray")
ax2.set_title("non_vehicle")
plt.show()
```
![2](imgs/2.jpg)
- 提取空间特征和颜色直方图特征,且把之上的特征进行合并。
```python
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):
"""
颜色直方图
"""
channel1_hist = np.histogram(img[:,:,0], bins=nbins)
channel2_hist = np.histogram(img[:,:,1], bins=nbins)
channel3_hist = np.histogram(img[:,:,2], bins=nbins)
hist_features = np.concatenate((channel1_hist[0], channel2_hist[0], channel3_hist[0]))
return hist_features
def extract_img_features(img):
"""
把之上的图像处理方法进行综合,且对结果进行归一化处理
"""
img = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)
hist_feature = color_hist(img)
spatial_feature = bin_spatial(img)
ch1 = img[:, :, 0]
ch2 = img[:, :, 1]
ch3 = img[:, :, 2]
ch1_hog_feature = get_hog_features(ch1, 9, 8, 2)
ch2_hog_feature = get_hog_features(ch2, 9, 8, 2)
ch3_hog_feature = get_hog_features(ch3, 9, 8, 2)
hog_features = np.hstack((ch1_hog_feature, ch2_hog_feature, ch3_hog_feature))
features = np.concatenate((hist_feature, spatial_feature, hog_features))
return features
```
- 根据上边的图像特征提取方法,对所有的数据进行特征提取
```python
X = []
y = []
for path in vehicles_file_paths:
img = mpimg.imread(path)
features = extract_img_features(img)
X.append(features)
y.append(vehicle_lable)
for path in non_vehicles_file_paths:
img = mpimg.imread(path)
features = extract_img_features(img)
X.append(features)
y.append(non_vehicle_label)
```
- 数据预处理
> shuffle —> train_test_split—>StandardScaler
>
> 注意:数据标准化的时候,使用训练数据作为fit的对象。
```python
from skimage.feature import hog
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from sklearn.preprocessing import StandardScaler
# shuffle
X, y = shuffle(X, y)
# split
rand_state = np.random.randint(0, 100)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=rand_state)
# scaller
X_scaler = StandardScaler().fit(X_train)
X_train = X_scaler.transform(X_train)
X_test = X_scaler.transform(X_test)
```
- 训练模型
```python
from sklearn.svm import LinearSVC
svm = LinearSVC()
svm.fit(X_train, y_train)
```
- 测试模型
> 最后模型的准确率为:0.986204954955
```python
from sklearn.metrics import accuracy_score
predicts = svm.predict(X_test)
score = accuracy_score(predicts, y_test)
print("在测试数据上的准确率为:" + score)
```
- 从图片中检测汽车
> **滑动窗口技术是关键**
>
> 因为不知道检测物体的大小,远的地方车辆看起来小,近的地方看起来大。为了得到合适的检测数据,需要图片进行适当的缩放。但是这样带来的后果是,计算量会急剧增加。可以采查找车辆只在图像的下半部分,因为上半部分是天空。为了提高效率,先对整幅图像进行HOG特征提取,然后再根据需要进行特征分割。
```python
def find_cars(img, ystart, ystop, scale, svc, X_scaler, orient, pix_per_cell, cell_per_block, spatial_size, hist_bins):
"""
检测当前图像中的所有可能的汽车位置
"""
boxs = []
draw_img = np.copy(img)
img = img.astype(np.float32)/255
img_tosearch = img[ystart:ystop,:,:]
ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2YCrCb)
if scale != 1:
imshape = ctrans_tosearch.shape
ctrans_tosearch = cv2.resize(ctrans_tosearch, (np.int(imshape[1]/scale), np.int(imshape[0]/scale)))
ch1 = ctrans_tosearch[:,:,0]
ch2 = ctrans_tosearch[:,:,1]
ch3 = ctrans_tosearch[:,:,2]
# Define blocks and steps as above
nxblocks = (ch1.shape[1] // pix_per_cell) - cell_per_block + 1
nyblocks = (ch1.shape[0] // pix_per_cell) - cell_per_block + 1
nfeat_per_block = orient*cell_per_block**2
# 64 was the orginal sampling rate, with 8 cells and 8 pix per cell
window = 64
nblocks_per_window = (window // pix_per_cell) - cell_pe
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
<项目介绍> 基于Python对道路上的车辆进行检测和识别+源代码+文档说明+演示视频 车辆检测 将会进行如下操作: 定义图像特征提取方 读入车辆和非车辆图像数据 数据预处理,shuffle,标准化 训练模型,使用的是svm 测试模型 使用滑动窗口和多尺度的图像大小在图像上检测车辆 滑动窗口 多尺度图像 heatmap label box cente - 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 --------
资源推荐
资源详情
资源评论
收起资源包目录
Vehicle-Detection-master.zip (24个子文件)
Vehicle-Detection-master
P5.ipynb 299KB
test_images
test1.jpg 212KB
test3.jpg 144KB
test6.jpg 227KB
test5.jpg 238KB
test4.jpg 196KB
test2.jpg 170KB
project_video_output.mp4 17.77MB
project_video.mp4 24.1MB
examples
labels_map.png 12KB
car_not_car.png 821KB
bboxes_and_heat.png 559KB
sliding_windows.jpg 374KB
HOG_example.jpg 209KB
sliding_window.jpg 273KB
output_bboxes.png 530KB
P5.html 588KB
imgs
2.jpg 51KB
6.jpg 486KB
1.jpg 90KB
5.jpg 155KB
3.jpg 125KB
4.jpg 30KB
README.md 14KB
共 24 条
- 1
资源评论
机智的程序员zero
- 粉丝: 2068
- 资源: 4235
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功