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

조회 수: 6 (최근 30일)
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

채택된 답변

asirajdin
asirajdin 2020년 11월 10일
Due to continuous effort, I am able to modified the code and its now working properly. see the working code below:
Thanks.
clear classes;
%Instantiate video device, face detector, and KLT object tracker
vidObj = 'specify the video file directory';
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;
pathName ='specify the path to the crop folder here';
while keepRunning
if frameNumber>=v.NumFrames
close(videoPlayer);
clear vidObj;
break;
end
framergb = readFrame(v,'native');
frame = rgb2gray(framergb);
if mod(frameNumber, 5) == 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(bboxes, 1)
J = imcrop(framergb,bboxes(i,:));
fileName = fullfile(pathName,sprintf('Face%d.png',i));
imwrite(J,fileName) ; % Save image
figure(2);
subplot(11, 3,i);
imshow(J);
end
frameNumber = frameNumber + 1;
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tracking and Motion Estimation에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by