Main Content

classifySequence

Classify video and optical flow sequence

Since R2021b

Description

label = classifySequence(i3d) classifies a video and optical flow sequence using the Inflated-3D (I3D) video classifier i3d. The function returns label, a scalar categorical that specifies the classification of the video or optical flow sequence. label is one of the values of the Classes property of the video classifier object.

example

[label,score] = classifySequence(i3d) additionally returns the classification score associated with the label. The score represents the confidence of the predicted class label, and contains values between 0 and 1.

[___] = classifySequence(i3d,ExecutionEnvironment=env) specifies the hardware resources for running the classifier in addition to any combination of arguments from previous syntaxes, as one of these options:

  • "auto" — Sets the execution environment to the GPU, if available. Otherwise the function sets it to the CPU.

  • "gpu" — Sets the execution environment to the GPU. Usage of the GPU requires Parallel Computing Toolbox™ and a CUDA® enabled NVIDIA® GPU. For information about the supported compute capabilities, see GPU Computing Requirements (Parallel Computing Toolbox).

  • "cpu" — Sets the execution environment to the CPU.

Examples

collapse all

This example shows how to classify video sequences in a video file using a SlowFast Video Classifier pretrained on the Kinetics-400 video activity recognition dataset. To learn more about how to train a video classifier network for your dataset, see Gesture Recognition using Videos and Deep Learning.

Load SlowFast Video Classifier

sf = slowFastVideoClassifier();

Setup Video Player and Video Reader

Specify the video file name to stream video frames.

videoFilename = "pushup.mp4";

Create a VideoReader to read video.

reader = VideoReader(videoFilename);

Setup a video player.

player = vision.VideoPlayer;

Classify Video Sequences

Specify how frequently the classifier should be applied to incoming video frames.

classifyInterval = 10;

A value of 10 balances runtime performance against classification performance. Increase this value to improve runtime performance at the cost of missing actions from the video file.

Obtain the sequence length of the SlowFast Video Classifier. Classify only after capturing at least sequenceLength number of frames from the video file.

sequenceLength = sf.InputSize(4);

Read video frames using the hasFrame and readFrame functions of the VideoReader. Using the updateSequence function update the video classifier's sequence. Using the classifySequence function classify the updated sequence.

numFrames = 0;
text = "";

while hasFrame(reader)
    frame = readFrame(reader);
    numFrames = numFrames + 1;

    % Update the sequence with the next video frame.
    sf = updateSequence(sf,frame);

    % Classify the sequence only at every classifyInterval number of frames.
    if mod(numFrames, classifyInterval) == 0 && numFrames >= sequenceLength
        [label,score] = classifySequence(sf);
        text = string(label) + "; " + num2str(score, "%0.2f");
    end
    frame = insertText(frame,[30,30],text,'FontSize',24);
    step(player,frame);
end

Input Arguments

collapse all

Classifier, specified as an inflated3dVideoClassifier object.

Output Arguments

collapse all

Classification of the video or optical flow sequence, returned as a categorical scalar.

Classification score associated with the label, returned as a scalar value between 0 and 1. The score represents the confidence of the predicted class label.

Version History

Introduced in R2021b