Skip to content

Commit 1548e05

Browse files
authored
Add files via upload
1 parent c41de0d commit 1548e05

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed

falldetection_via_ipcamera.m

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
function [] = sample()
2+
3+
% finding path where all files in the program are stored
4+
%[path,name,ext] = fileparts(mfilename('C:\Users\Lenovo\Desktop\Fall-Detection-master\videos'));
5+
6+
% reading the video
7+
cam = ipcam('http://192.168.43.1:8080/video');
8+
%while true
9+
%picture=camera.snapshot;
10+
% image(picture);
11+
%drawnow;
12+
%end
13+
%vid = vision.VideoFileReader('video.mp4');
14+
15+
% initializing foreground and blob detectors
16+
detector = vision.ForegroundDetector(...
17+
'NumTrainingFrames',10,'NumGaussians',5,...
18+
'MinimumBackgroundRatio', 0.7,'InitialVariance',0.05,...
19+
'LearningRate',0.0002);
20+
blob = vision.BlobAnalysis(...
21+
'CentroidOutputPort', true, 'AreaOutputPort', true, ...
22+
'BoundingBoxOutputPort', true, ...
23+
'MinimumBlobAreaSource', 'Property', 'MinimumBlobArea', 500);
24+
25+
%duration of mhi (or) max. value of mhi (or) no of frames taken into acc. for mhi
26+
tmhi = 15;
27+
28+
%strel parameters
29+
strelType = 'square';
30+
strelSize = 5;
31+
32+
%tolerance while find coordinate of y at min. x in counding ellipse
33+
%tolerance for yxmin <= Cy+1 in degrees
34+
noiseYxmin = 3;
35+
36+
%resize Parameter (fraction of input frame)
37+
resizeFactor = 0.25;
38+
39+
% Fall Detection Parameters
40+
%threshold for detecting high motion
41+
thresMotion = 1.8;
42+
%threshold speed for concluding fall
43+
thSpeed = 2;
44+
%threshold orientation change for concluding fall
45+
thOrChg = 15;
46+
%threshold area change for concluding fall
47+
thAreaChg = 15;
48+
%no of frames that form a fall sequence
49+
noFrFallSeq = 5;
50+
51+
%object that contains possible fall sequences
52+
%object contains ->speed , ->noFrames, ->avgOrChg, ->avgAreaChg
53+
%noFrames - no. of frames that have been taken into acc. till now
54+
posFalls = struct([]);
55+
56+
prevOr = [];
57+
58+
frameNo = 0;
59+
%while ~isDone(camera)
60+
while true
61+
pause(0.1);
62+
frame=snapshot(cam);
63+
%frame = step(camera);
64+
frameNo = frameNo+1;
65+
%resizing original frame
66+
%frame = imresize(frame,resizeFactor);
67+
68+
%assigning initial value to motion history image
69+
if frameNo == 1
70+
mhimage = zeros(size(frame,1),size(frame,2));
71+
end
72+
73+
%detecting foreground mask
74+
fgMask = step(detector,frame);
75+
%modifying mask to close gaps and fill holes
76+
fgClose = modifyMask(fgMask,strelType,strelSize);
77+
78+
%finding largest blob
79+
[area,centroid,box] = step(blob,fgClose);
80+
pos = find(area==max(area));
81+
box = box(pos,:);
82+
83+
speed = [];orientation = [];area = [];
84+
if ~isempty(box)
85+
%fgBBox - the mask after inside bouding box
86+
%removing cordinates outside bounding box
87+
fgBBox = maskInsideBBox(fgClose,box);
88+
[mhimage,speed] = calcSpeed(mhimage,fgBBox,tmhi);
89+
if speed >= thresMotion
90+
posFalls = initializeFallObj(posFalls,size(posFalls,2)+1);
91+
end
92+
93+
filledCannyMask = logical(edge(fgBBox,'Roberts'));
94+
[xcoord,ycoord] = coordInsideMask(filledCannyMask,box);
95+
ellipse = fitellipse(xcoord,ycoord);
96+
if ~isempty(ellipse)
97+
orientation = calcOrientation(ellipse,noiseYxmin);
98+
area = pi*ellipse(3)*ellipse(4);
99+
100+
%output
101+
subplot(2,3,1);
102+
imshow(frame);
103+
title(sprintf('Original Video\nFrame no - %d',frameNo),'FontSize',20);
104+
subplot(2,3,3);
105+
imshow(fgMask);
106+
title(sprintf('Human Detection\nFrame no - %d',frameNo),'FontSize',20);
107+
subplot(2,3,4);
108+
imshow(uint8((mhimage*255)/tmhi));
109+
title(sprintf('Motion History Image\nSpeed - %f',speed),'FontSize',20);
110+
subplot(2,3,6);
111+
imshow(filledCannyMask);
112+
drawEllipse(ellipse(1),ellipse(2),ellipse(3),ellipse(4),ellipse(5));
113+
title(sprintf('Shape of body\nOrientation - %f',orientation),'FontSize',20);
114+
else
115+
%output
116+
subplot(2,3,1);
117+
imshow(frame);
118+
title(sprintf('Original Video\nFrame no - %d',frameNo),'FontSize',20);
119+
subplot(2,3,3);
120+
imshow(fgMask);
121+
title(sprintf('Human Detection\nFrame no - %d',frameNo),'FontSize',20);
122+
subplot(2,3,4);
123+
imshow(uint8((mhimage*255)/tmhi));
124+
title(sprintf('Motion History Image\nSpeed - '),'FontSize',20);
125+
subplot(2,3,6);
126+
imshow(filledCannyMask);
127+
title(sprintf('Shape of body\nOrientation - '));
128+
end
129+
else
130+
%output
131+
subplot(2,3,1);
132+
imshow(frame);
133+
title(sprintf('Original Video\nFrame no - %d',frameNo),'FontSize',20);
134+
subplot(2,3,3);
135+
imshow(fgMask);
136+
title(sprintf('Human Detection\nFrame no - %d',frameNo),'FontSize',20);
137+
subplot(2,3,4);
138+
imshow(uint8((mhimage*255)/tmhi));
139+
title(sprintf('Motion History Image\nSpeed - '),'FontSize',20);
140+
subplot(2,3,6);
141+
imshow(zeros(size(fgMask)));
142+
title(sprintf('Shape of body\nOrientation - %f',orientation),'FontSize',20);
143+
end
144+
145+
%no possible fall sequences
146+
if isempty(posFalls)
147+
if ~isempty(orientation) && ~isempty(area)
148+
prevOr = orientation;
149+
prevArea = area;
150+
else
151+
%do not increment last changed frame number
152+
end
153+
continue;
154+
end
155+
156+
%no object is found in foreground mask
157+
if isempty(speed) || isempty(orientation) || isempty(area)
158+
% speed,orientation change, area change have to assigned a certain
159+
% value
160+
posFalls = struct([]);
161+
%do not increment last changed frame number
162+
continue;
163+
end
164+
165+
if isempty(prevOr)
166+
orChg = 0;
167+
areaChg = 0;
168+
else
169+
orChg = findOrChg(orientation,prevOr);
170+
areaChg = 20;%have to find it.
171+
end
172+
prevOr = orientation;
173+
prevArea = area;
174+
[fallDetected,posFalls] = updateCheckPosFalls(posFalls,size(posFalls,2),noFrFallSeq,orChg,areaChg,speed,thOrChg,thAreaChg,thSpeed);
175+
if fallDetected == true
176+
177+
subplot(2,3,1);
178+
imshow(frame);
179+
title(sprintf('Original Video\nFrame no - %d',frameNo),'FontSize',20);
180+
subplot(2,3,3);
181+
imshow(fgMask);
182+
title(sprintf('Human Detection\nFrame no - %d',frameNo),'FontSize',20);
183+
subplot(2,3,4);
184+
imshow(uint8((mhimage*255)/tmhi));
185+
title(sprintf('Motion History Image\nSpeed - %f',speed),'FontSize',20);
186+
subplot(2,3,6);
187+
imshow(filledCannyMask);
188+
title(sprintf('Shape of body\nOrientation - %f',orientation),'FontSize',20);
189+
sendemail(); %send email alert
190+
pause(4);
191+
sgtitle('');
192+
end
193+
end
194+

0 commit comments

Comments
 (0)