matlab的旋律 发表于 2018-4-4 19:17:05

脸部检测跟踪算法及GUI演示

本帖最后由 matlab的旋律 于 2018-4-11 11:52 编辑

动手做这个项目的想法源自公司的保密要求,在离开计算机屏幕前三分钟以上必须关机或者锁定屏幕。本项目主要的目标是开发一个简单的系统来跟踪网络摄像头捕获的实时视频流中的单个人脸,并以此结果控制计算机屏幕保护的开启和关闭。1.硬件准备:USB摄像头一个,计算机一台,或者仅用一台带摄像头的笔记本;2.软件准备:windows7环境,Matlab2017a,MATLAB Support Package for USB Webcams(可以在Mathwoks下载安装,也可以在Home界面下拉菜单栏的GetHardware Support Package查找下载安装)。3.具体步骤:①. 实现PC与摄像头的实时通讯功能,其中的Matlab代码为:
Cam = webcam();% Create the webcam object.
videoFrame = snapshot(Cam);% Capture one frame to get its size.
这里webcam创建摄像头对象是需要注意,如果PC连接的摄像头有多个,需要指定具体的摄像头标识,标识可以使序列标识,如Cam = webcam(1);也可以是对应摄像头的设备名,如Cam = webcam('ASHU V5');在PC连接的摄像头信息未知时可以先在命令行输入用函数webcamlist进行查询。
②. 对摄像头获取的图片videoFrame进行脸部跟踪与检测,其中的Matlab代码为:
handles.faceDetector = vision.CascadeObjectDetector();
handles.pointTracker = vision.PointTracker('MaxBidirectionalError',2);% Create the point tracker object.videoFrameGray = rgb2gray(videoFrame);
if numPts < 10
    bbox =handles.faceDetector.step(videoFrameGray);% Detection mode.    if ~isempty(bbox)
      points =detectMinEigenFeatures(videoFrameGray, 'ROI', bbox(1, : );% Find corner pointsinside the detected region.
      % Re-initialize thepoint tracker.      xyPoints =points.Location;      numPts =size(xyPoints,1);
       release(handles.pointTracker);       initialize(handles.pointTracker, xyPoints, videoFrameGray);
      oldPoints = xyPoints;%Save a copy of the points.
      % Convert therectangle represented as into an      % M-by-2 matrix of coordinates of the four corners. This      % is needed to be ableto transform the bounding box to display      % the orientation ofthe face.      bboxPoints =bbox2points(bbox(1, : );
      % Convert the boxcorners into the       bboxPolygon =reshape(bboxPoints', 1, []);% format required by insertShape.
      % Display a boundingbox around the detected face.      videoFrame =insertShape(videoFrame, 'Polygon', bboxPolygon, 'LineWidth', 3);
      videoFrame =insertMarker(videoFrame, xyPoints, '+', 'Color', 'white');% Display detectedcorners.    end
else
    % Tracking mode.
    =step(handles.pointTracker, videoFrameGray);    visiblePoints =xyPoints(isFound, : );    oldInliers =oldPoints(isFound, : );
    numPts =size(visiblePoints, 1);
    if numPts >= 10
      % Estimate thegeometric transformation between the old points      % and the new points.
       = estimateGeometricTransform(oldInliers, visiblePoints,'similarity', 'MaxDistance', 4);      bboxPoints =transformPointsForward(xform, bboxPoints);% Apply the transformation to thebounding box.      % Convert the boxcorners into the       % format required byinsertShape.      bboxPolygon =reshape(bboxPoints', 1, []);      videoFrame =insertShape(videoFrame, 'Polygon', bboxPolygon, 'LineWidth', 3);% Display abounding box around the face being tracked.      videoFrame =insertMarker(videoFrame, visiblePoints, '+', 'Color', 'white');% Displaytracked points.
      % Reset the points.
      oldPoints =visiblePoints;       setPoints(handles.pointTracker, oldPoints);    end
end
本例中的人脸跟踪系统可以处于两种模式之一:检测或跟踪。在检测模式下,可以使用vision.CascadeObjectDetector对象来检测当前帧中的人脸。如果检测到面部,则必须检测面部上的角点,初始化vision.PointTracker对象,然后切换到跟踪模式。
在跟踪模式下,必须使用点跟踪器跟踪点。当追踪这些点时,其中一些会因为阻塞而丢失。如果跟踪的点数低于阈值,则意味着不再跟踪面部。然后必须切换回检测模式以尝试重新采集脸部。
③. 设定定时器,定时进行图片采集和脸部检测跟踪算法的运行,其中的Matlab代码为:
fs =str2num(handles.sf_tag.String);%sample frequency handles.setTimerFunc =timer('Period',1/fs,'ExecutionMode','fixedRate');%peroid set(handles.setTimerFunc,'TimerFcn',{@faceDectAndTrackFunc,handles});%timerexecution functionstart(handles.setTimerFunc);
其中fs为算法检测的频率,可以在GUI界面进行设置,默认为50Hz。
④. 通过对脸部检测跟踪的结果控制计算机屏幕保护的开启和关闭,对应的Matlab代码为:
if numPts > 0
    turnOffScreenFlag = 0;
else
    turnOffScreenFlag =turnOffScreenFlag + 1;end

if turnOffScreenFlag >= fs*interval_time
   system('c:\windows\system32\Ribbons.scr /s');%turn off screen% system('c:\windows\system32\rundll32.exeuser32.dll,LockWorkStation');%shut down screenend
关闭屏幕保护的命令和解除屏幕锁定的windows命令暂时还没有找到,熟悉的朋友可以留言指教。
⑤. 设置操作友好的GUI,具体的实现如下图所示:


页: [1]
查看完整版本: 脸部检测跟踪算法及GUI演示