Classify a video file
predicts the class label for the video file, specified by
label = classifyVideoFile(
using the specified classifier
classifier. The video file must be
readable by 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
[___] = classifySequence(___,
specifies options using name-value arguments in addition to any combination of arguments
from previous syntaxes. For example,
classifySequence(i3d,ExecutionEnvironment="cpu") specifies for the classifier to
run on the CPU.
Classify Video File Using Video Classifier
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
[label,score] = classifyVideoFile(sf,videoFilename,NumSequences=numSequences)
label = categorical washing hands
score = single 0.0034
Display the classified label using a
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); step(player,frame); end
videoFilename — Video file name
character vector | string scalar
Video file name, specified as a character vector or a string scalar. The video file
must be readable by the
Specify optional pairs of
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.
label = classifySequence(i3d,ExecutionEnvironment="cpu")specifies for the classifier to run on the CPU.
ExecutionEnvironment — Hardware execution environment
"auto" (default) |
Hardware execution environment on which to run the classifier, specified as
"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.
NumSequences — Maximum number of sequences to sample from video file
"auto" (default) | positive integer
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
the uniform coverage of the video.
MiniBatchSize — Mini-batch size for processing a large video
16 | positive integer
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
increases processing speed, but uses more memory.
label — Classification of video
Classification of video, returned as a categorical scalar.
score — Classification score associated with the label
scalar value between
Classification score associated with the label, returned as a scalar value between
1. The score represents the confidence of
the predicted class label.