Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. clear all
  2. clf('reset');
  3.  
  4. cam=webcam(); % Create webcam object
  5. right=imread('RIGHT.jpg');
  6. left=imread('LEFT.jpg');
  7. noface=imread('no_face.jpg');
  8. straight=imread('STRAIGHT.jpg');
  9.  
  10. a = serial('/dev/cu.usbmodem1421','BaudRate',9600); % Create Arduino serial object
  11. fopen(a); % Open serial communication
  12.  
  13. s = 1;
  14. l = 1000;
  15. ri = 10000;
  16. no = 100000;
  17.  
  18. detector = vision.CascadeObjectDetector(); % Create a detector for face using Viola-Jones
  19. detector1 = vision.CascadeObjectDetector('EyePairSmall'); % Create detector for eyepair
  20.  
  21. while true % Infinite loop to continuously detect the face
  22.  
  23. vid = snapshot(cam); % Get a snapshot of webcam
  24. vid = rgb2gray(vid); % Convert to grayscale
  25. img = flip(vid, 2); % Flips the image horizontally
  26.  
  27. bbox = step(detector, img); % Creating bounding box using detector
  28.  
  29. if ~ isempty(bbox) % If face exists
  30. biggest_box=1;
  31. for i=1:rank(bbox) % Find the biggest face
  32. if bbox(i,3)>bbox(biggest_box,3)
  33. biggest_box=i;
  34. end
  35. end
  36. faceImage = imcrop(img,bbox(biggest_box,:)); % Extract the face from the image
  37. bboxeyes = step(detector1, faceImage); % locations of the eyepair using detector
  38.  
  39. for i=1:size(bbox,1) %draw all the regions that contain face
  40. rectangle('position', bbox(i, :), 'lineWidth', 2, 'edgeColor', 'y');
  41. end
  42.  
  43. if ~ isempty(bboxeyes) %check it eyepair is available
  44.  
  45. biggest_box_eyes=1;
  46. for i=1:rank(bboxeyes) %find the biggest eyepair
  47. if bboxeyes(i,3)>bboxeyes(biggest_box_eyes,3)
  48. biggest_box_eyes=i;
  49. end
  50. end
  51.  
  52. 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
  53.  
  54. eyesImage = imcrop(faceImage,bboxeyeshalf(1,:)); %extract the half eyepair from the face image
  55. eyesImage = imadjust(eyesImage); %adjust contrast
  56.  
  57. r = bboxeyeshalf(1,4)/4;
  58. [centers, radii, metric] = imfindcircles(eyesImage, [floor(r-r/4) floor(r+r/2)], 'ObjectPolarity','dark', 'Sensitivity', 0.975); % Hough Transform
  59. [M,I] = sort(radii, 'descend');
  60.  
  61. eyesPositions = centers;
  62.  
  63. hold on;
  64.  
  65. viscircles(centers, radii,'EdgeColor','b');
  66.  
  67. if ~isempty(centers)
  68. pupil_x=centers(1);
  69. disL=abs(0-pupil_x); %distance from left edge to center point
  70. disR=abs(bboxeyes(1,3)/3-pupil_x);%distance from right edge to center point
  71. disU=abs(0-pupil_x); %distance from upper edge to center point
  72. if disL>disR+16
  73. ri = ri + 1
  74. fprintf(a,'%i','2'); % This will send 2 to the Proto Arduino
  75. %writeDigitalPin(a,'D13',1); %Turn LED on
  76. else if disR>disL
  77. l = l + 1
  78. fprintf(a,'%i','3'); % This will send 3 to the Proto Arduino
  79. %writeDigitalPin(a,'D13',1); %Turn LED on
  80. else
  81. s = s + 1
  82. fprintf(a,'%i','1'); % this will send 1 to the Proto Arduino
  83. %writeDigitalPin(a,'D13',1); %Turn LED on
  84. end
  85. end
  86.  
  87. end
  88. end
  89. else
  90. no = no + 1
  91. fprintf(a,'%i','4'); % this will send 4 to the Proto Arduino
  92. %writeDigitalPin(a,'D13',1); %Turn LED on
  93. end
  94. set(gca,'XtickLabel',[],'YtickLabel',[]);
  95.  
  96. hold off;
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement