Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear all
- clf('reset');
- cam=webcam(); % Create webcam object
- right=imread('RIGHT.jpg');
- left=imread('LEFT.jpg');
- noface=imread('no_face.jpg');
- straight=imread('STRAIGHT.jpg');
- a = serial('/dev/cu.usbmodem1421','BaudRate',9600); % Create Arduino serial object
- fopen(a); % Open serial communication
- s = 1;
- l = 1000;
- ri = 10000;
- no = 100000;
- detector = vision.CascadeObjectDetector(); % Create a detector for face using Viola-Jones
- detector1 = vision.CascadeObjectDetector('EyePairSmall'); % Create detector for eyepair
- while true % Infinite loop to continuously detect the face
- vid = snapshot(cam); % Get a snapshot of webcam
- vid = rgb2gray(vid); % Convert to grayscale
- img = flip(vid, 2); % Flips the image horizontally
- bbox = step(detector, img); % Creating bounding box using detector
- if ~ isempty(bbox) % If face exists
- biggest_box=1;
- for i=1:rank(bbox) % Find the biggest face
- if bbox(i,3)>bbox(biggest_box,3)
- biggest_box=i;
- end
- end
- faceImage = imcrop(img,bbox(biggest_box,:)); % Extract the face from the image
- bboxeyes = step(detector1, faceImage); % locations of the eyepair using detector
- for i=1:size(bbox,1) %draw all the regions that contain face
- rectangle('position', bbox(i, :), 'lineWidth', 2, 'edgeColor', 'y');
- end
- if ~ isempty(bboxeyes) %check it eyepair is available
- biggest_box_eyes=1;
- for i=1:rank(bboxeyes) %find the biggest eyepair
- if bboxeyes(i,3)>bboxeyes(biggest_box_eyes,3)
- biggest_box_eyes=i;
- end
- end
- bboxeyeshalf=[bboxeyes(biggest_box_eyes,1),bboxeyes(biggest_box_eyes,2),bboxeyes(biggest_box_eyes,3)/3,bboxeyes(biggest_box_eyes,4)]; %resize the eyepair width in half
- eyesImage = imcrop(faceImage,bboxeyeshalf(1,:)); %extract the half eyepair from the face image
- eyesImage = imadjust(eyesImage); %adjust contrast
- r = bboxeyeshalf(1,4)/4;
- [centers, radii, metric] = imfindcircles(eyesImage, [floor(r-r/4) floor(r+r/2)], 'ObjectPolarity','dark', 'Sensitivity', 0.975); % Hough Transform
- [M,I] = sort(radii, 'descend');
- eyesPositions = centers;
- hold on;
- viscircles(centers, radii,'EdgeColor','b');
- if ~isempty(centers)
- pupil_x=centers(1);
- disL=abs(0-pupil_x); %distance from left edge to center point
- disR=abs(bboxeyes(1,3)/3-pupil_x);%distance from right edge to center point
- disU=abs(0-pupil_x); %distance from upper edge to center point
- if disL>disR+16
- ri = ri + 1
- fprintf(a,'%i','2'); % This will send 2 to the Proto Arduino
- %writeDigitalPin(a,'D13',1); %Turn LED on
- else if disR>disL
- l = l + 1
- fprintf(a,'%i','3'); % This will send 3 to the Proto Arduino
- %writeDigitalPin(a,'D13',1); %Turn LED on
- else
- s = s + 1
- fprintf(a,'%i','1'); % this will send 1 to the Proto Arduino
- %writeDigitalPin(a,'D13',1); %Turn LED on
- end
- end
- end
- end
- else
- no = no + 1
- fprintf(a,'%i','4'); % this will send 4 to the Proto Arduino
- %writeDigitalPin(a,'D13',1); %Turn LED on
- end
- set(gca,'XtickLabel',[],'YtickLabel',[]);
- hold off;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement