动手学无人驾驶(7):车道线检测
1.Method介绍
概率 P i , j , : P_{i, j,:} Pi,j,: 是 ( w + 1 ) (w+1) (w+1) 维的向量。
2.自采数据集车道线检测
下载完源码后,创建一个lane_detection.py,首先导入需要的库:
import torch import scipy.special import os, cv2 from PIL import Image from model.model import parsingNet import numpy as np import torchvision.transforms as transforms from data.constant import tusimple_row_anchor
net = parsingNet(pretrained = False, backbone=18,cls_dim = (101,56,4), use_aux=False) # we dont need auxiliary segmentation in testing test_model = tusimple_18.pth state_dict = torch.load(test_model, map_location=cpu)[model] compatible_state_dict = { } for k, v in state_dict.items(): if module. in k: compatible_state_dict[k[7:]] = v else: compatible_state_dict[k] = v net.load_state_dict(compatible_state_dict, strict=False) net.eval()
img_w, img_h = 1280, 560 size = (img_w,img_h) cls_num_per_lane = 56 row_anchor = tusimple_row_anchor img_transforms = transforms.Compose([ transforms.Resize((288, 800)), transforms.ToTensor(), transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), ])
下一步就是车道线检测与可视化:
with torch.no_grad(): out = net(img) col_sample = np.linspace(0, 800-1, 100) col_sample_w = col_sample[1] - col_sample[0] out_j = out[0].data.cpu().numpy() out_j = out_j[:, ::-1, :] prob = scipy.special.softmax(out_j[:-1, :, :], axis=0) idx = np.arange(100) + 1 idx = idx.reshape(-1, 1, 1) loc = np.sum(prob * idx, axis=0) out_j = np.argmax(out_j, axis=0) loc[out_j == 100] = 0 out_j = loc for i in range(out_j.shape[1]): if np.sum(out_j[:, i] != 0) > 2: for k in range(out_j.shape[0]): if out_j[k, i] > 0: ppp = (int(out_j[k,i] * col_sample_w * img_w / 800) - 1, int(img_h * (row_anchor[cls_num_per_lane-1-k]/288))-1) cv2.circle(vis,ppp,4,(0,0,255),-1) Image.fromarray(vis).show()
最后检测结果如下: