Home Matlab. How to implement tracking with counter on top of boxes?
Reply: 0

Matlab. How to implement tracking with counter on top of boxes?

user9371
1#
user9371 Published in July 22, 2018, 8:36 pm

Hello I have tried using the example in https://www.mathworks.com/help/vision/examples/tracking-pedestrians-from-a-moving-car.html and now I want to add a counter to replace the score. I have tried mixing the example in https://www.mathworks.com/help/vision/examples/motion-based-multiple-object-tracking.html. I have tried using the method it use to add in counter above the box. but to no avil.

Below are the codes.

function PedestrianTrackingFromMovingCameraExample()

videoFile       = 'vippedtracking.mp4';
scaleDataFile   = 'pedScaleTable.mat'; % An auxiliary file that helps to determine the size of a pedestrian at different pixel locations.
video = VideoReader(videoFile);
obj = setupSystemObjects(videoFile, scaleDataFile);

detector = peopleDetectorACF('caltech');

tracks = initializeTracks();

nextId = 1;

option.scThresh             = 0.3;
option.gatingThresh         = 0.9;
option.gatingCost           = 100;
option.costOfNonAssignment  = 10;
option.timeWindowSize       = 16;
option.confidenceThresh     = 2;
option.ageThresh            = 8;
option.visThresh            = 0.6;

while hasFrame(video)
    frame   = readFrame();

    [centroids, bboxes, scores] = detectPeople();

    predictNewLocationsOfTracks();

    [assignments, unassignedTracks, unassignedDetections] = ...
        detectionToTrackAssignment();

    updateAssignedTracks();
    updateUnassignedTracks();
    deleteLostTracks();
    createNewTracks();

    displayTrackingResults();

    if ~isOpen(obj.videoPlayer)
        break;
    end
end

    function obj = setupSystemObjects(videoFile,scaleDataFile)

        obj.reader = vision.VideoFileReader(videoFile, 'VideoOutputDataType', 'uint8');

        obj.videoPlayer = vision.VideoPlayer('Position', [29, 597, 643, 386]);

        ld = load(scaleDataFile, 'pedScaleTable');
        obj.pedScaleTable = ld.pedScaleTable;
    end

    function tracks = initializeTracks()
        tracks = struct(...
            'id', {}, ...
            'color', {}, ...
            'bboxes', {}, ...
            'scores', {}, ...
            'kalmanFilter', {}, ...
            'age', {}, ...
            'totalVisibleCount', {}, ...
            'confidence', {}, ...
            'predPosition', {}, ...
            'consecutiveInvisibleCount', {});
    end

    function frame = readFrame()
        frame = step(obj.reader);
    end

    function [centroids, bboxes, scores] = detectPeople()

        resizeRatio = 1.5;
        frame = imresize(frame, resizeRatio, 'Antialiasing',false);

        [bboxes, scores] = detect(detector, frame, ...
            'WindowStride', 2,...
            'NumScaleLevels', 4, ...
            'SelectStrongest', false);

        height = bboxes(:, 4) / resizeRatio;
        y = (bboxes(:,2)-1) / resizeRatio + 1;
        yfoot = min(length(obj.pedScaleTable), round(y + height));
        estHeight = obj.pedScaleTable(yfoot);

        invalid = abs(estHeight-height)>estHeight*option.scThresh;
        bboxes(invalid, :) = [];
        scores(invalid, :) = [];

        [bboxes, scores] = selectStrongestBbox(bboxes, scores, ...
                            'RatioType', 'Min', 'OverlapThreshold', 0.6);

        if isempty(bboxes)
            centroids = [];
        else
            centroids = [(bboxes(:, 1) + bboxes(:, 3) / 2), ...
                (bboxes(:, 2) + bboxes(:, 4) / 2)];
        end
    end

    function predictNewLocationsOfTracks()
        for i = 1:length(tracks)
            bbox = tracks(i).bboxes(end, :);

            predictedCentroid = predict(tracks(i).kalmanFilter);

            tracks(i).predPosition = [predictedCentroid - bbox(3:4)/2, bbox(3:4)];
        end
    end

    function [assignments, unassignedTracks, unassignedDetections] = ...
            detectionToTrackAssignment()

        predBboxes = reshape([tracks(:).predPosition], 4, [])';
        cost = 1 - bboxOverlapRatio(predBboxes, bboxes);

        cost(cost > option.gatingThresh) = 1 + option.gatingCost;

        [assignments, unassignedTracks, unassignedDetections] = ...
            assignDetectionsToTracks(cost, option.costOfNonAssignment);
    end

    function updateAssignedTracks()
        numAssignedTracks = size(assignments, 1);
        for i = 1:numAssignedTracks
            trackIdx = assignments(i, 1);
            detectionIdx = assignments(i, 2);

            centroid = centroids(detectionIdx, :);
            bbox = bboxes(detectionIdx, :);

            correct(tracks(trackIdx).kalmanFilter, centroid);

            T = min(size(tracks(trackIdx).bboxes,1), 4);
            w = mean([tracks(trackIdx).bboxes(end-T+1:end, 3); bbox(3)]);
            h = mean([tracks(trackIdx).bboxes(end-T+1:end, 4); bbox(4)]);
            tracks(trackIdx).bboxes(end+1, :) = [centroid - [w, h]/2, w, h];

            tracks(trackIdx).age = tracks(trackIdx).age + 1;

            tracks(trackIdx).scores = [tracks(trackIdx).scores; scores(detectionIdx)];

            tracks(trackIdx).totalVisibleCount = ...
                tracks(trackIdx).totalVisibleCount + 1;
            tracks(trackIdx).consecutiveInvisibleCount = 0;

            T = min(option.timeWindowSize, length(tracks(trackIdx).scores));
            score = tracks(trackIdx).scores(end-T+1:end);
            tracks(trackIdx).confidence = [max(score), mean(score)];
        end
    end

    function updateUnassignedTracks()
        for i = 1:length(unassignedTracks)
            idx = unassignedTracks(i);
            tracks(idx).age = tracks(idx).age + 1;
            tracks(idx).bboxes = [tracks(idx).bboxes; tracks(idx).predPosition];
            tracks(idx).scores = [tracks(idx).scores; 0];
            tracks(idx).consecutiveInvisibleCount = ...
                tracks(idx).consecutiveInvisibleCount + 1;

            T = min(option.timeWindowSize, length(tracks(idx).scores));
            score = tracks(idx).scores(end-T+1:end);
            tracks(idx).confidence = [max(score), mean(score)];
        end
    end

    function deleteLostTracks()
        if isempty(tracks)
            return;
        end

        ages = [tracks(:).age]';
        totalVisibleCounts = [tracks(:).totalVisibleCount]';
        visibility = totalVisibleCounts ./ ages;

        confidence = reshape([tracks(:).confidence], 2, [])';
        maxConfidence = confidence(:, 1);

        lostInds = (ages <= option.ageThresh & visibility <= option.visThresh) | ...
             (maxConfidence <= option.confidenceThresh);

        tracks = tracks(~lostInds);
    end

    function createNewTracks()
        unassignedCentroids = centroids(unassignedDetections, :);
        unassignedBboxes = bboxes(unassignedDetections, :);
        unassignedScores = scores(unassignedDetections);

        for i = 1:size(unassignedBboxes, 1)
            centroid = unassignedCentroids(i,:);
            bbox = unassignedBboxes(i, :);
            score = unassignedScores(i);

            kalmanFilter = configureKalmanFilter('ConstantVelocity', ...
                centroid, [2, 1], [5, 5], 100);

            newTrack = struct(...
                'id', nextId, ...
                'color', 255*rand(1,3), ...
                'bboxes', bbox, ...
                'scores', score, ...
                'kalmanFilter', kalmanFilter, ...
                'age', 1, ...
                'totalVisibleCount', 1, ...
                'confidence', [score, score], ...
                'predPosition', bbox, ...
                'consecutiveInvisibleCount', 0);

            tracks(end + 1) = newTrack; %#ok<AGROW>

            nextId = nextId + 1;
        end
    end

    function displayTrackingResults()

        displayRatio = 4/3;
        frame = imresize(frame, displayRatio);
        minVisibleCount = 8;

        if ~isempty(tracks)

            reliableTrackInds = ...
                [tracks(:).totalVisibleCount] > minVisibleCount;
            reliableTracks = tracks(reliableTrackInds);
            ids = int32([reliableTracks(:).id]);
            labels = cellstr(int2str(ids'));
                predictedTrackInds = ...
                    [reliableTracks(:).consecutiveInvisibleCount] > 0;
                isPredicted = cell(size(labels));
                isPredicted(predictedTrackInds) = {'predicted'};
                labels = strcat(labels, isPredicted);            

            ages = [tracks(:).age]';
            confidence = reshape([tracks(:).confidence], 2, [])';
            maxConfidence = confidence(:, 1);
            avgConfidence = confidence(:, 2);
            opacity = min(0.5,max(0.1,avgConfidence/3));
            noDispInds = (ages < option.ageThresh & maxConfidence < option.confidenceThresh) | ...
                       (ages < option.ageThresh / 2);

            for i = 1:length(tracks)
                if ~noDispInds(i)

                    bb = tracks(i).bboxes(end, :);
                    bb(:,1:2) = (bb(:,1:2)-1)*displayRatio + 1;
                    bb(:,3:4) = bb(:,3:4) * displayRatio;

                    frame = insertObjectAnnotation(frame, ...
                                            'rectangle', bb, ...
                                            labels, ...
                                            'Color', tracks(i).color);
                end
            end
        end



        step(obj.videoPlayer, frame);

    end

end
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.312011 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO