www.gusucode.com > matlab编程人眼疲劳源码程序 > matlab编程人眼疲劳源码程序/matlab人眼疲劳/main.m
clc; clear all; close all; %% load DB load svm cl = {'open','close'}; dim = [30 60; 30 60 40 65]; delete(imaqfind) vid=webcam(); videoFrame = snapshot(vid); frameSize = size(videoFrame); videoPlayer = vision.VideoPlayer('Position', [100 100 [frameSize(2), frameSize(1)]+30]); % 定位眼睛和嘴 faceDetector = vision.CascadeObjectDetector; faceDetectorLeye = vision.CascadeObjectDetector('EyePairBig'); faceDetectorM = vision.CascadeObjectDetector('Mouth'); tic% 保存当前时间,开始计时 % 初始向量 LC = 0; RC = 0; MC = 0; TF = 0; TC = 0; Feature = []; c1p = 1; species = 'Non-Fatigue';%识别疲劳与否 for ii = 1:600 %设置频率为600帧 im=snapshot(vid); % 采集图片 imshow(im) subplot(3,4,[1 2 5 6 9 10]); imshow(im) %脸部定位 bbox = step(faceDetector, im); if ~isempty(bbox); bbox = bbox(1,:);%循环,直到检测成功 % 画框 rectangle('Position',bbox,'edgecolor','r'); S = skin_seg2(im); % 分离皮肤区域 bw3 = cat(3,S,S,S); % 原始图像与输出结果相乘 Iss = double(im).*bw3; Ic = imcrop(im,bbox); Ic1 = imcrop(Iss,bbox);%剪切 subplot(3,4,[3 4]); imshow(uint8(Ic1)) bboxeye = step(faceDetectorLeye, Ic); %同理定位眼睛 if ~isempty(bboxeye); bboxeye = bboxeye(1,:); Eeye = imcrop(Ic,bboxeye); %画框 rectangle('Position',bboxeye,'edgecolor','y'); else disp('Eyes not detected, Please adapt to your posion')%输出 end if isempty(bboxeye) continue;%循环 end Ic(1:bboxeye(2)+2*bboxeye(4),:,:) = 0; % 同理定位嘴 bboxM = step(faceDetectorM, Ic); if ~isempty(bboxM); bboxMtemp = bboxM; if ~isempty(bboxMtemp) bboxM = bboxMtemp(1,:); Emouth = imcrop(Ic,bboxM); % 画框 rectangle('Position',bboxM,'edgecolor','y'); else disp('Mouth not detected, Please adapt your posion') continue; end else disp('Mouth not detected') continue; end [nre,nce,k ] = size(Eeye); Leye = Eeye(:,1:round(nce/2),:); Reye = Eeye(:,round(nce/2+1):end,:); subplot(3,4,7) imshow(edge(rgb2gray(Leye),'sobel'));%sobel边缘算子 subplot(3,4,8) imshow(edge(rgb2gray(Reye),'sobel')); Emouth3 = Emouth; Leye = rgb2gray(Leye); Reye = rgb2gray(Reye); Emouth = rgb2gray(Emouth); X = Emouth(:); [nr1, nc1 ] = size(Emouth); cid = kmeans(double(X),2,'emptyaction','drop'); kout = reshape(cid,nr1,nc1); %调整矩阵 subplot(3,4,[11,12]); Ism = zeros(nr1,nc1,3); % Ism(:,:,3) = 255; % Ism(:,:,3) = 125; Ism(:,:,3) = 255; bwm = kout-1; bwm3 = cat(3,bwm,bwm,bwm); Ism(logical(bwm3)) = Emouth3(logical(bwm3)); imshow(uint8(Ism)); % 相关系数模板匹配 Leye = imresize(Leye,[dim(1,1) dim(1,2)]);% 左眼重定义到标准尺寸 c1 =match_DB(Leye,DBL); subplot(3,4,7) title(cl{c1}) % 右眼 Reye = imresize(Reye,[dim(2,1) dim(2,2)]); c2 = match_DB(Reye,DBR); subplot(3,4,8) title(cl{c2}) %嘴 Emouth = imresize(Emouth,[dim(3,1) dim(3,2)]); c3 = match_DB(Emouth,DBM); subplot(3,4,[11,12]); title(cl{c3}) if c1 == 2 LC = LC+1; if c1p == 1 TC = TC+1; end end if c2==2 RC = RC+1; end if c3 == 1 MC = MC + 1; end TF = TF + 1; toc time = toc; if toc>8 Feature = [LC/TF RC/TF MC/TF TC]; disp(Feature) species = svmclassify(svmStruct,Feature);%支持向量机 tic % 初始向量 LC = 0; % RC = 0; % MC = 0; % TF = 0; % TC = 0; % end subplot(3,4,[1 2 5 6 9 10]); if strcmpi(species,'Fatigue') && time >8%输出结果 text(20,20,species,'fontsize',14,'color','r','Fontweight','bold') else text(20,20,species,'fontsize',14,'color','g','Fontweight','bold') end c1p = c1; pause(0.00005) end end