Hi everyone, I modified this code designed for detecting and tracking faces captured by a camera to work with a recorded video. Now, I want to crop the detected and tracked faces into a separate folder
3 views (last 30 days)
Show older comments
Hi everyone, I modified this code designed for detecting and tracking faces captured by a camera to work with a recorded video. Now, I want to crop the detected and tracked faces into a separate folder but was given me error. Please, someone should help me with the cropping. The code is as shown below. Thanks
clear classes;
Instantiate video device, face detector, and KLT object tracker
vidObj = 'C:\Users\Asirajdin\Documents\T Chapters\Practical Implementation of face detection in matlab\New folder\matlab-viola-jones-master\detectAndTrackFaces1\classroom.mp4';
v = VideoReader(vidObj);
faceDetector = vision.CascadeObjectDetector(); % Finds faces by default
tracker = MultiObjectTrackerKLT;
vision.VideoFileReader
Get a frame for frame-size information
frame = read(v,1);
frameSize = size(frame);
Create a video player instance
videoPlayer = vision.VideoPlayer('Position',[200 100 fliplr(frameSize(1:2)+30)]);
Iterate until we have successfully detected a face
bboxes = [];
while isempty(bboxes)
framergb = readFrame(v,'native');
frame = rgb2gray(framergb);
bboxes = faceDetector.step(frame);
end
tracker.addDetections(frame, bboxes);
And loop until the player is closed
frameNumber = 0;
keepRunning = true;
disp('Press Ctrl-C to exit...');
pathName ='C:\Users\Asirajdin\Documents\T Chapters\Practical Implementation of face detection in matlab\New folder\matlab-viola-jones-master\detectAndTrackFaces1\Cropped';
while keepRunning
if v.NumFrames==0
break
end
framergb = readFrame(v,'native');
frame = rgb2gray(framergb);
if mod(frameNumber, 10) == 0
% (Re)detect faces.
%
% NOTE: face detection is more expensive than imresize; we can
% speed up the implementation by reacquiring faces using a
% downsampled frame:
% bboxes = faceDetector.step(frame);
bboxes = 2 * faceDetector.step(imresize(frame, 0.5));
if ~isempty(bboxes)
tracker.addDetections(frame, bboxes);
end
else
% Track faces
tracker.track(frame);
end
% Display bounding boxes and tracked points.
displayFrame = insertObjectAnnotation(framergb, 'rectangle',...
tracker.Bboxes, tracker.BoxIds);
displayFrame = insertMarker(displayFrame, tracker.Points);
videoPlayer.step(displayFrame);
for i = 1 : size(displayFrame, 1)
J = imcrop(videoPlayer.step(displayFrame),displayFrame(i,:));
fileName = fullfile(pathName,sprintf('Face%d.png',frameNumber));
imwrite(J,fileName) ; % Save image
imshow(J);
end
frameNumber = frameNumber + 1;
end
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Tracking and Motion Estimation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!