-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheyebot.m
291 lines (284 loc) · 9.68 KB
/
eyebot.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
function []=eyebot(url,varargin)
% eyebot Function detect eyes movement and passes that data via Bluetooth.
% eyebot(URL) takes URL of IP-Cam in http,sttp or other protocols.
% Only URL is required, if given more than one argument, function exits
% with error.
%
% Copyright 2016-"Until World Ends"
if(nargin > 1)
error('Too many arguments, Only URL required.');
end
%To make sure just one input is given as url.
url=[url '/video'];
%Picks ipcam url to live stream video
webcam=ipcam(url); %webcam named object created
savepath = 'D:\Minor'; %save path
imageNum = 0; %starting image number
%Intializing Bluetooth
b=instrhwinfo('Bluetooth');
b.RemoteNames
bt=Bluetooth('HC-05',1);
fopen(bt);
%Calibration Code
disp('Clabirating threshold for face');
thr_temp=0;
thl_temp=0;
for i=1:1:5
%Reading Image
img=snapshot(webcam);
img=rgb2gray(img);
img=imadjust(img);
img=adapthisteq(img);
thl=6;
thr=6;
th_flag=0;
while(th_flag~=1)
%Creating Object Using Voila-Jones Algorithm for detecting Eyes
leye=vision.CascadeObjectDetector('LeftEyeCART','MergeThreshold',thl);
lbox=step(leye,img);
reye=vision.CascadeObjectDetector('RightEyeCART','MergeThreshold',thr);
rbox=step(reye,img);
%Checking for set Threshold
if( isequal(size(lbox),[1 4]) && isequal(size(rbox),[1 4]) )
th_flag=1;
else
if( isequal(size(lbox),[1 4]) && ~isequal(size(rbox),[1 4]) )
thr=thr+2;
else
if( ~isequal(size(lbox),[1 4]) && isequal(size(rbox),[1 4]) )
thl=thl+2;
else
if( ~isequal(size(lbox),[1 4]) && ~isequal(size(rbox),[1 4]) )
thl=thl+2;
thr=thr+2;
end
end
end
end
end
t=i*20;
temp=num2str(t);
str=[temp '% Completed'];
disp(str);
thr_temp=thr_temp+thr;
thl_temp=thl_temp+thl;
end
disp('Face Calibrated, Dont move camera now');
thr=ceil(thr_temp/5);
thl=ceil(thl_temp/5);
stop=0;
while(1)
%Reading Image
img=snapshot(webcam);
img=rgb2gray(img);
img=imadjust(img);
img=adapthisteq(img);
%Creating Object Using Voila-Jones Algorithm for detecting Eyes
leye=vision.CascadeObjectDetector('LeftEyeCART','MergeThreshold',thl);
lbox=step(leye,img);
reye=vision.CascadeObjectDetector('RightEyeCART','MergeThreshold',thr);
rbox=step(reye,img);
%Checking If Eyes Detected
lcheck=isempty(lbox);
rcheck=isempty(rbox);
if(lcheck==1 && rcheck==1)
disp('================No Eyes Detected======================');
disp('================Postion Camera Correctly==============');
stop=stop+1;
%Stopping
if stop==3
disp('Stoping Since No Activity for more than 3 seconds');
break;
end
continue;
end
%Cropping only correct eye
if( isequal(size(lbox),[1 4]) && isequal(size(rbox),[1 4]) )
lI=img(lbox(2)+ceil(lbox(4)/2.7):lbox(2)+lbox(4)-10,lbox(1)+ceil(lbox(3)/8):lbox(1)+lbox(3)-ceil(lbox(3)/7));
lI=imbinarize(lI,0.15);
imageNum = imageNum + 1;
savename = 'image_%04dl.jpeg'; %name pattern
fileName = sprintf(savename, imageNum); %create filename
path = [savepath '/' fileName]; %folder and all
if(~isempty(lI))
imwrite(lI, path); %write the original binary image
end
lI=corner(lI);
lI=matTri(lI);
lI=imcomplement(lI);
lI=erode(lI);
savename = 'image_%04d.jpeg'; %name pattern
fileName = sprintf(savename, imageNum); %create filename
path = [savepath '/' fileName]; %folder and all
imwrite(lI, path); %write the complemented binary image
rI=img(rbox(2)+ceil(lbox(4)/2.4):rbox(2)+rbox(4)-10,rbox(1)+ceil(lbox(3)/8):rbox(1)+rbox(3)-ceil(lbox(3)/7));
rI=imbinarize(rI,0.15);
imageNum = imageNum + 1;
savename = 'image_%04dr.jpeg'; %name pattern
fileName = sprintf(savename, imageNum); %create filename
path = [savepath '/' fileName]; %folder and all
if(~isempty(rI))
imwrite(rI,path); %Write original binary image
end
rI=corner(rI);
rI=matTri(rI);
rI=imcomplement(rI);
rI=erode(rI);
savename = 'image_%04d.jpeg'; %name pattern
fileName = sprintf(savename, imageNum); %create filename
path = [savepath '/' fileName]; %folder and all
imwrite(rI,path); %Write complemented binary image
%Movement Detection
[~,wl]=size(lI);
[~,wr]=size(rI);
l=min(wl,wr);
lsumm=zeros([1 l]);
rsumm=zeros([1 l]);
for i=1:l
lsumm(i)=sum(lI(:,i));
rsumm(i)=sum(rI(:,i));
end
[~,lindex]=max(lsumm);
[~,rindex]=max(rsumm);
%Left Or Right
index=ceil((1.3*lindex+0.7*rindex)/2);
if isempty(index)
fwrite(bt,uint8(2));
fwrite(bt,120);
continue;
end
if index<(l/2.7)
disp(['Left=' num2str(index)])
fwrite(bt,uint8(1));
fwrite(bt,index);
else
if (index>ceil((l/2.7)) && index<ceil((2*l/3)))
disp(['Straight=' num2str(index)])
fwrite(bt,uint8(2));
fwrite(bt,index);
else
if index>floor((2*l/3))
disp(['Right=' num2str(index)])
fwrite(bt,uint8(3));
fwrite(bt,index);
else
disp('Closed Eye detected');
end
end
end
%Checking just right eye
else
if( isequal(size(rbox),[1 4]) )
rI=img(rbox(2)+ceil(lbox(4)/2.4):rbox(2)+rbox(4)-10,rbox(1)+ceil(lbox(3)/8):rbox(1)+rbox(3)-ceil(lbox(3)/7));
rI=imbinarize(rI,0.15);
imageNum = imageNum + 1;
savename = 'image_%04dr.jpeg'; %name pattern
fileName = sprintf(savename, imageNum); %create filename
path = [savepath '/' fileName]; %folder and all
if(~isempty(rI))
imwrite(rI,path);
end
rI=corner(rI);
rI=matTri(rI);
rI=imcomplement(rI);
rI=erode(rI);
savename = 'image_%04d.jpeg'; %name pattern
fileName = sprintf(savename, imageNum); %create filename
path = [savepath '/' fileName]; %folder and all
if(~isempty(rI))
imwrite(rI,path);
end
%Movement Detection
[~,l]=size(rI);
summ=zeros([1 l]);
for i=1:l
summ(i)=sum(rI(:,i));
end
%Left Or Right
[~,index]=max(summ);
if isempty(index)
fwrite(bt,uint8(2));
fwrite(bt,120);
continue;
end
if index<ceil((l/2.7))
disp(['Left=' num2str(index)])
fwrite(bt,uint8(1));
fwrite(bt,index);
else
if (index>ceil((l/2.7)) && index<ceil((2*l/3)))
disp(['Straight=' num2str(index)])
fwrite(bt,uint8(2));
fwrite(bt,index);
else
if index>ceil((2*l/3))
disp(['Right=' num2str(index)])
fwrite(bt,uint8(3));
fwrite(bt,index);
else
disp('Closed Eye Detected');
end
end
end
else
%Detecting only left eye
if( isequal(size(lbox),[1 4]) )
lI=img(lbox(2)+ceil(lbox(4)/2.7):lbox(2)+lbox(4)-10,lbox(1)+ceil(lbox(3)/8):lbox(1)+lbox(3)-ceil(lbox(3)/7));
lI=imbinarize(lI,0.15);
savename = 'image_%04dl.jpeg'; %name pattern
imageNum = imageNum + 1;
fileName = sprintf(savename, imageNum); %create filename
path = [savepath '/' fileName]; %folder and all
if(~isempty(lI))
imwrite(lI, path); %write the image there as jpeg
end
lI=corner(lI);
lI=matTri(lI);
lI=imcomplement(lI);
lI=erode(lI);
savename = 'image_%04d.jpeg'; %name pattern
fileName = sprintf(savename, imageNum); %create filename
path = [savepath '/' fileName]; %folder and all
if(~isempty(lI))
imwrite(lI, path); %write the image there as jpeg
end
%Movement Detection
[~,l]=size(lI);
summ=zeros([1 l]);
for i=1:l
summ(i)=sum(lI(:,i));
end
[~,index]=max(summ);
if isempty(index)
fwrite(bt,uint8(2));
fwrite(bt,120);
continue;
end
%Left Or Right
if index<ceil((l/2.7))
disp(['Left=' num2str(index)])
fwrite(bt,uint8(1));
fwrite(bt,index);
else
if (index>ceil((l/2.7)) && index<ceil((2*l/3)))
disp(['Straight=' num2str(index)])
fwrite(bt,uint8(2));
fwrite(bt,index);
else
if index>floor(2*l/3)
disp(['Right=' num2str(index)])
fwrite(bt,uint8(3));
fwrite(bt,index);
else
disp('Closed Eye Detected');
end
end
end
else
disp('Closed Eye Detected');
end
end
end %Cropping and detecting if ended
end %while ended
%Function ended
end