Real-Time Eye Tracking ... help!

61 views (last 30 days)
Surashree
Surashree on 5 Apr 2013
Commented: Bharath Ch on 25 Jun 2022
My project is to perform eye detection and move the mouse cursor using the eyes, based on their position in the image the webcam captures. I have performed eye detection already.. the code gives the coordinates of the eyes in the image captured .I need to map these coordinates onto the screen, taking into consideration the gaze direction/distance from screen, etc. How do I do this? Please help!
I am attaching my code here. Please go through it, and help. (It's a bit shabby, I am new to MATLAB)
***************************
MATLAB CODE:
clc
clear all
vid=videoinput('winvideo',1,'RGB24_320x240');
preview(vid);
start(vid);
%camlight('headlight')
im1= getdata(vid,1);
im=rgb2gray(im1);
%im=imadjust(imgray,[],[
%STEP 2: K MEANS CLUSTERING%
k=10;
%histogram calculation
img_hist = zeros(256,1);
hist_value = zeros(256,1);
for i=1:256
img_hist(i)=sum(sum(im==(i-1)));
end;
for i=1:256
hist_value(i)=i-1;
end;
%cluster initialization
cluster = zeros(k,1);
cluster_count = zeros(k,1);
for i=1:k
cluster(i)=uint8(rand*255);
end;
old = zeros(k,1);
while (sum(sum(abs(old-cluster))) >k)
old = cluster;
closest_cluster = zeros(256,1);
min_distance = uint8(zeros(256,1));
min_distance = abs(hist_value-cluster(1));
%calculate the minimum distance to a cluster
for i=2:k
min_distance =min(min_distance, abs(hist_value-cluster(i)));
end;
%calculate the closest cluster
for i=1:k
closest_cluster(min_distance==(abs(hist_value-cluster(i)))) = i;
end;
%calculate the cluster count
for i=1:k
cluster_count(i) = sum(img_hist .*(closest_cluster==i));
end;
for i=1:k
if (cluster_count(i) == 0)
cluster(i) = uint8(rand*255);
else
cluster(i) = uint8(sum(img_hist(closest_cluster==i).*hist_value(closest_cluster==i))/cluster_count(i));
end;
end;
end;
imresult=uint8(zeros(size(im)));
for i=1:256
imresult(im==(i-1))=cluster(closest_cluster(i));
end;
clustersresult=uint8(zeros(size(im)));
for i=1:256
clustersresult(im==(i-1))=closest_cluster(i);
end;
clusters = cluster;
result_image = imresult;
clusterized_image = clustersresult;
figure, imshow(result_image);
imtool(result_image);
%STEP 2 COMPLETE!%
%STEP 3: THRESHOLDING%
[m n]=size(result_image);
row1=round(m/3);
col1=round(n/3);
row2= 2*row1;
col2=col1*2;
min=30;
for i=row1:row2
for j=col1:col2
if(result_image(i,j)<min)
min=result_image(i,j);
end
end
end
display(min);
for i=1:m
for j=1:n
if(result_image(i,j)<60)
result_image(i,j)=0;
end
if( result_image(i,j)>60)
result_image(i,j)=255;
end
end
end
%figure, imshow(result_image);
%STEP 3 COMPLETE!%
%FILLING HOLES%
[x y]=size(result_image);
row1=round(x/3);
col1=round(y/3);
row2= 2*row1;
col2=col1*2;
cntwhite=0; cntblk=0;
for i=1:row1
for j=1:y
if(result_image(i,j)==255)
result_image(i,j)=0;
end
end
end
for i=row2:x
for j=1:y
if(result_image(i,j)==255)
result_image(i,j)=0;
end
end
end
for i=1:x
for j=1:col1
if(result_image(i,j)==255)
result_image(i,j)=0;
end
end
end
for i=1:x
for j=col2:y
if(result_image(i,j)==255)
result_image(i,j)=0;
end
end
end
for i=row1:row2
for j=col1:col2
if(result_image(i,j)==255)
cntwhite=cntwhite+1;
else
cntblk=cntblk+1;
end
end
end
imtool(result_image);
%filling holes done%
display(cntwhite);
display(cntblk);
%connected component labelling + filtering%
[L, n]= bwlabel(result_image, 4);
filter= [0,0,0,0,0; 0,0,255,0,0; 0,255,255,255,0; 0,0,255,0,0; 0,0,0,0,0];
%dbstop if error
flag=0;
for k=1:n
[r, c]=find(L==k);
if(flag==1)
break;
end
for i=(r-6):r
for j=10:(y-10)
counter=0;
if(flag==1)
break;
end
if(i==x || j==y || i==(x-1) || j==(y-1) || i<=0 || (i-1)<=0 || (i-2)<=0 || i<row1 || j<col1 || j>col2 || i>row2)
continue
end
if(result_image(i-2,j-2)==filter(1,1))
counter=counter+1;
end
if(result_image(i-2,j-1)==filter(1,2))
counter=counter+1;
end
if(result_image(i-2,j)==filter(1,3))
counter=counter+1;
end
if(result_image(i-2,j+1)==filter(1,4))
counter=counter+1;
end
if(result_image(i-2,j+2)==filter(1,5))
counter=counter+1;
end
if(result_image(i-1,j-2)==filter(2,1))
counter=counter+1;
end
if(result_image(i-1,j-1)==filter(2,2))
counter=counter+1;
end
if(result_image(i-1,j)==filter(2,3))
counter=counter+1;
end
if(result_image(i-1,j+1)==filter(2,4))
counter=counter+1;
end
if(result_image(i-1,j+2)==filter(2,5))
counter=counter+1;
end
if(result_image(i,j-2)==filter(3,1))
counter=counter+1;
end
if(result_image(i,j-1)==filter(3,2))
counter=counter+1;
end
if(result_image(i,j)==filter(3,3))
counter=counter+1;
end
if(result_image(i,j+1)==filter(3,4))
counter=counter+1;
end
if(result_image(i,j+2)==filter(3,5))
counter=counter+1;
end
if(result_image(i+1,j-2)==filter(4,1))
counter=counter+1;
end
if(result_image(i+1,j-1)==filter(4,2))
counter=counter+1;
end
if(result_image(i+1,j)==filter(4,3))
counter=counter+1;
end
if(result_image(i+1,j+1)==filter(4,4))
counter=counter+1;
end
if(result_image(i+1,j+2)==filter(4,5))
counter=counter+1;
end
if(result_image(i+2,j-2)==filter(5,1))
counter=counter+1;
end
if(result_image(i+2,j-1)==filter(5,2))
counter=counter+1;
end
if(result_image(i+2,j)==filter(5,3))
counter=counter+1;
end
if(result_image(i+2,j+1)==filter(5,4))
counter=counter+1;
end
if(result_image(i+2,j+2)==filter(5,5))
counter=counter+1;
end
if(counter>21)
%display('found match');
if(j<(round(y/2)))
xcoor=j+25;
end
if(j>(round(y/2)))
xcoor=j-25;
end
ycoor=i;
display(xcoor);
display(ycoor);
flag=1;
break;
%end
end
end
end
end
if(flag==0)
display('error!');
end
stop(vid);
close all;
********************************
Please help! How should I map the eye coordinates in image onto the screen?
  4 Comments
Megha  Chandu
Megha Chandu on 9 Apr 2018
Please can I get eye tracking code at meghachandu97@gmail.com
Yara Alhrmi
Yara Alhrmi on 11 Mar 2020
anyone can help me to find the eye tracking plese

Sign in to comment.

Answers (2)

Jos (10584)
Jos (10584) on 12 Feb 2018
You could perform some kind of mapping from eye position in the camera to the coordinates on the screen, that is, a calibration.
Have people look to a known position on the screen, record pupil position within the camera and repeat for as many points as desired. Then do some kind of mapping using procrustes or other better, techniques.

Bashar Shami
Bashar Shami on 31 Dec 2021
Use Viola Jounes Hough detection Algorithm :
clc;
clear;
close all;
vid=videoinput('winvideo',1,'RGB24_320x240');
start(vid);
preview(vid);
start(vid);
%%
right=imread('RIGHT.jpg');
left=imread('LEFT.jpg');
noface=imread('no_face.jpg');
straight=imread('STRAIGHT.jpg');
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
im1= getdata(vid,1);
img= rgb2gray(im1);
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
subplot(2,2,1),subimage(img); hold on; % Displays full image
for i=1:size(bbox,1) %draw all the regions that contain face
rectangle('position', bbox(i, :), 'lineWidth', 2, 'edgeColor', 'y');
end
subplot(2,2,3),subimage(faceImage); %display face image
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.93); % Hough Transform
[M,I] = sort(radii, 'descend');
eyesPositions = centers;
subplot(2,2,2),subimage(eyesImage); 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
subplot(2,2,4);
if disL>disR+16
subimage(right);
ccf = 4;
else if disR>disL
subimage(left);
ccf = 3;
else
subimage(straight);
ccf = 1;
end
end
% HARDWARE
instrumentObjects = instrfind; % don't pass it anything - find all of them.
delete(instrumentObjects)
a = serial('COM10','BaudRate',9600);
fopen(a);
if ccf == 1
fwrite(a,'1');
elseif ccf == 3
fwrite(a,'3');
elseif ccf == 4
fwrite(a,'4');
end
fclose(a);
pause(2)
end
end
else
subplot(2,2,4);
subimage(noface);
end
set(gca,'XtickLabel',[],'YtickLabel',[]);
hold off;
end
  1 Comment
Bharath Ch
Bharath Ch on 25 Jun 2022
Can i know what is the input images given to right ,left and straight face?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!