필터 지우기
필터 지우기

HELP, How to do real time face tracking in matlab?

조회 수: 3 (최근 30일)
Anand
Anand 2014년 2월 20일
댓글: VALARMATHY K 2017년 8월 21일
Hi, can someone PLEASE tell me how to track a face in a real time video using A webcam in MATLAB. Im using this sample code which outputs a video file with my face being being tracked. All i want it to do is to track my face live using the webcam. Its probably a small change in the code but i have no idea how to do it.
Can anybody please help me with this?
Detect the face
% Create a cascade detector object.
faceDetector = vision.CascadeObjectDetector();
% Read a video frame and run the detector.
videoFileReader = vision.VideoFileReader('facevid.WMV');
videoFrame = step(videoFileReader);
bbox = step(faceDetector, videoFrame);
% Draw the returned bounding box around the detected face.
videoOut = insertObjectAnnotation(videoFrame,'rectangle',bbox,'Face');
figure, imshow(videoOut), title('Detected face');
% Get the skin tone information by extracting the Hue from the video frame
% converted to the HSV color space.
[hueChannel,~,~] = rgb2hsv(videoFrame);
% Display the Hue Channel data and draw the bounding box around the face.
figure, imshow(hueChannel), title('Hue channel data');
rectangle('Position',bbox(1,:),'LineWidth',2,'EdgeColor',[1 1 0])
Track the face
% Detect the nose within the face region. The nose provides a more accurate
% measure of the skin tone because it does not contain any background
% pixels.
noseDetector = vision.CascadeObjectDetector('Nose');
faceImage = imcrop(videoFrame,bbox(1,:));
noseBBox = step(noseDetector,faceImage);
% The nose bounding box is defined relative to the cropped face image.
% Adjust the nose bounding box so that it is relative to the original video
% frame.
noseBBox(1,1:2) = noseBBox(1,1:2) + bbox(1,1:2);
% Create a tracker object.
tracker = vision.HistogramBasedTracker;
% Initialize the tracker histogram using the Hue channel pixels from the
% nose.
initializeObject(tracker, hueChannel, noseBBox(1,:));
% Create a video player object for displaying video frames.
videoInfo = info(videoFileReader);
videoPlayer = vision.VideoPlayer('Position',[300 300 videoInfo.VideoSize+30]);
% Track the face over successive video frames until the video is finished.
while ~isDone(videoFileReader)
% Extract the next video frame
videoFrame = step(videoFileReader);
% RGB -> HSV
[hueChannel,~,~] = rgb2hsv(videoFrame);
% Track using the Hue channel data
bbox = step(tracker, hueChannel);
% Insert a bounding box around the object being tracked
videoOut = insertObjectAnnotation(videoFrame,'rectangle',bbox,'Face');
% Display the annotated video frame using the video player object
step(videoPlayer, videoOut);
end
% Release resources
release(videoFileReader);
release(videoPlayer);

채택된 답변

Anand
Anand 2014년 2월 21일
You can use imaqhwinfo to see which cameras are connected to your machine and then accordingly use the videoinput command to connect to the camera of your choice. Within the loop, use the getsnapshot command to capture an image.
This video should be helpful:
  댓글 수: 8
Anand
Anand 2014년 2월 27일
i have managed to read the video frames, but how do i extract them in screen shots?
videoFReader = vision.VideoFileReader('face.avi');
videoPlayer = vision.VideoPlayer;
while ~isDone(videoFReader)
videoFrame = step(videoFReader);
step(videoPlayer, videoFrame);
end
release(videoPlayer);
release(videoFReader);
VALARMATHY K
VALARMATHY K 2017년 8월 21일
you can use this code
obj = VideoReader('cute.mp4');
vid = read(obj); frames = obj.NumberOfFrames; ST='.jpg';
for x = 1:25
Sx=num2str(x);
Strc=strcat(Sx,ST);
Vid=vid(:,:,:,x);
imwrite(Vid,Strc);
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by