Detecting faces in video file. Error in using data type

167 Views Asked by At

I have the following question. I'm working with matlab 2016. I want to detect faces in a video file, using the Viola-Jones algorithm. When I try to enter a video file in the step statement, I get the following error:

Error using
   vision.CascadeObjectDetector/validateInputsImpl
   (line 330)
   Expected input number 2 to be
   one of these types:

   uint8, uint16, double, single,int16

   Instead its type was
   vision.VideoFileReader.

   Error in VJ1_video (line 12)
   bboxes = step(faceDetector, videoFReader);

I understand that somehow the type of VideoFileReader should be converted to one of the types: uint8, uint16, double, single, int16. But I do not understand how this can be done. Tell me please. Here is my program code:

clear all;
%Load the video using a video reader object
  videoFReader = vision.VideoFileReader('D:\465.avi');
%Create a detector object.
  faceDetector = vision.CascadeObjectDetector;
%Detect faces.
  bboxes = step(faceDetector, videoFReader);
%Annotate detected faces
  IFaces = insertObjectAnnotation(videoFReader, 'rectangle', bboxes, 'Лицо');
%Create a video player object to play the video file.
  videoPlayer = vision.VideoPlayer;

%Use a while loop to read and play the video frames.
  while ~isDone(videoFReader)
    videoFrame = videoFReader();
    videoPlayer(videoFrame);
  end
%Release the objects.
  release(videoPlayer);
  release(videoFReader);
1

There are 1 best solutions below

0
Honeybear On

The CascadeObjectDetector works on single frames (images), not on a sequence of frames (video). Accordingly input of the step function is, as you already suggested, an image. Change your code so that the face detection happens while you iterate over the frames.

An (untested) suggestion:

    %Load the video using a video reader object
      videoFReader = vision.VideoFileReader('D:\465.avi');
    %Create a video player object to play the video file.
      videoPlayer = vision.VideoPlayer;
    %Create a detector object.
      faceDetector = vision.CascadeObjectDetector;

    %Use a while loop to read and play the video frames.
      while ~isDone(videoFReader)
        videoFrame = videoFReader();
        %Detect faces.
        bboxes = step(faceDetector, videoFReader);
        %Annotate detected faces
        IFaces = insertObjectAnnotation(videoFReader, 'rectangle', bboxes, 'Лицо');
        %Show annotated frames IFaces (instead of videoFrame)
        videoPlayer(IFaces);
      end

    %Release the objects.
      release(videoPlayer);
      release(videoFReader);