基于MATLAB&SIMULINK开发自动驾驶系统第三十五讲自动标注对象的属性

智驾社 2022-03-02
2656 字丨阅读本文需 9 分钟

本讲展示了如何开发车辆检测和距离估计算法,并使用它来使用Ground Truth Labeler应用程序自动标记。在本讲中,你将学习如何:

开发一个计算机视觉算法来检测视频中的车辆,并使用单目摄像头配置来估计与检测到的车辆的距离。使用 AutomationAlgorithm API 创建自动化算法。所创建的自动化算法可与 Ground Truth Labeler 应用程序一起使用,以自动为车辆等目标打标签,同时使用属性来存储估计距离。

35.1 Ground Truth Labeler 应用程序

良好的地面真实数据对于开发驾驶算法和评估其性能至关重要。然而,创建一套丰富多样的标注驾驶数据需要付出巨大的努力。Ground Truth Labeler应用使这一过程变得高效。您可以将该应用作为一个完全手动的标记工具,为自动驾驶系统标记车辆边界框、车道线边界和其他感兴趣的对象。您还可以手动指定标记对象的属性。然而,手动标注需要大量的时间和资源。作为替代方案,该应用程序提供了一个创建算法的框架,以扩展和自动化标签过程。您可以使用您创建的算法来快速标记整个数据集,自动对标签进行属性注释,然后用更高效、更短的手动验证步骤进行跟进。您还可以编辑自动化步骤的结果,以说明自动化算法可能遗漏的挑战性场景。 本讲介绍了如何将车辆检测和距离估计自动化算法插入到应用程序的自动化工作流中。这个例子重用ACF车辆检测自动化算法,首先检测车辆,然后自动估计检测到的车辆与安装在被控车辆上的摄像头的距离。然后,该算法为每一个检测到的车辆创建一个标签,标签上的属性指定了与车辆的距离。 

35.2 从单目摄像头检测车辆

首先,创建一个车辆检测算法。使用单目摄像头的视觉感知示例介绍了如何创建一个预先训练的车辆检测器,并配置它使用校准的单目摄像头配置来检测车辆边界框。要检测车辆,请在单个视频帧上试用该算法。

% Reada frame of interest from a video.vidObj   = VideoReader('05_highway_lanechange_25s.mp4');vidObj.CurrentTime = 0.1;I = readFrame(vidObj); % Loadthe monoCamera object.data =load('FCWDemoMonoCameraSensor.mat', 'sensor');sensor =data.sensor; % Loadthe pretrained detector for vehicles.detector =vehicleDetectorACF(); % Widthof a common vehicle is between 1.5 to2.5 meters.vehicleWidth= [1.5, 2.5]; %Configure the detector to take into account configuration of the camera% andexpected vehicle widthdetector =configureDetectorMonoCamera(detector, sensor, vehicleWidth); %Detect vehicles and show the bounding boxes.[bboxes, ~]= detect(detector, I);Iout = insertShape(I,'rectangle', bboxes);figure;imshow(Iout)
title('DetectedVehicles')

35.3 估计与被检测车辆的距离

MonoCamera 提供了一个 imageToVehicle 方法,将点从图像坐标转换为车辆坐标。这可以用来估计从摄像机到检测到的车辆沿地面的距离。本例将距离指定为检测到的车辆的中心点,沿其正下方的地面。

% Findthe midpoint for each bounding box in image coordinates.midPtsImg =[bboxes(:,1)+bboxes(:,3)/2 bboxes(:,2)+bboxes(:,4)./2];midPtsWorld= imageToVehicle(sensor, midPtsImg);x =midPtsWorld(:,1);y =midPtsWorld(:,2);distance  = sqrt(x.^2 + y.^2); %Display vehicle bounding boxes and annotate them with distance in meters.distanceStr= cellstr([num2str(distance) repmat(' m',[length(distance) 1])]);Iout =insertObjectAnnotation(I, 'rectangle', bboxes, distanceStr);imshow(Iout)title('Distances of Vehicles from Camera')

35.4 准备车辆检测与测距自动化类

将车辆检测和距离估计自动化类纳入应用程序的自动化工作流中。从现有的ACF车辆检测自动化算法开始,用校准的单目摄像头执行车辆检测。然后修改算法以执行属性自动化。在本例中,使用车辆与摄像头的距离作为检测车辆的属性。本节描述了对现有ACF车辆检测自动化算法类进行修改的步骤:

步骤1包含定义算法名称和描述的属性,以及使用算法的方向

%--------------------------------------------------------------------   % Define algorithm Name, Description, andUserDirections.   properties(Constant)       %Name: Algorithm Name       %  Character vector specifying name ofalgorithm.       Name = 'Vehicle Detection and DistanceEstimation';       % Description: Provide a one-linedescription for your algorithm.       Description = 'Detect vehicles using apretrained ACF vehicle detector and compute distance of detected vehicles from camera.';       % UserDirections: Provide a set of directions that are displayed       %                 when this algorithm isinvoked. The directions       %                 are to be provided as a cellarray of character       %                 vectors, with each element of the cell array       %                 representing a step in thelist of directions.       UserDirections = {...           'Define a rectangle ROI Label to labelvehicles.',...           'For the label definition created, define an Attribute with name Distance, type Numeric Value and defaultvalue 0.', ...           'Run the algorithm',...           'Manually inspect and modify resultsif needed'};   end

步骤2包含支持车辆检测和距离估计自动化所需的自定义属性。

%--------------------------------------------------------------------   % Vehicle Detector Properties  %--------------------------------------------------------------------   properties       %SelectedLabelName Selected label name       %  Name of selected label. Vehicles detected by the algorithmwill       %  be assigned this variable name.       SelectedLabelName       %Detector Detector       %  Pretrained vehicle detector, an objectof class       %  acfObjectDetector.       Detector       %VehicleModelName Vehicle detector modelname       %  Name of pretrained vehicledetector model.       VehicleModelName = 'full-view';       %OverlapThreshold Overlap threshold       %  Threshold value used to eliminate overlapping bounding boxes       %  around the reference bounding box, between 0 and 1. The       %  bounding box overlap ratio denominator, 'RatioType' is set to       %  'Min'       OverlapThreshold = 0.65;       %ScoreThreshold Classification Score Threshold       %  Threshold value used to reject detections with low detection       %  scores.       ScoreThreshold = 30;       %ConfigureDetector Boolean value todecide on configuring the detector       %  Boolean value which decides if the detector is configured using       %   monoCamera sensor.       ConfigureDetector = true;       %SensorObj monoCamerasensor       %   Monocular Camera Sensor object used to configure the detector.       %  A configured detector will run faster and can potentially       %  result in better detections.       SensorObj = [];       %SensorStr monoCamera sensor variablename       %  Monocular Camera Sensor objectvariable name used to configure       %  the detector.       SensorStr = '';       %VehicleWidth Vehicle Width       %  Vehicle Width used to configure the detector, specified as       %  [minWidth, maxWidth] describing the approximate width of the       %  object in world units.       VehicleWidth = [1.5 2.5];       %VehicleLength Vehicle Length       %  Vehicle Length used to configure the detector, specified as       %  [minLength, maxLength] describing the approximate length of the       %  object in world units.       VehicleLength = [ ];      end  %--------------------------------------------------------------------   % Attributeautomation Properties  %--------------------------------------------------------------------      properties (Constant, Access = private)       % Flag to enable Distance attribute estimation automation       AutomateDistanceAttriibute = true;       % Supported Distance attribute name.       % The labelmust have an attribute with the namespecified.       SupportedDistanceAttribName ='Distance';   end   properties (Access = private)       % Actual attributename for distance       DistanceAttributeName;       % Flag to check if attribute specified is a valid distance       % attribute       HasValidDistanceAttribute = false;   end

Step 3 初始化属性.

 %-------------------------------------------------------------------

-   % Initialize sensor, detector and otherrelevant properties.   function initialize(algObj, ~)       % Store the name of the selected labeldefinition. Use this       % name to labelthe detected vehicles.       algObj.SelectedLabelName = algObj.SelectedLabelDefinitions.Name;       % Initialize the vehicle detector with apretrained model.       algObj.Detector= vehicleDetectorACF(algObj.VehicleModelName);       % Initialize parameters tocompute vehicle distance       if algObj.AutomateDistanceAttriibute           initializeAttributeParams(algObj);       end              end   function initializeAttributeParams(algObj)       % Initialize properties relevant to attribute automation.       % The labelmust have an attribute with nameDistance and type       % Numeric Value.       hasAttribute =isfield(algObj.ValidLabelDefinitions, 'Attributes')&& ...                     isstruct(algObj.ValidLabelDefinitions.Attributes);       if hasAttribute                    attributeNames =fieldnames(algObj.ValidLabelDefinitions.Attributes);          idx = find(contains(attributeNames,algObj.SupportedDistanceAttribName));          if ~isempty(idx)             algObj.DistanceAttributeName =attributeNames{idx};              algObj.HasValidDistanceAttribute =validateDistanceType(algObj);          end       end    end    function tf = validateDistanceType(algObj)       % Validate the attribute type.       tf = isfield(algObj.ValidLabelDefinitions.Attributes, algObj.DistanceAttributeName)&& ...           isfield(algObj.ValidLabelDefinitions.Attributes.(algObj.DistanceAttributeName),'DefaultValue') && ...           isnumeric(algObj.ValidLabelDefinitions.Attributes.(algObj.DistanceAttributeName).DefaultValue);   end

步骤4包含更新后的运行方法,计算检测到的汽车的距离,并将标签和属性信息写入输出标签。

 %--------------------------------------------------------------------

function autoLabels = run(algObj, I)       autoLabels = [];       % Configure the detector.       if algObj.ConfigureDetector &&~isa(algObj.Detector,'acfObjectDetectorMonoCamera')           vehicleSize =[algObj.VehicleWidth;algObj.VehicleLength];           algObj.Detector= configureDetectorMonoCamera(algObj.Detector, algObj.SensorObj,vehicleSize);                      end       % Detect vehicles using the initializedvehicle detector.       [bboxes, scores] =detect(algObj.Detector, I,...           'SelectStrongest', false);       [selectedBbox, selectedScore] =selectStrongestBbox(bboxes, scores, ...           'RatioType', 'Min','OverlapThreshold', algObj.OverlapThreshold);       % Reject detections with detection scorelower than       % ScoreThreshold.       detectionsToKeepIdx = (selectedScore> algObj.ScoreThreshold);       selectedBbox =selectedBbox(detectionsToKeepIdx,:);       if ~isempty(selectedBbox)           % Add automatedlabels at bounding box locationsdetected           % by the vehicle detector, of type Rectangle having name of           % the selected label.           autoLabels.Name     = algObj.SelectedLabelName;           autoLabels.Type     = labelType.Rectangle;           autoLabels.Position = selectedBbox;           if(algObj.AutomateDistanceAttriibute && algObj.HasValidDistanceAttribute)               attribName =algObj.DistanceAttributeName;               % Attributevalue is of type 'Numeric Value'               autoLabels.Attributes = computeVehicleDistances(algObj,selectedBbox, attribName);           end                        else           autoLabels = [];       end   end   function midPts =helperFindBottomMidpoint(bboxes)      % Find midpoint of bottom edge of thebounding box.      xBL = bboxes(:,1);      yBL = bboxes(:,2);      xM = xBL + bboxes(:,3)/2;      yM = yBL + + bboxes(:,4)./2;      midPts = [xM yM];   end   function distances= computeDistances(algObj,bboxes)      % Helper function to compute vehicledistance.      midPts =helperFindBottomMidpoint(bboxes);      xy =algObj.SensorObj.imageToVehicle(midPts);      distances = sqrt(xy(:,1).^2 + xy(:,2).^2);   end   function attribS = computeVehicleDistances(algObj,bboxes, attribName)       % Compute vehicle distance.       numCars = size(bboxes, 1);       attribS = repmat(struct(attribName, 0),[numCars, 1]);       for i=1:numCars                 distanceVal= computeDistances(algObj, bboxes(i,:));           attribS(i).(attribName) =distanceVal;       end   end

35.5 使用应用程序中的车辆检测和距离估计自动化类车辆距离计算算法的打包版本可以在VehicleDetectionAndDistanceEstimation类中使用。要在应用中使用这个类。 在当前文件夹下创建所需的文件夹结构,并将自动化类复制到其中。

mkdir('+vision/+labeler')。    copyfile(fullfile(matlabroot,'examples','driving','main','VehicleDetectionAndDistanceEstimation.m'),'+vision/+labeler');将monoCamera信息加载到工作空间中。这个摄像头传感器信息适用于本例中使用的视频05_highway_lanechange_25s.mp4中的摄像头。如果您加载不同的视频,请使用适合该视频的传感器信息。    load('FCWDemoMonoCameraSensor.mat', 'sensor')打开groundTruthLabeler应用程序
groundTruthLabeler05_highway_lanechange_25s.mp4

在左侧的 ROI 标签定义窗格中,单击标签。定义一个标签,名称为 "车辆",类型为 "矩形"。可选地,添加标签描述。然后单击 "确定"。

- 在左侧的ROI标签定义窗格中,单击 "属性"。定义一个属性,名称为Distance,类型为Numeric Value,默认值为0。可选地,添加属性描述。然后单击"确定"。

- 选择算法 > 选择算法 > 刷新列表。- 选择算法 > 车辆检测和距离估计。如果没有看到这个选项,请确保当前工作文件夹中有一个名为+vision/+labeler的文件夹,文件名为VehicleDetectionAndDistanceEstimation.m在其中。

- 单击 "自动"。打开一个新的选项卡,显示算法的使用说明。- 单击设置,在打开的对话框中,在第一个文本框中输入传感器。如果需要的话,可以修改其他参数,然后再单击 "确定"。

- 单击 "运行"。车辆检测和距离计算算法在视频中进行。注意,在一些帧中,结果并不令人满意。- 运行完成后,使用滑块或方向键在视频中滚动,找到算法失败的帧。

- 通过移动车辆边界框或改变距离值来手动调整结果。您也可以删除边界框和相关的距离值。

- 一旦您对整个视频的车辆边界框及其距离感到满意,请单击 "接受"。视频上的自动车辆检测和距离属性标注就完成了。现在您可以标记其他感兴趣的对象并设置它们的属性,保存会话,或导出本次标记运行的结果。

35.6 总结

这个例子展示了将车辆检测和距离属性估计自动化算法纳入Ground Truth Labeler应用程序的步骤。可以将此概念扩展到其他自定义算法,以扩展应用程序的功能。视频的像素标签的自动化就完成了。现在可以继续标记其他感兴趣的对象,保存会话,或导出本次标记运行的结果。

免责声明:凡注明来源本网的所有作品,均为本网合法拥有版权或有权使用的作品,欢迎转载,注明出处本网。非本网作品均来自其他媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如您发现有任何侵权内容,请依照下方联系方式进行沟通,我们将第一时间进行处理。

0赞 好资讯,需要你的鼓励
来自:智驾社
0

参与评论

登录后参与讨论 0/1000

为你推荐

加载中...