Main Content


Classify a video file



label = classifyVideoFile(classifier,videoFilename) predicts the class label for the video file, specified by videoFilename using the specified classifier classifier. The video file must be readable by the VideoReader object.

Use the classifyVideoFile object function for applications where the entire input video can be classified with one label. For example, you can use this object function to evaluate the performance of the classifier on a collection of ground truth video files. If the video contains sequences with different class labels, use the classifySequence object function.

This function selects sequences of frames from the video file using uniform random sampling to select the start of time of each sequence. The number of frames is set by the InputSize property of the classifier object. The function selects the most frequently occurring label in the video file as the classification label for the file. The video file must contain at least the number of frames set by the InputSize property.

[label,score] = classifyVideoFile(___) additionally returns the classification score associated with the label using the combination of input arguments from the previous syntax.. The score represents the confidence of the predicted class label and contains values between 0 and 1.

[___] = classifySequence(___,Name=Value) specifies options using name-value arguments in addition to any combination of arguments from previous syntaxes. For example, label = classifySequence(i3d,ExecutionEnvironment="cpu") specifies for the classifier to run on the CPU.


collapse all

Load a slowfast video classifier pretrained on the Kinetics-400 data set.

sf = slowFastVideoClassifier;

Specify the file name of the video to classify.

videoFilename = "washingHands.avi";

For video classification, set the number of randomly selected video sequences to 15.

numSequences = 15;

Classify the video using the classifyVideoFile function.

[label,score] = classifyVideoFile(sf,videoFilename,NumSequences=numSequences)
label = categorical
     washing hands 

score = single

Display the classified label using a vision.VideoPlayer.

player = vision.VideoPlayer('Name','Washing Hands');
reader = VideoReader(videoFilename);
while hasFrame(reader)    
    frame = readFrame(reader);
    % Resize the frame by 1.5 times for display
    frame = imresize(frame,1.5);
    frame = insertText(frame,[2,2], string(label),'FontSize',18);

Input Arguments

collapse all

Video file name, specified as a character vector or a string scalar. The video file must be readable by the VideoReader object.

Name-Value Arguments

Example: label = classifySequence(i3d,ExecutionEnvironment="cpu") specifies for the classifier to run on the CPU.

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Hardware execution environment on which to run the classifier, specified as "auto", "gpu", or "cpu".

  • "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 Support by Release (Parallel Computing Toolbox).

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

Maximum number of sequences to sample from the video file, specified as "auto" or a positive integer. When you set this value to "auto", the function sets the number of sequences to twice the number of sequences that can be selected from the video file based on the number of frames available. The "auto" setting provides a uniform coverage of the entire video. Increasing the value of NumSequences increases the uniform coverage of the video.

Mini-batch size used for processing a large video, specified as a positive integer. Video sequences are grouped and processed in mini-batches to improve computational efficiency. Increasing the value of MiniBatchSize increases processing speed, but uses more memory.

Output Arguments

collapse all

Classification of video, 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