function frame = updateVideoAnnotations(frame, sensor, ...
    mostImportantObject, confirmedTracks, lane, positionSelector, showUnknownClass)

% Boundary type information.
laneTypes = {'Unmarked', 'Unmarked', 'Solid', 'Dashed', 'Unmarked', 'BottsDots', ...
    'DoubleSolid', 'Unmarked', 'DoubleSolid'};
objectClassName = {'Unknown', 'UnknownSmall', 'UnknownBig', 'Pedestrian', 'Bike', 'Car', 'Truck', 'Barrier'};

% Display the lane boundaries on the video frame.
xRangeWorld = [1, 100];
xPtsInWorld = linspace(xRangeWorld(1), xRangeWorld(2), 100)';

% Draw left lane boundary.
if lane.left.isValid
    % Construct parabolicLaneBoundary objects for left lane boundaries.
    leftParams = cast([lane.left.curvature, lane.left.headingAngle, lane.left.offset], 'double');
    leftBoundaries = parabolicLaneBoundary(leftParams);
    leftBoundaries.BoundaryType = laneTypes{lane.left.boundaryType+1};
    frame = insertLaneBoundary(frame, leftBoundaries, sensor, xPtsInWorld, 'Color', 'yellow');
end

% Draw right lane boundary.
if lane.right.isValid
    % Construct parabolicLaneBoundary objects for left and right lane
    % boundaries.
    rightParams = cast([lane.right.curvature, lane.right.headingAngle, lane.right.offset], 'double');
    rightBoundaries = parabolicLaneBoundary(rightParams);
    rightBoundaries.BoundaryType = laneTypes{lane.right.boundaryType+1};
    frame = insertLaneBoundary(frame, rightBoundaries, sensor, xPtsInWorld, 'Color', 'yellow');
end

if isempty(confirmedTracks)
    return
end

% Prepare tracks and most important object.
trackClass = [confirmedTracks.ObjectClassID];
trackPositions = getTrackPositions(confirmedTracks, positionSelector);

if ~showUnknownClass
    % Exclude unknown class.
    confirmedTracks = confirmedTracks(trackClass>0); 
end
numTracks = numel(confirmedTracks);

widths = arrayfun(@(x) x.ObjectAttributes(1).Size(2), confirmedTracks);
x = trackPositions(:,1);
y = trackPositions(:,2);
objectClass  = [confirmedTracks.ObjectClassID];
colors = cellstr(repmat('white', numTracks,1));
IDs = [confirmedTracks.TrackID];

% Check if mio exists.
if ~isempty(mostImportantObject.TrackIndex)
    mioIndex = find(IDs==mostImportantObject.ObjectID);
    if ~isempty(mioIndex)
        colors{mioIndex} = mostImportantObject.ThreatColor;
    end
end

validIdx = find(x>1 & x<100);
if isempty(validIdx)
    return;
end

widths = widths(validIdx);
x = x(validIdx);
y = y(validIdx);
objectClass = objectClass(validIdx);
colors = colors(validIdx);
%IDs = IDs(validIdx);
numObjects = numel(widths);

% Allocate memory for bounding boxes.
bboxes = zeros(numObjects, 4);
labels = {};

% Create the bounding boxes.
for i = 1:numObjects
    % Use monoCamera sensor to convert the position in world coordinates to
    % position in image coordinates.
    %
    % Notes:
    %   1. The width of the object is reported and we use it to calculate
    %      the size of the bounding box around the object (half width on
    %      each side). The height of the object is not reported. Instead,
    %      we are using a height/width ratio of 0.85 for cars and 3 for
    %      pedestrians.
    %   2. The reported location is at the center of the object at ground
    %      level, i.e., the bottom of the bounding box.
    xyLocation1 = vehicleToImage(sensor, [x(i), y(i)] + [0,widths(i)/2]);
    xyLocation2 = vehicleToImage(sensor, [x(i), y(i)] - [0,widths(i)/2]);
    W = xyLocation2(1) - xyLocation1(1);
    
    % Define the height/width ratio based on object class.
    switch objectClass(i)
        case {3,4} % Pedestrian or Bike
            H = W * 3;
            colors{i} = 'magenta';
        case 5 % Car
            H = W * 0.85;
        case 6 % Truck
            H = W * 0.85;
        otherwise
            H = W;
    end
    
    % Estimate the bounding box around the vehicle. Subtracting the height
    % of the bounding box to define the top-left corner.
    bboxes(i,:) =[(xyLocation1 - [0, H]), W, H];
    
    if strcmp(colors{i},'red')
        labels{i} = ['FCW!!-> ', objectClassName{objectClass(i)+1}]; %#ok<AGROW>
    else
        labels{i} = objectClassName{objectClass(i)+1}; %#ok<AGROW>
    end    
end

% Add bounding boxes to the frame.
if ~isempty(labels)
%    frame = insertShape(frame, 'rectangle', bboxes, 'Color', colors, 'LineWidth', 2);
    frame = insertObjectAnnotation(frame, 'rectangle', bboxes, labels,...
        'Color', colors,'Font','Arial','FontSize', 14, 'TextBoxOpacity', .6, 'LineWidth', 2);
end
end
